#!/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,-pie
# PIE should only affect the static library.
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: configure.in.bak
	autoconf
# Unused, but configure insists on seeing it.
install-sh:
	touch install-sh
# Patch version number for documentation.
configure.in.bak:
	mv configure.in $@
	sed '2s/,[^,]*/,$(DEB_VERSION_UPSTREAM)/' $@ > configure.in
override_dh_auto_clean::
	rm -f config.guess config.sub configure install-sh
	if test -f configure.in.bak; then \
	  mv -f configure.in.bak configure.in; \
	fi

configure-stamp:
	dh_auto_configure -- --enable-gpl --enable-shared \
	  $(foreach v,ADAFLAGS 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

override_dh_auto_configure: auto.cgpr
auto.cgpr:
	gprconfig --config=Ada --config=C,,,,gnatgcc
override_dh_auto_clean::
	rm -f auto.cgpr

# patches/inherit_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]" -o -name "*.[hc]" \) \
	  -a -newermt '$(DEB_DATE)' -print0 | \
	  xargs -0r touch --no-dereference --date='$(DEB_DATE)'
	$(MAKE) 'GPRBUILD_OPTIONS=$(BUILDER_OPTIONS)' \
	  build_library_type/static \
	  build_library_type/relocatable \
	  build_tools/relocatable

# 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.
override_dh_auto_clean::
	if test -f Makefile.conf; then dh_auto_clean -- GPRCLEAN_OPTIONS=; fi

.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
