#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS = hardening=+bindnow

include /usr/share/dpkg/architecture.mk

# Only riscv64 can use the RV64 backend present in this source tree.
# Other architectures use the upstream interpreter configuration.
ifeq ($(DEB_HOST_ARCH),riscv64)
RVTRANS_ARCH_FLAGS = -DRV64=ON -DRV64_DYNAREC=ON
else
RVTRANS_ARCH_FLAGS = -DRV64=OFF -DRV64_DYNAREC=OFF
endif

# Match the upstream x86-64 build instructions for long double and alignment.
ifeq ($(DEB_HOST_ARCH),amd64)
RVTRANS_ARCH_FLAGS += -DLD80BITS=ON -DNOALIGN=ON
endif

%:
	dh $@ --buildsystem=cmake --builddirectory=debian/build

override_dh_auto_configure:
	dh_auto_configure -- \
		-DCMAKE_BUILD_TYPE=RelWithDebInfo \
		-DNOLOADADDR=ON \
		$(RVTRANS_ARCH_FLAGS) \
		-DARM_DYNAREC=OFF \
		-DLARCH64_DYNAREC=OFF \
		-DNOGIT=ON \
		-DNO_LIB_INSTALL=ON \
		-DNO_CONF_INSTALL=ON

# Use rvtrans.install for the executable: upstream's install rule skips it
# on x86 hosts. Keep the Debian package contents consistent across hosts.
override_dh_auto_install:
	@:

# The upstream CTest suite executes bundled x86 binaries, which are removed
# from the repacked source. Exercise the newly built native executable instead.
override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
ifeq ($(DEB_BUILD_ARCH),$(DEB_HOST_ARCH))
	debian/build/rvtrans --version
	debian/build/rvtrans --help
else
	@echo "Skipping native smoke test when cross-compiling."
endif
else
	@echo "Skipping smoke test because DEB_BUILD_OPTIONS contains nocheck."
endif

# CMake's clean target removes checked-in wrapper sources declared as
# byproducts. dh_clean removes the build directory via debian/clean instead.
override_dh_auto_clean:
	@:
