#!/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.

GNATCOLL_VERSION != \
  sed -n '/^Package: libgnatcoll\([0-9.]\+\)$$/{s//\1/p;q}' debian/control

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
DEB_GPRBUILDFLAGS_NO_J := 1
include /usr/share/dpkg/architecture.mk
include /usr/share/dpkg/buildflags.mk
include /usr/share/dpkg/buildopts.mk
include $(wildcard /usr/share/ada/debian_packaging.mk)
# wildcard means: not during -indep builds.

%:
	dh $@

.PHONY: $(addprefix override_dh_auto_,configure clean build-arch build-indep test install)
# Disable autodetection of upstream targets.
# We want to avoid clean and install.  Moreover, test does not exist,
# but while figuring this during -indep builds, debhelper
# would run Make and Make would fail to run gprconfig.

# static and relocatable, but not static-pic
# Prevent Makefile from messing with gprconfig
# (NORMALIZED_TARGET is wrong at least for i386 and hurd-i386).
override_dh_auto_build-arch:
	dh_auto_build -- \
	  TARGET=$(DEB_HOST_GNU_TYPE) \
	  NORMALIZED_TARGET=$(DEB_HOST_GNU_TYPE) \
	  LIBRARY_TYPES='static relocatable' \
	  GNATCOLL_VERSION=$(GNATCOLL_VERSION) \
	  GPRBUILD_OPTIONS='$(GPRBUILDFLAGS)' \
	  $(addprefix PROCESSORS=,$(DEB_BUILD_OPTION_PARALLEL))

override_dh_auto_build-indep:
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
	dh_auto_build --sourcedirectory=docs -- html latexpdf text \
	  $(addprefix SPHINXOPTS=-j,$(DEB_BUILD_OPTION_PARALLEL))
endif

# Force regeneration of runtime.py.
override_dh_auto_configure:
	rm -f distrib/gnatcoll/runtime.py
override_dh_auto_build-indep: distrib/gnatcoll/runtime.py
distrib/gnatcoll/runtime.py:
	cd distrib && python3 gen_gps.py

# Avoid a warning about doc-main-package.
doc_pkg := libgnatcoll-doc
please_644 := debian/$(doc_pkg)/usr/share/doc/$(doc_pkg)/examples/coders/base64-demo
override_dh_installdocs:
	dh_installdocs --package=$(doc_pkg) --doc-main-package=$(doc_pkg)
	dh_installdocs --remaining-packages
override_dh_installexamples:
	dh_installexamples --package=$(doc_pkg) --doc-main-package=$(doc_pkg)
	dh_installexamples --remaining-packages
  # Fix a silly permission.
  # The file does not exist during -arch builds.
	if test -f $(please_644); then chmod 644 $(please_644); fi
