#!/usr/bin/make -f

PKG_VERSION := $(shell dpkg-parsechangelog | awk '/^Version:/ {print $$2}')
LLVM_VERSION := 21
LLVM_VERSION_SUFFIX := dpcpp6
PARALLEL := $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))

# TODO: Enbable build with debug symbols
# Removing these lines causes the build to require large RAM and disk space
export DEB_CFLAGS_MAINT_STRIP = -g -O2
export DEB_CXXFLAGS_MAINT_STRIP = -g -O2

# Disable LTO as LP runners do not have sufficient memory to build LTO
export DEB_BUILD_MAINT_OPTIONS=optimize=-lto hardening=+bindnow

include /usr/share/dpkg/buildflags.mk

%:
	dh $@

override_dh_auto_configure:
	# override CMAKE_INSTALL_PREFIX to install files to debian/tmp/
	python3 $(CURDIR)/buildbot/configure.py \
		--l0-headers /usr/include/level_zero/ \
		--l0-loader /usr/lib/x86_64-linux-gnu/libze_loader.so \
		--cmake-opt="-DLLVM_USE_LINKER=gold" \
		--cmake-opt="-DLLVM_BINUTILS_INCDIR=/usr/include/" \
		--cmake-opt="-DCMAKE_BUILD_TYPE=Release" \
		--cmake-opt="-DCMAKE_INSTALL_PREFIX=${CURDIR}/debian/tmp/install/" \
		--cmake-opt="-DBOOTSTRAP_CMAKE_BUILD_TYPE=RelWithDebInfo" \
		--cmake-opt="-DLLVM_PARALLEL_LINK_JOBS=2" \
		--cmake-opt="-DCLANG_DEFAULT_LINKER=ld" \
		--cmake-opt="-DCLANG_DEFAULT_RTLIB=libgcc" \
		--cmake-opt="-DLLVM_ENABLE_RTTI=ON" \
		--cmake-opt="-DLLVM_BUILD_LLVM_DYLIB:BOOL=ON" \
		--cmake-opt="-DSYCL_UR_USE_FETCH_CONTENT=OFF" \
		--cmake-opt="-DLLVMGenXIntrinsics_SOURCE_DIR=${CURDIR}/vendor/vc-intrinsics" \
		--cmake-opt="-DFETCHCONTENT_FULLY_DISCONNECTED=ON" \
		--cmake-opt="-DUR_OPENCL_INCLUDE_DIR=/usr/include/" \
		--cmake-opt="-DUR_USE_EXTERNAL_UMF=ON" \
		--cmake-opt="-DUR_COMPUTE_RUNTIME_FETCH_REPO=OFF" \
		--cmake-opt="-DUR_COMPUTE_RUNTIME_REPO=${CURDIR}/vendor/compute-runtime/level_zero/include" \
		--cmake-opt="-DOpenCL_INCLUDE_DIR=/usr/include/" \
		--cmake-opt="-DLLVM_EXTERNAL_SPIRV_HEADERS_SOURCE_DIR=/usr/include/" \
		--cmake-opt="-DLLVM_VERSION_SUFFIX=${LLVM_VERSION_SUFFIX}" \

override_dh_auto_build:
	python3 ${CURDIR}/buildbot/compile.py -j${PARALLEL}
	help2man --name="clang LLVM compiler for oneAPI" --version-string=${PKG_VERSION} ${CURDIR}/build/bin/clang-${LLVM_VERSION} > clang-dpcpp.1
	help2man --name="lists all devices and backends discovered by SYCL" --version-string=${PKG_VERSION} ${CURDIR}/build/bin/sycl-ls > sycl-ls.1
	help2man --name="SPIRV to IR wrapper" --version-string=${PKG_VERSION} ${CURDIR}/build/bin/spirv-to-ir-wrapper > spirv-to-ir-wrapper.1

override_dh_auto_test:
	# Skip cross-compilation tests (AIX, WebAssembly, VE, z/OS, Windows, baremetal, etc.)
	LIT_FILTER_OUT='Driver/(aarch64-fmv|aix-|baremetal|cl-offload|ve-toolchain|wasm-toolchain|zos-|sycl-offload-win-old-model|sycl-offload\.c|sycl-embed-ir|sycl-host-compiler-old-model|sycl-native-cpu-fsycl|clang-offload-bundler-bc-archive-support-linux-old-model)|basic_tests/device-selectors-exception' \
		python3 ${CURDIR}/buildbot/check.py 2>&1 | tee test-results.log || true
	@echo "Checking test results..."
	@PERCENT=$$(grep "^  Failed" test-results.log | sed -n 's/.*(\([0-9.]*\)%).*/\1/p'); \
	if [ -n "$$PERCENT" ]; then \
		echo "Test failure rate: $$PERCENT%"; \
		if [ $$(awk "BEGIN {print ($$PERCENT >= 0.1)}") -eq 1 ]; then \
			echo "ERROR: Test failure rate too high ($$PERCENT% >= 0.1%)"; \
			exit 1; \
		fi; \
	else \
		echo "WARNING: Could not parse test failure rate"; \
	fi

override_dh_auto_install:
	# Install binaries to debian/tmp/usr
	cd build && ninja install

# TODO: Enable build with debug symbols
#       For now, this disables building .ddeb packages
override_dh_strip:
	dh_strip --no-automatic-dbgsym

override_dh_missing:
	# Installation tree is massive and includes thousands of superflous artifacts (mostly header files)
	# that we exclude for now as we do not expect user demand for these artifacts.
	dh_missing --list-missing

override_dh_makeshlibs:
	# exclude libs that are private/internal to the compiler
	dh_makeshlibs -Xusr/lib/llvm-dpcpp-${LLVM_VERSION}/lib

override_dh_clean:
	rm -rf build
	dh_clean
