#!/usr/bin/make -f

export DH_VERBOSE = 1
export DEB_BUILD_MAINT_OPTIONS = hardening=+bindnow

include /usr/share/dpkg/architecture.mk
ifeq ($(DEB_HOST_ARCH_OS),hurd)
  export DEB_CXXFLAGS_MAINT_APPEND = -DPATH_MAX=4096
endif

%:
	dh $@ --buildsystem=cmake

cmake_flags := \
	-DCMAKE_BUILD_TYPE=Release \
	-DBUILD_SHELL=TRUE \
	-DBUILD_UNITTESTS=FALSE \
	-DENABLE_SANITIZER=FALSE

# Sets MAJOR/MINOR/PATCH version variables and the SOVERSION on the shared
# library. DUCKDB_EXPLICIT_VERSION does NOT set these.
cmake_flags += \
	-DOVERRIDE_GIT_DESCRIBE=v1.5.1 \
	-DSET_DUCKDB_LIBRARY_VERSION=TRUE

# In-tree extensions to compile statically into libduckdb.so, in addition to
# the built-ins (parquet, core_functions, jemalloc).
cmake_flags += \
	-DBUILD_EXTENSIONS="json;icu;autocomplete;tpch;tpcds"

# Use system zstd and xxhash via shim headers in src/include/shims/zstd/.
cmake_flags += \
	-DUSE_SYSTEM_ZSTD=TRUE

# Use system snappy, lz4 and brotli for parquet codecs.
cmake_flags += \
	-DUSE_SYSTEM_SNAPPY=TRUE \
	-DUSE_SYSTEM_LZ4=TRUE \
	-DUSE_SYSTEM_BROTLI=TRUE

# Use system yyjson for JSON parsing.
cmake_flags += \
	-DUSE_SYSTEM_YYJSON=TRUE

# Use system mbedtls instead of bundled 3.6.x sources.
cmake_flags += \
	-DUSE_SYSTEM_MBEDTLS=TRUE

# Use system ICU instead of bundled ICU 66.1 which lacks loong64 support in
# double-conversion. C++17 is required by system ICU 75+ headers.
cmake_flags += \
	-DWITH_INTERNAL_ICU=FALSE \
	-DCMAKE_CXX_STANDARD=17

# Both are FALSE by default in the source.
# Keep disabled for Debian:
# users must explicitly run INSTALL and LOAD to avoid silent internet connections.
cmake_flags += \
	-DENABLE_EXTENSION_AUTOLOADING=FALSE \
	-DENABLE_EXTENSION_AUTOINSTALL=FALSE

override_dh_auto_configure:
	@echo " ### Build flags: $$(date '+%Y-%m-%dT%H:%M:%S')"
	@echo "  CFLAGS:   $$(dpkg-buildflags --get CFLAGS)"
	@echo "  CXXFLAGS: $$(dpkg-buildflags --get CXXFLAGS)"
	@echo "  CPPFLAGS: $$(dpkg-buildflags --get CPPFLAGS)"
	@echo "  LDFLAGS:  $$(dpkg-buildflags --get LDFLAGS)"
	dh_auto_configure -- $(cmake_flags)

# Static archives are needed during linking but are not shipped.
# Remove them because Salsa is running out of disk space.
override_dh_auto_install:
	dh_auto_install
	find debian/tmp/usr/lib -name '*.a' -delete

# Building all extensions statically into libduckdb produces a binary
# exceeding dwz's default 50M DIE limit; raise it to unlimited.
override_dh_dwz:
	dh_dwz -- -L none
