#!/usr/bin/make -f

DEB_BUILDDIR = debian/build
DEB_MAKE_FLAVORS = deb udeb refdbg
DISABLE_UPDATE_UPLOADERS := 1

include /usr/share/cdbs/1/rules/autoreconf.mk
include /usr/share/cdbs/1/rules/debhelper.mk
include /usr/share/cdbs/1/rules/utils.mk
include /usr/share/cdbs/1/class/gnome.mk
include /usr/share/gnome-pkg-tools/1/rules/uploaders.mk
include /usr/share/gnome-pkg-tools/1/rules/gnome-get-source.mk

GNOME_MODULE := glib

# Enable parallel build support
DEB_BUILD_PARALLEL = 1

# Ensure the build aborts when there are still references to undefined symbols
LDFLAGS += -Wl,-z,defs

# NB: do NOT use -Wl,--as-needed to build glib; for instance the link to
# pthread is carefully crafted to allow dlopen()ing pthread-using libs; see
# http://mid.gmane.org/1257999019.21780.15.camel@marzipan
LDFLAGS += -Wl,--no-as-needed

# Make the linker work a bit harder so dynamic loading can be done faster
LDFLAGS += -Wl,-O1

APIVER := 2.0
SONAME := 0

# package names
SHARED_PKG := libglib$(APIVER)-$(SONAME)
DATA_PKG := libglib$(APIVER)-data
TEST_PKG := libglib$(APIVER)-tests
DEV_PKG := libglib$(APIVER)-dev
BIN_PKG := libglib$(APIVER)-bin
UDEB_PKG := libglib$(APIVER)-udeb
DOC_PKG := libglib$(APIVER)-doc
DEBUG_PKG := $(SHARED_PKG)-dbg
REFDBG_PKG := libglib$(APIVER)-$(SONAME)-refdbg

DEB_MAKE_DESTDIRSKEL = $(CURDIR)/debian/install/@FLAVOR@

DEB_CLEAN_EXCLUDE = org.gtk.test.gschema.xml.orig

DEB_DH_INSTALL_ARGS += --sourcedir=debian/install/deb
DEB_DH_INSTALL_ARGS_$(TEST_PKG) += --sourcedir=debian/install/deb -X.la
DEB_DH_INSTALL_ARGS_$(UDEB_PKG) += --sourcedir=debian/install/udeb
DEB_DH_INSTALL_ARGS_$(REFDBG_PKG) += --sourcedir=debian/install/refdbg

DEB_DH_MAKESHLIBS_ARGS_$(SHARED_PKG) += -V --add-udeb=$(UDEB_PKG) -- -c4
DEB_DH_MAKESHLIBS_ARGS_$(REFDBG_PKG) = --no-act
DEB_DH_STRIP_ARGS_$(REFDBG_PKG) = --no-act
# Don't put the symbols in the -dbg package
DEB_DH_STRIP_ARGS_$(UDEB_PKG) =

# Don't strip translations from the installed tests; makes them environment dependent.
DEB_DH_TRANSLATIONS_ARGS = -Xinstalled-tests

# Do not clutter $HOME with ~/.dbus-keyrings and avoid failure on the buildds
# where creating /home/buildd/.dbus-keyrings fails
export HOME=$(CURDIR)/debian/build

# Make sure that everything that uses D-Bus is creating its own temporary
# session rather than polluting the developer's (or failing, on buildds)
export DBUS_SESSION_BUS_ADDRESS=this-should-not-be-used-and-will-fail:

ifeq ($(DEB_HOST_ARCH_OS), linux)
  DEB_MAKE_CHECK_TARGET = $(if $(filter deb, $(cdbs_make_curflavor)), -k check -j1)
else
  DEB_MAKE_CHECK_TARGET = $(if $(filter deb, $(cdbs_make_curflavor)), -k check -j1 || true)
endif

# The tests assume this directory exists and is writable
export XDG_RUNTIME_DIR=$(CURDIR)/debian/tmp-xdg-runtime-dir
# Let's get failing tests' stdout and stderr so we have some information when
# a build fails
export VERBOSE=1

# configure flags
DEB_CONFIGURE_EXTRA_FLAGS := \
			--with-html-dir=\$${prefix}/share/doc/$(DOC_PKG) \
			--libdir=\$${prefix}/lib/$(DEB_HOST_MULTIARCH) \
			--with-pcre=system

ifneq ($(DEB_BUILD_ARCH),$(DEB_HOST_ARCH))
  DEB_CONFIGURE_EXTRA_FLAGS += \
			--disable-modular-tests \
			--disable-gtk-doc
endif

DEB_CONFIGURE_FLAGS_deb := \
			--enable-gtk-doc \
			--enable-static \
			--enable-installed-tests \
			--enable-always-build-tests \
			--enable-debug=minimum

DEB_CONFIGURE_FLAGS_udeb := \
			--disable-selinux

DEB_CONFIGURE_FLAGS_refdbg := \
			--disable-Bsymbolic \
			--enable-debug=yes

pre-build::
	mkdir -p debian/stamp-makefile-check \
		 debian/tmp-xdg-runtime-dir

binary-install/libglib2.0-0::
	set -e; for script in postinst postrm; do \
		sed -e"s/#MULTIARCH#/$(DEB_HOST_MULTIARCH)/g" \
		    -e"s/#ARCH#/$(DEB_HOST_ARCH)/g" \
		    debian/$(cdbs_curpkg).$$script.in \
		    > debian/$(cdbs_curpkg).$$script ; \
	done

binary-install/libglib2.0-dev::
	dh_python2 -plibglib2.0-dev /usr/share/glib-2.0/codegen

common-install-arch::
	set -e; for file in libgio-fam.install libglib2.0-0.install \
	            libglib2.0-0-refdbg.install libglib2.0-0-dbg.install \
	            libglib2.0-dev.install libglib2.0-udeb.install \
		    libglib2.0-bin.links libglib2.0-dev.links \
	            libglib2.0-0.dirs libglib2.0-0.triggers; \
	do \
		sed -e"s,\$${DEB_HOST_MULTIARCH},${DEB_HOST_MULTIARCH},g" \
		        debian/$${file}.in > debian/$$file; \
	done

clean::
	sed \
		-e "s#@SONAME@#$(SONAME)#g" \
		-e "s#@APIVER@#$(APIVER)#g" \
		-e "s#@VERSION@#$(DEB_UPSTREAM_VERSION)#g" \
		-e "s#@SHARED_PKG@#$(SHARED_PKG)#g" \
		-e "s#@BIN_PKG@#$(BIN_PKG)#g" \
		-e "s#@DATA_PKG@#$(DATA_PKG)#g" \
		-e "s#@DEV_PKG@#$(DEV_PKG)#g" \
		-e "s#@UDEB_PKG@#$(UDEB_PKG)#g" \
		-e "s#@DOC_PKG@#$(DOC_PKG)#g" \
		-e "s#@DEBUG_PKG@#$(DEBUG_PKG)#g" \
		-e "s#@GNOME_TEAM@#$(UPLOADERS)#g" \
		-e "s#@REFDBG_PKG@#$(REFDBG_PKG)#g" \
		debian/control.in > debian/control
	rm -rf $(CURDIR)/debian/build \
	       $(CURDIR)/debian/install \
	       $(CURDIR)/debian/stamp-makefile-check \
	       $(CURDIR)/debian/tmp-xdg-runtime-dir

list-missing:
	# FIXME: remove this when CDBS' list-missing works with multiple flavors
	perl -w debian/dh_listmissing.pl $(foreach f,$(DEB_MAKE_FLAVORS),debian/install/$(f)) 2>&1 | \
	    egrep -v '(\.la|\.pyc|\.pyo) has been installed' >&2
