#!/usr/bin/make -f
# Build script for XML/Ada in Debian.
# Copyright (c) 2003-2009 Ludovic Brenta <lbrenta@debian.org>
# Copyright (c) 2013-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, see <http://www.gnu.org/licenses/>.

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

GNAT_VERSION := 6
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-$(GNAT_VERSION).mk

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

ADAFLAGS += -gnatafno -gnatVa

# Ignore gnat.adc in top directory.
BUILDER_OPTIONS += -gnatA

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

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

CONFIGURED := xmlada-config
override_dh_auto_build-arch: $(CONFIGURED)
$(CONFIGURED): %: %.in
	sed "\
          s/@PACKAGE_VERSION@/$(DEB_VERSION_UPSTREAM)/g; \
          s/@DEFAULT_LIBRARY_TYPE@/relocatable/g; \
          s%@libdir@%/usr/lib/$(DEB_HOST_MULTIARCH)%g; \
          " $< > $@
# Installation will append once unicode_DEV_PKG has been computed.
override_dh_auto_clean::
	rm -f $(CONFIGURED)

# Rewrite upstream targets without gprbuild or gprclean.
# gnatmake does not support projects anymore, we cannot use xmlada.gpr.
# Also avoid a circular build-dependency between gprbuild and xmlada.

ADS_DIR := usr/share/ada/adainclude
GPR_DIR := usr/share/ada/adainclude
ALI_DIR := usr/lib/$(DEB_HOST_MULTIARCH)/ada/adalib
SO_DIR  := usr/lib/$(DEB_HOST_MULTIARCH)
A_DIR   := usr/lib/$(DEB_HOST_MULTIARCH)

grep_pkg = $(shell sed -n '/^Package: \(libxmlada-$(1)\)$$/{s//\1/p;q}' debian/control)

# All libraries built so far will be prepended to that list, for -aI
# -aO and -l options. The order matters for -l, and redundancy does
# not because we link with --as-needed.
LIBS :=

# Imported projects and -dev packages, unsorted subset of LIBS.
unicode_LIBS       :=
input_sources_LIBS := unicode
sax_LIBS           := unicode input_sources
dom_LIBS           := sax unicode
schema_LIBS        := dom sax unicode

define library_template

  $(library)_DEV_PKG := $(call grep_pkg,$(subst _,-,$(library))[0-9.]\+-dev)
  $(library)_LIB_PKG := $(call grep_pkg,$(subst _,-,$(library))[0-9.]\+)

  # Build dynamic library.
  override_dh_auto_build-arch: $(library)/lib/relocatable/libxmlada_$(library).so.$(DEB_VERSION_UPSTREAM)
  $(library)/lib/relocatable/libxmlada_$(library).so.$(DEB_VERSION_UPSTREAM):
	gnatmake -c $(library)/*.ad[bs] -D $(library)/obj/relocatable \
          $(BUILDER_OPTIONS) \
          $(foreach l,$(LIBS),-aI$(l) -aO$(l)/obj/relocatable) \
          -cargs $(ADAFLAGS) -fPIC
	gnatgcc -shared \
          -o $(library)/lib/relocatable/libxmlada_$(library).so.$(DEB_VERSION_UPSTREAM) \
          -Wl,-soname,libxmlada_$(library).so.$(DEB_VERSION_UPSTREAM) \
          $(LDFLAGS) \
          $(library)/obj/relocatable/*.o $(foreach l,$(LIBS),-L$(l)/lib/relocatable -lxmlada_$(l)) -lgnat-$(GNAT_VERSION)

  # Create development link so that the other libraries may link with it.
  override_dh_auto_build-arch: $(library)/lib/relocatable/libxmlada_$(library).so
  $(library)/lib/relocatable/libxmlada_$(library).so:
	ln -s libxmlada_$(library).so.$(DEB_VERSION_UPSTREAM) $(library)/lib/relocatable/libxmlada_$(library).so

  # Build static library.
  override_dh_auto_build-arch: $(library)/obj/static/libxmlada_$(library).a
  $(library)/obj/static/libxmlada_$(library).a:
	gnatmake -c $(library)/*.ad[bs] -D $(library)/obj/static \
          $(BUILDER_OPTIONS) \
          $(foreach l,$(LIBS),-aI$(l) -aO$(l)/obj/static) \
          -cargs $(ADAFLAGS) -fPIE
	ar rc $(library)/obj/static/libxmlada_$(library).a $(library)/obj/static/*.o
	ranlib $(library)/obj/static/libxmlada_$(library).a

  LIBS := $(library) $(LIBS)

  override_dh_install::
  # dynamic library
	dh_install --package=$$($(library)_LIB_PKG) \
	  $(library)/lib/relocatable/libxmlada_$(library).so.$(DEB_VERSION_UPSTREAM) \
	  $(SO_DIR)
  # static library
	dh_install --package=$$($(library)_DEV_PKG) \
	  $(library)/obj/static/libxmlada_$(library).a $(A_DIR)
  # development symbolic link
	dh_link --package=$$($(library)_DEV_PKG) \
	  $(SO_DIR)/libxmlada_$(library).so.$(DEB_VERSION_UPSTREAM) \
	  $(SO_DIR)/libxmlada_$(library).so
  # ALI files
	dh_install --package=$$($(library)_DEV_PKG) \
	  $(library)/obj/relocatable/*.ali $(ALI_DIR)/xmlada_$(library)
  # Sources
	dh_install --package=$$($(library)_DEV_PKG) \
	  $(library)/*.ad[bs] $(ADS_DIR)/xmlada_$(library)
  # Substitution variables for the control file.
	echo 'ada:Depends=gnat, gnat-$(GNAT_VERSION), \
	  $$(foreach l,$$($(library)_LIBS),$$($$(l)_DEV_PKG) (= $$$${binary:Version}),)$$($(library)_LIB_PKG)' \
	  >> debian/$$($(library)_DEV_PKG).substvars

  override_dh_auto_clean::
	rm -f $(library)/lib/relocatable/*
	rm -f $(library)/obj/relocatable/*
	rm -f $(library)/obj/static/*

  # Externally built project installed for end users.
  override_dh_auto_build-arch: debian/xmlada_$(library).gpr
  debian/xmlada_$(library).gpr: debian/template.gpr
	sed ' \
	  s/@IMPORTS@/$$(foreach d,$$($(library)_LIBS),with "xmlada_$$(d).gpr";\n)/; \
	  s/\n with/\nwith/; \
	  $(foreach v,library SO_DIR ADS_DIR ALI_DIR,s|@$(v)@|$($(v))|;) \
	  ' $$< > $$@
  override_dh_install::
	dh_install --package=$$($(library)_DEV_PKG) \
	  debian/xmlada_$(library).gpr $(GPR_DIR)
  override_dh_auto_clean::
	rm -f debian/xmlada_$(library).gpr

endef
$(foreach library,unicode input_sources sax dom schema,$(eval $(call library_template)))

# 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)")
# Upstream docs target does not generate text output.
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)
override_dh_auto_clean::
	rm -fr docs/_build

# Compatibility project, importing all XMLAda libraries.
XMLADA_GPR := debian/xmlada.gpr
override_dh_auto_build-arch: $(XMLADA_GPR)
$(XMLADA_GPR): distrib/xmlada.gpr
	sed s/xmlada_input/xmlada_input_sources/ $< > $@
override_dh_install::
	dh_install --package=$(schema_DEV_PKG) $(XMLADA_GPR) $(GPR_DIR)
override_dh_auto_clean::
	rm -f $(XMLADA_GPR)

# Remaining install tasks.
.PHONY: override_dh_install
override_dh_install::
	dh_install --package=$(unicode_DEV_PKG) xmlada-config usr/bin
	dh_install --remaining-packages

######################################################################
# 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=libxmlada-doc \
           --exclude=.adb --exclude=.ads --exclude=.gpr \
           --exclude=.xml --exclude=.xsd
	dh_compress --remaining-packages
