#!/usr/bin/make -f

# Copyright (c) 2003-2009 Ludovic Brenta <lbrenta@debian.org>
# Copyright (c) 2009-2016 Nicolas Boulenguez <nicolas@debian.org>

# This build script 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.

# On Debian systems, the full text of the GPL is in the file
# /usr/share/common-licenses/GPL-3.

DEB_BUILD_MAINT_OPTIONS=hardening=+all,-pie
include /usr/share/dpkg/default.mk
include /usr/share/ada/debian_packaging*.mk

DEB_DATE := $(shell dpkg-parsechangelog -S date)

ADAFLAGS += -gnatn

SOVERSION := $(shell sed -n -e "s/^Package: $(DEB_SOURCE)\([[: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

# All these targets are redefined or empty. No implicit default behaviour.
.PHONY: $(addprefix override_dh_auto_, \
  configure build-arch build-indep test-arch test-indep install clean)

OPTS += $(foreach var, \
          ADAFLAGS CC CFLAGS CPPFLAGS LDFLAGS SOVERSION,$(var)="$($(var))") \
          GNATMAKE_OPTIONS="$(BUILDER_OPTIONS)"
override_dh_auto_clean:
	dh_auto_clean -- $(OPTS)
	rm -f default.cgpr
default.cgpr:
	gprconfig --batch --config=Ada --config=C,,,,gnatgcc
override_dh_auto_build-arch: default.cgpr
 # Ensure deterministic timestamps in ALI files.
	find . -depth -name "*.ad[bs]" -a -newermt '$(DEB_DATE)' -print0 | \
	  xargs -0r touch --no-dereference --date='$(DEB_DATE)'
	dh_auto_build -a -- $(OPTS)
override_dh_auto_test-arch:
	dh_auto_test -a -- $(OPTS)

.PHONY: override_dh_compress
override_dh_compress:
	dh_compress --all $(addprefix --exclude=,.ads .adb .c .h)
