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

binary binary-arch binary-indep build build-arch build-indep clean:
	dh $@ --with ada-library

# Ignore upstream's build system
override_dh_auto_configure:
override_dh_auto_build-arch:: # Will receive prerequisites
override_dh_auto_build-indep: # Will receive prerequisites
override_dh_auto_test:
override_dh_auto_install:
override_dh_auto_clean::      # Will receive recipes

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

include /usr/share/dpkg/default.mk
include /usr/share/ada/debian_packaging.mk
CFLAGS += -Wall -Wextra -fstack-check
ADAFLAGS += -Wall -Wextra -fstack-check -gnatVa -gnata -gnatf -gnatn -gnato

exported_var_names := ADAFLAGS LDFLAGS \
  gtkada_SOVERSION gtkada_LDLIBS \
  gnomeada_SOVERSION gnomeada_LDLIBS \
  gtkglada_SOVERSION gtkglada_LDLIBS
exported_variables = $(foreach v,$(exported_var_names),"-X$(v)=$($(v))")

######################################################################
# We override the dh_ada_library instead of using a file because we
# need to export LDLIBS variables. SOVERSIONS will be ignored but need
# to be defined.
# The override comes very soon, so that every library may append
# recipes creating a versioned one for compatibility.

override_dh_ada_library::
	dh_ada_library $(foreach l,gtkada gtkglada gnomeada, \
          KIND=dynamic \
          $(foreach v,$(exported_var_names),"$(v)=$($(v))") \
          debian/$(l).gpr)

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

# Template to avoid redundant code for libraries.

define library_template
# 1: library name, as in libNAME.so.xxx
# 1_pkg_config: arguments for pkg-config.
# 1_VERSIONED_GPR: if not empty, add a compatibility copy of system gpr file.

$(1)_SOVERSION  := $(shell sed -n -r 's/^Package: lib$(1)([[:digit:].]+)$$/\1/p' debian/control)
$(1)_LDLIBS     := `pkg-config --libs-only-l $($(1)_pkg_config)`

override_dh_auto_build-arch::
	gnatmake -Pdebian/$(1).gpr -XKIND=dynamic $(BUILDER_OPTIONS) $$(exported_variables)
	gnatmake -Pdebian/$(1).gpr -XKIND=static  $(BUILDER_OPTIONS) $$(exported_variables)
override_dh_auto_clean::
	rm -f debian/$(1)_lib_dynamic/* \
              debian/$(1)_lib_static/* \
              debian/$(1)_obj_dynamic/* \
              debian/$(1)_obj_static/*
# The shared library dir only needs to be cleaned once.

# For compatibility with previous versions, add a duplicate project
# with a versioned name. Of course, this is only possible after
# dh_ada_library has created the first one.
# The -dev subdir may not exist (for -indep targets).
ifneq (,$($(1)_VERSIONED_GPR))
override_dh_ada_library::
	if cd debian/lib$(1)*-dev/usr/share/ada/adainclude 2> /dev/null; then \
          sed -e "s/library project $(1) is/library project $(1)2 is/" \
              -e "s/end $(1);/end $(1)2;/" \
              $(1).gpr > $(1)2.gpr; \
        fi
endif

endef # library_template

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

##########
# gtkada #
##########

# The sources, Ada and C, call these C functions:
#   cairo_* gdk_* g_* g_module_* gtk_* pango_* XWarpPointer
# When this list changes, update:
#   control: Build-Depends
#   control: Depends for libgtkada*-dev
#   and this variable:
gtkada_pkg_config := cairo gdk-2.0 glib-2.0 gmodule-2.0 gtk+-2.0 pango x11
# In case you are wondering, gmodule is part of libglib-dev.

# Compile C files into the object directory, so that gnatlink embeds
# them. The project must mention the C language, and we must store
# objects from distinct libraries in distinct directories.
C_SRC := $(wildcard src/*.c)

C_DYNAMIC := $(patsubst src/%.c,debian/gtkada_obj_dynamic/%.o,$(C_SRC))
C_STATIC  := $(patsubst src/%.c,debian/gtkada_obj_static/%.o,$(C_SRC))
override_dh_auto_build-arch:: $(C_DYNAMIC) $(C_STATIC)
$(C_DYNAMIC) $(C_STATIC): CFLAGS += `pkg-config --cflags-only-I $(gtkada_pkg_config)`
$(C_DYNAMIC): debian/gtkada_obj_dynamic/%.o: src/%.c
	$(CC) -c $(CPPFLAGS) $(CFLAGS) $< -o $@ -fPIC
$(C_STATIC): debian/gtkada_obj_static/%.o: src/%.c
	$(CC) -c $(CPPFLAGS) $(CFLAGS) $< -o $@
# Will be cleaned as Ada objects.

override_dh_auto_build-arch:: src/gtkada-intl.adb
src/gtkada-intl.adb: src/gtkada-intl.gpb
	gnatprep -DGETTEXT_INTL=False -DHAVE_GETTEXT=True $< $@
override_dh_auto_clean::
	rm -f src/gtkada-intl.adb

$(C_STATIC) $(C_DYNAMIC): src/config.h
src/config.h: src/config.h.in
	touch $@
override_dh_auto_clean::
	rm -f src/config.h

gtkada_VERSIONED_GPR := true
$(eval $(call library_template,gtkada))

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

############
# gtkglada #
############

# The only C source files, gdkgl.[ch] and gtkglarea.[ch], are ignored
# in favor of the packaged libgtkgl-dev.
# The Ada sources call these C functions:
# gl[A-Z]* glu[A-Z]* g[dt]k_gl_*
# However, no glu function is ever actually called and we can drop the
# dependency. But we Build-Depend: libglu-dev to circumvent #720991.
# When this list changes, update:
#   control: Build-Depends
#   control: Depends for libgtkglada*-dev
#   and this variable:
gtkglada_pkg_config := gl gtkgl-2.0

# It seems that x11 is not needed anymore for gtkglada.

$(eval $(call library_template,gtkglada))

############
# gnomeada #
############

# The sources, only in Ada, call these C functions: bonobo_* gnome_*.
# When this list changes, update:
#   control: Build-Depends
#   control: Depends for libgnomeada*-dev
#   and this variable:
gnomeada_pkg_config := libbonoboui-2.0 libgnomeui-2.0

gnomeada_VERSIONED_GPR := true
$(eval $(call library_template,gnomeada))

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

TOOLS := src/tools/obj/gtkada-dialog

override_dh_auto_build-arch::
	gnatmake -Pdebian/tools.gpr -XKIND=dynamic $(BUILDER_OPTIONS) $(exported_variables)
override_dh_auto_clean::
	rm -f src/tools/obj/*

override_dh_install-arch::
	dh_install --package=libgtkada-bin $(TOOLS) usr/bin
	dh_installman --package=libgtkada-bin docs/gtkada-dialog.1

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

GTKADA_CONFIG := debian/gtkada-config
GTKADA_CONFIG_VARS := \
  DEB_HOST_MULTIARCH \
  gtkada_LDLIBS \
  gtkada_SOVERSION

override_dh_auto_build-arch:: $(GTKADA_CONFIG)
override_dh_install-arch::
	dh_install --package=libgtkada-bin $(GTKADA_CONFIG) usr/bin
	dh_installman --package=libgtkada-bin docs/gtkada-config.1

$(GTKADA_CONFIG): %: %.sed
	sed $< $(foreach v,$(GTKADA_CONFIG_VARS),-e 's/@$(v)@/$($(v))/') > $@
override_dh_auto_clean::
	rm -f $(GTKADA_CONFIG)

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

TESTGTK_TGZ := testgtk.tgz

override_dh_auto_build-indep: $(TESTGTK_TGZ)
override_dh_install-indep::
	dh_installexamples --package=libgtkada-doc $(TESTGTK_TGZ)

$(TESTGTK_TGZ):
	tar czf $@ testgtk
override_dh_auto_clean::
	rm -f $(TESTGTK_TGZ)

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

UG_DIR := docs/gtkada_ug


UG_TXT  := $(UG_DIR)/gtkada_ug.txt
override_dh_auto_build-indep: $(UG_TXT)
override_dh_install-indep::
	dh_installdocs --package=libgtkada-doc $(UG_TXT)

$(UG_TXT): %/gtkada_ug.txt: %/gtkada_ug.texi %/hierarchy.txt
	cd $* && makeinfo --plaintext -o $(notdir $@ $<)
override_dh_auto_clean::
	rm -f $(UG_TXT)


UG_INFO := $(UG_DIR)/gtkada_ug.info
override_dh_auto_build-indep: $(UG_INFO)
override_dh_install-indep::
	dh_installinfo --package=libgtkada-doc $(UG_INFO)

# Generated in situ so that makeinfo references the images. This does
# not change much as long dh_installinfo dose not embed them.
$(UG_INFO): %/gtkada_ug.info: %/gtkada_ug.texi %/boxes.jpg %/hierarchy.jpg %/hierarchy.txt
	cd $* && makeinfo -o $(notdir $@ $<)
	sed -i $@ -e \
          's|\(image src="\)\([^"]\+"\)|\1/usr/share/doc/libgtkada-doc/\2|g'

override_dh_auto_clean::
	rm -f $(UG_INFO)


UG_PDF  := $(UG_DIR)/gtkada_ug.pdf
override_dh_auto_build-indep: $(UG_PDF)
override_dh_install-indep::
	dh_installdocs --package=libgtkada-doc $(UG_PDF)

# Generated in situ so that texi2pdf embeds images.
$(UG_PDF): %/gtkada_ug.pdf: %/gtkada_ug.texi %/boxes.jpg %/hierarchy.jpg
	cd $* && texi2pdf --silent -o $(notdir $@ $<)
override_dh_auto_clean::
	rm -f $(addprefix $(UG_DIR)/gtkada_ug.,aux cp cps fn ky log pdf pg toc tp vr)


UG_HTML := $(UG_DIR)/gtkada_ug.html
override_dh_auto_build-indep: $(UG_HTML)
override_dh_install-indep::
	dh_installdocs --package=libgtkada-doc $(UG_HTML) $(UG_DIR)/boxes.jpg $(UG_DIR)/hierarchy.jpg

$(UG_DIR)/gtkada_ug.html: %/gtkada_ug.html: %/gtkada_ug.texi %/boxes.jpg %/hierarchy.jpg
	cd $* && makeinfo --no-validate --no-split --html -o $(notdir $@ $<)
override_dh_auto_clean::
	rm -f $(UG_HTML)


# fig2dev correctly embeds boxes.gif even from another directory.
$(UG_DIR)/boxes.jpg: %.jpg: %.fig %.gif
	fig2dev -L jpeg $< $@
override_dh_auto_clean::
	rm -f $(UG_DIR)/boxes.jpg

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

# TODO: the reference manual needs generated parts:
# docs/gtkada_rm/generated.texi docs/gtkada_rm/generated_menu.texi:
# cd docs/gtkada_rm && \ ./generate_doc.pl `find
# $(addprefix ../../src/, . generated gnome opengl) -name "*.ads"`

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

override_dh_compress:
	dh_compress --package=libgtkada-doc \
           --exclude=.glade \
           --exclude=.adb --exclude=.ads \
           --exclude=.c --exclude=.h \
           --exclude=Makefile \
           --exclude=.pdf
	dh_compress --remaining-packages

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

GET_ORIG_MTN := mtn://www.ada-france.org?com.adacore.gtkada.debian
GET_ORIG_DIR := libgtkada
GET_ORIG_TGZ := libgtkada.tgz
GET_ORIG_TMP := libgtkada-get-orig-XXXXXXXXXX

get-orig-source:
	TMPDIR=$$(mktemp --tmpdir --directory $(GET_ORIG_TMP)) && \
        mtn clone -d $$TMPDIR/db.mtn $(GET_ORIG_MTN) -k "" $$TMPDIR/$(GET_ORIG_DIR) && \
        (cd $$TMPDIR && tar --create --gzip --exclude="_MTN" $(GET_ORIG_DIR)) > $(GET_ORIG_TGZ) ; \
        rm -f -r $$TMPDIR
	@echo "The file $(GET_ORIG_TGZ) contains the latest Debian snapshot of upstream sources."

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

RUN_TESTS_TMP := libgtkada-run-tests-XXXXXXXXXX

run-tests:
	ADTTMP=`mktemp --tmpdir --directory $(RUN_TESTS_TMP)` && \
        ADTTMP=$$ADTTMP sh debian/tests/link-with-shared 2> $$ADTTMP/stderr; \
        status=$$?; cat $$ADTTMP/stderr; \
        if test $$status != 0; then \
          echo "Test failed: non zero exit status ($$status)."; \
        elif test -s $$ADTTMP/stderr; then \
          echo "Test failed: activity on stderr."; \
        fi; \
        rm -f -r $$ADTTMP
