#!/usr/bin/make -f
VERSION=0.14

export DEB_BUILD_MAINT_OPTIONS=hardening=+all
export ZIG_GLOBAL_CACHE_DIR=/tmp/zig-cache
%:
	dh $@ --buildsystem=cmake

override_dh_auto_configure:
	ln -s /usr/lib/llvm-20/lib/clang/20/include/ lib/
	# without specifying MCPU, it targets native
	#	we want to specify -DZIG_TARGET_TRIPLE=$(DEB_HOST_MULTIARCH), but it breaks PIE
	dh_auto_configure -- \
		-DZIG_PIE=ON \
		-DZIG_SHARED_LLVM=ON \
		-DZIG_TARGET_MCPU=baseline

# output of build is stage3/bin and stage3/lib
export BBASE:=obj-$(DEB_HOST_MULTIARCH)/stage3

# make a man page and move the docs to usr/share/doc
override_dh_auto_install:
	help2man -s 1 -N -n "zig compiler and build tool" -v version $(BBASE)/bin/zig -o zig$(VERSION).1
	mkdir -p debian/tmp/usr/share/doc/zig$(VERSION)
	mv $(BBASE)/lib/zig/docs/* debian/tmp/usr/share/doc/zig$(VERSION)/
	rmdir $(BBASE)/lib/zig/docs
	dh_auto_install

	# dh_shlibdeps fails to get dependency information for /usr/lib/llvm-20/lib/libLLVM.so.20.1
	# llvm-20-dev is explicitly added as a runtime dependency of zig0.14
	chrpath -d debian/tmp/usr/bin/zig

	# Create symlink /usr/bin/zig0.14
	ln -s ../lib/zig0.14/bin/zig debian/tmp/usr/bin/zig0.14

override_dh_auto_test:
	$(BBASE)/bin/zig test test/behavior.zig
	dh_auto_test
