#!/usr/bin/make -f

export PYBUILD_NAME = rapidarchive
# The Python module is built from python/rapidgzip (the upstream pip package).
# PYBUILD_DIR is honoured by every pybuild step (configure/build/install/test).
export PYBUILD_DIR = python/rapidgzip

# Build the Python module (python/rapidgzip) against Debian's system libraries
# instead of the vendored copies in src/external (which are not shipped).
export RAPIDGZIP_BUILD_ISAL = disable
export RAPIDGZIP_BUILD_ZLIB = system
export RAPIDGZIP_BUILD_RPMALLOC = disable
export RAPIDGZIP_BUILD_CXXOPTS = system

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

# Build C++ headers and CMake configs via CMake, Python via pybuild
override_dh_auto_configure:
	# Install the CMake package config under the non-multiarch lib dir:
	# librapidarchive-dev is Architecture: all and lintian rejects the amd64
	# triplet directory (usr/lib/x86_64-linux-gnu) in an arch:all package.
	# Overrides the lib/x86_64-linux-gnu that dh_auto_configure sets.
	dh_auto_configure --buildsystem=cmake -- \
		-DBUILD_TESTS=OFF \
		-DBUILD_PYTHON=OFF \
		-DWITH_ISAL=OFF \
		-DUSE_SYSTEM_ZLIB=ON \
		-DDEBIAN_BUILD=ON \
		-DCMAKE_INSTALL_LIBDIR=lib
	# WITH_ISAL is OFF since rapidarchive is using non-public interfaces declared in igzip_huffman.h
	# which is not shipped in libisal-dev
	# Thus replace embedded ISA-L build with a dummy interface target if system internal headers are missing
	dh_auto_configure --buildsystem=pybuild

override_dh_auto_build:
	# Limit parallelism: compiling the C++ sources (huge -fconstexpr-ops-limit)
	# at -j$(nproc) can OOM (cc1plus killed) in a memory-constrained buildd.
	dh_auto_build --buildsystem=cmake --max-parallel=2
	dh_auto_build --buildsystem=pybuild

override_dh_auto_install:
	# Install into debian/tmp (dh_auto_install default for multiple binary
	# packages); debian/librapidarchive-dev.install then picks the headers and
	# CMake config and dh_install copies them into the package directory.
	dh_auto_install --buildsystem=cmake
	dh_auto_install --buildsystem=pybuild

# The upstream C++ test suite (ctest) is not run during the build: several
# tests shell out to external compression tools (pigz, igzip, bgzip, the
# Python "pgzip" module) which are not (and should not be) build-dependencies
# of a header-only library, so e.g. testParallelGzipReader aborts in the
# build environment. Functional coverage is provided by the autopkgtests
# (debian/tests/python and debian/tests/cpp) instead.
override_dh_auto_test:
