#!/usr/bin/make -f

export DH_VERBOSE = 1
# Waf -v otherwise prints Python argument lists, obscuring flags from blhc.
export WAF_CMD_FORMAT = string
export ARDUPILOT_BUILD_ROOT = /usr/share/ardupilot
export PYTHON = /usr/bin/python3
export DEB_BUILD_MAINT_OPTIONS = hardening=+all

# Keep GCC 16 diagnostics visible without treating them as errors.
export DEB_CXXFLAGS_MAINT_APPEND = -Wno-error=maybe-uninitialized -Wno-error=format-truncation

include /usr/share/dpkg/buildtools.mk

SITL_TARGETS = bin/arducopter,bin/arduplane,bin/ardurover,bin/ardusub,bin/antennatracker,bin/blimp,tool/Replay
JOBS = $(or $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS))),1)

.PHONY: build build-arch build-indep binary binary-arch binary-indep clean
build build-arch build-indep binary binary-arch binary-indep clean:
	dh $@

# The Waf build does not use the Autotools projects bundled in modules/.
override_dh_update_autotools_config override_dh_autoreconf:

override_dh_auto_configure:
	CC="$(CC)" CXX="$(CXX)" ./waf configure --board sitl --no-submodule-update --disable-Werror $(if $(filter nocheck,$(DEB_BUILD_OPTIONS)),--disable-tests)

override_dh_auto_build:
	./waf build -v --targets=$(SITL_TARGETS) -j$(JOBS)
	# Waf applies the Heli vehicle definition to every requested target when
	# "heli" occurs in its target list, so it needs a separate invocation.
	./waf heli -v -j$(JOBS)

# Install only the artifacts listed in ardupilot-sitl.install. The upstream
# Makefile's catch-all target interprets "install" as a board name.
override_dh_auto_install:

# The expanded Git checkout can still contain files removed by the repack.
override_dh_install:
	dh_install -X.mexa64 -X.mexw64 -X.gitignore -X__pycache__ -X.pyc \
		-XWebots_Python/protos/BlueBoat.proto -XWebots_Python/protos/stl \
		-XWebots_Python/worlds/ocean.wbt -XWebots_Python/protos/Iris.proto \
		-XWebots_Python/protos/meshes -XWebots_Python/worlds/iris.wbt \
		-XWebots_Python/worlds/iris_camera.wbt -XWebots_Python/worlds/iris_depth_camera.wbt
	# Preserve the example text while making its legacy encoding explicit.
	for f in Tools/autotest/aircraft/Rascal/README.Rascal libraries/SITL/examples/JSON/MATLAB/tcp_udp_ip_2.0.6/pnet.m; do \
		iconv -f ISO-8859-1 -t UTF-8 "debian/ardupilot-sitl/usr/share/ardupilot/$$f" > "debian/ardupilot-sitl/usr/share/ardupilot/$$f.utf8"; \
		mv "debian/ardupilot-sitl/usr/share/ardupilot/$$f.utf8" "debian/ardupilot-sitl/usr/share/ardupilot/$$f"; \
	done

override_dh_fixperms:
	dh_fixperms
	chmod 0644 debian/ardupilot-sitl/usr/share/ardupilot/Tools/autotest/aircraft/Rascal/Models/Rascal.rgb

# dh_clean runs afterwards and removes Python __pycache__ trees.
override_dh_auto_clean:
	rm -f .lock-waf_linux_build tasklist.json
	rm -rf build
	rm -f eeprom.bin

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	./waf build -v --targets=tests/test_vector2,tests/test_vector3 -j$(JOBS)
	build/sitl/tests/test_vector2
	build/sitl/tests/test_vector3
	python3 debian/tests/sitl --binary-dir=$(CURDIR)/build/sitl/bin --data-dir=$(CURDIR)/Tools/autotest
endif

# Record the source version actually compiled, including correct binNMU handling.
override_dh_gencontrol:
	libcanard_built_using="$$(dpkg-query -W -f='$${source:Package} (= $${source:Version})' libcanard-dev:$(DEB_HOST_ARCH))" && \
		dh_gencontrol -- -Vlibcanard:Built-Using="$$libcanard_built_using"

override_dh_python3:
	dh_python3 /usr/share/ardupilot
