#!/usr/bin/make -f
# $MirOS: contrib/hosted/tg/deb/jupp/debian/rules,v 1.23 2012/07/18 17:39:01 tg Exp $
#-
# Possible flavourisation (in addition to stock Debian rules):
# export DEB_BUILD_OPTIONS=diet
# export DEB_BUILD_OPTIONS=klibc
# export DEB_BUILD_OPTIONS=klibc CC='klcc -shared'
# export DEB_BUILD_OPTIONS='diet debug' (or 'klibc debug')
# export DEB_BUILD_OPTIONS=static (for eglibc)
# All can be combined with 'nopwnam' (aids static linkage).
# The 'nostrip' and 'parallel=n' options are supported as well.
#
# Setting diet or klibc disables -O0/-O2 and -g and, if unset,
# sets CC. Adding debug enables -g3. Cross-compiling also sets
# CC if unset.

shellescape='$(subst ','\'',$(1))'
shellexport=$(1)=$(call shellescape,${$(1)})

ifeq (,$(findstring diet,${DEB_BUILD_OPTIONS}))
ifeq (,$(findstring klibc,${DEB_BUILD_OPTIONS}))
USE_LIBC:=system
else
USE_LIBC:=klibc
endif
else
USE_LIBC:=dietlibc
endif

DEB_BUILD_ARCH=$(shell dpkg-architecture -qDEB_BUILD_ARCH)
DEB_HOST_ARCH=$(shell dpkg-architecture -qDEB_HOST_ARCH)
DEB_HOST_GNU_TYPE=$(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)

# is ${CC} defined anywhere (other than implicit rules?)
ifneq (,$(findstring $(origin CC),default undefined))
# no - then default to gcc (or cross-gcc)
ifneq (${DEB_BUILD_ARCH},${DEB_HOST_ARCH})
CC=			${DEB_HOST_GNU_TYPE}-gcc
else
ifeq (,$(findstring diet,${DEB_BUILD_OPTIONS}))
ifeq (,$(findstring klibc,${DEB_BUILD_OPTIONS}))
CC=			gcc
else
# export CC='klcc -shared' manually if you want that
CC=			klcc
endif
else
CC=			diet -Os gcc
endif
endif
endif

EXTRA_CFLAGS+=		-Wall -Wformat
EXTRA_CFLAGS+=		-fno-omit-frame-pointer -fwrapv -fno-strict-aliasing
# adjust when backporting
EXTRA_CFLAGS+=		-Wextra -Wno-pointer-sign -Wno-unused-parameter \
			-Wno-missing-field-initializers \
			-Wno-old-style-definition -Wno-strict-prototypes \
			-Wno-cast-qual \
			-Wno-missing-prototypes -Wno-missing-declarations
EXTRA_LDFLAGS=		-Wl,--as-needed

ifneq (${USE_LIBC},system)
ifneq (,$(findstring debug,${DEB_BUILD_OPTIONS}))
EXTRA_CFLAGS+=		-g3
endif
# SSP currently works with neither dietlibc nor klibc
EXTRA_CFLAGS+=		-fno-stack-protector
else
ifneq (,$(findstring static,${DEB_BUILD_OPTIONS}))
EXTRA_LDFLAGS+=		-static
endif
endif

ifneq (,$(wildcard /usr/share/dpkg/buildflags.mk))
# dpkg-dev (>= 1.16.1~)
DEB_CFLAGS_MAINT_APPEND=${EXTRA_CFLAGS}
DEB_LDFLAGS_MAINT_APPEND=${EXTRA_LDFLAGS}
ifneq (${USE_LIBC},system)
# the diet and klcc wrappers take care of optimisation for us
DEB_CFLAGS_MAINT_STRIP=-O0 -O1 -O2 -O3 -Os -g -g1 -g2 -g3 -ggdb -gdwarf -gstabs
# do not use PIE with strange libcs, either
DEB_BUILD_MAINT_OPTIONS=hardening=+all,-pie
else
ifneq (,$(findstring static,${DEB_BUILD_OPTIONS}))
# cannot mix PIE with static linkage
DEB_BUILD_MAINT_OPTIONS=hardening=+all,-pie
else
DEB_BUILD_MAINT_OPTIONS=hardening=+all
endif
endif
include /usr/share/dpkg/buildflags.mk
else
# old-fashioned way to determine build flags
ifneq (${USE_LIBC},system)
CFLAGS=
else
CFLAGS=			-O$(if $(findstring noopt,${DEB_BUILD_OPTIONS}),0,2) -g
endif
CFLAGS+=		${EXTRA_CFLAGS}
LDFLAGS+=		${EXTRA_LDFLAGS}
endif

ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
NUMJOBS=		$(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
MAKEFLAGS+=		-j${NUMJOBS}
endif

CONFIGURE_ARGS=		--sysconfdir=/etc \
			--prefix=/usr \
			--mandir=/usr/share/man \
			--disable-dependency-tracking \
			--disable-termidx

ifneq (${USE_LIBC},system)
# prevent picking up system libraries linked against the system C library
CONFIGURE_ARGS+=	--disable-search-libs
endif

ifneq (,$(findstring nopwnam,${DEB_BUILD_OPTIONS}))
CONFIGURE_ARGS+=	--disable-getpwnam
endif

debian/.configure_stamp:
	dh_testdir
	-rm -f debian/.*_stamp
	-rm -rf builddir
	# apply autotools-dev if it exists
	for x in config.guess config.sub; do \
		test -e /usr/share/misc/$$x || continue; \
		rm -f $$x; \
		cp /usr/share/misc/$$x .; \
	done
	mkdir builddir
	cd builddir && env \
	    $(foreach i,CC CFLAGS CPPFLAGS LDFLAGS,$(call shellexport,$i)) \
	    sh ../configure ${CONFIGURE_ARGS}
	@:>$@

debian/.build_stamp: debian/.configure_stamp
	dh_testdir
	cd builddir && ${MAKE} sysconfjoesubdir=/jupp
	ln -f builddir/joe builddir/jupp
	ln -f builddir/joe.1 builddir/jupp.1
	@:>$@

build: build-arch build-indep
build-arch: debian/.build_stamp
build-indep: debian/.configure_stamp

clean:
	dh_testdir
	-rm -f debian/.*_stamp
	-rm -rf builddir
	# unapply autotools-dev if it exists
	for x in config.guess config.sub; do \
		test -e /usr/share/misc/$$x || continue; \
		rm -f $$x; \
	done
	dh_clean

binary-indep: build-indep
	dh_testdir
	dh_testroot
	dh_prep -i
	dh_installchangelogs -i -Njupp
	dh_install -i
	dh_installmenu -i
	# will be replaced by links to jupp
	rm -rf debian/joe-jupp/usr/share/doc/joe-jupp
	dh_link -i
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

# no -Njupp because that's the only arch:any package
binary-arch: build-arch
	dh_testdir
	dh_testroot
	dh_prep -a
	dh_installchangelogs -pjupp -k NEWS
	dh_installdocs -a
	dh_install -a
	dh_installmenu -a
	dh_installman -a
	dh_link -a
	dh_strip -a
	dh_compress -a
	dh_fixperms -a
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

binary: binary-arch binary-indep

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