#!/usr/bin/make -f
# debian/rules for libgtkada
# Copyright (c) 2003-2012 Ludovic Brenta <lbrenta@debian.org>
# Copyright (c) 2011-2017 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.

%:
	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

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

$(foreach line,$(shell sed -n '\
  s/^ gnat, gnat-\([0-9.]\+\),$$/ \
    GNAT_VERSION:=\1 \
  /p;\
  s/^Package: \(libgtkada[0-9.]\+-dev\)$$/ \
    dev_pkg:=\1 \
  /p;\
  s/^Package: libgtkada\([0-9.]\+\)$$/ \
    PACKAGE_VERSION:=\1 \
  /p;\
  ' debian/control),$(eval $(line)))

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-$(GNAT_VERSION).mk
ADAFLAGS += -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

# 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.
# OpenGL is disabled: it copies gtkgl2/gtk2 sources while gtk3 seems
#   to include more recent versions of the same files.
override_dh_auto_configure:
 # Requires pkg-config package.
	dh_auto_configure -- \
	  --with-GL=no \
	  --enable-shared \
	  --disable-static-pic \
	  '--includedir=$${prefix}/share/ada/adainclude' \
	  $(foreach f,CC CFLAGS CPPFLAGS LDFLAGS LIBS PACKAGE_VERSION,"$(f)=$($(f))")

 # Used by -arch (compilation) and -indep (testgtk.tgz).
	$(MAKE) src/gtkada-intl.adb testgtk/opengl/view_gl.adb

override_dh_auto_build-arch:
 # 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))")'

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
testgtk_exclude := obj/* test_rtree testgtk testgtk.gpr.orig
testgtk_gpr := testgtk/testgtk.gpr
reproducible_tar_options := --owner=0 --group=0 --numeric-owner
reproducible_gz_options  := --no-name

ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
override_dh_auto_build-indep: $(TESTGTK_TGZ)
endif
$(TESTGTK_TGZ):
	patch -b $(testgtk_gpr) debian/testgtk-no-opengl.diff
	GZIP='$(reproducible_gz_options)' \
	tar --create --auto-compress --file=$@ \
	  $(reproducible_tar_options) \
	  $(addprefix --exclude=testgtk/,$(testgtk_exclude)) \
	  testgtk
  # Restore the project, so that the clean target works as expected.
	mv -f $(testgtk_gpr).orig $(testgtk_gpr)
override_dh_auto_clean::
	rm -f $(TESTGTK_TGZ)

doc_main_tools := override_dh_installdocs override_dh_installexamples
.PHONY: $(doc_main_tools)
$(doc_main_tools): override_%:
	$* --package=libgtkada-doc --doc-main-package=$(dev_pkg)
	$* --remaining-packages