#!/usr/bin/make -f

export DH_VERBOSE = 1

export DEB_BUILD_MAINT_OPTIONS := hardening=+all
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk

DEB_HOST_ARCH_BITS ?= $(shell dpkg-architecture -qDEB_HOST_ARCH_BITS)
DEB_HOST_ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)

ifneq (,$(filter 64,$(DEB_HOST_ARCH_BITS))$(filter i386,$(DEB_HOST_ARCH)))
  t64_provides := libswipl9
endif

ifeq ($(DEB_BUILD_ARCH),riscv64)
    DEB_LDFLAGS_MAINT_APPEND += -Wl,--no-as-needed -Wl,-latomic -Wl,--as-needed
    export DEB_LDFLAGS_MAINT_APPEND
endif

PLBASENAME := swi-prolog
PLBASE     := /usr/lib/$(PLBASENAME)/
JNIDIR     := /usr/lib/$(DEB_BUILD_MULTIARCH)/jni

CMAKE_OPTIONS = \
	-DSWIPL_PACKAGES_QT=OFF \
	-DSWIPL_INSTALL_DIR=$(PLBASENAME) \
	-DSWIPL_INSTALL_IN_LIB=ON \
	-DSWIPL_INSTALL_IN_SHARE=ON \
	-DINSTALL_TESTS=ON \
	-DCMAKE_BUILD_TYPE=DEB \
	-DJAVA_COMPATIBILITY=ON \
	-DJNIDIR=$(JNIDIR)

# List of architectures where -java package should be built.
JAVA_ARCHS = $(shell grep-dctrl -PX swi-prolog-java -nsArchitecture debian/control)

ifeq ($(filter $(DEB_BUILD_ARCH),$(JAVA_ARCHS)),)
    CMAKE_OPTIONS  += -DSWIPL_PACKAGES_JAVA=OFF
else
    JAVA_COMPONENTS = Java_interface
endif

CORE_COMPONENTS = \
	Core_system
COREPKGS_COMPONENTS = \
	Core_packages
NOX_COMPONENTS = \
	Archive_interface \
	Commandline_editors \
	Perl_regex \
	YAML_support \
	OpenSSL_interface
X_COMPONENTS = \
	Graphics_subsystem
ODBC_COMPONENTS = \
	ODBC_interface
BDB_COMPONENTS = \
	BerkeleyDB_interface
DOC_COMPONENTS = \
	Documentation \
	Examples
TEST_COMPONENTS = \
	Tests

ifeq ($(shell dpkg-architecture -qDEB_BUILD_ARCH_OS),linux)
    NOX_COMPONENTS += TIPC_networking
else
    CMAKE_OPTIONS  += -DSWIPL_PACKAGES_TIPC=OFF
endif

# install_component component deb_pkg
# env -C build doesn't work on Ubuntu 16.04 or older
define install_component
	cd build && env DESTDIR=$(CURDIR)/debian/$(2) \
	cmake -DCMAKE_INSTALL_COMPONENT=$(1) \
	      -P cmake_install.cmake

endef

# install_components plog_pkg_list deb_pkg
define install_components
	$(foreach component,$(1),$(call install_component,$(component),$(2)))
endef

%:
	dh $@ --builddirectory=build

override_dh_auto_configure:
	dh_auto_configure --builddirectory=build -- $(CMAKE_OPTIONS)

override_dh_auto_install:
	$(call install_components,$(CORE_COMPONENTS),swi-prolog-core)
	$(call install_components,$(COREPKGS_COMPONENTS),swi-prolog-core-packages)
	$(call install_components,$(NOX_COMPONENTS),swi-prolog-nox)
	$(call install_components,$(X_COMPONENTS),swi-prolog-x)
	$(call install_components,$(JAVA_COMPONENTS),swi-prolog-java)
	$(call install_components,$(ODBC_COMPONENTS),swi-prolog-odbc)
	$(call install_components,$(BDB_COMPONENTS),swi-prolog-bdb)
	$(call install_components,$(DOC_COMPONENTS),swi-prolog-doc)
	$(call install_components,$(TEST_COMPONENTS),swi-prolog-test)

	rm debian/swi-prolog-core/$(PLBASE)/library/INDEX.pl # This file is (re-)generated by postinst scripts

	rm -f debian/swi-prolog-test/usr/lib/swi-prolog/test/Tests/COPYING # Remove extra license file

	find debian/ -depth -type d -empty -exec rm -rfv {} \; # Remove empty directories

override_dh_install:

# tell the tests NOT to try to access http://www.swi-prolog.org.  This
# is used in some of the HTTP tests
USE_PUBLIC_NETWORK_TESTS=false
export USE_PUBLIC_NETWORK_TESTS

# we exclude the JPL test (-E jpl:) because embedded Java in Linux is
# currently non-functional. See
# https://stackoverflow.com/questions/44763387/jni-createjavavm-stack-corruption-in-recent-ubuntu-16-04

override_dh_auto_test:
	-cd build && ctest -V -E jpl:

override_dh_gencontrol:
	echo 't64:Provides=$(t64_provides)' >> debian/swi-prolog-core.substvars
	dh_gencontrol -- -Vswi-prolog:ABI="$(shell LD_LIBRARY_PATH=$(shell dirname $(shell find debian/swi-prolog-core -name libswipl.so)) debian/swi-prolog-core/usr/bin/swipl --abi-version | sed 's/.*-abi-//')" -Vswi-prolog:FABI="$(shell LD_LIBRARY_PATH=$(shell dirname $(shell find debian/swi-prolog-core -name libswipl.so)) debian/swi-prolog-core/usr/bin/swipl --abi-version | sed 's/.*-abi-//' | cut --delimiter=- -f 1)" -Vswi-prolog:BABI="$(shell LD_LIBRARY_PATH=$(shell dirname $(shell find debian/swi-prolog-core -name libswipl.so)) debian/swi-prolog-core/usr/bin/swipl --abi-version | sed 's/.*-abi-//' | cut --delimiter=- -f 2)" -Vswi-prolog:QLF="$(shell LD_LIBRARY_PATH=$(shell dirname $(shell find debian/swi-prolog-core -name libswipl.so)) debian/swi-prolog-core/usr/bin/swipl --abi-version | sed 's/.*-abi-//' | cut --delimiter=- -f 3)" -Vswi-prolog:SSTATES="$(shell LD_LIBRARY_PATH=$(shell dirname $(shell find debian/swi-prolog-core -name libswipl.so)) debian/swi-prolog-core/usr/bin/swipl --abi-version | sed 's/.*-abi-//' | cut --delimiter=- -f 4)"

.PHONY: override_dh_auto_configure override_dh_auto_install override_dh_auto_test
.PHONY: override_dh_install override_dh_gencontrol

