#!/usr/bin/make -f
include /usr/share/dpkg/architecture.mk
include /usr/share/dpkg/buildflags.mk
include /usr/share/rustc/architecture.mk
export CFLAGS CXXFLAGS CPPFLAGS LDFLAGS
export DEB_HOST_RUST_TYPE DEB_HOST_GNU_TYPE
export CARGO_PROFILE_RELEASE_DEBUG=true
export SKIP_UTILS=stdbuf

# Use lld whenever it is available: it links the big multicall binary much
# faster than GNU ld. Fall back to the default linker on arches where lld is
# not installed. Always ask the linker to print its version banner (-Wl,-v)
# so the build log records which linker actually ran.
LLD := $(shell command -v ld.lld 2>/dev/null)
ifneq ($(LLD),)
export RUSTFLAGS = -C link-arg=-fuse-ld=lld -C link-arg=-Wl,-v
else
export RUSTFLAGS = -C link-arg=-Wl,-v
endif

# On s390x the all-utils multicall build hits the 150min buildd inactivity
# timeout. The cost is LLVM doing fat LTO of one giant codegen unit with full
# debug info (not the final ld step), so disable LTO, split codegen and trim
# debuginfo for *every* cargo invocation (build, test and install) via cargo's
# profile env overrides - the previous sed only touched the install step.
ifeq ($(DEB_HOST_ARCH),s390x)
export CARGO_PROFILE_RELEASE_LTO = false
export CARGO_PROFILE_RELEASE_CODEGEN_UNITS = 16
export CARGO_PROFILE_RELEASE_DEBUG = 1
endif
VERSION := $(shell dpkg-parsechangelog -S Version|sed -e "s|.*~\(.*\)-.*|\1|g")
UPSTREAM_VERSION := $(shell dpkg-parsechangelog -S Version|cut -d- -f1)

# Build a multicall binary instead of multiple binaries
# to reduce the storage footprint
export MULTICALL = y

%:
	dh $@ --buildsystem cargo

override_dh_auto_test:
ifeq ($(DEB_HOST_ARCH),s390x)
	# The release test build (every util + every test) is what actually hits
	# the buildd inactivity timeout on s390x. Tests are non-gating here (||true)
	# anyway, so skip them and rely on the other arches for coverage.
	@echo "Skipping the test suite on s390x to stay under the buildd timeout"
else
	# for now, don't fail the build if tests are failing
	CARGO_HOME=$(CURDIR)/debian/cargo_home make PROFILE=release MULTICALL=$(MULTICALL) test||true
endif

override_dh_auto_install:
	CARGO_HOME=$(CURDIR)/debian/cargo_home DEB_CARGO_CRATE=coreutils_$(VERSION) /usr/share/cargo/bin/cargo prepare-debian debian/cargo_registry --link-from-system
# s390x LTO/debuginfo/codegen are handled globally via CARGO_PROFILE_RELEASE_*
# env overrides at the top of this file, so no Cargo.toml editing is needed here.
ifeq ($(DEB_HOST_ARCH_BITS),32)
# Use thin LTO on 32-bit to avoid running out of address space.
	sed -i 's/lto = true/lto = "thin"/' Cargo.toml
endif
	CARGO_HOME=$(CURDIR)/debian/cargo_home DESTDIR=$(CURDIR)/debian/tmp/ make SELINUX_ENABLED=1 PROFILE=release MULTICALL=$(MULTICALL) install
ifeq ($(DEB_HOST_ARCH_BITS),32)
	sed -i 's/lto = "thin"/lto = true/' Cargo.toml
endif
# Create the symlink early to be able to generate the manpages
	cd debian/tmp/usr/share/zsh/site-functions && mkdir rust-coreutils && chmod -x _* && mv _* rust-coreutils
	cd debian/tmp/usr/share/bash-completion/completions/ && chmod -x rust-*
	cd debian/tmp/usr/share/fish/vendor_completions.d/ && chmod -x *.fish
	cd debian/tmp/usr/bin && mv rust-coreutils coreutils && rm rust-*
#	cd debian/tmp/usr/share/fish/vendor_completions.d && for f in *fish; do mv $$f rust-$$f; done
	dh_link

override_dh_missing:
	dh_missing --fail-missing

override_dh_dwz:
	# Don't do anything. fails because of the
	# https://github.com/rust-lang/rust/issues/66118

execute_after_dh_auto_configure:
	# Log the toolchain and linker up front, on every arch, so the build log
	# always records which linker was used - even if a later step times out
	# before any link runs. The leading '-' keeps the build going on the rare
	# arch where ld.lld is absent (we fall back to the default linker there).
	rustc --version --verbose
	-ld.lld --version
	-ld --version | head -n1
ifeq ($(DEB_HOST_ARCH_BITS),32)
	# use lower debuginfo level on 32-bit to avoid running out of address space.
	if test -f debian/cargo_home/config; then \
		sed -i s/debuginfo=2/debuginfo=1/ debian/cargo_home/config; \
	fi
endif

override_dh_clean:
	rm -f Cargo.toml.orig Cargo.lock
	dh_clean --exclude=Cargo.toml.orig --exclude=config.guess

override_dh_update_autotools_config:
	# don't do anything - impact vendoring otherwise
