#!/usr/bin/make -f
# Build script for XML/Ada in Debian.
# Copyright (c) 2003-2009 Ludovic Brenta <lbrenta@debian.org>
# Copyright (c) 2013-2023 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.

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

# NOTE:
# In order to prevent a circular build-dependency, the Debian packages
# for XML/Ada and GPRBuild bother to build without project support.
# Please keep the two debian/rules files similar enough to ease
# backport of good ideas.

######################################################################

DEB_BUILD_MAINT_OPTIONS := hardening=+all
DEB_ADAFLAGS_MAINT_APPEND := -gnatwa -Wall -gnatafno -gnatVa
DEB_LDFLAGS_MAINT_APPEND := \
  -Wl,--no-undefined \
  -Wl,--no-copy-dt-needed-entries \
  -Wl,--no-allow-shlib-undefined
include /usr/share/dpkg/buildflags.mk
include /usr/share/dpkg/buildopts.mk
include $(wildcard /usr/share/ada/packaging.mk)
# wildcard in case only Build-Depends-Indep are installed.

# Ignore gnat.adc in top directory.
GNATMAKEFLAGS += -gnatA

######################################################################
%:
	dh $@

# Rewrite upstream configure/make build system.
.PHONY: $(addprefix override_dh_auto_, \
  configure build-arch build-indep test install-arch install-indep clean)

# Rewrite upstream targets without gprbuild or gprclean.
# gnatmake does not support projects anymore, we cannot use xmlada.gpr.
# The priority is to bootstrap, not a complete dependency graph.

######################################################################
# Rewrite unicode/importer/Makefile without network and gprbuild.
# Generate the unicode-names-*.ads before the override_dh_auto_build
# phase so that wildcard below sees the generated files.
override_dh_auto_build-arch::
	mkdir -p obj unicode/importer/generated
	gnatmake unicode/importer/convert.adb -D obj -o obj/convert \
	  $(GNATMAKEFLAGS) \
	  -cargs $(ADAFLAGS) \
	  -largs $(LDFLAGS)
	cd unicode/importer && ../../obj/convert \
	  /usr/share/unicode/Blocks.txt \
	  /usr/share/unicode/NameAliases.txt \
	  /usr/share/unicode/UnicodeData.txt
	rm -f unicode/unicode-names-*.ads
	mv unicode/importer/generated/unicode-names-*.ads unicode

######################################################################

# For source directories (-aI) and ALI directories (-aO), blindly list
# all build dependencies and append each library once it is
# built. This does not hurt, and covers all direct and indirect
# dependencies.

# For installed shared libraries (-l) and locally built shared
# objects), the same strategy works thanks to --as-needed, which is
# the default since gcc-9, provided a proper ordering.

# Libraries provided by Ada -dev packages in Build-Depends.
ada_build_deps :=

# These list grow each time the template is expanded.
gnatmakeflags := $(GNATMAKEFLAGS) \
                 $(ada_build_deps:%=-aI/$(DEB_ADA_SOURCE_DIR)/%) \
                 $(ada_build_deps:%=-aO/$(DEB_ADA_LIB_INFO_DIR)/%)
shared_objects_so_far := $(ada_build_deps:%=-l%) \
                         -lgnarl-$(DEB_GNAT_VERSION) \
                         -lgnat-$(DEB_GNAT_VERSION)

define library_template
# Parameters:
# _SRCDIR: directory containing the sources files
# _SO_VERSION : part of the shared object name
# _imported_projects: for the generated project.

  override_dh_auto_build-arch::

	mkdir -p obj/shared_$1 obj/static_$1

  # Build dynamic library.
    # -fPIC/-shared overrides -fPIE/-pie in ADAFLAGS/LDFLAGS.
	gnatmake -c `grep -L '^separate ' $($1_SRCDIR)/*.ad[bs] | grep -v __` \
	  $(gnatmakeflags) -D obj/shared_$1 -cargs $(ADAFLAGS) -fPIC
	gcc-$(DEB_GNAT_VERSION) -shared \
	  -o obj/lib$1.so.$($1_SO_VERSION) \
	  -Wl,-soname,lib$1.so.$($1_SO_VERSION) \
	  $(LDFLAGS) obj/shared_$1/*.o $(shared_objects_so_far)

  # Mark ALI files as read-only.
	chmod 444 obj/shared_$1/*.ali

  # Build static library.
	gnatmake -c `grep -L '^separate ' $($1_SRCDIR)/*.ad[bs] | grep -v __` \
	  $(gnatmakeflags) -D obj/static_$1 -cargs $(ADAFLAGS)
	ar rc obj/lib$1.a obj/static_$1/*.o
	ranlib obj/lib$1.a

  # Externally built project installed for end users.
	sed ' \
	  /@IMPORTS@/ { \
	    s//$($1_imported_projects:%=with "%.gpr";\n)/; \
	    s/\n /\n/g; }; \
	  s/@library@/$1/; \
	  s|@DEB_LIB_DIR@|$(DEB_LIB_DIR)|; \
	  s|@DEB_ADA_SOURCE_DIR@|$(DEB_ADA_SOURCE_DIR)|; \
	  s|@DEB_ADA_LIB_INFO_DIR@|$(DEB_ADA_LIB_INFO_DIR)|; \
	  ' debian/template.gpr > obj/$1.gpr

  override_dh_auto_install-arch::
	install -m644 -Dtdebian/tmp/$(DEB_ADA_SOURCE_DIR)/$1 $($1_SRCDIR)/*.ad[bs]
	install -m444 -Dtdebian/tmp/$(DEB_ADA_LIB_INFO_DIR)/$1 obj/shared_$1/*.ali
	install -m644 -Dtdebian/tmp/$(DEB_GNAT_PROJECT_DIR) obj/$1.gpr
	install -m644 -Dtdebian/tmp/$(DEB_LIB_DIR) \
	  obj/lib$1.so.$($1_SO_VERSION) obj/lib$1.a
	ln -fs lib$1.so.$($1_SO_VERSION) debian/tmp/$(DEB_LIB_DIR)/lib$1.so

  gnatmakeflags += -aI$($1_SRCDIR) -aOobj/shared_$1
  shared_objects_so_far := obj/lib$1.so.$($1_SO_VERSION) \
                           $(shared_objects_so_far)
endef

xmlada_unicode_SRCDIR := unicode
$(eval $(call library_template,xmlada_unicode))

xmlada_input_SRCDIR := input_sources
xmlada_input_imported_projects := xmlada_unicode
$(eval $(call library_template,xmlada_input))

xmlada_sax_SRCDIR := sax
xmlada_sax_imported_projects := xmlada_input xmlada_unicode
$(eval $(call library_template,xmlada_sax))

xmlada_dom_SRCDIR := dom
xmlada_dom_imported_projects := xmlada_sax xmlada_unicode
$(eval $(call library_template,xmlada_dom))

xmlada_schema_SRCDIR := schema
xmlada_schema_imported_projects := xmlada_dom xmlada_sax xmlada_unicode
$(eval $(call library_template,xmlada_schema))

######################################################################
override_dh_auto_build-indep:
  ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
	$(MAKE) -Cdocs html latexpdf text \
	  $(DEB_BUILD_OPTION_PARALLEL:%=SPHINXOPTS=-j%)
  endif

# Compatibility project, importing all XMLAda libraries.
execute_after_dh_install-arch:
	dh_install --package=libxmlada-schema-dev \
	  distrib/xmlada.gpr \
	  $(DEB_GNAT_PROJECT_DIR)
