#!/usr/bin/make -f
export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export DEB_CFLAGS_MAINT_APPEND =
export DEB_CXXFLAGS_MAINT_APPEND =
# XXX: Warning: -Wl,--as-needed will trigger FTBFS!!
export DEB_LDFLAGS_MAINT_APPEND =

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

%:
	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 all_proto.ninja
	dh_auto_build -- -f proto_text.ninja
	dh_auto_build -- -f libtensorflow_framework.ninja
	ln -sr libtensorflow_framework.so libtensorflow_framework.so.1.10  # temporary dirty fix
	dh_auto_build -- -f ccop.ninja
	dh_auto_build -- -f libtensorflow.ninja
	dh_auto_build -- -f libtensorflow_cc.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
