#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk

# Final build flags passed to gn.
FINAL_CFLAGS := $(filter-out -Werror=implicit-function-declaration,${CFLAGS}) ${CPPFLAGS}
FINAL_CXXFLAGS := ${CXXFLAGS} ${CPPFLAGS}

# See https://lintian.debian.org/tags/shared-library-lacks-version.html
# libperfetto.so is a private shared library. To address "shared-library-lacks-version"
# lintian warning, it is installed in a subdirectory of /usr/lib. The rpath is
# needed so the other binaries in this package can find it.
FINAL_LDFLAGS := ${LDFLAGS} -Wl,-rpath=/usr/lib/perfetto

# Build directory for gn/ninja.
BUILD_DIR := out/release

%:
	dh $@

override_dh_auto_configure:
	gn gen ${BUILD_DIR} --args="\
	  is_debug=false \
	  use_custom_libcxx=false \
	  is_hermetic_clang=false \
	  is_system_compiler=true \
	  is_clang=false \
	  skip_buildtools_check=true \
	  enable_perfetto_integration_tests=false \
	  enable_perfetto_unittests=false \
	  enable_perfetto_x64_cpu_opt=false \
	  perfetto_use_system_protobuf=true \
	  perfetto_use_system_zlib=true \
	  perfetto_enable_git_rev_version_header=false \
	  extra_cflags=\"${FINAL_CFLAGS}\" \
	  extra_cxxflags=\"${FINAL_CXXFLAGS}\" \
	  extra_ldflags=\"${FINAL_LDFLAGS}\" \
	  cc=\"${CC}\" \
	  cxx=\"${CXX}\" \
	  host_cpu=\"$(shell debian/get-cpu)\" \
	"

override_dh_auto_build:
	# Build binaries.
	ninja -v -C ${BUILD_DIR} perfetto traced traced_probes

	# Generate manpages.
	debian/make-manpage docs/reference/perfetto-cli.md debian/perfetto.1
	debian/make-manpage docs/reference/traced.md debian/traced.8
	debian/make-manpage docs/reference/traced_probes.md debian/traced_probes.8

# Ensure the traced and traced-probes services are not enabled or started on install.
override_dh_installsystemd:
	dh_installsystemd traced.service --no-enable --no-start
	dh_installsystemd traced-probes.service --no-enable --no-start

override_dh_auto_clean:
	rm -rf ${BUILD_DIR}

licensecheck:
	licensecheck --deb-machine -r * \
		> debian/copyright_newhints
	cmp debian/copyright_hints debian/copyright_newhints \
		&& rm debian/copyright_newhints
