#!/usr/bin/make -f

# export DH_VERBOSE=1

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export LIBS="pthread"

## Memory consumption issues on some autobuilders have been causing
## build failures.  There are three ways to address such issues:
## (a) reduce parallelism, e.g., dh $@ --max-parallel=2
## (b) reduce GCC memory usage, e.g., g++ --param ggc-min-expand=20 to
## reduce abrupt memory spikes, and g++ -g0 to skip debugging info.
## (c) switch to clang.

# Force Compiler
export CC=clang
export CXX=clang++

%:
	dh $@ --without python2 --with python3 --buildsystem=cmake ${DH_FLAGS}

override_dh_auto_configure:
	dh_auto_configure -- -DCMAKE_LIBRARY_PATH=$(DEB_HOST_MULTIARCH) \
		-DDEBUG=ON \
		-DPROFILE=OFF \
		-DARMA_EXTRA_DEBUG=OFF \
		-DBUILD_CLI_EXECUTABLES=ON \
		-DBUILD_PYTHON_BINDINGS=ON \
		-DPYTHON_EXECUTABLE=/usr/bin/python3 \
		-DBUILD_JULIA_BINDINGS=ON \
		-DBUILD_TESTS=ON \
		-DBUILD_SHARED_LIBS=ON \
		-DDISABLE_DOWNLOADS=ON \
		-DSTB_IMAGE_INCLUDE_DIR=/usr/include/stb \
		-DUSE_OPENMP=ON

execute_after_dh_auto_build:
	@echo "building PDF reference manual"
	cd obj-*/doc/latex && \
	 latexmk --pdf refman.tex

override_dh_auto_test:
	@echo "do not ask do not tell do not test"

execute_before_dh_python3:
	dh_numpy3

execute_after_dh_install:
	@echo "use shared jquery.js javascript library"
	for f in $$(find debian/mlpack-doc -name jquery.js); do \
	  ln --verbose --symbolic --force /usr/share/javascript/jquery/jquery.js $$f; \
	done
	@echo "remove byte-compiled python files per policy"
	-find debian/python-mlpack  -name '*.pyc' -ls -delete || true
	-find debian/python3-mlpack -name '*.pyc' -ls -delete || true

override_dh_installdocs:
	dh_installdocs -pmlpack-doc --doc-main-package libmlpack-dev
	dh_installdocs -Nmlpack-doc

override_dh_installexamples:
	dh_installexamples -pmlpack-doc --doc-main-package libmlpack-dev
	dh_installexamples -Nmlpack-doc

execute_after_dh_installexamples:
	@echo "Search and destroy stray VCS support files"
	-find debian/mlpack-doc -name .gitignore -ls -delete || true
	@echo "Remove empty directory"
	-rmdir debian/mlpack-doc/usr/share/doc/libmlpack-dev/examples/sample-ml-app/sample-ml-app/data/

override_dh_installchangelogs:
	dh_installchangelogs HISTORY.md

override_dh_compress:
	dh_compress -Xrefman.pdf -Xdoc/html/
