#!/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 build this package in a chroot:
#
# edit version in ../src/gpr_version.ads
# edit patches/version.patch to match
#
# schroot -d `cd ..; pwd` --chroot=unstable -- dpkg-buildpackage -uc -us -i"_MTN|\\.mtn-ignore"
#
# To run lintian:
# schroot -d `cd ..; pwd` --chroot=unstable -- make -f debian/rules lint
#
# To install the packages and run the examples:
#
# We want to test that gprbuild doesn't depend on the current gcc,
# only on current gcc-x.y, because if someone installs gprbuild on a
# 'testing' system that has only gcc-4.3, gcc will stay pointing at
# gcc-4.3. To do that, we install both and old and the current version
# of gcc-x.y, and manually change the symlink gcc -> gcc-4.3 (various
# build tools require gcc). Similarly, we don't install 'g++', but
# rather 'g++-4.4', etc.
#
# sudo schroot --chroot=unstable -- aptitude install gcc-4.3
# sudo schroot --chroot=unstable -- aptitude install g++-4.4
# sudo schroot --chroot=unstable -- aptitude install gfortran-4.4
# sudo schroot --chroot=unstable -- aptitude install liblapack3gf
# sudo schroot --chroot=unstable -- aptitude install libaunit1-dev
# sudo shcroot --chroot=unstable -- rm /usr/bin/gcc
#
# schroot --chroot=unstable -- ./test_installed.sh
# sudo schroot --chroot=unstable -- ln -s /usr/bin/gcc /usr/bin/gcc-4.4
#
# 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: 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

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 Ada compiler driver as opposed to the C compiler driver;
# they may be different on the build host.
target := `gnatgcc -dumpmachine`
version := `gnatgcc -dumpversion`
libsubdir := /usr/lib/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

clean:
	dh_testdir
	dh_testroot
	dh_clean -a
	rm -rf debian/gprbuild-doc
	rm -f *-stamp
	rm -f src/gprconfig-sdefault.ads

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

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 share/gprconfig/*.xml usr/share/gprconfig
	dh_install -p gprbuild doc/gprbuild_gps.xml /usr/share/gps/plug-ins/
	dh_strip -p gprbuild
	dh_installdirs -p gprbuild /usr/share/gprconfig
	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_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
