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

$(foreach line,$(shell sed -n '\
  s/^ gnat-\([0-9.]\+\),$$/                          GNAT_VERSION:=\1        /p; \
  s/^Package: libgnatcoll-\([a-z]\+\)\([0-9.]\+\)$$/ gnatcoll_\1_version:=\2 /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

# 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.
ifneq (,$(filter mips64el,$(DEB_HOST_ARCH)))
  DEB_CFLAGS_MAINT_APPEND := -mxgot
endif

# Use -Os on mipsel to workaround further jump length issues
ifneq (,$(filter mipsel,$(DEB_HOST_ARCH)))
  DEB_CFLAGS_MAINT_APPEND += -Os
endif

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

LD_LIBRARY_PATH := \
  sql/lib/relocatable:sqlite/lib/relocatable:postgres/lib/relocatable:xref/lib/relocatable
ADA_PROJECT_PATH := $(subst /lib/relocatable,,$(LD_LIBRARY_PATH))
libs := $(subst :, ,$(ADA_PROJECT_PATH))

# The scenario variables passed to gprbuild and dh_ada_library.
GNATCOLL_SQLITE := external
vars := \
  $(foreach l,$(libs),gnatcoll_$(l)_version) \
  GNATCOLL_SQLITE \

gprbuild := \
  ADA_PROJECT_PATH=$(ADA_PROJECT_PATH) \
  gprbuild -p $(BUILDER_OPTIONS) \
  $(foreach v,$(vars),-X$(v)="$($(v))")

%:
	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:

	rm -f xref/generated/*

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

# 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=$(LD_LIBRARY_PATH) \
	  gnatcoll_db2ada/obj/gnatcoll_db2ada \
	  -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

.PHONY: override_dh_auto_clean
override_dh_auto_clean:
	dh_auto_clean --sourcedir=docs
	rm -f xref/generated/*
	rm -fr \
	  $(addsuffix /lib,$(libs)) \
	  $(addsuffix /obj,$(libs) gnatcoll_db2ada gnatinspect)

# dh_ada_library does not accept -aP arguments.
.PHONY: override_dh_ada_library
override_dh_ada_library:
	ADA_PROJECT_PATH=$(ADA_PROJECT_PATH) \
	dh_ada_library $(foreach l,$(libs), \
	  LIBRARY_TYPE=relocatable \
	  $(foreach v,$(vars),$(v)="$($(v))") \
	  $(l)/gnatcoll_$(l).gpr)

override_dh_auto_build-indep:
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
	dh_auto_build --sourcedir=docs -- html latexpdf text
endif

# Avoid a warning about doc-main-package.
doc_pkg := libgnatcoll-db-doc
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
