#!/usr/bin/make -f

# resolve DEB_HOST_ARCH_BITS
include /usr/share/dpkg/architecture.mk

# generate documentation unless nodoc requested
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
MANPAGES = debian/sccache.1
endif

# use local fork of dh-cargo and cargo wrapper
PATH := $(CURDIR)/debian/dh-cargo/bin:$(PATH)
PERL5LIB = $(CURDIR)/debian/dh-cargo/lib
export PATH PERL5LIB

# reduce risk of out-of-memory issues during build for weak arches
ifneq (,$(filter $(DEB_HOST_ARCH),armel armhf mipsel))
CARGO_ARGS += --no-default-features
CARGO_BUILD_ARGS += $(CARGO_ARGS) --bins
CARGO_TEST_ARGS += $(CARGO_ARGS) || true
CARGO_INSTALL_ARGS += $(CARGO_ARGS)
DEB_BUILD_OPTIONS += parallel=1
endif

%:
	dh $@ --buildsystem cargo

override_dh_auto_build:
	dh_auto_build --buildsystem cargo -- $(CARGO_BUILD_ARGS)

# ensure ccache is not in use during tests
override_dh_auto_test:
	PATH=$(CURDIR)/debian/dh-cargo/bin:/usr/sbin:/usr/bin:/sbin:/bin dh_auto_test --buildsystem cargo -- $(CARGO_TEST_ARGS)

override_dh_auto_install:
	dh_auto_install --buildsystem cargo -- $(CARGO_INSTALL_ARGS)

execute_after_dh_auto_install: $(MANPAGES)

# build manpages
debian/sccache.1: debian/%.1: debian/sccache/usr/bin/%
	help2man --section 1 --no-info \
		--name "a fast C/C++/Rust compiler cache" \
		--output $@ $< \
		|| { $1 --help; false; }
