all: frontend mlir isqc simulator isq-simulator.ll

.PHONY: check-env frontend mlir isqc simulator isq-simulator.ll all lock develop run clean upload

check-env:
ifndef ISQ_ROOT
	$(error ISQ_ROOT is undefined)
endif
	$(info $(shell mkdir -p $(ISQ_ROOT)/bin))

frontend: check-env
	cd frontend && make all
	cd frontend && isqc1_bin=$$(cabal list-bin isqc1) && \
	cd ${ISQ_ROOT}/bin && \
	rm -f isqc1 && \
	ln -s "$$isqc1_bin" isqc1

mlir: check-env
	cd mlir && mkdir -p build && cd build && cmake ../ -GNinja && ninja
	cd ${ISQ_ROOT}/bin && \
	rm -f isq-opt && rm -f isq-codegen && \
	ln -s ../mlir/build/tools/isq-opt isq-opt && \
	ln -s ../mlir/build/tools/isq-codegen isq-codegen

isqc: check-env
	cd isqc && cargo build;
	cd ${ISQ_ROOT}/bin && \
	rm -f isqc && \
	ln -s ../target/debug/isqc isqc

simulator: check-env
	cd simulator &&	cargo build --release
	cd ${ISQ_ROOT}/bin && \
	rm -f simulator && \
	rm -f isq-simulator-config && \
	ln -s ../target/release/simulator ./simulator && \
	ln -s ../target/release/isq-simulator-config ./isq-simulator-config

isq-simulator.ll: check-env
	mkdir -p ${ISQ_ROOT}/share/isq-simulator;
	linker=`which llvm-link` && if [ $$linker == "" ]; then linker=$(ISQ_ROOT)/bin/llvm-link; fi \
	&& cd simulator && eval $$linker src/facades/qir/shim/qir_builtin/shim.ll src/facades/qir/shim/qsharp_core/shim.ll \
	src/facades/qir/shim/qsharp_foundation/shim.ll src/facades/qir/shim/isq/shim.ll -S -o ${ISQ_ROOT}/share/isq-simulator/isq-simulator.ll &&\
	rm -f ${ISQ_ROOT}/share/isq-simulator/simulator-targets.json && \
	ln -s ${ISQ_ROOT}/simulator/share/simulator-targets.json ${ISQ_ROOT}/share/isq-simulator/

upload:
	nix flake archive --json \
	| jq -r '.path,(.inputs|to_entries[].value.path)' \
	| cachix push arclight-quantum
	nix build --json \
	| jq -r '.[].outputs | to_entries[].value' \
	| cachix push arclight-quantum

cargo2nix:
	if [ -z ${ISQ_USE_PROXYCHAINS} ]; then yes yes | cargo2nix; else yes yes | proxychains4 cargo2nix; fi
	nixpkgs-fmt Cargo.nix
clean: check-env
	rm .build -r
	mkdir -p .build
develop:
	@exec nix develop
bin:
	mkdir -p bin
	cd bin && ln -s ../frontend/dist/build/isqc1/isqc1 ./isqc1
	cd bin && ln -s ../mlir/build/tools/isq-opt ./isq-opt
	cd bin && ln -s ../mlir/build/tools/isq-example ./isq-example
clean-examples:
	cd examples && find -name "*.so" -or -name "*.qcis" -exec rm {} \;
