#!/usr/bin/make -f
#
# Debian Makefile for AUnit
# Copyright (c) 2009      Stephen Leake <stephen_leake@stephe-leake.org>
# Copyright (c) 2004-2006 Ludovic Brenta <ludovic@ludovic-brenta.org>
# Copyright (c) 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.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.
#
include /usr/share/dpkg/default.mk
include /usr/share/ada/debian_packaging.mk

ADAFLAGS += -gnatfno -gnatwa -gnatVa

LIB_NAME := $(patsubst lib%,%,$(DEB_SOURCE))

SOVERSION := $(shell sed -n -e "s/^Package: lib$(LIB_NAME)\([[:digit:]]\+\)$$/\1/p" debian/control)
ifndef SOVERSION
  $(warning Could not guess SOVERSION from debian/control)
  # Not error. Policy defines targets that may be run from outside build dir.
endif

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

PROJECT := debian/$(LIB_NAME).gpr
override_dh_auto_build-arch:
	gnatmake $(BUILDER_OPTIONS) -P$(PROJECT) -XKIND=static \
          $(foreach var,ADAFLAGS,-X$(var)="$($(var))")
	gnatmake $(BUILDER_OPTIONS) -P$(PROJECT) -XKIND=dynamic \
          $(foreach var,ADAFLAGS LDFLAGS SOVERSION,-X$(var)="$($(var))")

override_dh_auto_clean:
	rm -f $(foreach k, dynamic static \
             ,$(foreach d, lib obj \
             ,debian/$(LIB_NAME)-$(d)-$(k)/*))

# First package is -dev, but we avoid depending on the aliversion.
override_dh_compress:
	dh_compress --exclude=-dev/examples/
	dh_compress --remaining-packages

######################################################################
RUN_TESTS_TMP := lib$(LIB_NAME)-run-tests-XXXXXXXXXX

# The plural is a lie: only link-with-shared is executed.
# You may run the other test with a manual modification.
# TODO: use adttest and remove all this.

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
