#!/usr/bin/make -f

# output every command that modifies files on the build system.
#export DH_VERBOSE = 1

include /usr/share/dpkg/buildflags.mk

export NM = llvm-nm-21
export AR = llvm-ar-21
export CC = clang-21

export BUILTINS_LIB = /usr/lib/llvm-21/lib/clang/21/lib/wasi/libclang_rt.builtins-wasm32.a

# stack protector not yet supported, see
# https://github.com/CraneStation/wasi-libc/issues/157
# https://bugs.debian.org/1051815
export DEB_BUILD_MAINT_OPTIONS = hardening=-stackprotector,-branch,-stackclash

# no rustc dependency here, so no rustc/architecture.mk
export PATH := $(shell find /usr/lib/rustlib -type d -name bin | head -n1):$(PATH)

BUILDS = wasi wasip2

%:
	dh $@

override_dh_auto_configure: $(patsubst %,dh_auto_configure.%,$(BUILDS))
override_dh_auto_build:     $(patsubst %,dh_auto_build.%,$(BUILDS))
override_dh_auto_install:   $(patsubst %,dh_auto_install.%,$(BUILDS))

dh_auto_configure.%:
		mkdir -p debian/build/source_$*
		mkdir -p debian/build/build_$*
		cp -al $(filter-out debian, $(wildcard *)) debian/build/source_$*/
	  dh_auto_configure --sourcedirectory=debian/build/source_$* --builddirectory=debian/build/build_$*  -- -DBUILTINS_LIB=${BUILTINS_LIB} -DBINDINGS_TARGET=off -DCHECK_SYMBOLS=on -DUSE_WASM_COMPONENT_LD=off -DCMAKE_C_LINKER_DEPFILE_SUPPORTED=false -DTARGET_TRIPLE=wasm32-$* -DCMAKE_INSTALL_LIBDIR=lib

dh_auto_build.%:
		dh_auto_build --sourcedirectory=debian/build/build_$* --builddirectory=debian/build/build_$*

dh_auto_install.%:
		dh_auto_install --sourcedirectory=debian/build/build_$* --builddirectory=debian/build/build_$*
