#!/usr/bin/make -f
# Build script for GNATColl in Debian.
# Copyright (c) 2014-2022 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.

libraries := python3
$(foreach line,$(shell sed -En '\
  s/^Package: (libgnatcoll-([a-z]+)[0-9.]+-dev)$$/libraries+=\2 \2_dev:=\1/p;\
  s/^Package: (libgnatcoll-python3-[0-9.]+-dev)$$/python3_dev:=\1         /p;\
  s/^Package: libgnatcoll-([a-z]+)([0-9.]+)$$/    \1_version:=\2          /p;\
  s/^Package: libgnatcoll-python3-([0-9.]+)$$/    python3_version:=\1     /p;\
  ' debian/control),$(eval $(line)))

DPKG_EXPORT_BUILDFLAGS := 1
DEB_BUILD_MAINT_OPTIONS := hardening=+all
DEB_LDFLAGS_MAINT_APPEND := \
  -Wl,--no-allow-shlib-undefined \
  -Wl,--no-copy-dt-needed-entries \
  -Wl,--no-undefined
include /usr/share/dpkg/buildflags.mk
include /usr/share/dpkg/buildopts.mk
include /usr/share/ada/debian_packaging.mk

BUILD := PROD
vars += BUILD

GNATCOLL_PYTHON_CFLAGS := `pkg-config --cflags python3-embed`
GNATCOLL_PYTHON_LIBS   := `pkg-config --libs   python3-embed`
vars += GNATCOLL_PYTHON_CFLAGS GNATCOLL_PYTHON_LIBS

GNATCOLL_ICONV_OPT :=
vars += GNATCOLL_ICONV_OPT

%:
	dh $@

.PHONY: override_dh_auto_build
override_dh_auto_build: $(libraries)

.PHONY: $(libraries)
$(libraries):
	gprbuild $@/gnatcoll_$(@:3=).gpr \
	  -XLIBRARY_TYPE=static \
	  -p $(GPRBUILDFLAGS) $(foreach v,$(vars),-X$(v)="$($(v))")
	gprbuild $@/gnatcoll_$(@:3=).gpr \
	  -XLIBRARY_TYPE=relocatable \
	  -XGNATCOLL_VERSION=$($@_version) \
	  -p $(GPRBUILDFLAGS) $(foreach v,$(vars),-X$(v)="$($(v))")

$(libraries): default.cgpr
gnat_major != gnatmake --version | sed 's/.* \([0-9]\+\).*/\1/;q'
default.cgpr:
	gprconfig --batch --config=Ada --config=C,,,,gcc-$(gnat_major)

.PHONY: override_dh_installdocs
override_dh_installdocs:
	dh_installdocs -p$(iconv_dev)       iconv/docs/index.rst
	dh_installdocs -p$(readline_dev) readline/docs/index.rst
	dh_installdocs -p$(syslog_dev)     syslog/docs/index.rst
	dh_installdocs -p$(python3_dev)    python/docs/index.rst
	dh_installdocs --remaining-packages

.PHONY: override_dh_installexamples
override_dh_installexamples:
	dh_installexamples -p$(gmp_dev) gmp/examples/*
	dh_installexamples --remaining-packages

.PHONY: override_dh_ada_library
override_dh_ada_library:
	dh_ada_library $(foreach l,$(libraries), \
	  LIBRARY_TYPE=relocatable \
	  GNATCOLL_VERSION=$($(l)_version) \
	  $(foreach v,$(vars),$(v)="$($(v))") \
	  $(l)/gnatcoll_$(l:3=).gpr)
