#!/usr/bin/make -f

include /usr/share/dpkg/architecture.mk

builddir = obj-$(DEB_HOST_GNU_TYPE)

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export PKG_CONFIG_LIBDIR = /usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig:/usr/share/pkgconfig
unexport PKG_CONFIG_PATH
unexport CMAKE_PREFIX_PATH
unexport LD_LIBRARY_PATH
unexport LD_PRELOAD
unexport QORE_AOT_LINK_CONF
unexport QORE_AOT_BIG_FN_THRESHOLD
unexport QORE_AOT_NO_DEBUG_INFO
unexport QORE_DOC_DEFINES
unexport QORE_INCLUDE_DIR
unexport QORE_INCLUDE_DIRS
unexport QORE_LIBDIR
unexport QORE_MODULE_DIR
# Tests, documentation, metadata extraction and qcc must see only the module
# paths supplied by this build, not optional modules from an older installation.
export QORE_MODULE_DIR_ONLY = 1
# Adaptive intra-module codegen partitions depend on transient jobserver tokens
# and change object bytes. Keep each qcc emission single-partition; debhelper
# still compiles independent modules in parallel.
export QCC_JOBS = 1
unexport QCC_SPLIT_THRESHOLD
# Schemas contain date values parsed during AOT compilation. Give those values
# a canonical build timezone without changing the installed runtime's timezone.
export TZ = UTC

# Documentation is generated only for builds that include qore-doc.
build_docs = $(if $(filter nodoc,$(DEB_BUILD_OPTIONS) $(DEB_BUILD_PROFILES)),,$(filter qore-doc,$(shell dh_listpackages)))

%:
	dh $@ --buildsystem=cmake --builddirectory=$(builddir)

override_dh_auto_configure:
	# Native build tools execute during compilation, including qpp and qcc.
	test '$(DEB_BUILD_GNU_TYPE)' = '$(DEB_HOST_GNU_TYPE)'
	dh_auto_configure -- \
		-DCMAKE_BUILD_TYPE=None \
		-DCMAKE_SKIP_INSTALL_RPATH=ON \
		-DCMAKE_BUILD_RPATH_USE_ORIGIN=ON \
		'-DCMAKE_IGNORE_PATH=/usr/local/include;/usr/local/lib;/usr/local/lib64' \
		-DCMAKE_IGNORE_PREFIX_PATH=/usr/local \
		-DOPENSSL_ROOT_DIR=/usr \
		-DOPENSSL_INCLUDE_DIR=/usr/include \
		-DOPENSSL_SSL_LIBRARY=/usr/lib/$(DEB_HOST_MULTIARCH)/libssl.so \
		-DOPENSSL_CRYPTO_LIBRARY=/usr/lib/$(DEB_HOST_MULTIARCH)/libcrypto.so \
		-DFETCHCONTENT_FULLY_DISCONNECTED=ON \
		-DQORE_REQUIRE_SYSTEM_DEPENDENCIES=ON \
		-DQORE_BUILD_WITH_EXTERNAL_MODULES=OFF \
		-DQORE_AOT_LINK_SOURCE_MODULES=OFF \
		-DQORE_AOT_MODULE_OPT_LEVEL=$(if $(filter noopt,$(DEB_BUILD_OPTIONS)),0,3) \
		-DCMAKE_DISABLE_FIND_PACKAGE_Doxygen=$(if $(build_docs),OFF,ON) \
		-DQORE_WITH_ONNXRUNTIME=ON \
		-DQORE_REQUIRE_ONNXRUNTIME=ON \
		-DSINGLE_COMPILATION_UNIT=1

override_dh_auto_build:
	dh_auto_build
ifneq (,$(build_docs))
	dh_auto_build -- docs
endif

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	PYTHONDONTWRITEBYTECODE=1 python3 debian/tests/test_aot_metadata.py
	PYTHONDONTWRITEBYTECODE=1 python3 debian/tests/test_artifacts.py
	PYTHONDONTWRITEBYTECODE=1 python3 debian/tests/test_source.py
	PYTHONDONTWRITEBYTECODE=1 QORE_QPP_EXECUTABLE=$(CURDIR)/$(builddir)/qpp \
		QORE_QCC_EXECUTABLE=$(CURDIR)/$(builddir)/qcc QORE_BINARY=$(CURDIR)/$(builddir)/qore \
		python3 -m unittest discover -s examples/test/cmake
	dh_auto_build -- linenoise-unicode-test
	$(builddir)/modules/linenoise/linenoise-unicode-test
	QORE_BINARY=$(CURDIR)/$(builddir)/qore \
		QORE_TEST_QMOD_DIR=$(CURDIR)/$(builddir)/qlib-qmod \
		PATH=$(CURDIR)/$(builddir):$$PATH \
		./run_tests.sh -E -d qore
else
	:
endif

override_dh_auto_install:
	dh_auto_install
	# The compiler fingerprint is architecture-dependent data. Keep it out of
	# /usr/bin, where dh_fixperms treats every file as an executable command.
	mv debian/tmp/usr/bin/qcc-format.stamp \
		debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/qore/qcc-format.stamp
	sed -i 's|/usr/bin/qcc-format.stamp|/usr/lib/$(DEB_HOST_MULTIARCH)/qore/qcc-format.stamp|' \
		debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/cmake/Qore/QoreConfig.cmake
	# Installed scripts must use the packaged interpreter regardless of PATH.
	for script in debian/tmp/usr/bin/* debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/cmake/Qore/*; do \
		if LC_ALL=C grep -Iq '^#!/usr/bin/env qore$$' "$$script"; then \
			sed -i '1s|^#!/usr/bin/env qore$$|#!/usr/bin/qore|' "$$script"; \
		fi; \
	done

override_dh_installdocs:
	dh_installdocs --remaining-packages -Nqore-doc
ifneq (,$(build_docs))
	dh_installdocs -pqore-doc --doc-main-package=qore-doc $(builddir)/docs
	python3 debian/clean-doxygen-cache.py debian/qore-doc/usr/share/doc/qore-doc
else
ifneq (,$(filter qore-doc,$(shell dh_listpackages)))
	dh_installdocs -pqore-doc --doc-main-package=qore-doc
endif
endif

override_dh_missing:
	dh_missing --fail-missing

override_dh_dwz:
	# AOT-compiled .qmod files contain LLVM DWARF sections that dwz on
	# current Debian/Ubuntu releases does not understand.
	dh_dwz -Nqore-stdlib

override_dh_strip_nondeterminism:
	dh_strip_nondeterminism --remaining-packages -Nqore-doc
ifneq (,$(build_docs))
	# Doxygen text is already deterministic. Avoid probing every HTML, script,
	# stylesheet and SVG for executable ZIP content; keep image/archive handling.
	dh_strip_nondeterminism -pqore-doc -X.html -X.js -X.css -X.svg
endif

override_dh_strip:
	# strip and objcopy discard Qore's EOF trailers. Preserve the dependency,
	# source-location and optional-module metadata around all dh_strip edits.
	python3 debian/preserve-aot-metadata.py debian/qore-stdlib dh_strip

override_dh_gencontrol:
	# The public LLVM plugin headers must match the LLVM used by libqore.
	if dh_listpackages | grep -qx libqore-dev; then \
		llvm_dep=$$(sed -n 's/^set(QORE_LLVM_MAJOR_VERSION \([0-9][0-9]*\))$$/llvm-\1-dev/p' \
			debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/cmake/Qore/QoreConfig.cmake); \
		test -n "$$llvm_dep" || exit 1; \
		dh_gencontrol -plibqore-dev -- "-Vqore:LLVM-Depends=$$llvm_dep"; \
	fi
	dh_gencontrol --remaining-packages
