#! /usr/bin/make -f

DEB_BUILD_MAINT_OPTIONS = hardening=+all

ifeq ($(DEB_BUILD_ARCH), i386)
boost_libs := atomic chrono date-time filesystem iostreams \
              regex serialization stacktrace system test thread
JAM_WITHOUT += --without-container --without-contract --without-exception \
               --without-graph --without-graph_parallel \
               --without-locale --without-log --without-math --without-mpi --without-nowide \
               --without-program_options --without-random \
               --without-timer --without-type_erasure \
               --without-wave --without-json --without-url
else
# Boost libraries for which we want separate packages
# context is conditionally compiled because it is not supported yet on several architectures
# coroutine, and fiber depend on context, so they are also conditionally compiled
boost_libs := atomic chrono container contract date-time exception filesystem	\
              graph graph-parallel iostreams locale log math mpi	\
              mpi-python nowide program-options python random regex	\
              serialization stacktrace system test thread timer \
              type-erasure wave json url
endif

# these are special cases, where /usr/lib name differs from Boost library name
boost_lib_log := log log_setup
boost_lib_math := math_c99 math_c99f math_tr1 math_tr1f
boost_lib_math_long_double := math_c99l math_tr1l
boost_lib_serialization := serialization wserialization
ifeq ($(DEB_BUILD_ARCH), m68k)
# See https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=962072
boost_lib_stacktrace := stacktrace_noop stacktrace_addr2line stacktrace_basic
else
boost_lib_stacktrace := stacktrace_noop stacktrace_addr2line stacktrace_backtrace stacktrace_basic
endif
boost_lib_test := prg_exec_monitor test_exec_monitor unit_test_framework

pyversions = $(shell py3versions -rv)
pyverids = $(subst .,,$(pyversions))

# These are special cases for suffixes.
boost_suffixes_python := $(pyverids)
boost_suffixes_numpy := $(pyverids)
boost_suffixes_mpi-python := $(pyverids)

# Files that are generated by filtering a template
filtered_files =

DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/default.mk

# set the number of build jobs
ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
  JOBS := -j$(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
endif

version_full := $(shell dpkg-parsechangelog | grep Version | cut -d' ' -f2)
version_upstream := $(shell echo $(version_full) | cut -d'-' -f1 | cut -d '+' -f1)
version_major := $(shell echo $(version_upstream) | cut -d'.' -f1,2)

PKGVERSION = $(version_major)
SOVERSION = $(version_upstream)

icuabi = $(shell apt show libicu-dev 2>/dev/null | sed -n 's/Depends: .*libicu\([0-9]*\) .*/\1/p')
regexicuabi = libboost-regex$(SOVERSION)-icu$(icuabi)

# Function to map Boost component name to set of shared library names
# Input: Boost component name
# Return: shared library names for the given Boost library
boost_lib = $(if $(boost_lib_$(1)), $(boost_lib_$(1)), $(1))

# Function to map Boost component name to set of suffixes for the library
# Input: Boost component name
# Return: suffixes for the given Boost component
boost_suffixes = $(if $(boost_suffixes_$(1)), $(boost_suffixes_$(1)),"")

# Helpers to make basic and decorated library names
# Input: library, suffix
# Return: base library filename for short or full name
mk_base_name = usr/lib/$(DEB_HOST_MULTIARCH)/libboost_$(subst -,_,$(1))$(2)
mk_cmake_name = usr/lib/$(DEB_HOST_MULTIARCH)/cmake/boost_$(subst -,_,$(1))-$(SOVERSION)/

t64_suffix_chrono = t64
# Input: component
# Return: package name for shared library or development
mk_pkg_lib = libboost-$(1)$(SOVERSION)$(t64_suffix_$(1))
mk_pkg_dev = libboost-$(1)$(PKGVERSION)-dev
# Helpers to generate debhelper input filenames.
# Input: component
# Return: prefix to debhelper filenames
mk_deb_lib = debian/$(call mk_pkg_lib,$(1))
mk_deb_dev = debian/$(call mk_pkg_dev,$(1))

# Helpers that update debhelper .install or .links files
# Input: component, library, suffix
# Output: none
mk_so_files = $(shell echo debian/tmp/$(call mk_base_name,$(2),$(3)).so.$(SOVERSION) >> $(call mk_deb_lib,$(1)).install)
mk_a_files = $(shell echo debian/tmp/$(call mk_base_name,$(2),$(3)).a >> $(call mk_deb_dev,$(1)).install)
mk_ln_files = $(shell echo $(call mk_base_name,$(2),$(3)).so.$(SOVERSION) $(call mk_base_name,$(2),$(3)).so >> $(call mk_deb_dev,$(1)).links)
mk_cmake_files = $(shell echo debian/tmp/$(call mk_cmake_name,$(2),$(3)) >> $(call mk_deb_dev,$(1)).install)

# Specify the type of files/links to install.
# Special cases first, then general rule
boost_filetypes_exception = a cmake
boost_filetypes_test_exec_monitor = a cmake
boost_filetypes = $(if $(boost_filetypes_$(1)), $(boost_filetypes_$(1)),a so ln cmake)

# Function that updates debhelper files for a given library
# Input: component, library, suffix
# Output: none
mk_files = $(foreach fn,$(call boost_filetypes,$(2)),$(call mk_$(fn)_files,$(1),$(2),$(3)))

# helpers to make and install lintian override files

# Input: package, override
add_override = echo $(1): $(2) >> debian/$(1).lintian-overrides;

# Input: override
add_dev_override = $(call add_override,libboost$(PKGVERSION)-dev,$(1))
add_doc_override = $(call add_override,libboost$(PKGVERSION)-doc,$(1))

# Input: component, lintian-warning
add_lib_override = $(call add_override,$(call mk_pkg_lib,$(1)),$(2))
add_libdev_override = $(call add_override,$(call mk_pkg_dev,$(1)),$(2))

# Input: package-name-base, versioned-package-name
cp_debhelper = set -e ; for s in doc-base examples postinst prerm README.Debian; do \
	if test -f debian/$(1).$$s; then cp -f debian/$(1).$$s debian/$(2).$$s; fi; done

# Function that updates debhelper files for all libraries shipped.
mk_debhelper_files = \
	$(call add_dev_override,extra-license-file) \
	$(call add_doc_override,extra-license-file) \
	$(foreach l, $(boost_libs), \
		echo "making debhelper files for $(l)..."; \
		$(call add_lib_override,$(l),package-name-doesnt-match-sonames) \
		$(foreach ll, $(call boost_lib,$(l)), \
			$(foreach suf, $(call boost_suffixes,$(l)), \
				$(call mk_files,$(l),$(ll),$(suf)) \
			) \
		) \
	)

TOOLSET_CONFIG = 'using gcc : : : <compileflags>"$(CPPFLAGS)" <cflags>"$(CFLAGS)" <cxxflags>"$(CXXFLAGS) -Wno-unused-local-typedefs" <linkflags>"$(LDFLAGS)" ;'
BUILD_CONTEXT = yes
BUILD_LONG_DOUBLE = yes
BUILD_NUMPY = yes

ifeq ($(DEB_VENDOR)-$(DEB_HOST_ARCH),Ubuntu-i386)
BUILD_CONTEXT = no
BUILD_NUMPY = no
endif

# Disable long double on some architectures
ifneq (,$(filter $(DEB_HOST_ARCH), alpha arm armel armhf arm64 hppa mips mipsel sh4 powerpc ppc64))
BUILD_LONG_DOUBLE = no
endif

# Disable context, coroutine, fiber on some architectures
ifneq (,$(filter $(DEB_HOST_ARCH), alpha hppa ia64 m68k mips64 powerpcspe s390 sh4 sparc sparc64 x32))
BUILD_CONTEXT = no
endif

ifeq ($(DEB_BUILD_ARCH), arm64)
JAM_OPT += pch=off
endif

# if we're building with context, coroutine, fiber, add the libraries to the list
ifeq ($(BUILD_CONTEXT), yes)
boost_libs += context coroutine fiber
else
JAM_WITHOUT += --without-context --without-coroutine --without-fiber
endif

# if we're building with long double, add the libraries to the list
ifeq ($(BUILD_LONG_DOUBLE), yes)
boost_lib_math += $(boost_lib_math_long_double)
else
JAM_OPT += --disable-long-double
endif

# if we're building with numpy, add the libraries to the list
ifeq ($(BUILD_NUMPY), yes)
boost_libs += numpy
endif

exampledir = debian/libboost$(PKGVERSION)-doc/usr/share/doc/libboost$(PKGVERSION)-doc/examples
b2 = $(CURDIR)/b2
bbv2dir = $(CURDIR)/tools/build

# With --ignore-site-config, can probably drop Build-Conflicts on boost-build.
JAM = $(b2) $(JOBS) -q -d2 $(JAM_OPT) --layout=system --ignore-site-config --user-config=$(CURDIR)/user-config.jam debug-symbols=on
JAM_DOC = $(b2) $(JOBS) -q -d2 --ignore-site-config --user-config=$(CURDIR)/user-config-doc.jam --enable-index

%:
	dh $@ --with python3

override_dh_auto_configure: user-config.jam make-debhelper
ifeq ($(DEB_BUILD_ARCH), x32)
	cp tools/build/src/tools/gcc.jam tools/build/src/tools/gcc.jam.bak
	sed -i -e 's|^.*compile-link-flags.*-m32.*|#\0|g' \
	       -e 's|^.*toolset.flags.*-march=i686.*|#\0|g' \
		tools/build/src/tools/gcc.jam
endif

override_dh_auto_build-common: $(b2) b2.1 bjam.1
	$(JAM) $(JAM_WITHOUT) --without-python
ifneq ($(DEB_BUILD_ARCH), i386)
	set -e ; for pyver in $(pyversions); do \
		echo "Building Boost.Python for python version $$pyver"; \
		$(JAM) --build-dir=build-$$pyver --stagedir=stage-$$pyver --user-config=$(CURDIR)/user-config-$$pyver.jam --with-python --with-mpi python=$$pyver; \
	done
endif

	cd $(bbv2dir) && ./bootstrap.sh --with-toolset=gcc
	cd tools/bcp && $(JAM)
	cd tools/inspect/build && $(JAM)
	cd tools/quickbook && $(JAM)

	touch $@

override_dh_auto_build-arch: override_dh_auto_build-common

# Unfortunately documentation is built from headers generated by b2
# during compilation, so we need to run compilation before generating
# documentation
override_dh_auto_build-indep: $(b2) override_dh_auto_build-common
ifneq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
	mkdir -p doc
else
	$(JAM_DOC) --build-dir=build-doc doc
endif

testsuite:
	cd status && $(JAM) $(JAM_WITHOUT)

override_dh_auto_clean: clean-debhelper
	-cd tools && $(JAM) clean
	-$(JAM) clean
ifeq ($(DEB_BUILD_ARCH), x32)
	-mv tools/build/src/tools/gcc.jam.bak tools/build/src/tools/gcc.jam
endif
	rm -fr tools/build/src/engine/jamgram.{ch}pp
	rm -rf tools/jam/src/bootstrap
	rm -rf tools/jam/src/bin.*
	rm -ff tools/jam/src/b2
	rm -rf tools/regression/build/bin
	rm -rf bin.v2 dist
	rm -rf user-config.jam
	rm -rf build-* user-config-*.jam stage-*
	rm -rf b2.1 bjam.1
	rm -rf override_dh_auto_build-common override_dh_install-common
	dh_auto_clean

override_dh_compress-indep:
	dh_compress -Xlibboost$(PKGVERSION)-doc/doc

override_dh_install-common:
	# Install Boost.Build v2 & jam
	cd $(bbv2dir) && ./b2 install --prefix=$(CURDIR)/debian/tmp/usr --libdir=$(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)

	touch $@

override_dh_install-arch: override_dh_install-common
	$(JAM) --prefix=$(CURDIR)/debian/tmp/usr $(JAM_WITHOUT) \
		--libdir=$(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH) \
		--without-python install
ifneq ($(DEB_BUILD_ARCH), i386)
	set -e ; for pyver in $(pyversions); do \
		$(JAM) --build-dir=build-$$pyver --stagedir=stage-$$pyver --user-config=$(CURDIR)/user-config-$$pyver.jam --prefix=$(CURDIR)/debian/tmp/usr \
			--libdir=$(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH) \
			install --with-python --with-mpi python=$$pyver; \
	done
endif

	find debian/tmp/usr/include debian/tmp/usr/share/b2 -type f -print0 | xargs -0 chmod 644
	find debian/tmp -name .cvsignore | xargs rm -f
	find debian -empty -type f | xargs rm -f

	# Write substvars for Python packages
	echo 'boost:Provides=' $(foreach verid,$(pyverids),libboost-python$(SOVERSION)-py$(verid),) >> debian/libboost-python$(SOVERSION).substvars
	echo 'boost:Provides=' $(foreach verid,$(pyverids),libboost-mpi-python$(SOVERSION)-py$(verid),) >> debian/libboost-mpi-python$(SOVERSION).substvars
	echo 'boost:Provides=' $(foreach verid,$(pyverids),libboost-numpy$(SOVERSION)-py$(verid),) >> debian/libboost-numpy$(SOVERSION).substvars
	# Write substvars for Regex (icu abi)
	echo 'boost:Provides=$(regexicuabi)' >> debian/libboost-regex$(SOVERSION).substvars

	# package libboost$(PKGVERSION)-dev
	dh_install -plibboost$(PKGVERSION)-dev \
	   debian/tmp/usr/include/boost \
	   usr/include
	dh_install -plibboost$(PKGVERSION)-tools-dev \
	   debian/tmp/usr/bin/b2 \
	   dist/bin/bcp \
	   dist/bin/inspect \
	   dist/bin/quickbook \
	   usr/bin
	dh_link -plibboost$(PKGVERSION)-tools-dev usr/bin/b2 usr/bin/bjam
	dh_installman -plibboost$(PKGVERSION)-tools-dev b2.1 bjam.1 debian/bcp.1 debian/inspect.1 debian/quickbook.1
	dh_install -plibboost$(PKGVERSION)-tools-dev tools/boostbook/xsl/* usr/share/boostbook/xsl
	dh_install -plibboost$(PKGVERSION)-tools-dev tools/boostbook/dtd/* usr/share/boostbook/dtd
	dh_install -plibboost$(PKGVERSION)-tools-dev debian/tmp/usr/share/b2

	# package libboost-date-time$(PKGVERSION)-dev
	dh_installdocs -plibboost-date-time$(PKGVERSION)-dev libs/date_time/data

	# package libboost-mpi-python$(SOVERSION)
	dh_install -plibboost-mpi-python$(SOVERSION)

	# python3 install
	dh_installdirs -plibboost-mpi-python$(SOVERSION) usr/lib/python3/dist-packages/boost
	dh_install -plibboost-mpi-python$(SOVERSION) libs/mpi/build/__init__.py usr/lib/python3/dist-packages/boost/
	cp stage-3*/lib/*/mpi.*.so debian/libboost-mpi-python$(SOVERSION)/usr/lib/python3/dist-packages/boost/ || true

	find debian -iname '*.so.*' -type f -print0 | xargs -0 chrpath --delete
	dh_install

override_dh_install-indep: override_dh_install-common
	# package libboost-doc
	mkdir -p debian/libboost$(PKGVERSION)-doc/usr/share/doc/libboost$(PKGVERSION)-doc
	cp -a doc debian/libboost$(PKGVERSION)-doc/usr/share/doc/libboost$(PKGVERSION)-doc
	# provide a constant symlink to the latest documents and examples
	dh_link -plibboost$(PKGVERSION)-doc \
	   usr/share/doc/libboost$(PKGVERSION)-doc/doc \
	   usr/share/doc/libboost-doc/doc
	dh_link -plibboost$(PKGVERSION)-doc \
	   usr/share/doc/libboost$(PKGVERSION)-doc/examples \
	   usr/share/doc/libboost-doc/examples

	mkdir -p $(exampledir)
	cat debian/example-files | xargs cp -a --parents --target-directory=$(exampledir)
	find $(exampledir) -type f | xargs chmod 644

override_dh_makeshlibs:
	dh_makeshlibs -plibboost-regex$(SOVERSION) -V '$(regexicuabi)'
	dh_makeshlibs --remaining-packages
ifneq ($(DEB_BUILD_ARCH), i386)
	sed -i -r 's/^(libboost_python([0-9]+) \S+ (\S+).*)$$/\1, \3-py\2/' debian/libboost-python$(SOVERSION)/DEBIAN/shlibs
	sed -i -r 's/^(libboost_mpi_python([0-9]+) \S+ (\S+).*)$$/\1, \3-py\2/' debian/libboost-mpi-python$(SOVERSION)/DEBIAN/shlibs
ifeq ($(BUILD_NUMPY), yes)
	sed -i -r 's/^(libboost_numpy([0-9]+) \S+ (\S+).*)$$/\1, \3-py\2/' debian/libboost-numpy$(SOVERSION)/DEBIAN/shlibs
endif
endif

override_dh_gencontrol:
	dh_gencontrol -- -V'gxx:major=$(shell dpkg-query -f '$${version}' -W g++ | sed 's/.*://;s/\..*//')'

$(b2):
	cd tools/build && bison -y -d -o src/engine/jamgram.cpp src/engine/jamgram.y
	./bootstrap.sh --with-icu=/usr --prefix=$(CURDIR)/debian/tmp/usr \
		--libdir=$(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH) \
	  || cat bootstrap.log

b2.1 bjam.1: $(b2)
	help2man --name 'software build tool' --no-info ./b2 > $@

user-config.jam:
	echo $(TOOLSET_CONFIG) > $@
	echo "using mpi ;"     >> $@
ifneq ($(DEB_BUILD_ARCH), i386)
	set -e ; for pyver in $(pyversions); do \
		cp $@ user-config-$$pyver.jam; \
		echo "using python : $$pyver : /usr ;" >> user-config-$$pyver.jam; \
	done
endif
	echo "using boostbook ;" > user-config-doc.jam
	echo "using quickbook ;" >> user-config-doc.jam
	echo "using doxygen ;" >> user-config-doc.jam

$(filtered_files): % : %.in
	sed -e 's/@PKGVERSION@/$(PKGVERSION)/g' < $< > $@

clean-debhelper:
	rm -rf debian/*.install
	rm -rf debian/*.links
	rm -rf debian/*.lintian-overrides

# Make all the generated debhelper files.
make-debhelper: clean-debhelper $(filtered_files)
	@$(call mk_debhelper_files)
	@$(call cp_debhelper,libboost-dev,libboost$(PKGVERSION)-dev)
	@echo usr/lib/$(DEB_HOST_MULTIARCH)/cmake/Boost*-$(SOVERSION)* >> debian/libboost$(PKGVERSION)-dev.install
	@echo usr/lib/$(DEB_HOST_MULTIARCH)/cmake/boost_headers-$(SOVERSION)* >> debian/libboost$(PKGVERSION)-dev.install
	@$(call cp_debhelper,libboost-doc,libboost$(PKGVERSION)-doc)
ifneq ($(DEB_BUILD_ARCH), i386)
	@$(call cp_debhelper,libboost-python-dev,libboost-python$(PKGVERSION)-dev)
	@$(call cp_debhelper,libboost-mpi-python-dev,libboost-mpi-python$(PKGVERSION)-dev)
endif
