#!/usr/bin/make -f
# Build script for GNATColl in Debian.
# Copyright (c) 2014-2023 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 := gmp iconv lzma omp python3 readline syslog zlib

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/ada/packaging.mk

vars := -XGNATCOLL_BUILD_MODE=PROD

python3 install-python3: vars += \
  -XGNATCOLL_PYTHON_CFLAGS=`pkg-config --cflags python3-embed` \
  -XGNATCOLL_PYTHON_LIBS=`pkg-config --libs python3-embed`

iconv install-iconv: vars += -XGNATCOLL_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) $(vars)
	gprbuild $@/gnatcoll_$(@:3=).gpr \
	  -XLIBRARY_TYPE=relocatable \
	  -XGNATCOLL_VERSION=$(gnatcoll_$@_SO_VERSION) \
	  -p $(GPRBUILDFLAGS) $(vars)

$(libraries): default.cgpr
default.cgpr:
	gprconfig --batch $(GPRCONFIGFLAGS)

override_dh_auto_install: $(libraries:%=install-%)
  # The concrete project with all information available must be named
  # after the library name for dh-ada-library.
	sed s/GnatColl_Python/GnatColl_Python3/ \
	   debian/tmp/$(DEB_GNAT_PROJECT_DIR)/gnatcoll_python.gpr \
	 > debian/tmp/$(DEB_GNAT_PROJECT_DIR)/gnatcoll_python3.gpr
	rm debian/tmp/$(DEB_GNAT_PROJECT_DIR)/gnatcoll_python.gpr

install-%:
	gprinstall $*/gnatcoll_$(*:3=).gpr $(vars) \
	  -XLIBRARY_TYPE=static $(static_GPRINSTALLFLAGS)
	gprinstall $*/gnatcoll_$(*:3=).gpr $(vars) \
	  -XLIBRARY_TYPE=relocatable \
	  -XGNATCOLL_VERSION=$(gnatcoll_$*_SO_VERSION) \
	  $(call shared_GPRINSTALLFLAGS,gnatcoll_$*)

execute_before_dh_install:
  # Install a wrapper project so that 'with "gnatcoll_python.gpr";' works..
	dh_install -p$(gnatcoll_python3_DEV_PKG) debian/gnatcoll_python.gpr \
	  $(DEB_GNAT_PROJECT_DIR)

.PHONY: execute_before_dh_installdocs
execute_before_dh_installdocs:
	dh_installdocs -p$(gnatcoll_iconv_DEV_PKG)       iconv/docs/index.rst
	dh_installdocs -p$(gnatcoll_python3_DEV_PKG)    python/docs/index.rst
	dh_installdocs -p$(gnatcoll_readline_DEV_PKG) readline/docs/index.rst
	dh_installdocs -p$(gnatcoll_syslog_DEV_PKG)     syslog/docs/index.rst

.PHONY: execute_before_dh_installexamples
execute_before_dh_installexamples:
	dh_installexamples -p$(gnatcoll_gmp_DEV_PKG) gmp/examples/*

override_dh_gencontrol:
	dh_gencontrol -- -Vpython-version=$(shell \
	  python3 -V | sed 's/^Python \(3\.[0-9]\+\)\.[0-9]\+$$/\1/')
