#!/usr/bin/make -f

# Verbose build. See:
# http://qa.debian.org/bls/bytag/W-compiler-flags-hidden.html
export V=1

DEB_HOST_ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)

# If building for kfreebsd-i386, disable OpenMP.
# On this architecture, if OpenMP is enabled the map2alm_iter2 function
# fails to converge, so the check_alm2mapalm unit test runs indefinitely.
ifeq ($(DEB_HOST_ARCH),kfreebsd-i386)
EXTRA_CONFIGURE_ARGS=--disable-openmp
else
EXTRA_CONFIGURE_ARGS=
endif

# If DEB_BUILD_OPTIONS=parallel=n is set, then use that many OpenMP threads
# in the unit test suite too.
ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
export OMP_NUM_THREADS=$(NUMJOBS)
endif

# Enable build hardening.
export DEB_BUILD_MAINT_OPTIONS = hardening=+all
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk

# Link-time optimisation demands knowledge about all CFLAGS both at
# compile time (as represented by CFLAGS and CXXFLAGS) but also at
# at link time.
#
# Adapted from
# http://anonscm.debian.org/cgit/pkg-boinc/boinc.git/tree/debian/rules
ifeq (,$(findstring nolto,$(DEB_BUILD_OPTIONS)))
 $(info I: Activating LinkTimeOptimisation)
 export CFLAGS += -flto
 export CXXFLAGS += -flto
 export LDFLAGS := $(CPPFLAGS) $(CXXFLAGS) $(LDFLAGS)
 export AR := gcc-ar
 export RANLIB := gcc-ranlib
endif

%:
	dh $@

override_dh_auto_configure:
	dh_auto_configure -- $(EXTRA_CONFIGURE_ARGS)

override_dh_strip:
	dh_strip --dbgsym-migration libhealpix-cxx0-dbg
