#!/usr/bin/make -f
#
# Debian Makefile for gprbuild
# Copyright (c) 2009, 2010 Stephen Leake <stephen_leake@stephe-leake.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
#
# 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=

# We use gnatmake, not make, for parallel builds.
.NOTPARALLEL:
export CPUS := $(shell getconf _NPROCESSORS_ONLN)

# Targets mandated by the Debian Policy

.PHONY: build binary binary-arch binary-indep clean

build: build-arch build-indep

build-arch: generate-stamp build-stamp
build-indep: generate-stamp build-stamp

C_OBJS := debian/tmp/obj/gprbuild_dummies.o debian/tmp/obj/link.o

generate-stamp: src/gprconfig-sdefault.ads sdefault-body-stamp snames-stamp

build-stamp: $(C_OBJS)
	dh_testdir
	gnatmake -j$(CPUS) -p -Pdebian/gprbuild_build
	touch $@

# 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.
debian/tmp/obj/gprbuild_dummies.o: src/gprbuild_dummies.c | debian/tmp/obj
	gcc -c $(CFLAGS) -o $@ $<

debian/tmp/obj/link.o: gnat/link.c | debian/tmp/obj
	gcc -c $(CFLAGS) -o $@ $<

debian/tmp/obj:
	mkdir -p $@

# It is important to use the default Ada compiler driver as opposed to
# the default C compiler driver; they may be different on the build
# host.
#
# note that dpkg-architecture uses 'gcc' not 'gnatgcc', but that
# doesn't matter; the architecture is not compiler version dependent.
DEB_HOST_MULTIARCH := $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

target := $(shell gnatgcc -dumpmachine)
version := $(shell gnatgcc -dumpversion)
libsubdir := /usr/lib/$(DEB_HOST_MULTIARCH)/gcc/$(target)/$(version)

src/gprconfig-sdefault.ads :
	echo "package GprConfig.Sdefault is" > src/gprconfig-sdefault.ads
	echo "   pragma Warnings (Off);" >> src/gprconfig-sdefault.ads
	echo "   Hostname : constant String := \"$(target)\";" >> src/gprconfig-sdefault.ads
	echo "end GprConfig.Sdefault;" >> src/gprconfig-sdefault.ads

sdefault-body-stamp:
	echo "pragma Style_Checks (Off);" > gnat/sdefault.adb
	echo "package body Sdefault is" >> gnat/sdefault.adb
	echo "   function Include_Dir_Default_Name return String_Ptr is" >> gnat/sdefault.adb
	echo "   begin" >> gnat/sdefault.adb
	echo "      return new String'(\"$(libsubdir)/adainclude/\");" >> gnat/sdefault.adb
	echo "   end Include_Dir_Default_Name;" >> gnat/sdefault.adb
	echo "   function Object_Dir_Default_Name return String_Ptr is" >> gnat/sdefault.adb
	echo "   begin" >> gnat/sdefault.adb
	echo "      return new String'(\"$(libsubdir)/adalib/\");" >> gnat/sdefault.adb
	echo "   end Object_Dir_Default_Name;" >> gnat/sdefault.adb
	echo "   function Target_Name return String_Ptr is" >> gnat/sdefault.adb
	echo "   begin" >> gnat/sdefault.adb
	echo "      return new String'(\"$(target)/\");" >> gnat/sdefault.adb
	echo "   end Target_Name;" >> gnat/sdefault.adb
	echo "   function Search_Dir_Prefix return String_Ptr is" >> gnat/sdefault.adb
	echo "   begin" >> gnat/sdefault.adb
	echo "      return new String'(\"/usr/share/ada/adainclude/\");" >> gnat/sdefault.adb
	echo "   end Search_Dir_Prefix;" >> gnat/sdefault.adb
	echo "end Sdefault;" >> gnat/sdefault.adb
	touch sdefault-body-stamp

snames-stamp:
	(cd gnat; gnatmake -q xsnamest ; ./xsnamest )
	mv gnat/snames.ns gnat/snames.ads
	mv gnat/snames.nb gnat/snames.adb
	mv gnat/snames.nh gnat/snames.h
	touch snames-stamp

clean:
	dh_testdir
	dh_testroot
	dh_clean -a
	rm -rf debian/gprbuild-doc
	rm -f debian/gprbuild-doc.substvars
	rm -f *-stamp
	rm -f src/gprconfig-sdefault.ads
	rm -f src/sdefault.adb
	rm -f gnat/snames.ads
	rm -f gnat/snames.adb
	rm -f gnat/snames.h
	rm -f gnat/xsnamest
	rm -f gnat/xsnamest.ali
	rm -f gnat/xsnamest.o
	rm -f gnat/xutil.ali
	rm -f gnat/xutil.o

binary: binary-indep binary-arch

# we are not patching the doc sources, so we can just install the
# pre-built files from the orig tarball.
binary-indep:
	dh_installchangelogs -p gprbuild-doc
	dh_installdocs -p gprbuild-doc doc/html
	dh_installinfo -p gprbuild-doc doc/info/gprbuild_ug.info
	dh_installdocs -p gprbuild-doc doc/pdf/gprbuild_ug.pdf
	dh_installdocs -p gprbuild-doc features-*
	dh_installdocs -p gprbuild-doc known-problems-*
	dh_installexamples -p gprbuild-doc examples/*
	dh_lintian -p gprbuild-doc
	dh_compress -p gprbuild-doc
	dh_fixperms -p gprbuild-doc
	dh_gencontrol -p gprbuild-doc
	dh_md5sums -p gprbuild-doc
	dh_builddeb -p gprbuild-doc

# not clear why gprbuild_gps.xml is marked executable; fix it here
# (not covered by dh_fixperms)
binary-arch: build
	dh_testdir
	dh_testroot
	dh_installchangelogs -p gprbuild
	dh_install -p gprbuild debian/tmp/gprbuild usr/bin
	dh_install -p gprbuild debian/tmp/gprclean usr/bin
	dh_install -p gprbuild debian/tmp/gprconfig usr/bin
	dh_install -p gprbuild debian/tmp/gprbind usr/lib
	dh_install -p gprbuild debian/tmp/gprlib usr/lib
	dh_install -p gprbuild doc/gprbuild_gps.xml /usr/share/gps/plug-ins/
	chmod a-x debian/gprbuild/usr/share/gps/plug-ins/gprbuild_gps.xml
	dh_strip -p gprbuild
	dh_install -p gprbuild share/gprconfig/* /usr/share/gprconfig
	dh_installman -p gprbuild debian/gprbuild.1 debian/gprclean.1 debian/gprconfig.1
	dh_installdocs -p gprbuild
	dh_fixperms
	dh_lintian -p gprbuild
	dh_shlibdeps -p gprbuild
	dh_compress -p gprbuild
	dh_fixperms -p gprbuild
	dh_gencontrol -p gprbuild
	dh_md5sums -p gprbuild
	dh_builddeb -p gprbuild

lint: version := $(shell dpkg-parsechangelog | grep "Version: " | sed "s/Version: //")
lint: arch := $(shell dpkg-architecture -qDEB_BUILD_ARCH)
lint:
	cd ..; lintian -i gprbuild_$(version)_$(arch).changes

# end of file
