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

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
# USA

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

projects :=
versions :=
$(foreach line,$(shell sed -n '\
  s/^ gnat, gnat-\([0-9.]\+\),$$/GNAT_VERSION:=\1/p; \
  /^Package: lib\([a-z-]\+\)\([0-9.]\+\(gpl[0-9]\{4\}\)\?\)$$/ { \
    s~~projects+=src/\1.gpr versions+=\U\1\E_VERSION=\2~; \
    y/-/_/; \
    p; \
  }; \
  ' debian/control),$(eval $(line)))

DEB_BUILD_MAINT_OPTIONS := hardening=+all
include /usr/share/dpkg/default.mk
include /usr/share/ada/debian_packaging-$(GNAT_VERSION).mk

%:
	dh $@ --with ada-library

override_dh_auto_configure:
  # Patch version number for documentation.
	dh_auto_configure -- \
	  --enable-gpl \
	  --enable-shared \
	  --enable-shared-python \
	  PACKAGE_VERSION=$(DEB_VERSION_UPSTREAM) \
	  $(foreach v,CC CFLAGS CPPFLAGS LDFLAGS,"$(v)=$($(v))") \
	  --without-postgresql \
	  --with-sqlite=/usr/include/$(DEB_HOST_MULTIARCH)

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

# Upstream insists on setting the -j option inside projects.
BUILDER_OPTIONS := $(patsubst -j%,-XPROCESSORS=%,$(BUILDER_OPTIONS))

# Upstream insists on unpredictable directories.
BUILDER_OPTIONS += -p

# See patches/compilation_options.diff
BUILDER_OPTIONS += \
  $(foreach v,ADAFLAGS CFLAGS CPPFLAGS LDFLAGS,"-X$(v)=$($(v))")

# patches/library_versions.diff
BUILDER_OPTIONS += $(addprefix -X,$(versions))

xref_generated := src/xref.generated

override_dh_auto_build-arch:
 # Copy upstream's Makefile, but do not build tools twice.
 # Also, do not randomly regenerate the sources.
	gprbuild gnatcoll_full.gpr -XLIBRARY_TYPE=static $(BUILDER_OPTIONS)
	gprbuild src/gnatcoll_gtk.gpr -XLIBRARY_TYPE=static $(BUILDER_OPTIONS)
	gprbuild gnatcoll_full.gpr $(BUILDER_OPTIONS)
	gprbuild src/gnatcoll_gtk.gpr $(BUILDER_OPTIONS)
	gprbuild src/gnatcoll_tools.gpr $(BUILDER_OPTIONS)
 # I see no sensible way to recreate the xref sources without depending
 # on a previous libgnatcoll-sqlite-bin and causing bootstrap
 # problems.  At least let the build fail if the generated sources
 # differ, probably because the schema has been modified.
	mv $(xref_generated) $(xref_generated).bak
	mkdir $(xref_generated)
	LD_LIBRARY_PATH=src/lib/gnatcoll/relocatable:src/lib/iconv/relocatable:src/lib/sqlite/relocatable \
	$(MAKE) generate_sources
 # If a difference is found, fail and display it.
	diff -urN $(xref_generated).bak $(xref_generated)
 # Keep the version with the correct timestamps.
	rm -fr $(xref_generated)
	mv $(xref_generated).bak $(xref_generated)

# Upstream docs target does not generate text output.
override_dh_auto_build-indep:
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
  # TODO: append SPHINXOPTS=-j$(BUILDER_JOBS) once #869098 is fixed.
	$(MAKE) -C docs html latexpdf text
endif

# Default dh_auto_clean would test -f Makefile.
# distclean target is added by a Debian patch.
# Gprclean ignores shared object versions, remove the whole directory.
override_dh_auto_clean::
	if test -f Makefile.conf; then $(MAKE) clean distclean; fi
	rm -fr src/lib

.PHONY: override_dh_clean
override_dh_clean:
	dh_clean --exclude=testsuite/xref/F516-004/default.gpr.orig

# Check dependencies across -dev packages (or generated projects)
# after any change in the structure of upstream projects.
.PHONY: override_dh_ada_library-arch override_dh_ada_library-indep
override_dh_ada_library-arch:
	dh_ada_library --arch $(foreach p,$(projects),$(versions) $(p))

# Mimic 'dh --with sphinxdoc', but without adding sphinx-common to
# Build-Depends for arch-only builds.
.PHONY: override_dh_installdocs-indep
override_dh_installdocs-indep:
	dh_installdocs -i
	dh_sphinxdoc

.PHONY: override_dh_compress
override_dh_compress:
	dh_compress --package=libgnatcoll-doc --exclude=.adb --exclude=.ads
	dh_compress --remaining-packages
