#!/usr/bin/make -f

# Uncomment this to compile with gcc-snapshot.
#export  CC=/usr/lib/gcc-snapshot/bin/gcc
#export CXX=/usr/lib/gcc-snapshot/bin/g++
#export CPP=/usr/lib/gcc-snapshot/bin/cpp

# Uncomment for particular version of gcc
#export  CC := gcc-4.7
#export CXX := g++-4.7
#export CPP := cpp-4.7

# Uncomment for clang (llvm)
#export CC  = clang
#export CXX = clang

# Set default flags with dpkg-buildflags
export DEB_BUILD_MAINT_OPTIONS = hardening=+all
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk

DEB_BUILD_GNU_CPU  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_CPU)
DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)


# PKG = boinc-app-eah-brp

export CPPFLAGS := $(shell dpkg-buildflags --get CPPFLAGS)
export CXXFLAGS := $(shell dpkg-buildflags --get CXXFLAGS)
export LDFLAGS  := $(shell dpkg-buildflags --get LDFLAGS)

export EINSTEIN_RADIO_SRC := $(CURDIR)
export EINSTEINBINARY_TARGET = einsteinbinary_BRP4

# Uncomment this to enable compilation with UltraSPARC specific compiler
# flags. This will most probably increase the application's performance or if
# your are unlucky will break it.
#
#CPUTYPE = $(shell head -1 /proc/cpuinfo | cut -f 2 -d ':' | cut -f 3 -d '\' )
#ifeq (UltraSparc, $(CPUTYPE))
#  CPUSUBTYPE = $(shell head -1 /proc/cpuinfo | cut -f 2 -d ':' | \
#    cut -f 4 -d '\' | cut -f 1 -d '+')
#  CFLAGS += -mcpu=v9
#  CXXFLAGS += -mcpu=v9
#  ifeq (III, $(CPUSUBTYPE))
#    CFLAGS += -mcpu=ultrasparc3
#    CXXFLAGS += -mcpu=ultrasparc3
#  else
#    CFLAGS += -mcpu=ultrasparc
#    CXXFLAGS += -mcpu=ultrasparc
#  endif
#  CFLAGS += -m64
#  CXXFLAGS += -m64
#  DEB_CONFIGURE_EXTRA_FLAGS += --enable-bitness=64
#endif


# Append this to CFLAGS and CXXFLAGS.
CFLAGS_APPEND += -I/usr/include/boinc -I/usr/include/boinc/lib -I/usr/include/boinc/api

ifneq (clang,$(CXX))
CFLAGS_APPEND += -O3
#CFLAGS_APPEND += -mno-avx
#CFLAGS_APPEND += -msse4.2
GCCVERSION=$(shell $(CXX) --version | tr " " "\n" | egrep '^[0-9]+\.[0-9]+\.[0-9]+$$' | cut -f1,2 -d.)
endif

ifeq (clang,$(CXX))
GCCVERSION=$(shell $(CXX) --version | tr " " "\n" | egrep '^[0-9]+\.[0-9]+-[0-9]+$$' | cut -f1 -d-)
CFLAGS_APPEND += -O3
# -O4 implies link-time optimisation with clang, fails
endif

ifeq (linux,$(shell dpkg-architecture -qDEB_TARGET_ARCH_OS))
ifneq (,$(findstring local,$(DEB_BUILD_OPTIONS)))
LOCALOPTIMISATION="yes"
endif
ifeq (,$(shell dpkg-parsechangelog | egrep '^Distribution:' |egrep -v 'UNRELEASED'))
LOCALOPTIMISATION="yes"
endif
endif

#CFLAGS_APPEND += -O3
#CFLAGS_APPEND += -fomit-frame-pointer

# If the package is not meant to be distributed to main servers, then maximise optimisation
ifneq (,$(LOCALOPTIMISATION))
  $(info I: Setting native flags for optimisation)
  CFLAGS_APPEND += -march=native -mtune=native
else
  $(info W: _Not_ setting native flags for optimisation)
ifeq ($(DEB_BUILD_GNU_CPU),i686)
  $(info W: Not supporting earlier systems than i686)
  # Demand i686 class system
  CFLAGS_APPEND += -march=i686 -mcpu=i686
endif
ifeq ($(DEB_BUILD_GNU_CPU),x86_64)
  #CFLAGS_APPEND += -mavx -msse3 
endif
endif

CXXFLAGS += $(CFLAGS_APPEND)
$(info CXXFLAGS $(CXXFLAGS))


# Get upstream version from debian version.
UPSTREAM_VERSION = $(shell dpkg-parsechangelog | egrep '^Version:' | \
  cut -d ' ' -f 2 | cut -d '-' -f 1 | cut -d '+' -f 1 | cut -d '~' -f 1 | perl -pe 's/^.*?://')
# Remove dot "." in upstream version for use in app_info.xml
#APP_INFO_VERSION = $(subst .,,$(UPSTREAM_VERSION))
APP_INFO_VERSION = 1


%:
	dh $@

override_dh_install: 
	cat debian/extra/app_info.xml.in | perl -ne 'print unless /^#/' | \
	  sed -e 's/VERSION/$(APP_INFO_VERSION)/' > debian/extra/app_info.xml
	dh_install --list-missing

override_dh_auto_clean:
	make clean || echo "I: Make clean fails with no patches applied - please ignore"
	dh_clean stderr.old stderr.txt boinc_EinsteinRadio_0 boinc_EinsteinRadio_0 boinc_finish_called
	dh_clean einsteinbinary einsteinbinary_BRP4
