#!/usr/bin/make -f

# Currently gcc can not be compiled with format-security and it is not needed
# for Ada code anyway
export DEB_BUILD_MAINT_OPTIONS = hardening=+all,-format

#export DH_VERBOSE = 1

# These variables are used to find and unpack the gcc source
GCC_DIR := /usr/src/gcc-12
GCC_VER := 12
GCC_TARBALL := $(notdir $(wildcard $(GCC_DIR)/gcc-*.tar.*))

include /usr/share/dpkg/default.mk
include /usr/share/ada/debian_packaging.mk

# This variable is used in Makefile.in to adjust src/version.in
export GHDL_VER_DESC := $(DEB_VENDOR) $(DEB_VERSION)

# Get parallel option to parallelize the gcc build specifically (Ada builds
# are already parallelized by code included from debian_packaging-*.mk above).
ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
    MAKEPARALLEL = -j$(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
endif

# build profile control over what backends get built
ifeq ($(filter pkg.ghdl.nomcode,$(DEB_BUILD_PROFILES)),)
    WITH_MCODE := 1
endif
ifeq ($(filter pkg.ghdl.nogcc,$(DEB_BUILD_PROFILES)),)
    WITH_GCC   := 1
endif
ifeq ($(filter pkg.ghdl.nollvm,$(DEB_BUILD_PROFILES)),)
    WITH_LLVM  := 1
endif

# force disable mcode build on non-x86 architectures where it isn't supported
ifeq ($(filter amd64 i386,$(DEB_HOST_ARCH_CPU)),)
    WITH_MCODE :=
endif

BUILDDIR := $(CURDIR)/builddir
TESTRUNDIR := $(CURDIR)/testrundir
CC := gnatgcc
export CC


%:
	dh ${@}

override_dh_clean:
	# An .orig file exists in the release, don't delete it to not create
	# spurious differences to the tarball/git.
	dh_clean -Xtestsuite/synth/synth14/top.vhdl.orig

override_dh_auto_clean:
	# dh_auto_clean complains about python-distutils and fails if it
	# doesn't see a Makefile, so override to only do a distclean when the
	# Makefile exists and do nothing otherwise.
	if [ -f Makefile ]; then \
		$(MAKE) distclean; \
	fi

override_dh_installdocs:
	dh_installdocs --link-doc=ghdl-common

override_dh_auto_configure:
	mkdir -p $(BUILDDIR)/mcode $(BUILDDIR)/llvm $(BUILDDIR)/gcc
	@echo
	@echo ------------------------------------------------------------
	@echo Configuring with mcode backend
	@echo ------------------------------------------------------------
	if [ -n "$(WITH_MCODE)" ]; then \
		cd $(BUILDDIR)/mcode && \
		../../configure --srcdir=../.. --prefix=/usr --disable-werror \
			--libdir=lib/ghdl/mcode --incdir=lib/ghdl/include \
			--disable-libghdl --enable-gplcompat; \
	fi
	@echo
	@echo ------------------------------------------------------------
	@echo Configuring with llvm backend
	@echo ------------------------------------------------------------
	if [ -n "$(WITH_LLVM)" ]; then \
		cd $(BUILDDIR)/llvm && \
		../../configure --srcdir=../.. --prefix=/usr --disable-werror \
			--libdir=lib/ghdl/llvm --incdir=lib/ghdl/include \
			--disable-libghdl --enable-gplcompat \
			--with-llvm-config; \
	fi
	@echo
	@echo ------------------------------------------------------------
	@echo Configuring with gcc backend
	@echo ------------------------------------------------------------
	# gcc unpack sequence cribbed from gcc-7-cross debian/rules
	# We have to disable the gcc-verbose-lto-link patch since it replaces
	# the LLINKER value with one that runs the linker under /usr/bin/time
	# and the spaces are not escaped properly somewhere on the ghdl side.
	set -e && \
	if [ -n "$(WITH_GCC)" ]; then \
		cd $(BUILDDIR)/gcc && \
		ln -sf ${GCC_DIR}/$(GCC_TARBALL) $(GCC_TARBALL) && \
		cp -a  ${GCC_DIR}/debian/ . && \
		if [ -n "$$(grep -v '^\#' ${CURDIR}/debian/gcc-patches/gcc-${GCC_VER}/series)" ]; then \
			cp -n ${CURDIR}/debian/gcc-patches/gcc-${GCC_VER}/*.diff debian/patches/  && \
			cat ${CURDIR}/debian/gcc-patches/gcc-${GCC_VER}/series >> debian/patches/series  && \
			sed -i "s/\(^series_stamp.*\)/debian_patches += $$(grep -v '^#' ${CURDIR}/debian/gcc-patches/gcc-${GCC_VER}/series|sed 's/\..*//'|tr '\n' ' ')\n\n\1/" debian/rules.patch ; \
		fi && \
		echo -n > $(BUILDDIR)/gcc/debian/patches/gcc-verbose-lto-link.diff && \
		debian/rules patch && \
		../../configure --srcdir=../.. --prefix=/usr --disable-werror \
			--libdir=lib/ghdl/gcc --incdir=lib/ghdl/include \
			--disable-libghdl --enable-gplcompat \
			--with-gcc=src && \
		make copy-sources && \
		mkdir -p gccbuild && \
		cd gccbuild && \
		../src/configure --prefix=/usr/lib/ghdl/gcc --enable-languages=vhdl \
			--enable-multiarch --enable-default-pie \
			--disable-bootstrap --disable-lto --disable-multilib \
			--disable-libssp --disable-libgomp --disable-libquadmath \
			--with-system-zlib --without-isl \
			$(DEB_TARGET_GNU_TYPE); \
	fi

override_dh_auto_build:
	@echo
	@echo ------------------------------------------------------------
	@echo Building with mcode backend
	@echo ------------------------------------------------------------
	if [ -n "$(WITH_MCODE)" ]; then \
		$(MAKE) -C $(BUILDDIR)/mcode; \
	fi
	@echo
	@echo ------------------------------------------------------------
	@echo Building with llvm backend
	@echo ------------------------------------------------------------
	if [ -n "$(WITH_LLVM)" ]; then \
		$(MAKE) -C $(BUILDDIR)/llvm; \
	fi
	@echo
	@echo ------------------------------------------------------------
	@echo Building with gcc backend
	@echo ------------------------------------------------------------
	if [ -n "$(WITH_GCC)" ]; then \
		$(MAKE) $(MAKEPARALLEL) -C $(BUILDDIR)/gcc/gccbuild && \
		$(MAKE) -C $(BUILDDIR)/gcc lib/ghdl/gcc/libgrt.a all.vpi && \
		$(MAKE) -C $(BUILDDIR)/gcc ghdllib \
			GHDL_GCC_BIN=$(BUILDDIR)/gcc/gccbuild/gcc/ghdl \
			GHDL1_GCC_BIN="--GHDL1=$(BUILDDIR)/gcc/gccbuild/gcc/ghdl1"; \
	fi

override_dh_auto_install:
	install -pD debian/ghdl.wrapper $(CURDIR)/debian/tmp/usr/bin/ghdl
	@echo
	@echo ------------------------------------------------------------
	@echo Installing with mcode backend
	@echo ------------------------------------------------------------
	if [ -n "$(WITH_MCODE)" ]; then \
		$(MAKE) -C $(BUILDDIR)/mcode install DESTDIR=../../debian/tmp; \
	fi
	@echo
	@echo ------------------------------------------------------------
	@echo Installing with llvm backend
	@echo ------------------------------------------------------------
	if [ -n "$(WITH_LLVM)" ]; then \
		$(MAKE) -C $(BUILDDIR)/llvm install DESTDIR=../../debian/tmp; \
	fi
	@echo
	@echo ------------------------------------------------------------
	@echo Installing with gcc backend
	@echo ------------------------------------------------------------
	# Place a symlink for the binary ahead of time so that the compilation
	# of the VHDL standard libraries works during the gcc install step. The
	# binary is properly moved afterwards.
	if [ -n "$(WITH_GCC)" ]; then \
		mkdir -p $(CURDIR)/debian/tmp/usr/bin && \
		ln -sf ../lib/ghdl/gcc/bin/ghdl-gcc $(CURDIR)/debian/tmp/usr/bin/ghdl-gcc && \
		$(MAKE) -C $(BUILDDIR)/gcc/gccbuild install \
			DESTDIR=$(CURDIR)/debian/tmp && \
		$(MAKE) -C $(BUILDDIR)/gcc install DESTDIR=$(CURDIR)/debian/tmp && \
		mv debian/tmp/usr/lib/ghdl/gcc/bin/ghdl-gcc debian/tmp/usr/bin/ghdl-gcc && \
		mv debian/tmp/usr/lib/ghdl/gcc/lib/ghdl/libbacktrace.a debian/tmp/usr/lib/ghdl/gcc/vhdl/libbacktrace.a; \
	fi
	@echo
	@echo ------------------------------------------------------------
	@echo Moving parts to required locations
	@echo ------------------------------------------------------------
	if [ -n "$(WITH_MCODE)" ]; then \
		$(RM) -r debian/tmp/usr/lib/ghdl/src && \
		mv debian/tmp/usr/lib/ghdl/mcode/vhdl/src debian/tmp/usr/lib/ghdl && \
		ln -s ../../src debian/tmp/usr/lib/ghdl/mcode/vhdl/src; \
	fi
	if [ -n "$(WITH_LLVM)" ]; then \
		$(RM) -r debian/tmp/usr/lib/ghdl/src && \
		mv debian/tmp/usr/lib/ghdl/llvm/vhdl/src debian/tmp/usr/lib/ghdl && \
		ln -s ../../src debian/tmp/usr/lib/ghdl/llvm/vhdl/src; \
	fi
	if [ -n "$(WITH_GCC)" ]; then \
		$(RM) -r debian/tmp/usr/lib/ghdl/src && \
		mv debian/tmp/usr/lib/ghdl/gcc/vhdl/src debian/tmp/usr/lib/ghdl && \
		ln -s ../../src debian/tmp/usr/lib/ghdl/gcc/vhdl/src; \
	fi
	$(RM) debian/tmp/usr/lib/ghdl/src/ieee2008/LICENSE

override_dh_dwz:
	# dwz currently has problems with some debug sections created by the
	# current LLVM compiler and would cause this step to fail.
	#dh_dwz -a -Nghdl-llvm
	# Furthermore it appears to have problems with something in
	# libghdlvpi.so and runs into assertions, so disable it altogether for
	# now.

override_dh_auto_test:
	@echo
	@echo ------------------------------------------------------------
	@echo Run testsuite
	@echo ------------------------------------------------------------
	# With some paths patched for the Debian packaging, the tests will not
	# work right when run from the build directory. To keep it simple
	# simply install into a temporary location and tell the testsuite to
	# find ghdl there.
	if [ -n "$(WITH_MCODE)" ]; then \
		$(MAKE) -C $(BUILDDIR)/mcode install DESTDIR=$(TESTRUNDIR)/mcode && \
		debian/tests/ghdl-tests buildtest mcode; \
	fi
	if [ -n "$(WITH_LLVM)" ]; then \
		$(MAKE) -C $(BUILDDIR)/llvm install DESTDIR=$(TESTRUNDIR)/llvm && \
		debian/tests/ghdl-tests buildtest llvm; \
	fi
	# Place a symlink for the binary ahead of time so that the compilation
	# of the VHDL standard libraries works during the gcc install step. The
	# binary is properly moved afterwards (so that it can find its
	# libraries as relative paths from the executable's location).
	if [ -n "$(WITH_GCC)" ]; then \
		mkdir -p $(TESTRUNDIR)/gcc/usr/bin && \
		ln -sf ../lib/ghdl/gcc/bin/ghdl-gcc $(TESTRUNDIR)/gcc/usr/bin/ghdl-gcc && \
		$(MAKE) -C $(BUILDDIR)/gcc/gccbuild install DESTDIR=$(TESTRUNDIR)/gcc && \
		$(MAKE) -C $(BUILDDIR)/gcc install DESTDIR=$(TESTRUNDIR)/gcc && \
		mv $(TESTRUNDIR)/gcc/usr/lib/ghdl/gcc/bin/ghdl-gcc \
		   $(TESTRUNDIR)/gcc/usr/bin/ghdl-gcc && \
		mv $(TESTRUNDIR)/gcc/usr/lib/ghdl/gcc/lib/ghdl/libbacktrace.a \
		   $(TESTRUNDIR)/gcc/usr/lib/ghdl/gcc/vhdl/libbacktrace.a && \
		ln -sf lib/ghdl/gcc/libexec $(TESTRUNDIR)/gcc/usr/libexec && \
		debian/tests/ghdl-tests buildtest gcc; \
	fi

override_dh_gencontrol:
	if [ -n "$(WITH_GCC)" ]; then \
		dh_gencontrol -- -VBuilt-Using-GCC="$(shell dpkg-query -f '$${Source} (= $${Version})' -W gcc-$(GCC_VER)-source)"; \
	else \
		dh_gencontrol; \
	fi
