this_makefile := $(lastword $(MAKEFILE_LIST))
curr_dir := $(abspath $(dir $(this_makefile)))
IBEX = ${root_dir}/../third_party/ibex

#############################
####      SYNTHESIS      ####
#############################

${IBEX}/.gitpatch:
	cd ${IBEX} && git apply ${curr_dir}/top_artya7_core.patch && touch $@

uhdm/yosys/synth-ibex: clean-build ${IBEX}/.gitpatch
	virtualenv ${root_dir}/venv-ibex
	(export PATH=${root_dir}/../image/bin:${PATH} && \
		. ${root_dir}/venv-ibex/bin/activate && \
		pip install -r ${IBEX}/python-requirements.txt && \
		pip install git+https://github.com/antmicro/edalize@svplugin_support && \
		fusesoc --cores-root=${IBEX} run --build --tool yosys --target=synth lowrisc:ibex:top_artya7_surelog --SRAMInitFile="${curr_dir}/led.vmem")

uhdm/yosys/synth-ibex-build: clean-build ${IBEX}/.gitpatch
	virtualenv ${root_dir}/venv-ibex
	(export PATH=${root_dir}/../image/bin:${PATH} && \
		. ${root_dir}/venv-ibex/bin/activate && \
		pip install -r ${IBEX}/python-requirements.txt && \
		pip install git+https://github.com/antmicro/edalize@svplugin_support && \
		fusesoc --cores-root=${IBEX} run --build --tool vivado --target=synth lowrisc:ibex:top_artya7_surelog --part xc7a35ticsg324-1L --SRAMInitFile="${curr_dir}/led.vmem")

##############################
####   F4PGA-TOOLCHAIN    ####
##############################

# Environment setup

# Call arguments:
# 1: URL to fetch the package's URL from.
# 2: File where the package's URL is saved.
# 3: Directory where the package is unpacked.
define download_and_unpack_archive_from_url_fetched_from_url
	curl -fsSL $(1) > $(2);
	@printf '%s URL:\n  %s\n\n' $(basename $(notdir $(strip $(2)))) "$$(<$(2))";
	curl -fsSL "$$(<$(strip $(2)))" | tar -xvJC $(3);
	@echo
endef

${root_dir}/env/f4pga:
	mkdir -p $@

.PHONY: download-f4pga
download-f4pga: | ${root_dir}/env/f4pga
	$(call download_and_unpack_archive_from_url_fetched_from_url, \
		'https://github.com/f4pga/f4pga-arch-defs/releases/download/latest/symbiflow-install-xc7-latest', \
		'${root_dir}/env/f4pga/symbiflow-install-xc7-latest.url', \
		'${root_dir}/env/f4pga')

	# Do not install yosys nor yosys-f4pga-plugins from conda, we'll use our own builds.
	sed -ie '/yosys/ d' ${root_dir}/env/f4pga/xc7_env/xc7_environment.yml

	$(call download_and_unpack_archive_from_url_fetched_from_url, \
		'https://github.com/f4pga/f4pga-arch-defs/releases/download/latest/symbiflow-xc7a50t_test-latest', \
		'${root_dir}/env/f4pga/symbiflow-xc7a50t_test-latest.url', \
		'${root_dir}/env/f4pga')

.PHONY: install-plugins
install-plugins:
	export PATH=${root_dir}/../image/bin:${PATH} && \
		make -C ${root_dir}/../third_party/yosys_f4pga_plugins \
			install_xdc install_fasm install_params install_sdc install_design_introspection

TOP_DIR := ${root_dir}
REQUIREMENTS_FILE := ${curr_dir}/f4pga_requirements.txt
ENVIRONMENT_FILE := ${curr_dir}/f4pga_environment.yml

include ${root_dir}/../third_party/make_env/conda.mk

# We need to apply the patch before Conda uses ${REQUIREMENTS_FILE}
${REQUIREMENTS_FILE}: ${IBEX}/.requirementspatch

# Install conda packages from f4pga/xc7 environment and set environment variables.
env:: download-f4pga install-plugins
	${IN_CONDA_ENV} conda env update --name ${CONDA_ENV_NAME} --file ${root_dir}/env/f4pga/xc7_env/xc7_environment.yml </dev/null | cat
	# Update edalize installed by Ibex to our own fork.
	${IN_CONDA_ENV} pip install git+https://github.com/antmicro/edalize@symbiflow-read_verilog_with_uhdm </dev/null | cat
	${IN_CONDA_ENV} conda env config vars set \
		F4PGA_INSTALL_DIR=${root_dir}/env/f4pga \
		F4PGA_SHARE_DIR=${root_dir}/env/f4pga/share/f4pga </dev/null | cat

# Ibex build

${IBEX}/.requirementspatch:
	cd ${IBEX} && git apply ${curr_dir}/dont_install_riscv_dv_requirements.patch && touch $@

${IBEX}/.gitf4pgapatch: ${IBEX}/.gitpatch
	cd ${IBEX} && git apply ${curr_dir}/prim_generic_clock_gating.patch
	cd ${IBEX} && git apply ${curr_dir}/pins_artya7_xdc.patch && touch $@

uhdm/yosys/synth-ibex-f4pga: clean-build ${IBEX}/.gitf4pgapatch
	# We're using ${IN_CONDA_ENV} to enter the conda environment instead of using virtualenv.
	(export PATH=${root_dir}/../image/bin:${PATH} \
		&& ${IN_CONDA_ENV} fusesoc --verbose --cores-root=${IBEX} \
			run --build --tool symbiflow --target=synth lowrisc:ibex:top_artya7_surelog --SRAMInitFile="${curr_dir}/led.vmem")

