#!/usr/bin/make -f
# Build script for Ada Web Server in Debian.
# Copyright (c) 2003-2013 Ludovic Brenta <lbrenta@debian.org>
# 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, 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.

DEB_BUILD_MAINT_OPTIONS=hardening=+all,-pie
include /usr/share/dpkg/default.mk
include /usr/share/ada/debian_packaging*.mk
DEB_DATE := $(shell dpkg-parsechangelog -S date)

SSL := $(shell sed debian/control -ne ' \
  /^[ B].*\blibgnutls[0-9.]*-dev\b/ { s/.*/gnutls/ ; p; q };\
  /^[ B].*\blibssl-dev\b/           { s/.*/openssl/; p; q };\
  /^Package: /                      { s/.*/dummy/  ; p; q }')
soversion := $(shell sed debian/control -ne ' \
  /^Package: $(DEB_SOURCE)\([^-]\+\)$$/ { s//\1/; p; q}')

ADAFLAGS += -gnatafno -gnatVa -gnatwa
CFLAGS += `pkg-config --cflags $(filter-out dummy,$(SSL)) zlib`
LDLIBS := `pkg-config --libs   $(filter-out dummy,$(SSL)) zlib` -llber -lldap
soname := $(DEB_SOURCE).so.$(soversion)

# Circumvent #760211. Since LDFLAGS contains --as-needed, this setting
# only takes effect on architectures triggering the bug.
LDLIBS += -lpthread

.SUFFIXES:

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

# Ignore upstream makefile (and ./configure, if ever).
.PHONY: $(addprefix override_dh_auto_,configure build-arch build-indep test install clean)
# Configure test install exist and are empty so that dh_auto_* does
# nothing.  We will replace upstream makefiles with build-* and clean.

GPR_CONFIGURATION := default.cgpr
override_dh_auto_configure: $(GPR_CONFIGURATION)
$(GPR_CONFIGURATION):
	gprconfig --batch --config=Ada --config=C,,,,gnatgcc
override_dh_auto_clean::
	rm -f $(GPR_CONFIGURATION)

######################################################################
# See comments in aws-os_lib-tmplt.c.
build-aws-dynamic build-aws-static: src/core/aws-os_lib.ads
# Tell Make that the source must be created but never updated, even if
# its timestamp is tricked later for deterministic ALI files.
src/core/aws-os_lib.ads:|src/core/xoscons \
                         src/core/aws-os_lib-tmplt.i \
                         src/core/aws-os_lib-tmplt.s
	cd src/core; ./xoscons aws-os_lib
src/core/xoscons: ADAFLAGS += -fPIE
src/core/xoscons: LDFLAGS += -fPIE -pie
src/core/xoscons: config/setup/xoscons.adb
	gnatmake $(BUILDER_OPTIONS) $< -D src/core -o $@ -cargs $(ADAFLAGS) -largs $(LDFLAGS)
src/core/aws-os_lib-tmplt.s: %.s: %.i
	$(CC) -S $(CFLAGS) $< -o $@
src/core/aws-os_lib-tmplt.i: config/setup/aws-os_lib-tmplt.c
	$(CC) -C -E $(CPPFLAGS) -DTARGET=\"$(DEB_HOST_MULTIARCH)\" $< -o $@

override_dh_auto_clean::
	rm -f src/core/aws-os_lib* src/core/xoscons* src/core/xutil.*

######################################################################
# Build both versions of the library.

override_dh_auto_build-arch: build-aws-dynamic build-aws-static
.PHONY: build-aws-dynamic build-aws-static
build-aws-static:
	gprbuild $(BUILDER_OPTIONS) debian/build_aws.gpr -Xkind=static \
	  $(foreach v,ADAFLAGS CFLAGS CPPFLAGS SSL,"-X$(v)=$($(v))")
build-aws-dynamic:
 # Ensure deterministic timestamps in ALI files.
	find . -depth -name "*.ad[bs]" -a -newermt '$(DEB_DATE)' -print0 | \
	  xargs -0r touch --no-dereference --date='$(DEB_DATE)'
	gprbuild $(BUILDER_OPTIONS) debian/build_aws.gpr -Xkind=dynamic \
	  $(foreach v,ADAFLAGS CFLAGS CPPFLAGS LDFLAGS LDLIBS soname SSL,"-X$(v)=$($(v))")

override_dh_auto_clean::
	rm -f $(foreach d, lib obj,$(foreach k,dynamic static, \
                debian/build_aws_$(d)_$(k)/*))

# An override is necessary because LDLIBS contains a shell escape.
# The projet should import directly xmlada.gpr and
# templates-parser.gpr (both are used by specs).
.PHONY: override_dh_ada_library-arch
override_dh_ada_library-arch:
	dh_ada_library kind=dynamic \
          $(foreach v,LDLIBS soname SSL,"$(v)=$($(v))") \
          debian/build_aws.gpr

######################################################################
TOOLS_GPR := debian/tools.gpr

override_dh_auto_build-arch: build-tools
.PHONY: build-tools
build-tools: ADAFLAGS += -fPIE
build-tools: LDFLAGS += -fPIE -pie
build-tools: build-aws-dynamic
	gprbuild $(BUILDER_OPTIONS) $(TOOLS_GPR) -Xkind=dynamic \
          $(foreach v,ADAFLAGS LDFLAGS LDLIBS soname SSL,"-X$(v)=$($(v))")

override_dh_auto_clean::
	rm -f debian/tools_obj/*

######################################################################
.PHONY: override_dh_compress
override_dh_compress:
	dh_compress --package=libaws-doc -X.adb -X.ads -X.thtml -X.wsdl
	dh_compress --remaining-packages

INSTALLED_EXAMPLES := debian/libaws-doc/usr/share/doc/libaws-doc/examples
.PHONY: override_dh_installexamples-indep
override_dh_installexamples-indep:
	dh_installexamples --indep
 # Fix permissions.
	find $(INSTALLED_EXAMPLES) -type f -print0 | xargs -0r chmod 644
 # Replace duplicate files with symlinks, then make the links relative.
	rdfind -outputname /dev/null -makesymlinks true $(INSTALLED_EXAMPLES)
	symlinks -crs $(INSTALLED_EXAMPLES)

######################################################################
# Documentation.
# ./makefile would recompute all makefile.conf variables, assume only
# HTML output format, and assume the existence of templates_parser/.

# 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: ADAFLAGS += -fPIE
override_dh_auto_build-indep: LDFLAGS += -fPIE -pie
override_dh_auto_build-indep:
	faketime -f "$(DEB_DATE_FAKETIME)" \
	$(MAKE) -C docs html info latexpdf text style-guide.pdf \
          SPHINXOPTS=-j$(BUILDER_JOBS) $(foreach v,ADAFLAGS LDFLAGS,"$(v)=$($(v))")
override_dh_auto_clean::
	$(MAKE) -C docs clean
	rm -f docs/source/*.pyc docs/source/confvars.py

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