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

# colorized output reduces burden to my eye
export NINJA_STATUS=[1;36m[%es (%p) %f/%t][0;m 

# Maintainer notes:
#
#1. Shogun.py uses "bazel query" to dump source code and generated file
#   dependencies for the build targets. The build system is python3 plus
#   ninja. See debian/shogun.py for detail.
#
#2. This source packages provides CPU-only version of TF instead
#   of CUDA version.
#
#   (1) CPU-only version is compilable on all architectures.
#       CUDA is specific to amd64 and ppc64el, and aarch64.
#
#   (2) CPU-only version can stay in the main section.
#       Once compiled with CUDA, the source must enter contrib section.
#
#   src:caffe and src:caffe-contrib is an example for illustration.
#

%:
	dh $@ -Sninja -D.

override_dh_auto_clean:
	$(SHELL) debian/shogun-clean.sh

override_dh_auto_configure:
	-find . -type f -name '.gitignore' -delete
	-find tensorflow/examples/ -type f -exec chmod a-x '{}' +
	$(SHELL) debian/embedded.sh
	patch -p1 < debian/patches/fft-cxx-no-mangle.patch
	$(SHELL) debian/shogun-gen.sh

override_dh_auto_build:
	dh_auto_build -- -f proto_text.gen.ninja
	dh_auto_build -- -f proto_text.ninja
	dh_auto_build -- -f libtensorflow_framework.gen.ninja
	dh_auto_build -- -f libtensorflow_framework.ninja
	dh_auto_build -- -f libtensorflow.gen.ninja
	dh_auto_build -- -f libtensorflow.ninja
	dh_auto_build -- -f libtensorflow_cc.gen.ninja
	dh_auto_build -- -f libtensorflow_cc.ninja
	#dh_auto_build -- -f pippackage.gen.ninja

override_dh_auto_install:
	$(SHELL) debian/shogun-install.sh
	cd debian/pkgconfig; $(SHELL) pc-gen.sh
	install -Dm0644 debian/pkgconfig/tensorflow.pc debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig/tensorflow.pc
	install -Dm0644 debian/pkgconfig/tensorflow_cc.pc debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig/tensorflow_cc.pc
	install -Dm0644 debian/pkgconfig/tensorflow_framework.pc debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig/tensorflow_framework.pc

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	# Some basic sanity tests
	-$(SHELL) debian/tests/capi.sh
	-$(SHELL) debian/tests/tf_c_api_test.sh
	-$(SHELL) debian/tests/tf_cc_tutorial.sh
	-$(SHELL) debian/tests/tf_doc_api_guide_cc.sh
	-$(SHELL) debian/tests/tf_ex_adding_an_op.sh
	-$(SHELL) debian/tests/tf_ex_label_image.sh
	-$(SHELL) debian/tests/tf_lib_package_libtensorflow_test.sh
	-$(SHELL) debian/tests/tf_tool_benchmark_model.sh
	-$(SHELL) debian/tests/tf_tool_graph_transforms.sh
	# TODO: compile and run more upstream gtest testsuites
endif
