#!/usr/bin/make -f

# Link against musl-libc on arm64, as dietlibc is not available on
# that platform.
ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)
ifeq ($(ARCH),arm64)
export CC = musl-gcc
else
export CC = diet gcc
endif
export DEB_CFLAGS_MAINT_APPEND = -Os
# Neither dietlibc nor musl libc is not compiled with -fPIC, so
# creating statically linked -fPIE executables won't work (also we'd
# need to use -Wl,-Bstatic instead of -static to make this work with
# crtbeginT.so even if dietlibc were to be compiled with -fPIC, see
# <https://bugzilla.redhat.com/show_bug.cgi?id=214465>)
# The static versions of other libc implementations (e.g. glibc) are
# also not compiled with -fPIC, so this is not unique to dietlibc or
# musl-libc.
# (dietlibc upstream is interested in supporting static PIE
# executables, so this will change in the future.)
export DEB_BUILD_MAINT_OPTIONS = hardening=+all,-pie

DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

%:
	dh $@  --with autoreconf

# Build two variants: the regular one with all features enabled,
# and a debugging version that is verbose.
VARIANTS = standard debug

override_dh_auto_build:     $(foreach variant,$(VARIANTS),dh_auto_build_$(variant))
override_dh_auto_install:   $(foreach variant,$(VARIANTS),dh_auto_install_$(variant))

override_dh_auto_configure: $(foreach variant,$(VARIANTS),dh_auto_configure_$(variant))
	sed 's%@@libexecdir@@%/usr/lib/$(DEB_HOST_MULTIARCH)%g' < debian/extra/mktirfs.in > debian/extra/mktirfs
	chmod 0755 debian/extra/mktirfs

override_dh_auto_clean:     $(foreach variant,$(VARIANTS),dh_auto_clean_$(variant))
	rm -f debian/extra/mktirfs

# Call both binaries -static (hence variant-name also for the standard
# variant), because that will not cause a lintian error, and the
# binaries are supposed to be statically linked.

dh_auto_configure_standard:
	dh_auto_configure -Bbuild.standard -- \
	  --enable-nfs4 --enable-uuid --enable-modules --disable-debug \
	  --with-variant-name=static

dh_auto_configure_debug:
	dh_auto_configure -Bbuild.debug -- \
	  --enable-nfs4 --enable-uuid --enable-modules --enable-debug \
	  --with-variant-name=debug-static

dh_auto_build_%:
	dh_auto_build -Bbuild.$(subst dh_auto_build_,,$@)

dh_auto_clean_%:
	dh_auto_clean -Bbuild.$(subst dh_auto_clean_,,$@)

dh_auto_install_%:
	dh_auto_install -Bbuild.$(subst dh_auto_install_,,$@)
