#!/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
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.
# Note: despite what the help string says, --enable-shared does *not*
# link tools dynamically when both libraries are built. We use
# LIBRARY_KIND_FOR_TOOLS later to ensure dynamic linking.
# PACKAGE_VERSION is used as shared object version for the library.
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' \
	  PACKAGE_VERSION=`sed -nr '/^Package: libgtkada([0-9.]+)$$/{s//\1/p;q}' debian/control` \
	  $(foreach f,CC CFLAGS CPPFLAGS LDFLAGS 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]" -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 tools \
	  "PROCESSORS=$(BUILDER_JOBS)" \
	  LIBRARY_TYPE_FOR_TOOLS=relocatable \
	  'GPRBUILD_OPTIONS= \
	    $(filter-out -j$(BUILDER_JOBS),$(BUILDER_OPTIONS)) \
	    $(foreach v,ADAFLAGS CFLAGS CPPFLAGS LDFLAGS,"-X$(v)=$($(v))")'

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 \
	  "PROCESSORS=$(BUILDER_JOBS)" \
	  LIBRARY_TYPE_FOR_TOOLS=relocatable \
	  'GPRBUILD_OPTIONS= \
	    $(filter-out -j$(BUILDER_JOBS),$(BUILDER_OPTIONS)) \
	    $(foreach v,ADAFLAGS CFLAGS CPPFLAGS LDFLAGS,"-X$(v)=$($(v))")'

# Archive testgtk sources, so that the user does not need to
# uncompress them separately.
# Remove objects and executables.
TESTGTK_TGZ := testgtk.tgz
override_dh_auto_build-indep: $(TESTGTK_TGZ)
$(TESTGTK_TGZ):
	tar --create --auto-compress --file=$@ \
	  testgtk \
	  --exclude=testgtk/obj/* \
	  --exclude=testgtk/test_rtree \
	  --exclude=testgtk/testgtk
override_dh_auto_clean::
	rm -f $(TESTGTK_TGZ)

$(TESTGTK_TGZ): gnatprep-stamp
