#!/usr/bin/make -f
export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export VERBOSE=1

CMAKE_FLAGS = \
	-DCMAKE_BUILD_TYPE=Release \
	-DROCM_SYMLINK_LIBS=OFF \
	-DBUILD_FILE_REORG_BACKWARD_COMPATIBILITY=OFF \
	-DBUILD_CLIENTS_TESTS=ON \
	-DSUPPORT_HIP=OFF

%:
	dh $@ -Scmake

override_dh_auto_configure-arch:
	dh_auto_configure -- $(CMAKE_FLAGS)

# Initially empty test filter; to be fullfiled as needed for the build architecture.
GTEST_FILTER_VAL := *

# On ARM64, the test local_vector_test.extract_coarse_mapping_int causes
# a segmentation fault, triggering a SIGSEGV in the extract_coarse_mapping
# function as described in (https://github.com/ROCm/rocALUTION/issues/322).
# Since different compilers handle memory allocation differently from one
# another, the test is being skipped to allow for successfull builds
# on those ARM plaforms.
ifeq ($(DEB_HOST_ARCH),arm64)
    GTEST_FILTER_VAL := *:-local_vector_test.extract_coarse_mapping_int
endif

override_dh_auto_test-arch:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	# Run tests applying the defined filter.
	export GTEST_FILTER="$(GTEST_FILTER_VAL)" && \
		obj-$(DEB_HOST_GNU_TYPE)/clients/staging/rocalution-test
endif

override_dh_auto_configure-indep:
	:

execute_after_dh_auto_build-indep:
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
	perl -pi -e 's/WARN_AS_ERROR.*=.*YES/WARN_AS_ERROR = NO/' docs/doxygen/Doxyfile
	http_proxy='127.0.0.1:9' \
	https_proxy='127.0.0.1:9' \
	rocm-docs-build
endif

override_dh_auto_test-indep:
	:

override_dh_auto_install-indep:
	:

override_dh_sphinxdoc:
	dh_sphinxdoc
	# Since dh_sphinxdoc depends on the redundant license file it should be removed only
	# after the execution of the rule.
	find debian/librocalution-doc -name "*license.rst*" -delete || true
	# Delete the unused .doctree file.
	find debian/librocalution-doc -name "*.doctree*" -delete || true
	# Delete unused fonts.
	rm -rf debian/librocalution-doc/usr/share/doc/*/html/_static/fonts/ || true
	rm -rf debian/librocalution-doc/usr/share/doc/*/html/_static/vendor/fontawesome/webfonts/ || true
	# Fix Privacy Breach (MathJax) by using local references instead of embedded js scripts.
	find debian/librocalution-doc/usr/share/doc -name "*.html" -exec \
		sed -i 's|https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js|/usr/share/javascript/mathjax/tex-mml-chtml.js|g' {} + || true

override_dh_makeshlibs:
	dh_makeshlibs -V -plibrocalution1 -- -c4
