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

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

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

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

######################################################################
# Policy specifies that get-orig-source may be run outside the source
# tree, so we rely on a temporary directory and hard-code the version.

orig_version    := 2.24.4
orig_archive    := libgtkada_$(orig_version).orig.tar.xz
orig_tmp_dir    := /tmp/libgtkada_get_orig_source_tmp_dir
# Set this to your own database on the command line if you already
# have a local clone of the upstream branch.
# By default, a temporary database is populated.
orig_mtn_db     := $(orig_tmp_dir)/tmpdb
orig_server     := www.ada-france.org
orig_server_key := f8a11727e8983cf9083c08c6a2acaa27e439dd39
orig_branch     := com.adacore.gtkada.debian
orig_revision   := t:libgtkada-$(orig_version)
orig_directory  := libgtkada-$(orig_version).orig

get-orig-source:
	test ! -e $(orig_archive) # Stop if the archive exists.
	mkdir $(orig_tmp_dir) # Stop if the temp dir exists.
	@echo
	@echo The server key below should be $(orig_server_key).
	@echo
	mtn clone --db=$(orig_mtn_db) mtn://$(orig_server)?$(orig_branch) \
          --branch=$(orig_branch) --revision=$(orig_revision) \
          $(orig_tmp_dir)/$(orig_directory)
	tar -c -J -f $(orig_archive) -C $(orig_tmp_dir) $(orig_directory) \
          --exclude=_MTN --exclude=.mtn-ignore
	rm -f -r $(orig_tmp_dir)
	@echo
	@echo Created $(orig_archive) in the current directory.
