#!/usr/bin/make -f
# -*- makefile -*-

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1
export DEB_BUILD_OPTIONS=nocheck

export DEB_BUILD_ARCH ?= $(shell dpkg-architecture -qDEB_BUILD_ARCH)
export DEB_HOST_ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)
export DEB_HOST_ARCH_OS ?= $(shell dpkg-architecture -qDEB_HOST_ARCH_OS)
export DEB_HOST_ARCH_CPU ?= $(shell dpkg-architecture -qDEB_HOST_ARCH_CPU)
export DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
export DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
ifeq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
        gm_confflags += --build $(DEB_HOST_GNU_TYPE)
else
	gm_confflags += --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE)
endif

ARCHS_CHECK_NOFAIL:=hppa sparc
ifneq (,$(findstring $(DEB_BUILD_ARCH),$(ARCHS_CHECK_NOFAIL)))
CHECK_FAILED_ACTION:=echo "Ignoring testsuite failures on this architecture"
else
CHECK_FAILED_ACTION:= { \
		echo "Aborting build due to testsuite failures"; \
		if test -e test-suite.log; then \
			echo "==== test-suite.log ===="; \
			cat test-suite.log; \
			echo "==== end of test-suite.log ===="; \
		fi; \
		/bin/false; \
	}
endif

# on i386, with glibc-2.18, the linker hangs on i386
# $ cat test.c
# int main(){return(0);}
# $ gcc -fPIE -pie -static test.c
# *** Error in `/usr/bin/ld': corrupted double-linked list: 0x09da0260 ***
# <linker hangs>
ifneq (,$(filter $(DEB_HOST_ARCH),i386))
  DEB_BUILD_HARDENING_PIE:=0
endif

DEB_BUILD_MAINT_OPTIONS := hardening=+all
DPKG_EXPORT_BUILDFLAGS := 1
include /usr/share/dpkg/buildflags.mk

PACKAGENAME=graphicsmagick

configure: configure-stamp
configure-stamp:
	dh_testdir
	dh_autoreconf
	CFLAGS="$(CFLAGS)" CXXFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)" \
	    ./configure $(gm_confflags) \
		--enable-shared \
		--enable-static \
		--enable-libtool-verbose \
		--prefix=/usr \
		--mandir=\$${prefix}/share/man \
		--infodir=\$${prefix}/share/info \
		--docdir=\$${prefix}/share/doc/graphicsmagick \
		--with-gs-font-dir=/usr/share/fonts/type1/gsfonts \
		--with-x \
		--x-includes=/usr/include/X11 \
		--x-libraries=/usr/lib/X11 \
		--without-dps \
		--without-modules \
		--without-frozenpaths \
		--with-webp=yes \
		--with-zstd=yes \
		--with-perl \
		--with-perl-options="INSTALLDIRS=vendor" \
		--enable-quantum-library-names \
		--with-quantum-depth=16

	touch configure-stamp

build: build-arch build-indep

build-arch: build-arch-stamp
build-arch-stamp:  configure-stamp
	dh_testdir

	# Teach Perl binding to build without an installed library.
	# Also, we need to prune several of the security options we
	# added above:
	# * The compiler chokes on Perl's standard macro MEM_WRAP_CHECK() 
	#   if -Werror=format-security is set.
	# * The Perl Makefile generator ignores linker flags it doesn't
	#   grok (ie. -pie -z relro). Hence, we need to strip -fPIE from
	#   CCFLAGS as well, or the linker would barf about non-PIC symbols.
	test -e PerlMagick/Makefile.PL.debsave || sed -i -e \
	 "/LIBS/s|\(-L[[:space:]]*\)[^[:space:]]\+|\1$(CURDIR)/magick/.libs|" \
	 -e "s/-Werror=format-security/-Wformat-security/g" \
	 -e "s/-fPIE//g" \
	 -e "s/-pie//g" \
	 PerlMagick/Makefile.PL


## Check whether segfaults on arm are gone with current toolchain.
#ifeq ($(DEB_BUILD_ARCH),arm)
#	# Calling variadic functions can lead to stack corruption on arm
#	# with -O2 due to what I believe to be a compiler bug. Verfied for
#	# DrawRoundRectangle() in magick/draw.c and wand/drawing_wand.c.
#	# The following workaround prevents crashes of the drawtest runs
#	# during make check until the compiler issue is fully understood.
#	$(MAKE) CFLAGS="-Wall -g -O0" -C magick \
#	        magick_libGraphicsMagick_la-draw.lo
#	$(MAKE) CFLAGS="-Wall -g -O0" -C wand drawing_wand.lo
#endif

	$(MAKE) 
	# Perl is built separately. Make sure to override the stupid
	# rpath picked up from the LIBS hack above.
	$(MAKE) perl-build LD_RUN_PATH=

	touch build-arch-stamp

build-indep: build-indep-stamp
build-indep-stamp:  configure-stamp
	dh_testdir

	pod2man debian/Magick.pm > 'debian/Image::Magick.3pm'

	touch build-indep-stamp

check: check-stamp
check-stamp: build-arch-stamp
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	dh_testdir

	# Cannot patch a binary file, so do take a detour via uudecode...
	for i in `cd "$(CURDIR)/debian/reference-new"; find -name "*.uu"`; do \
		new="$(CURDIR)/debian/reference-new/$$i"; \
		orig="$${i%.uu}"; \
		save="$(CURDIR)/debian/reference-orig/$${orig}"; \
		test -f "$${orig}" || continue; \
		test ! -f "$${save}" || continue; \
		mkdir -p "`dirname $${save}`" && \
		mv "$${orig}" "$${save}" && \
		uudecode -o "$${orig}" "$${new}"; \
	done

	# FIXME Something's confusing libtool, so its wrappers don't do the
	#       library path magic they are supposed to do. I haven't found
	#       out what's wrong yet, so let's work around the problem here
	#       with a manual override to LD_LIBRARY_PATH.
	# XXX make check insists on rebuilding the Perl plugin, so we have
	#     to override LD_RUN_PATH once more. (NB: We cannot just build
	#     the plugin once here because make check is not called when
	#     cross-compiling.)
	# XXX Unset DISPLAY to make sure the PerlMagick X regression test
	#     does not pick up a stray and possibly unauthenticated entry.
	@if test "$(DEB_HOST_GNU_TYPE)" = "$(DEB_BUILD_GNU_TYPE)"; then \
		unset DISPLAY; \
		LD_LIBRARY_PATH="$(CURDIR)/magick/.libs:$(CURDIR)/wand/.libs:$(CURDIR)/Magick++/lib/.libs:$$LD_LIBRARY_PATH" \
		$(MAKE) check check-perl LD_RUN_PATH= || \
		        $(CHECK_FAILED_ACTION); \
	else \
		echo "Skipping regression tests because we appear to be cross-compiling"; \
	fi
endif
	touch check-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f configure-stamp build-arch-stamp build-indep-stamp check-stamp

	test ! -f Makefile || $(MAKE) distclean distclean-perl
	rm -f $(CURDIR)/PerlMagick/Makefile.PL

	rm -f 'debian/Image::Magick.3pm'

	# Clean up after 'patching' miff binaries.
	test ! -d "$(CURDIR)/debian/reference-orig" || \
	for i in `cd "$(CURDIR)/debian/reference-orig"; find -type f`; do \
		orig="$${i}"; \
		save="$(CURDIR)/debian/reference-orig/$$i"; \
		mv "$${save}" "$${orig}"; \
	done && rm -rf "$(CURDIR)/debian/reference-orig"
	rm -f config.log

	dh_autoreconf_clean
	dh_clean 

install: build install-indep install-arch
install-indep:
	dh_testdir
	dh_testroot
	
	dh_prep -i

	dh_install -i

install-arch:
	dh_testdir
	dh_testroot
	
	dh_prep -a

	# pkgdocdir is hardwired in configure.ac and can only be overridden
	# here.
	$(MAKE) install install-exec-perl \
		DESTDIR=$(CURDIR)/debian/tmp \
		pkgdocdir=/usr/share/doc/$(PACKAGENAME)

	# Push dependency libs to new .private entry.
	grep -q '^Libs.private:' \
		debian/tmp/usr/lib/pkgconfig/GraphicsMagick.pc || \
	  sed -e '/^Libs:/s|-L/usr/lib\(/X11\)\? ||g' \
	      -e '/^Libs:/s|\(-lGraphicsMagick\) \(.*\)|\1\nLibs.private: -L/usr/lib/X11 \2|' \
	      -i debian/tmp/usr/lib/pkgconfig/GraphicsMagick.pc

binary-indep: build-indep install-indep
	dh_testdir
	dh_testroot
	dh_installchangelogs -i
	dh_installdocs -i
	dh_installman -i
	dh_link -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

binary-arch: build-arch check install-arch
	dh_testdir
	dh_testroot
	dh_installdirs -a
	mv debian/tmp/usr/share/doc/graphicsmagick/www/api* \
	   debian/libgraphicsmagick1-dev/usr/share/doc/libgraphicsmagick1-dev/www
	mv debian/tmp/usr/share/doc/graphicsmagick/www/Magick++/ \
	   debian/libgraphicsmagick++1-dev/usr/share/doc/libgraphicsmagick++1-dev/www
	sed -e 's|\(HREF="\)\(Magick++/\)|\1../../libgraphicsmagick++1-dev/www/\2|' \
	    -e 's|\(HREF="\)\(api.html\)|\1../../libgraphicsmagick1-dev/www/\2|' \
	    -i debian/tmp/usr/share/doc/graphicsmagick/www/programming.html
	dh_install -a --list-missing --sourcedir=debian/tmp
	dh_installchangelogs -a ChangeLog
	dh_installdocs -a
	dh_installman -a
	dh_installexamples -a
	dh_installmenu -p graphicsmagick
	dh_installmime -p graphicsmagick
	dh_link -a
	dh_strip -a --dbg-package=graphicsmagick-dbg
	dh_compress -a
	dh_fixperms -a
	dh_makeshlibs -Nlibgraphicsmagick++-q16-12
	dh_makeshlibs -plibgraphicsmagick++-q16-12 \
		-V'libgraphicsmagick++-q16-12 (>= 1.3.26-5~)'
	dh_installdeb -a
	dh_perl -p libgraphics-magick-perl
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

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