#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

include /usr/share/dpkg/architecture.mk

# Manually implement multiarch support here since debhelper seems to pass
# this information in a proper form only to the Autoconf build system.
# When using "--buildsystem=make", no multiarch support is added.
# Chibi does not support Autoconf so we have to fix up the paths here.
EXTRA_ARGS += prefix="/usr"
EXTRA_ARGS += libdir="/usr/lib/$(DEB_HOST_MULTIARCH)"
EXTRA_ARGS += SNOWPREFIX="/usr/local"
EXTRA_ARGS += SNOWLIBDIR="/usr/local/lib/$(DEB_HOST_MULTIARCH)"

%:
	dh $@

override_dh_auto_build:
# Build the static library first. It requires special compilation flags without
# -fPIC and dlopen() support.
	dh_auto_build -- libchibi-scheme.a $(EXTRA_ARGS) CLIBFLAGS= SEXP_USE_DL=0
	$(MAKE) clean
# After cleaning up the object files we can build everything else.
	dh_auto_build -- all $(EXTRA_ARGS)
# Additionally, build the HTML documentation for Chibi and libraries.
	$(MAKE) doc

# Run a bit more tests than "make test" usually does to verify
# SRFI libraries and FFI things.
override_dh_auto_test:
	dh_auto_test -- test-all test-unicode $(EXTRA_ARGS)

override_dh_auto_install:
# Make sure that "make install" does not build and install image files.
# Those will be created on the user machine at package configuration time.
	dh_auto_install -- $(EXTRA_ARGS) IMAGE_FILES=
# The generated (chibi snow install) library hardcodes the multiarch
# snow binary module directory, so it must not go into the arch:all
# chibi-scheme-common package.  Move it into the multiarch module
# directory, which is also on the default module path, so that it is
# shipped in libchibi-scheme-ffi instead.
	mkdir -p debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/chibi/chibi/snow
	mv debian/tmp/usr/share/chibi/chibi/snow/install.sld \
	   debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/chibi/chibi/snow/
