#!/usr/bin/make -f
# Build script for GNATColl in Debian.
# Copyright (c) 2014-2016 Nicolas Boulenguez <nicolas@debian.org>

# This build script is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 3 of the
# License, or (at your option) any later version.

# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
# USA

# On Debian systems, the full text of the GPL is in the file
# /usr/share/common-licenses/GPL-3.

DEB_BUILD_MAINT_OPTIONS=hardening=+all
include /usr/share/dpkg/default.mk
include /usr/share/ada/debian_packaging*.mk

DEB_DATE := $(shell dpkg-parsechangelog -S date)

######################################################################
POLICY_TARGETS := binary binary-arch binary-indep build build-arch \
 build-indep clean
.PHONY: $(POLICY_TARGETS)
$(POLICY_TARGETS):
	dh $@ --with ada-library

configure-stamp: config.guess config.sub configure install-sh
# Build-Depends: autotools-dev
config.guess config.sub:
	ln -s /usr/share/misc/$@
# Build-Depends: autoconf
configure:
	autoconf
# Unused, but configure insists on seeing it.
install-sh:
	touch install-sh
override_dh_auto_clean::
	rm -f config.guess config.sub configure install-sh

configure-stamp:
  # Patch version number for documentation.
	dh_auto_configure -- \
	  --enable-gpl \
	  --enable-shared \
	  --enable-shared-python \
	  PACKAGE_VERSION=$(DEB_VERSION_UPSTREAM) \
	  $(foreach v,CC CFLAGS CPPFLAGS LDFLAGS,"$(v)=$($(v))") \
	  --without-postgresql \
	  --with-sqlite=/usr/include/$(DEB_HOST_MULTIARCH)
	touch $@

# Ignore upstream build system.
.PHONY: $(addprefix override_dh_auto_, \
 configure build-arch build-indep test install clean)

override_dh_auto_configure: configure-stamp

# Upstream insists on setting the -j option inside projects.
BUILDER_OPTIONS := $(patsubst -j%,-XPROCESSORS=%,$(BUILDER_OPTIONS))

# Upstream insists on unpredictable directories.
BUILDER_OPTIONS += -p

# See patches/compilation_options.diff
BUILDER_OPTIONS += \
  $(foreach v,ADAFLAGS CFLAGS CPPFLAGS LDFLAGS,"-X$(v)=$($(v))")

# patches/library_versions.diff
# This sed script constructs a -X option for every library package.
BUILDER_OPTIONS += $(shell sed debian/control -ne ' \
  /^Package: lib\([a-z-]\+\)\([0-9.]\+\(gpl[0-9]\{4\}\)\?\)$$/ { \
    s//\U\1\E_VERSION=\2/; \
    y/-/_/; \
    s/^/-X/; \
    p}')

override_dh_auto_build-arch:
 # Ensure deterministic timestamps in ALI files.
	find . -depth -name "*.ad[bs]" -a -newermt '$(DEB_DATE)' -print0 | \
	  xargs -0r touch --no-dereference --date='$(DEB_DATE)'
 # Copy upstream's Makefile, but do not build tools twice.
	gprbuild gnatcoll_full.gpr -XLIBRARY_TYPE=static $(BUILDER_OPTIONS)
	gprbuild src/gnatcoll_gtk.gpr -XLIBRARY_TYPE=static $(BUILDER_OPTIONS)
	gprbuild gnatcoll_full.gpr $(BUILDER_OPTIONS)
	gprbuild src/gnatcoll_gtk.gpr $(BUILDER_OPTIONS)
	gprbuild src/gnatcoll_tools.gpr $(BUILDER_OPTIONS)
 # I see no sensible way to recreate src/generated without depending
 # on a previous libgnatcoll-sqlite-bin and causing bootstrap
 # problems.  At least let the build fail if the generated sources
 # differ, probably because the schema has been modified.
	mv src/generated src/generated.bak
	mkdir src/generated
	LD_LIBRARY_PATH=src/lib/gnatcoll/relocatable:src/lib/iconv/relocatable:src/lib/sqlite/relocatable \
	$(MAKE) generate_sources
 # If a difference is found, fail and display it.
	diff -urN src/generated.bak src/generated
 # Keep the version with the correct timestamps.
	rm -fr src/generated
	mv src/generated.bak src/generated

# Upstream docs target does not generate text output.

# Freeze the clock for deterministic PDF timestamps. The faketime API
# does not allow an explicit time zone, introducing an implicit
# dependency on its local value.
DEB_DATE_FAKETIME := $(shell date "+%F %T" -d "$(DEB_DATE)")
override_dh_auto_build-indep:
# Freeze the clock for deterministic PDF timestamps.
	faketime -f "$(DEB_DATE_FAKETIME)" \
	$(MAKE) -C docs html latexpdf text SPHINXOPTS=-j$(BUILDER_JOBS)

# Default dh_auto_clean would test -f Makefile.
# distclean target is added by a Debian patch.
# Gprclean ignores shared object versions, remove the whole directory.
override_dh_auto_clean::
	if test -f Makefile.conf; then $(MAKE) clean distclean; fi
	rm -fr src/lib

.PHONY: override_dh_clean
override_dh_clean:
	dh_clean --exclude=testsuite/xref/F516-004/default.gpr.orig

# Check dependencies across -dev packages (or generated projects)
# after any change in the structure of upstream projects.
.PHONY: override_dh_ada_library-arch override_dh_ada_library-indep
override_dh_ada_library-arch:
	dh_ada_library --arch $(shell sed debian/control -ne ' \
          /^Package: lib\([a-z-]\+\)[0-9.]\+\(gpl[0-9]\{4\}\)\?-dev$$/ { \
            s~~src/\1.gpr~; \
            y/-/_/; \
            p}')

# Mimic 'dh --with sphinxdoc', but without adding sphinx-common to
# Build-Depends for arch-only builds.
.PHONY: override_dh_installdocs-indep
override_dh_installdocs-indep:
	dh_installdocs -i
	dh_sphinxdoc

.PHONY: override_dh_compress
override_dh_compress:
	dh_compress --package=libgnatcoll-doc --exclude=.adb --exclude=.ads
	dh_compress --remaining-packages
