#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

# Usually, dh_auto_configure initializes the build flags but we also need them
# for dh_auto_build where we append CPPFLAGS to CXXFLAGS so that the pioasm
# build does not ignore the CPPFLAGS.
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk

# for DEB_VERSION_UPSTREAM
include /usr/share/dpkg/pkg-info.mk

# for DEB_HOST_GNU_TYPE
include /usr/share/dpkg/architecture.mk

# we do not ship the resulting binaries but we want to make sure that the sdk
# can be built on this architecture
CMAKEFLAGS += -DPICO_SDK_TESTS_ENABLED=ON

# other than the source, we ship the docs
CMAKEFLAGS += -DPICO_BUILD_DOCS=ON

# setting PICO_EXAMPLES_PATH=/dev/null to avoid requiring
# a git clone of https://github.com/raspberrypi/pico-examples
CMAKEFLAGS += -DPICO_EXAMPLES_PATH=/dev/null

# to avoid a circular build dependency loop with picotool, it gets disabled
# it will skip anything that requires picotool, i.e UF2s for the test programs
CMAKEFLAGS += -DPICO_NO_PICOTOOL=ON

# Do not run in parallel as different cmake invocations override each other's
# output. This also makes blhc fail.
# The underlying issue is that the upstream build system spawns cmake children
# via add_custom_command(COMMAND ${CMAKE_COMMAND}) so this is not even cmake's
# fault.
%:
	dh $@ --no-parallel

execute_before_dh_auto_configure-indep:
	mkdir -p ./lib
	ln -s /usr/src/tinyusb ./lib/tinyusb
	test -e ./lib/tinyusb/src/portable/raspberrypi/rp2040

override_dh_auto_configure-indep:
	# Ignore dpkg-buildflags because the arch-indep build is running
	# arm-none-eabi-gcc for which the host-arch flags are nonsensical.
	# Setting CFLAGS and CXXFLAGS prevents dh from setting them.
	env CFLAGS=-DPICO_PROGRAM_BUILD_DATE="\\\"$(shell LC_ALL=C date -u --date=@"$(SOURCE_DATE_EPOCH)" --iso-8601)\\\"" \
		CPPFLAGS= CXXFLAGS= \
		dh_auto_configure -- $(CMAKEFLAGS)

override_dh_auto_configure-arch:
	dh_auto_configure --sourcedirectory=tools/pioasm --builddirectory=pioasm-obj-$(DEB_HOST_GNU_TYPE) -- -DPIOASM_VERSION_STRING=$(DEB_VERSION_UPSTREAM)

override_dh_auto_build:
	# debhelper only appends CPPFLAGS to CFLAGS and CXXFLAGS during
	# configure but the value of these environment variables affects the
	# indep build
	env CXXFLAGS="$$CXXFLAGS $$CPPFLAGS" CFLAGS="$$CFLAGS $$CPPFLAGS" dh_auto_build

override_dh_installdocs-indep:
	cp -a debian/roboto-debian.css obj-$(DEB_HOST_GNU_TYPE)/docs/doxygen/html/
	dh_installdocs obj-$(DEB_HOST_GNU_TYPE)/docs/doxygen/html/

execute_after_dh_auto_build-indep:
	dh_auto_build -- docs

# native architecture build flags are not supposed to be used by anything
# compiled with arm-none-eabi compilers
execute_before_dh_auto_build-arch:
	@echo 'blhc: ignore-line-regexp: .* /usr/\S+/arm-none-eabi-gcc .*'
	@echo 'blhc: ignore-line-regexp: .* /usr/\S+/arm-none-eabi-g\+\+ .*'
	@echo 'blhc: ignore-line-regexp: ^/usr/\S+/arm-none-eabi-gcc .*'
	@echo 'blhc: ignore-line-regexp: ^/usr/\S+/arm-none-eabi-g\+\+ .*'

execute_after_dh_auto_build-arch:
	dh_auto_build --sourcedirectory=tools/pioasm --builddirectory=pioasm-obj-$(DEB_HOST_GNU_TYPE)

# CMakeLists.txt unconditionally runs add_subdirectory(docs) but we don't want
# to ship the documentation source with pico-sdk-source so we create a dummy
override_dh_auto_install-indep:
	mkdir -p debian/pico-sdk-source/usr/src/pico-sdk/docs
	touch debian/pico-sdk-source/usr/src/pico-sdk/docs/CMakeLists.txt

override_dh_auto_install-arch:
	dh_auto_install --sourcedirectory=tools/pioasm --builddirectory=pioasm-obj-$(DEB_HOST_GNU_TYPE)

execute_before_dh_auto_clean:
	if [ -d ./lib/tinyusb ]; then rmdir ./lib/tinyusb; else rm -f ./lib/tinyusb; fi
	# remove empty directories so they don't get shipped in the binary package
	[ ! -d ./lib ] || find ./lib/ -type d -empty -delete
