#!/usr/bin/make -f
# Build script for GNATColl database components 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.

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/architecture.mk

# On mips64el gnatcoll-xref.adb generates long jump that do not fit in
# standard branch instructions. Use the mxgot option to workaround that.
# See bug #953069.
ifeq ($(DEB_HOST_ARCH),mips64el)
  DEB_ADAFLAGS_MAINT_APPEND := -mxgot
  DEB_CFLAGS_MAINT_APPEND := -mxgot
endif

# Use -Os on mipsel to workaround further jump length issues
# See bug #971018.
ifeq ($(DEB_HOST_ARCH),mipsel)
  DEB_ADAFLAGS_MAINT_APPEND := -Os
  DEB_CFLAGS_MAINT_APPEND := -Os
endif

include /usr/share/dpkg/buildflags.mk
include /usr/share/dpkg/buildopts.mk
include $(wildcard /usr/share/ada/packaging.mk)
# wildcard means: not during -indep builds.

# The order matters for dh_ada_library.
libs := sql sqlite postgres xref

gpr_vars := \
  -XGNATCOLL_SQLITE=external \
  $(libs:%=-aP%) \
  -XGNATCOLL_SQL_VERSION=$(gnatcoll_sql_SO_VERSION) \
  -XGNATCOLL_SQLITE_VERSION=$(gnatcoll_sqlite_SO_VERSION) \
  -XGNATCOLL_POSTGRES_VERSION=$(gnatcoll_postgres_SO_VERSION) \
  -XGNATCOLL_XREF_VERSION=$(gnatcoll_xref_SO_VERSION) \
  # EOL

gprbuild := \
  gprbuild -p $(GPRBUILDFLAGS) \
  $(gpr_vars)

%:
	dh $@

# Dependencies and parallelism are handled by gprbuild,
# avoid using Make for this (risky, without much gain).
# Anyway, there is only one possible build order.
# For the same reason, blindly regenerate xref/generated.

.PHONY: override_dh_auto_build-arch
override_dh_auto_build-arch:

  # Ensure the same major version for Ada and C compilers.
	gprconfig --batch $(GPRCONFIGFLAGS)

	rm -f xref/generated/*

  # Build db2ada (and relocatable sql, postgres, sqlite).
	$(gprbuild) gnatcoll_db2ada/gnatcoll_all2ada.gpr -XLIBRARY_TYPE=relocatable

# TODO: ask upstream to provide initialdata.txt and this Makefile
# snippet. They have probably forgotten them during the libgnatcoll
# source package split.
	LD_LIBRARY_PATH=$(subst $() ,:,$(libs:=/lib/relocatable)) \
	  gnatcoll_db2ada/obj/gnatcoll_all2ada \
	  -dbtype=sqlite \
	  -dbname=:memory: \
	  -output xref/generated \
	  -dbmodel=gnatcoll_db2ada/dbschema.txt \
	  -createdb \
	  -adacreate \
	  -api GNATCOLL.Xref.Database \
	  -load=debian/initialdata.txt \
	  -enum "f2f_kind,id,name,F2F,Integer" \
	  -enum "e2e_kind,id,name,E2E,Integer"

  # Build static xref (and sql, sqlite as dependencies).
	$(gprbuild) gnatcoll_xref.gpr           -XLIBRARY_TYPE=static

  # Build static postgres (and sql).
	$(gprbuild) gnatcoll_postgres.gpr       -XLIBRARY_TYPE=static

  # Build gnatinspect (and relocatable sql, sqlite, xref).
	$(gprbuild) gnatinspect/gnatinspect.gpr -XLIBRARY_TYPE=relocatable

override_dh_auto_install-arch: $(libs:%=install-%)
	gprinstall gnatinspect/gnatinspect.gpr $(gpr_vars) \
	  -XLIBRARY_TYPE=relocatable $(standard_GPRINSTALLFLAGS)
	gprinstall gnatcoll_db2ada/gnatcoll_all2ada.gpr $(gpr_vars) \
	  -XLIBRARY_TYPE=relocatable $(standard_GPRINSTALLFLAGS)
	mv debian/tmp/usr/bin/gnatcoll_all2ada \
	   debian/tmp/usr/bin/gnatcoll_db2ada
install-%:
	gprinstall $(gpr_vars) \
	  gnatcoll_$*.gpr -XLIBRARY_TYPE=static \
	  $(static_GPRINSTALLFLAGS)
	gprinstall $(gpr_vars) \
	  gnatcoll_$*.gpr -XLIBRARY_TYPE=relocatable \
	  $(call shared_GPRINSTALLFLAGS,gnatcoll_$*)

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

# Avoid a warning about doc-main-package.
# Exclude generated sources from examples.
doc_pkg := libgnatcoll-db-doc
execute_before_dh_installdocs:
	dh_installdocs --package=$(doc_pkg) --doc-main-package=$(doc_pkg)
execute_before_dh_installexamples:
	dh_installexamples --package=$(doc_pkg) --doc-main-package=$(doc_pkg) \
	  -Xgenerated
