#!/usr/bin/make -f

include /usr/share/dpkg/buildopts.mk

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export QT_QPA_PLATFORM = offscreen

# The Rust backend is built by CMake through cargo, offline. Crates come from
# the archive's librust-*-dev packages; vendor/ holds only those the archive
# cannot provide (see debian/README.source). The archive versions differ from
# rust/Cargo.lock, so the lock file is emptied and cargo resolves afresh.
export CARGO_HOME = $(CURDIR)/debian/cargo_home
export RUSTFLAGS = --remap-path-prefix=$(CURDIR)=.
ifneq ($(DEB_BUILD_OPTION_PARALLEL),)
export CARGO_BUILD_JOBS = $(DEB_BUILD_OPTION_PARALLEL)
endif

# GIPHY and KLIPY application keys, read by cmake/GenerateGifBuildKeys.cmake
# into a build-tree header. The same keys as the Flathub manifest.
export LIGHTNING_BUILD_GIPHY_API_KEY = cAOuVhjN4BI5vtH3iYuWpgKewsME80MF
export LIGHTNING_BUILD_KLIPY_API_KEY = sYbUpgwUKt4tAZj8LFPoUPEDVzJxYtqO2PA4LcF1kO7pFYELZD60OvdkcxstuPxa

BUILDDIR = builddir

%:
	dh $@ --buildsystem=cmake+ninja --builddirectory=$(BUILDDIR)

# CMAKE_BUILD_TYPE must be exactly Release: that is what selects cargo's
# release profile. The WebRTC and Qt SVG requirements turn a missing
# dependency into a configure error instead of a silently reduced build.
# DeepFilterNet and RNNoise are off because their model weights carry no
# explicit licence.
override_dh_auto_configure:
	@test -d vendor || { \
	    echo "vendor/ is missing: build from the source package, or run debian/vendor-crates" >&2; \
	    exit 1; }
	debian/assemble-cargo-registry vendor $(CARGO_HOME)/registry
	printf '%s\n' \
	    '[source.crates-io]' \
	    'replace-with = "merged"' \
	    '' \
	    '[source.merged]' \
	    'directory = "$(CARGO_HOME)/registry"' \
	    > $(CARGO_HOME)/config.toml
	: > rust/Cargo.lock
	dh_auto_configure -- \
	    -DCMAKE_BUILD_TYPE=Release \
	    -DBUILD_TESTING=OFF \
	    -DENABLE_RUST_SDK_BACKEND=ON \
	    -DLIGHTNING_CARGO_LOCKED=OFF \
	    -DLIGHTNING_RUST_ONLY=ON \
	    -DLIGHTNING_ENABLE_WEBRTC=ON \
	    -DLIGHTNING_REQUIRE_WEBRTC=ON \
	    -DLIGHTNING_REQUIRE_QT_SVG=ON \
	    -DLIGHTNING_REQUIRE_GIF_KEYS=ON \
	    -DLIGHTNING_ENABLE_DEEPFILTERNET=OFF \
	    -DLIGHTNING_ENABLE_RNNOISE=OFF \
	    -DLIGHTNING_RUST_SYSTEM_SQLITE=ON \
	    -DLIGHTNING_PORTABLE_QT_ABI=ON \
	    -DLIGHTNING_DISABLE_CCACHE=ON \
	    -DLIGHTNING_ARTIFACT_KIND=release

# The upstream suite is not built here: with -DBUILD_TESTING=ON the test
# executables alone need roughly 85 GB of build space and the link runs out of
# disk (see docs/build-performance.md upstream), and most of what it exercises
# needs hardware, GStreamer or a D-Bus bus that a build chroot lacks. Prove the
# binary starts and that it carries only the Rust backend; debian/tests/control
# repeats the same two checks against the installed package.
override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	$(BUILDDIR)/lightning-matrix --version
	$(BUILDDIR)/lightning-matrix --build-info
endif

# The Rust staticlib is linked into lightning-matrix, and most of its crates
# come from archive librust-*-dev packages rather than from vendor/. Record
# those in Built-Using and Static-Built-Using. This reads the .d files cargo
# left in the target directory, so it has to run after the build; it also has
# to run after dh_prep, which deletes debian/*.substvars. Crates from vendor/
# are part of this source package and are not listed.
override_dh_prep:
	dh_prep
	CARGO_BUILT_USING_TARGET_DIR=$(BUILDDIR)/rust/release \
	CARGO_REGISTRY=$(CARGO_HOME)/registry \
	    /usr/share/cargo/bin/dh-cargo-built-using lightning-matrix
	@grep -q '^cargo:Static-Built-Using=.*rust-' debian/lightning-matrix.substvars || \
	    { echo 'Static-Built-Using lists no rust-* source package: the archive crates were not found' >&2; exit 1; }

override_dh_install:
	dh_install
	install -D -m 0644 packaging-ci/packaging/common/lightning.metainfo.xml \
	    debian/lightning-matrix/usr/share/metainfo/org.lightning_matrix.Lightning.metainfo.xml

override_dh_clean:
	dh_clean
	rm -rf debian/cargo_home
