#!/usr/bin/make -f
# -*- makefile -*-

# Drop the "format" hardening feature (-Wformat -Werror=format-security):
# upstream valkey-json compiles with -Wno-format (CMakeLists.txt), which
# cancels -Wformat and leaves -Werror=format-security failing as
# "'-Wformat-security' ignored without '-Wformat'". All other hardening stays on.
export DEB_BUILD_MAINT_OPTIONS = hardening=+all,-format

# Path to valkeymodule.h. Defaults to the location shipped by
# percona-valkey-dev; overridable so a bundled header can be used for a
# fully self-contained / offline build.
VALKEY_MODULE_H ?= /usr/include/valkeymodule.h

%:
	dh $@

override_dh_auto_configure:
	cmake -S . -B obj-build \
	    -DCMAKE_BUILD_TYPE=Release \
	    -DBUILD_RELEASE=ON \
	    -DENABLE_UNIT_TESTS=OFF \
	    -DENABLE_INTEGRATION_TESTS=OFF \
	    -DVALKEY_MODULE_H_PATH=$(VALKEY_MODULE_H) \
	    -DFETCHCONTENT_SOURCE_DIR_RAPIDJSON=$(CURDIR)/deps/rapidjson

override_dh_auto_build:
	cmake --build obj-build -j$$(nproc)

override_dh_auto_install:
	install -d debian/percona-valkey-json/usr/lib/valkey/modules
	install -m 0755 obj-build/src/libjson.so \
	    debian/percona-valkey-json/usr/lib/valkey/modules/libjson.so
	# Embedded SBOM set (SPDX/CycloneDX json+xml, Syft table, license manifest),
	# generated from the built tree by the shipped gen-module-sbom.sh (fails loudly
	# if Syft is missing; installed by valkey_builder.sh --json_deps).
	sh gen-module-sbom.sh percona-valkey-json $$(dpkg-parsechangelog -S Version 2>/dev/null || echo 0) \
	    debian/percona-valkey-json/usr/share/percona-valkey-json/sbom .

override_dh_fixperms:
	dh_fixperms
	# Valkey refuses to load a module without the execute bit; dh_fixperms
	# normalises the .so back to 0644, so restore 0755 here.
	chmod 0755 debian/percona-valkey-json/usr/lib/valkey/modules/libjson.so

# The module build runs no tests during packaging.
override_dh_auto_test:

# Build the .deb with xz (not the zstd default on Ubuntu jammy+/Debian bookworm+):
# the Percona repo's reprepro can't read zstd-compressed control.tar/data.tar.
override_dh_builddeb:
	dh_builddeb -- -Zxz
