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

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

# compile with libtbb-dev only with linux 64bit intel/ppc architectures,
# fallback in pentium4 processors is not implemented, see also note of
# libtbb-dev:
#
# Note: if you are a user of the i386 architecture, i.e., 32-bit Intel
# or compatible hardware, this package only supports Pentium4-compatible
# and higher processors.
#
arch = $(shell dpkg-architecture -qDEB_BUILD_ARCH | egrep "(amd64|ia64|ppc64)")
os = $(shell dpkg-architecture -qDEB_BUILD_ARCH_OS | grep linux)

# force g++, lua5.2, and python2.7
CONFIGPARMS = CXX='g++' WITH_LUA='lua5.2' WITH_PYTHON='python2.7' CPPPATH='$${["/usr/include/lua5.2","/usr/include/python2.7"]}'
ifeq ($(and $(if $(os),good), $(if $(arch),good)), good)
	CONFIGPARMS += WITH_TBB="tbb"
endif

#
# export CPPFLAGS, CXXFLAGS, and LDFLAGS for scons
#
# default to -O3 -std=c++11 -Wall -DNDEBUG and enable all hardening flags
#
DEB_CXXFLAGS_MAINT_APPEND = -std=c++11 -O3 -Wall
DEB_CPPFLAGS_MAINT_APPEND = -DNDEBUG
export DEB_BUILD_MAINT_OPTIONS = hardening=+all

DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk

# inject compiler and linker flags to scons variables
SCCXXFLAGS =  $(shell echo $(CXXFLAGS) $(CPPFLAGS) | sed -e 's/^/\$${["/;s/ -/","-/g;s/$$/"]}/')
SCLINKFLAGS = $(shell echo $(LDFLAGS) | sed -e 's/^/\$${["/;s/ -/","-/g;s/$$/"]}/')

%:
	dh $@

# configure with LUA, Python, and possibly TBB
override_dh_auto_configure:
	scons configure --build-dir=release $(CONFIGPARMS) CXXFLAGS='$(SCCXXFLAGS)' LINKFLAGS='$(SCLINKFLAGS)'

# build gringo and clingo
override_dh_auto_build:
	scons --build-dir=release gringo clingo

override_dh_auto_clean:
	rm -rf ./build .sconf_temp .sconsign.dblite

override_dh_installchangelogs:
	dh_installchangelogs CHANGES
