#!/usr/bin/make -f
#
# Debian Makefile for gprbuild
# Copyright (c) 2009, 2012 Stephen Leake <stephen_leake@stephe-leake.org>
# Copyright (c) 2013       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.
#
# 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

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

# To update to a new upstream version:
# 	install the quilt package
# 	edit version in ../src/gpr_version.ads
# 	edit patches/version.patch to match (use "quilt edit file_name" then "quilt refresh")
# 	update other patches as appropriate
# 	edit control to match
#
# To build this package in a chroot:
# schroot -d `cd ..; pwd` --chroot=unstable -- dpkg-buildpackage -uc -us
#
# To run lintian:
# schroot -d `cd ..; pwd` --chroot=unstable -- make -f debian/rules lint
#
# To install the packages and run the examples:
#
# one time setup (see comment in test_installed.sh about compiler versions):
# sudo schroot --chroot=unstable -- aptitude install --without-recommends gnat-4.6-sjlj
# sudo schroot --chroot=unstable -- aptitude install --without-recommends gcc-4.4
# sudo schroot --chroot=unstable -- aptitude install --without-recommends g++-4.6
# sudo schroot --chroot=unstable -- aptitude install --without-recommends gfortran-4.6
# sudo schroot --chroot=unstable -- aptitude install --without-recommends liblapack3gf
# sudo schroot --chroot=unstable -- aptitude install --without-recommends libaunit2-dev
#
# run the test:
# schroot --chroot=unstable -- ./test_installed.sh
#
# to clean up after the test:
# sudo schroot --chroot=unstable -- ./test_clean.sh

.SUFFIXES=

include /usr/share/dpkg/default.mk
# Set CFLAGS (handling noopt from DEB_BUILD_OPTIONS), LDFLAGS,
# DEB_HOST_GNU_TYPE, DEB_HOST_ARCH, DEB_VERSION...

######################################################################
# Code snippet copied from dh_ada_library to avoid a circular
# dependency. See detail there.
ADAFLAGS := $(filter-out -Wformat -Wformat-security -Werror=format-security,\
  $(CFLAGS))
LDFLAGS += -Wl,--as-needed -Wl,-z,defs
CC := gnatgcc
.NOTPARALLEL:
BUILDER_JOBS := $(filter parallel=%,$(DEB_BUILD_OPTIONS))
ifneq (,$(BUILDER_JOBS))
  BUILDER_JOBS := $(subst parallel=,,$(BUILDER_JOBS))
else
  BUILDER_JOBS := $(shell getconf _NPROCESSORS_ONLN)
endif
BUILDER_OPTIONS := -R -j$(BUILDER_JOBS) -v -s -we


# TODO: check if this is different from DEB_HOST_GNUTYPE or DEB_HOST_MULTIARCH
target := $(shell $(CC) -dumpmachine)
version := $(shell $(CC) -dumpversion)

######################################################################
POLICY_TARGETS := binary binary-arch binary-indep build build-arch	\
  build-indep clean
.PHONY: $(POLICY_TARGETS)
$(POLICY_TARGETS):
	dh $@

######################################################################
# Ignore upstream configure and Makefile if ever created.
override_dh_auto_configure:
override_dh_auto_build-arch::
override_dh_auto_build-indep:
override_dh_auto_test:
override_dh_auto_install:
override_dh_auto_clean::

######################################################################
# not clear why gprbuild_gps.xml is marked executable; fix it here
# (not covered by dh_fixperms)
override_dh_fixperms-arch:
	chmod a-x debian/gprbuild/usr/share/gps/plug-ins/gprbuild_gps.xml
	dh_fixperms --arch

######################################################################
# The project uses this instead of gnat/sdefault.adb. Patching the
# original file would cause the usual problems, and cannot be done as
# an usual patch as the changes are target-specific.
SDEFAULT := gnat/sdefault_generated.adb

override_dh_auto_build-arch:: $(SDEFAULT)
$(SDEFAULT): debian/sdefault.adb.sed
	sed $(foreach var,DEB_HOST_MULTIARCH target version \
                ,-e 's~@$(var)@~$($(var))~') $< > $@
override_dh_auto_clean::
	rm -f $(SDEFAULT)

######################################################################
GPRCONFIG_SDEFAULT := src/gprconfig-sdefault.ads

override_dh_auto_build-arch:: $(GPRCONFIG_SDEFAULT)
$(GPRCONFIG_SDEFAULT): %: %.in
	sed -e 's/@host@/$(target)/' $< > $@
override_dh_auto_clean::
	rm -f $(GPRCONFIG_SDEFAULT)

######################################################################
# Generated source needed by debian/gprbuild_build.gpr
SNAMES := gnat/snames.ads gnat/snames.adb gnat/snames.h

gnat/xsnamest: gnat/xsnamest.adb gnat/xutil.adb gnat/xutil.ads
	cd gnat && gnatmake xsnamest $(BUILDER_OPTIONS) -cargs $(ADAFLAGS) -largs $(LDFLAGS)
override_dh_auto_clean::
	rm -f gnat/xsnamest gnat/xsnamest.ali gnat/xsnamest.o gnat/xutil.ali gnat/xutil.o

override_dh_auto_build-arch:: $(SNAMES)
$(SNAMES): gnat/xsnamest $(patsubst %,gnat/snames.%-tmpl,ads adb h)
	cd gnat && ./xsnamest
	mv gnat/snames.ns gnat/snames.ads
	mv gnat/snames.nb gnat/snames.adb
	mv gnat/snames.nh gnat/snames.h
override_dh_auto_clean::
	rm -f $(SNAMES)

######################################################################
# The upstream Makefile builds these C files with no optimization. The
# functions in gprbuild_dummies.c just call abort; link.c only has
# constant object declarations. So it doesn't hurt to have CFLAGS here.
C_OBJECTS := src/gprbuild_dummies.o gnat/link.o

override_dh_auto_build-arch:: $(C_OBJECTS)
$(C_OBJECTS): %.o: %.c
	$(CC) -c $(CPPFLAGS) $(CFLAGS) $< -o $@
override_dh_auto_clean::
	rm -f $(C_OBJECTS)

######################################################################
PROJECT := debian/gprbuild_build.gpr
# In the project, paths are relative to debian/.
C_OBJS := $(addprefix ../,$(C_OBJECTS))

override_dh_auto_build-arch::
	gnatmake $(BUILDER_OPTIONS) -P$(PROJECT) \
          $(foreach flag,ADAFLAGS LDFLAGS C_OBJS,-X$(flag)='$($(flag))')
override_dh_auto_clean::
	rm -f debian/gprbuild_build_obj/*

######################################################################
# The doc sources are patched, and updating the pre-built documents
# from the orig tarball would cause building twice to fail. So we
# create them with another name.
DOCS     := $(addprefix doc/gprbuild_ug.,html info pdf txt)

TEXI2ANY := cd doc && texi2any --force gprbuild_ug.texi
override_dh_auto_build-indep: $(DOCS)
doc/gprbuild_ug.html: %.html: %.texi doc/gprbuild.css
	$(TEXI2ANY) -o $(notdir $@) --html --no-headers --css-include=gprbuild.css
	cp doc/important.png doc/note.png doc/tip.png $@
doc/gprbuild_ug.info: %.info: %.texi
	$(TEXI2ANY) -o $(notdir $@) --info --no-split
doc/gprbuild_ug.pdf: %.pdf: %.texi
	$(TEXI2ANY) -o $(notdir $@) --pdf --quiet
doc/gprbuild_ug.txt: %.txt: %.texi
	$(TEXI2ANY) -o $(notdir $@) --plaintext --no-split --no-headers --fill-column=79
override_dh_auto_clean::
	rm -f $(addprefix doc/gprbuild_ug.,aux cp cps fn info ky log pdf pg toc txt tp vr)
	rm -f -r doc/gprbuild_ug.html

######################################################################
PROJECTS_TEXI := doc/projects_unw.texi
$(DOCS): $(PROJECTS_TEXI)
$(PROJECTS_TEXI): xgnatugn gnat/projects.texi gnat/ug_words
	./$< unw gnat/projects.texi gnat/ug_words $@
override_dh_auto_clean::
	rm -f $(PROJECTS_TEXI)

xgnatugn: gnat/xgnatugn.adb
	gnatmake $< $(BUILDER_OPTIONS) -cargs $(ADAFLAGS) -largs $(LDFLAGS)
override_dh_auto_clean::
	rm -f xgnatugn xgnatugn.ali xgnatugn.o

######################################################################
lint:
	lintian -i ../gprbuild_$(DEB_VERSION)_$(DEB_BUILD_ARCH).changes
