#!/usr/bin/make -f

##############
# Legal stuff
##############

# Copyright (c) 2003-2006, 2011-2014 Ludovic Brenta <lbrenta@debian.org>
# Copyright (c) 2010-2018 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.

.SUFFIXES:

########################
# Guess some variables.
########################

gnat_version := $(shell gnatgcc -dumpversion | sed 's/\..*//')

DEB_BUILD_MAINT_OPTIONS := hardening=+all
ifneq (,$(filter $(DEB_HOST_ARCH), ppc64el))
  export DEB_CFLAGS_MAINT_APPEND = -O2
  export DEB_CFLAGS_MAINT_STRIP = -O3
  export DEB_ADAFLAGS_MAINT_APPEND = -O2
  export DEB_ADAFLAGS_MAINT_STRIP = -O3
endif
DEB_LDFLAGS_MAINT_APPEND := -Wl,--as-needed -Wl,--no-allow-shlib-undefined \
  -Wl,--no-copy-dt-needed-entries -Wl,--no-undefined
include /usr/share/dpkg/default.mk
# Set CFLAGS (handling noopt from DEB_BUILD_OPTIONS), LDFLAGS,
# DEB_HOST_GNU_TYPE, DEB_HOST_ARCH, DEB_VERSION...
include /usr/share/ada/debian_packaging-$(gnat_version).mk

PKG_CONFIG_LIBRARIES := gtk+-3.0 glib-2.0 pygobject-3.0 python-2.7

CLANG_VERSION := $(shell dpkg -s libclang-dev \
  | sed -nr '/^Depends: libclang-(.*)-dev.*/{s//\1/p;q}')
LIBS := -L/usr/lib/llvm-$(CLANG_VERSION)/lib -lclang

PYTHON := python

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

# Do not try to call an upstream configure or Makefile.
.PHONY: $(addprefix override_dh_auto_, \
  clean configure build-arch build-indep test install)

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

# Note: the script generates both Ada and Python sources, the latter
# are embedded into the users_guide.
override_dh_auto_build-arch: docs/users_guide/GPS/generated_hooks.py
docs/users_guide/_build: docs/users_guide/GPS/generated_hooks.py
docs/users_guide/GPS/generated_hooks.py: kernel/src/hooks.py
	rm -fr kernel/generated
	mkdir kernel/generated
	cd kernel/src && $(PYTHON) hooks.py
override_dh_auto_clean::
	rm -fr kernel/generated docs/users_guide/GPS/generated_hooks.py

override_dh_auto_build-arch: libclang/gen
libclang/gen:
	$(MAKE) -C libclang \
	  CLANG_SRC_DIR=/usr/lib/llvm-$(CLANG_VERSION) \
	  GCC_DUMPMACHINE=$(DEB_HOST_MULTIARCH)
override_dh_auto_clean::
	rm -fr libclang/gen

GNAT_SWITCHES_PY := share/support/core/gnat_switches.py
override_dh_auto_build-indep: $(GNAT_SWITCHES_PY)
$(GNAT_SWITCHES_PY): distrib/gnat_switches.py \
                     gnat/building_executable_programs_with_gnat.rst
	$(PYTHON) distrib/gnat_switches.py > $@
override_dh_auto_clean::
	rm -f $(GNAT_SWITCHES_PY)

######################################################################
override_dh_auto_build-arch:
	gprbuild $(BUILDER_OPTIONS) -k debian/build_gps.gpr \
	  $(foreach v,ADAFLAGS CFLAGS CPPFLAGS DEB_HOST_GNU_TYPE LDFLAGS LIBS,"-X$(v)=$($(v))") \
	  "-XPKG_CONFIG_CFLAGS=`pkg-config --cflags $(PKG_CONFIG_LIBRARIES)`" \
	  "-XPKG_CONFIG_LIBS=`pkg-config --libs $(PKG_CONFIG_LIBRARIES)`"
override_dh_auto_clean::
	rm -f obj/*

database := kernel/generated/gps-kernel-properties-database.ads
override_dh_auto_build-arch: $(database)
$(database):
	cd $(dir $@) && gnatcoll_sqlite2ada -api=GPS.Kernel.Properties.Database -adacreate -dbmodel=../src/properties_schema.txt
override_dh_auto_clean::
	rm -f $(database)

######################################################################
configured := \
  common/core/src/config.ads \
  docs/gps-welcome.html \
  docs/gps-welcome-public.html \
  share/support/core/protocols.py
override_dh_auto_configure: $(configured)
$(configured): %: %.in
	sed \
	    -e 's/@CURRENT_YEAR@/20$(DEB_VERSION_UPSTREAM)/' \
	    -e 's/@GPS_VERSION@/$(DEB_VERSION_UPSTREAM)/' \
	    -e 's/@TARGET@/$(DEB_HOST_GNU_TYPE))/' \
	    -e 's/@TARGET_IS_DARWIN@/False/' \
	    -e 's!@prefix@!/usr!' \
	    -e 's/@HOST_TYPE@/Unix/' \
	    -e 's/@STRIP_CR@/False/' \
	    -e 's/@CAN_OUTPUT@/True/' \
	    -e 's!@EXEC_COMMAND@!/bin/sh -c!' \
	    -e 's/@DEFAULT_PS@/ps/' \
	    -e 's/@PRINT_CMD@/lpr/' \
	    -e 's/@OPAQUE_MDI@/True/' \
	    -e 's/@DIFF_CMD@/diff -u/' \
	    -e 's/@DIFF3_CMD@/diff3/' \
	    -e 's/@PATCH_CMD@/patch/' \
	    -e 's/@EXECUTION_WINDOW@/True/' \
	    -e 's/@CHARSET@/UTF-8/' \
	    -e 's/@SSH_ESCAPE@/~/' \
	    $< > $@
override_dh_auto_clean::
	rm -f $(configured)

######################################################################
DOCS := \
 docs/users_guide/_build \
 docs/tutorial/_build \
 docs/programmers_guide/_build \
 docs/release_notes/build \
 docs/release_notes_17/build \
 docs/release_notes_62/build \
 gnatdoc/docs/users_guide/_build

ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
override_dh_auto_build-indep: $(DOCS)
  # Give distinct names to release notes.
	cd docs/release_notes_17/build/latex && cp -f ReleaseNotes.pdf ReleaseNotes_17.pdf
	cd docs/release_notes_62/build/latex && cp -f ReleaseNotes.pdf ReleaseNotes_62.pdf
  # Sphinx creates Python precompiled files in every imported module,
  # we do not want them installed.
	find . -name "*.pyc" -delete
	find . -type d -name "__pycache__" -print0 | xargs -r0 rmdir
endif

# Note: see docs/users_guide/GPS/generated_hooks.py above.
$(DOCS):
	$(MAKE) -C $(dir $@) html latexpdf SPHINXOPTS=-j$(BUILDER_JOBS)

override_dh_auto_clean::
	rm -fr $(DOCS)
	find . -name "*.pyc" -delete
	find . -type d -name "__pycache__" -print0 | xargs -r0 rmdir

# A logo referenced in index.rst but missing.
docs/release_notes/build: docs/release_notes/gps18.png
docs/release_notes/gps18.png:
	cd docs/release_notes && ln -s ../release_notes_62/gps6.png gps18.png
override_dh_auto_clean::
	rm -f docs/release_notes/gps18.png

######################################################################
# Mimic dh --with sphinxdoc with sphinx-common in Build-Depends-Indep.
.PHONY: override_dh_installdocs-indep
override_dh_installdocs-indep:
	dh_installdocs -i
	dh_sphinxdoc -i

.PHONY: override_dh_install-indep
override_dh_install-indep:
	dh_install -i
  # Documentation and license are not useful here.
	rm -f debian/gnat-gps-common/usr/share/gps/color_themes/themes/*/README.*
	rm -f debian/gnat-gps-common/usr/share/gps/color_themes/themes/*/LICENSE*
  # Should be a wishlist for dh_fixperms.
	chmod 644 debian/gnat-gps-common/usr/share/gps/icons/hicolor/scalable/16x16/gps-*.svg
	chmod 644 debian/gnat-gps-common/usr/share/gps/color_themes/themes/tomorrow/*.tmTheme
  # Probably errors in upstream package.
	chmod 644 debian/gnat-gps-common/usr/share/gps/plug-ins/spark2014.py
	chmod 644 debian/gnat-gps-common/usr/share/gps/support/core/gps_utils/internal/dialogs.py
  # Removed by upstream install process in Makefile.
	rm -f debian/gnat-gps-common/usr/share/gps/icons/hicolor/scalable/*sketch
  # libadalang is disabled because it is not packaged in Debian
	rm -f debian/gnat-gps-common/usr/share/gps/support/core/lal.py

# Mimic dh --with linktree, with dh-linktree in Build-Depends-Indep.
.PHONY: override_dh_link-indep
override_dh_link-indep:
	dh_link -i
	dh_linktree -i

.PHONY: override_dh_compress
override_dh_compress:
	dh_compress --all -X.xml -X.ads -X.adb -X.c -X.py
