#!/usr/bin/make -f
# Build script for XML/Ada in Debian.
# Copyright (c) 2003-2009 Ludovic Brenta <lbrenta@debian.org>
# Copyright (c) 2013-2015 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 := 5
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 $@ --with ada-library --with sphinxdoc

# shared.gpr allows dh-ada-library to parse upstream projects.
CONFIGURED := shared.gpr xmlada-config

.PHONY: override_dh_auto_configure
override_dh_auto_configure: $(CONFIGURED)
$(CONFIGURED): %: %.in
	sed "\
          s/@PACKAGE_VERSION@/$(DEB_VERSION_UPSTREAM)/g; \
          s/@DEFAULT_LIBRARY_TYPE@/relocatable/g; \
          s/@BUILD_TYPE@/distrib/g; \
          s/@SO_EXT@/.so/g; \
          s%@libdir@%/usr/lib/$(DEB_HOST_MULTIARCH)%g; \
          " $< > $@
override_dh_auto_clean::
	rm -f $(CONFIGURED)

.PHONY: override_dh_auto_build-arch
.PHONY: override_dh_auto_clean


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

LIBS :=
define library_template

override_dh_auto_build-arch::

 # Build dynamic library.
	gnatmake -c $(library)/*.ad[bs] -D $(library)/obj/relocatable \
          $(BUILDER_OPTIONS) \
          $(foreach l,$(LIBS),-aI$(l) -aO$(l)/obj/relocatable) \
          -cargs $(ADAFLAGS) -fPIC
	gcc -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.
	ln -s libxmlada_$(library).so.$(DEB_VERSION_UPSTREAM) $(library)/lib/relocatable/libxmlada_$(library).so

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

 LIBS := $(library) $(LIBS)

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

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.
.PHONY: override_dh_auto_build-indep
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

######################################################################
DEV_PKG = `sed -n '/^Package: \(libxmlada-$(1)[0-9.]\+-dev\)$$/{s//\1/p;q}' \
  debian/control`
XMLADA_GPR := debian/xmlada.gpr

$(XMLADA_GPR): distrib/xmlada.gpr
	sed s/xmlada_input/xmlada_input_sources/ $< > $@
override_dh_clean::
	rm -f $(XMLADA_GPR)

.PHONY: override_dh_install
override_dh_install: $(XMLADA_GPR)
	dh_install --package=$(call DEV_PKG,unicode) xmlada-config usr/bin
	dh_install --package=$(call DEV_PKG,schema) \
          $(XMLADA_GPR) usr/share/ada/adainclude
	dh_install --remaining-packages

######################################################################
.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
