#!/usr/bin/make -f

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

export DPKG_EXPORT_BUILDFLAGS=1
export DEB_BUILD_MAINT_OPTIONS=hardening=+bindnow

include /usr/share/dpkg/buildflags.mk

execute_before_dh_auto_configure:
	# ignore blhc false positives where tools are just listed
	@echo 'blhc: ignore-line-regexp:  +LD +ld'
	@echo 'blhc: ignore-line-regexp:  +CXX +g\+\+'

# Use the built-in (vendored) nanopb instead of the system libnanopb.
# The system /usr/lib/<triplet>/libprotobuf-nanopb.a is not compiled with -fPIC
# on amd64, so linking it into the shared library fails with:
#   relocation R_X86_64_32S against `.text' can not be used when making a
#   shared object; recompile with -fPIC
# The vendored nanopb sources are compiled as part of librdkafka with the
# regular build flags (including -fPIC), which links cleanly.
override_dh_auto_configure:
	dh_auto_configure -- \
		--enable-c11threads \
		--enable-cjson-ext \
		--disable-nanopb-ext

# Upstream vendors pregenerated opentelemetry headers and C files under 
# src/opentelemetry/*.pb.*. Regenerate them here using nanopb, which puts
# them under the path src/opentelemetry/proto/
# 
# This needs a source-level patch alongside it:
#   debian/patches/Use-nanopb-generated-header-paths-for-opentelemetry.patch
execute_before_dh_auto_build:
	-mv src/opentelemetry src/opentelemetry.vendor
	nanopb_generator \
		--options-file=src/opentelemetry.vendor/metrics.options \
		--output-dir=src/ \
		-L "#include <nanopb/%s>" \
		-I/usr/include \
		/usr/include/opentelemetry/proto/common/v1/common.proto \
		/usr/include/opentelemetry/proto/resource/v1/resource.proto\
		/usr/include/opentelemetry/proto/metrics/v1/metrics.proto

execute_after_dh_auto_clean:
	rm -rf src/opentelemetry/proto/
	-rmdir src/opentelemetry
	-mv src/opentelemetry.vendor/ src/opentelemetry

%:
	dh $@
