#!/usr/bin/make -f
# debian/rules for libgtkada
# Copyright (c) 2003-2012 Ludovic Brenta <lbrenta@debian.org>
# Copyright (c) 2011-2016 Nicolas Boulenguez <nicolas@debian.org>
#
# This program 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.
#
# The full text of the GNU General Public License is in the file
# /usr/share/common-licenses/GPL-3 on Debian systems.

.SUFFIXES:

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

# Declaring these targets prevent dh calling upstream build system directly.
.PHONY: $(addprefix override_dh_auto_, \
  clean configure build-arch build-indep test-arch test-indep install)

override_dh_auto_clean::      # Will receive recipes
	dh_auto_clean

######################################################################

DEB_CFLAGS_MAINT_APPEND := -Wall -Wextra -fstack-check
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)

ADAFLAGS += -Wall -Wextra -fstack-check -gnatVa -gnatafo

# Work-around for gnat bug #717014: gtkada-canvas.adb uses math functions
# either built in gcc or provided by -lm, depending on the architecture.
LIBS := -lm

# Use a concrete target, because dh calls dh_configure for both -arch
# then -indep and we do not want ./configure to run twice.
override_dh_auto_configure: configure-stamp
configure-stamp: configure config.guess config.sub install-sh
 # Requires pkg-config package.
	dh_auto_configure -- \
	  --with-GL=no \
	  --enable-shared \
	  '--includedir=$${prefix}/share/ada/adainclude' \
	  $(foreach f,ADAFLAGS CFLAGS CPPFLAGS LDFLAGS OBJCFLAGS LIBS,"$(f)=$($(f))")
	touch $@
configure:
 # Requires autoconf package.
	autoconf
config.guess config.sub:
 # Requires autotools-dev package.
	ln -s /usr/share/misc/$@
install-sh:
 # Required, but actually unused.
	touch install-sh
override_dh_auto_clean::
	rm -f configure config.guess config.sub install-sh

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)'
 # Instead of running default target all: static relocatable tools tests,
 # - link tools dynamically
 # - run tests only if DEB_BUILD_OPTIONS does not contain nocheck.
	$(MAKE) static relocatable \
	  "PROCESSORS=$(BUILDER_JOBS)" \
	  "GPRBUILD_OPTIONS=$(filter-out -j$(BUILDER_JOBS),$(BUILDER_OPTIONS))"
	gprbuild -p ${BUILDER_OPTIONS} -XLIBRARY_TYPE=relocatable src/tools/tools.gpr

GNATPREP_BODIES := src/gtkada-intl testgtk/opengl/view_gl
override_dh_auto_build-arch: gnatprep-stamp
# gnatprep stamp is also a prerequisite for testgtk, see below.
gnatprep-stamp:
	$(MAKE) $(addsuffix .adb,$(GNATPREP_BODIES))
 # Makefile and *.gpb files are prerequisites for preprocessed *.adb.
 # The modification time of .adb will be forced to DEB_DATE.
 # Forbid Makefile to update .adb.
	touch --date='$(DEB_DATE)' Makefile $(addsuffix .gpb,$(GNATPREP_BODIES))
	touch $@

override_dh_auto_build-arch: | src/generated
src/generated:
	mkdir src/generated
 # Requires python package.
	$(MAKE) generate
override_dh_auto_clean::
	rm -fr src/generated
	rm -f contrib/*.pyc src/misc_generated.c

override_dh_auto_test-arch:
	$(MAKE) tests

# Installing the pkg-config script requires ALIversion and
# DEB_HOST_MULTIARCH, it is not convenient to use a separate file.
DEV_PKG := $(shell sed debian/control debian/control -ne \
  '/^Package: \(libgtkada[0-9.]\+-dev$$\)/{s//\1/;p;q}')
.PHONY: override_dh_install
override_dh_install:
	dh_install --package=${DEV_PKG} projects/gtkada.pc usr/lib/${DEB_HOST_MULTIARCH}/pkgconfig
	dh_install --remaining-packages

# Archive testgtk sources, so that the user does not need to
# uncompress them separately.
# Remove in-tree build system (README, *.gpr, objects and executables).
# Add project described in README.Debian. Move GL support to top dir.
TESTGTK_TGZ := testgtk.tgz
override_dh_auto_build-indep: $(TESTGTK_TGZ)
$(TESTGTK_TGZ):
	tar --create --auto-compress --file=$@ \
	  testgtk \
	  --exclude=testgtk/README \
	  --exclude=testgtk/obj/* \
	  --transform 's~^testgtk/opengl/view_gl\.ad~testgtk/view_gl.ad~' \
	  --exclude=testgtk/test_rtree \
	  --exclude=testgtk/testgtk \
	  --exclude=testgtk/testgtk.gpr \
	  debian/testgtk.gpr \
	  --transform 's/^debian/testgtk/'
override_dh_auto_clean::
	rm -f $(TESTGTK_TGZ)

$(TESTGTK_TGZ): gnatprep-stamp
