#!/usr/bin/make -f

# Uncomment below to enable verbose build messages.
#export DH_VERBOSE = 1

# Enable all build hardening flags.  <https://wiki.debian.org/HardeningWalkthrough>
export DEB_BUILD_MAINT_OPTIONS = hardening=+all

# Export the DPKG default build flags.
DPKG_EXPORT_BUILDFLAGS = 1

# Include the default build flags.  <https://wiki.debian.org/Hardening#dpkg-buildflags>
include /usr/share/dpkg/buildflags.mk

# Append the CPPFLAGS to the standard CFLAGS and CXXFLAGS variables, which is how CMake likes it.  <https://wiki.debian.org/Hardening#Notes_for_packages_using_CMake>
# These are currently required or -D_FORTIFY_SOURCE=2 is not correctly passed to generate_translations_header.c.
CFLAGS += $(CPPFLAGS)
CXXFLAGS += $(CPPFLAGS)

%:
	dh $@

override_dh_auto_configure:
	# Build with the default DARCH.  Otherwise, the monero code will be optimized for the build environment, but may produce errors in other environments.  <https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1121565>
	# Disable AES (maes) on all architectures (it is already disabled for some architectures in the upstream code), as this is not part of the AMD64 baseline.  <https://wiki.debian.org/ArchitectureSpecificsMemo#amd64>
	# Link the executable as a non-PIE binary.  The static library libbc-ur.a shipped by libbc-ur-dev is not
	# built with -fPIC, so linking the PIE executable that upstream builds (upstream adds -pie to
	# CMAKE_EXE_LINKER_FLAGS) fails on amd64 with "relocation R_X86_64_32S against `.rodata' can not be used
	# when making a PIE object; recompile with -fPIE".  Passing -no-pie (which also overrides the PIE
	# default of the amd64 toolchain) makes the link succeed; the other hardening flags are kept.
	dh_auto_configure -- -DARCH=default -DNO_AES=ON -DCMAKE_EXE_LINKER_FLAGS=-no-pie
