#!/usr/bin/make -f
# -*- makefile -*-

include /usr/share/dpkg/architecture.mk

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

# Build workaround to allow this to build on 32-bit arches.
#   https://lists.debian.org/debian-devel/2023/08/msg00156.html
# This makes the build much slower, but is needed for most arches. I needed to
# ALSO disable debug symbols and optimization for the python wrapper (-O0 -g0;
# set in the no-optimization-or-debug-flags-in-python-bindings.patch).
export DEB_CXXFLAGS_MAINT_APPEND += --param ggc-min-expand=5

# When building with gcc-13 (possibly with older ones as well) I get lots of
# these warnings. They are maybe helpful for upstream, but not at all helpful
# for Debian builds, so I disable these
export DEB_CXXFLAGS_MAINT_APPEND += -Wno-deprecated-declarations -Wno-overloaded-virtual

# Without this I get lots of warnings like this:
# note: parameter passing for argument of type 'std::_Rb_tree_const_iterator<long long unsigned int>' changed in GCC 7.1
export DEB_CXXFLAGS_MAINT_APPEND += -Wno-psabi

# On some arches ld says:
#   /usr/bin/ld: CMakeFiles/SolverComparer.dir/SolverComparer.cpp.o: undefined reference to symbol '__atomic_load_8@@LIBATOMIC_1.0'
ifneq (,$(filter $(DEB_HOST_ARCH), armel powerpc sh4))
export DEB_LDFLAGS_MAINT_APPEND += -latomic
endif

# On some arches there's simply too much debugging info. I turn it off. The
# build log says:
#   /usr/bin/ld: error: libgtsam.so.4.2.0(.debug_info) is too large (0x10e67cac bytes)
ifneq (,$(filter $(DEB_HOST_ARCH), mipsel))
export DEB_CXXFLAGS_MAINT_APPEND += -g0
endif



ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
    NUMJOBS   := $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
    MAKEFLAGS += -j$(NUMJOBS)
endif

%:
	dh $@ --with python3 --buildsystem=cmake

# I'm buliding without gtsam_unstable:
# https://github.com/borglab/gtsam/issues/1457
#
# I also building without examples. I don't ship the build products, and this
# build is REALLY slow, so I want to avoid building stuff that doesn't need to
# be built. It's good to build them as a test, but I'll just rely on the test
# suite for that
override_dh_auto_configure:
	dh_auto_configure --					\
		-DCMAKE_BUILD_TYPE=RelWithDebInfo		\
		-DGTSAM_BUILD_WITH_MARCH_NATIVE=OFF		\
		-DGTSAM_BUILD_TYPE_POSTFIXES=OFF		\
		-DGTSAM_BUILD_UNSTABLE=OFF			\
		-DGTSAM_BUILD_EXAMPLES_ALWAYS=OFF		\
		-DGTSAM_UNSTABLE_BUILD_PYTHON=OFF		\
		-DGTSAM_UNSTABLE_INSTALL_MATLAB_TOOLBOX=OFF	\
		-DBUILD_SHARED_LIBS=ON				\
		-DGTSAM_WITH_TBB=ON				\
		-DGTSAM_WITH_EIGEN_MKL=OFF			\
		-DGTSAM_WITH_EIGEN_MKL_OPENMP=OFF		\
		-DGTSAM_BUILD_PYTHON=ON				\
		-DGTSAM_INSTALL_MATLAB_TOOLBOX=OFF		\
		-DGTSAM_ALLOW_DEPRECATED_SINCE_V42=ON		\
		-DGTSAM_BUILD_WITH_CCACHE=OFF			\
		-DGTSAM_USE_SYSTEM_EIGEN=ON			\
		-DGTSAM_USE_SYSTEM_METIS=ON

ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))

# I build the lyx and doxygen docs. And I use the local packaged mathjax instead
# of the internet one
override_dh_auto_build-indep:
	lyx -userdir /tmp -e pdf $(filter-out %/macros.lyx,$(wildcard doc/*.lyx))
	$(MAKE) -C obj-$(DEB_HOST_GNU_TYPE) doc
	perl -p -i -e 's{http.*?/MathJax\.js}{file:///usr/share/javascript/mathjax/MathJax.js?config=TeX-MML-AM_CHTML}g' doc/html/*.html

override_dh_compress:
	dh_compress -O--buildsystem=cmake -X.pdf

else

override_dh_auto_build-indep:
	true

endif



ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))

override_dh_auto_test:
	$(MAKE) -C obj-$(DEB_HOST_GNU_TYPE)        check
	$(MAKE) -C obj-$(DEB_HOST_GNU_TYPE)/python python-test
else

override_dh_auto_test:
	true

endif


# /usr/include/gtwap just has matlab stuff today so I get rid of it
#
# We're supposed to "make python-install" to install this stuff: https://github.com/borglab/gtsam/issues/1513
# But it doesn't work: it uses pip, and is hard-coded to install into the
# system, not to a subdirectory
#
# I can use setup.py instead:
#
#   python3 obj-$(DEB_HOST_GNU_TYPE)/python/setup.py install --root=$(CURDIR)/debian/tmp --install-layout=deb
#
# But this doesn't work either: setup.py isn't defined properly, or something,
# and it installs the metadata only, but not any of the actual library. So I
# just copy the needed files myself
override_dh_auto_install:
	dh_auto_install -O--buildsystem=cmake

	mkdir -p $(CURDIR)/debian/tmp/usr/lib/python3/dist-packages/gtsam
	cp    obj-$(DEB_HOST_GNU_TYPE)/python/gtsam/*.so  $(CURDIR)/debian/tmp/usr/lib/python3/dist-packages/gtsam
	cp    obj-$(DEB_HOST_GNU_TYPE)/python/gtsam/*.py  $(CURDIR)/debian/tmp/usr/lib/python3/dist-packages/gtsam
	cp -r obj-$(DEB_HOST_GNU_TYPE)/python/gtsam/utils $(CURDIR)/debian/tmp/usr/lib/python3/dist-packages/gtsam

	rm -rf debian/tmp/usr/include/CppUnitLite
	rm -rf debian/tmp/usr/lib/*/libCppUnitLite.a
	rm -rf debian/tmp/usr/include/gtwrap

	chrpath -d debian/tmp/usr/lib/python3/dist-packages/*/*.so

# remove bogus executable bits, remove files I shouldn't ship
override_dh_installexamples-indep:
	dh_installexamples -O--buildsystem=cmake
	find debian/libgtsam-doc/usr/share/doc/libgtsam-dev/examples/Data/ -type f | xargs chmod a-x
	rm debian/libgtsam-doc/usr/share/doc/libgtsam-dev/examples/Data/.gitignore

