#!/usr/bin/make -f

include /usr/share/dpkg/architecture.mk
include /usr/share/dpkg/buildflags.mk
include /usr/share/dpkg/pkg-info.mk
include /usr/share/rustc/architecture.mk

export CFLAGS CXXFLAGS CPPFLAGS LDFLAGS
export DEB_HOST_RUST_TYPE DEB_HOST_GNU_TYPE
export PATH := /usr/share/cargo/bin:$(PATH)
export CARGO_HOME := $(CURDIR)/debian/cargo_home
export DEB_CARGO_CRATE := raygeo_$(DEB_VERSION_UPSTREAM)
export PYBUILD_NAME=raygeo

ifneq (,$(filter i386 armhf,$(DEB_HOST_ARCH)))
# Keep the native extension within the 32-bit compiler's memory limits.
export CARGO_PROFILE_RELEASE_OPT_LEVEL := 1
export CARGO_PROFILE_RELEASE_LTO := off
export RUSTFLAGS += -Cdebuginfo=line-tables-only
endif

%:
	dh $@ --buildsystem=pybuild

ifneq (,$(filter i386 armhf,$(DEB_HOST_ARCH)))
override_dh_auto_build:
	# The Debian Cargo wrapper reads its job count from DEB_BUILD_OPTIONS.
	env DEB_BUILD_OPTIONS="$(strip $(filter-out parallel parallel=%,$(DEB_BUILD_OPTIONS)) parallel=1)" dh_auto_build --no-parallel
endif

execute_before_dh_auto_configure:
	cargo prepare-debian debian/cargo_registry --link-from-system

execute_before_dh_auto_build:
	if test -f Cargo.lock; then mv Cargo.lock Cargo.lock.saved; fi
	cargo generate-lockfile --offline

execute_before_dh_auto_clean:
	if test -f Cargo.lock.saved; then mv Cargo.lock.saved Cargo.lock; fi
	rm -rf debian/cargo_home debian/cargo_registry

override_dh_clean:
	dh_clean -X Cargo.toml.bak
