#!/usr/bin/make -f
# export DH_VERBOSE=1
export DEB_BUILD_MAINT_OPTIONS   = hardening=+all
export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed

DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk
include /usr/share/dpkg/architecture.mk
CFLAGS+=$(CPPFLAGS)
CXXFLAGS+=$(CPPFLAGS)

ifneq ($(DEB_HOST_ARCH),$(DEB_BUILD_ARCH))

# Provide OS=
OS_MAP_linux = Linux
OS_MAP_kfreebsd = GNU/kFreeBSD
OS_MAP_hurd = GNU
ifneq ($(OS_MAP_$(DEB_HOST_ARCH_OS)),)
BUILD_FLAGS += OS=$(OS_MAP_$(DEB_HOST_ARCH_OS))
endif

# Provide compiler information
BUILD_FLAGS += compiler=gcc
BUILD_FLAGS += CC=$(DEB_HOST_GNU_TYPE)-gcc
BUILD_FLAGS += CXX=$(DEB_HOST_GNU_TYPE)-g++

# Provide arch=
arch_MAP_amd64 = intel64
arch_MAP_i386 = ia32
arch_MAP_mips64el = mips64
arch_MAP_powerpc = ppc32
arch_MAP_ppc64 = ppc64
arch_MAP_ppc64el = ppc64le
ifneq ($(arch_MAP_$(DEB_HOST_ARCH_CPU)),)
BUILD_FLAGS += arch=$(arch_MAP_$(DEB_HOST_ARCH_CPU))
else
ifeq ($(DEB_HOST_ARCH),armhf)
BUILD_FLAGS += arch=armv7
else
# covers arm64, ia64, mips, sparc64, etc.
BUILD_FLAGS += arch=$(DEB_HOST_GNU_CPU)
endif
endif

endif


%:
	dh $@

override_dh_auto_build-arch:
# dump out diagnostics
	cat /proc/cpuinfo
	gcc -dM -E - < /dev/null
	echo Build system page size is $$(($$(getconf PAGESIZE) >> 10)) KiB

	sed -e "s/@VERSION@/$(DEB_VERSION)/g" \
	    -e "s/@DEB_HOST_MULTIARCH@/$(DEB_HOST_MULTIARCH)/g" \
		debian/tbb.pc.in > debian/tbb.pc
	dh_auto_build -- $(BUILD_FLAGS)

override_dh_auto_build-indep:
	$(MAKE) doxygen

override_dh_auto_test-indep:
	:

override_dh_auto_install-arch:
	dh_auto_install
	# install cmake files (TBBConfig*.cmake) to libtbb2-dev
	-cmake -DINSTALL_DIR=debian/libtbb2-dev/usr/lib/$(DEB_HOST_MULTIARCH)/cmake/TBB \
		-DSYSTEM_NAME=Linux -DTBB_VERSION_FILE=include/tbb/tbb_stddef.h \
		-DLIB_REL_PATH="../../" \
		-DINC_REL_PATH="../../../../include" \
		-P cmake/tbb_config_installer.cmake

# Makefiles should not be compressed (tbb-examples)
override_dh_compress-indep:
	dh_compress -X*/examples/*

override_dh_auto_clean:
	dh_auto_clean
	-$(RM) debian/tbb.pc
