#!/usr/bin/make -f

export PYBUILD_NAME = polars
export PYBUILD_DIR = py-polars
export PATH := /usr/share/cargo/bin:$(PATH)
export CARGO_HOME = $(CURDIR)/debian/cargo_home
export CARGO_TARGET_DIR = $(CURDIR)/target
export DEB_BUILD_MAINT_OPTIONS = hardening=+all reproducible=+fixfilepath optimize=-lto
export DEB_CARGO_CRATE = polars-runtime-32_2.1.0

# The native runtime exceeds this laptop's RAM with concurrent rustc jobs.
POLARS_CARGO_JOBS ?= 1
export CARGO_BUILD_JOBS = $(POLARS_CARGO_JOBS)
export CARGO_INCREMENTAL = 0
export CARGO_PROFILE_DEV_DEBUG = 0
export CARGO_PROFILE_RELEASE_DEBUG = false
export CARGO_PROFILE_RELEASE_CODEGEN_UNITS = 256
export RUSTFLAGS += -C debuginfo=0 -C link-arg=-fuse-ld=bfd -C link-arg=-Wl,--no-keep-memory

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

%:
	dh $@ --with python3 --buildsystem=pybuild

override_dh_auto_configure:
	dh_auto_configure --buildsystem=pybuild

override_dh_auto_build:
	dh_auto_build --buildsystem=pybuild
	# Refresh flags even when resuming after configuration with -nc.
	cargo prepare-debian debian/cargo_registry --link-from-system
	DEB_BUILD_OPTIONS='$(filter-out parallel=%,$(DEB_BUILD_OPTIONS)) parallel=$(POLARS_CARGO_JOBS)' \
		cargo build --release -p polars-runtime-32 \
		--config profile.release.lto=false \
		--no-default-features --features debian-local

override_dh_auto_install:
	dh_auto_install --buildsystem=pybuild
	install -d debian/python3-polars/usr/lib/python3/dist-packages/_polars_runtime_32
	install -m 644 py-polars/runtime/polars-runtime-32/_polars_runtime_32/__init__.py \
		debian/python3-polars/usr/lib/python3/dist-packages/_polars_runtime_32/
	install -m 644 py-polars/runtime/polars-runtime-32/_polars_runtime_32/build_feature_flags.py \
		debian/python3-polars/usr/lib/python3/dist-packages/_polars_runtime_32/
	install -m 644 target/$(DEB_HOST_RUST_TYPE)/release/lib_polars_runtime.so \
		debian/python3-polars/usr/lib/python3/dist-packages/_polars_runtime_32/_polars_runtime.abi3.so
	dh-cargo-built-using python3-polars

override_dh_auto_test:
	@echo "The installed-package smoke test is run by autopkgtest."

override_dh_auto_clean:
	dh_auto_clean --buildsystem=pybuild
	cargo clean
	rm -rf debian/cargo_home debian/cargo_registry
