#!/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
export  CC := gcc-5
export CXX := g++-5
export CPP := cpp-5

# 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_TARGET_GNU_CPU  ?= $(shell dpkg-architecture -qDEB_TARGET_GNU_CPU)
DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_TARGET_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_TARGET_GNU_TYPE)

DEBIAN_VERSION ?= $(shell head -n 1 debian/changelog | cut -f2 -d\( | cut -f1 -d\) )

$(info DEB_TARGET_GNU_CPU: $(DEB_TARGET_GNU_CPU))
$(info DEB_TARGET_GNU_TYPE: $(DEB_TARGET_GNU_TYPE))
$(info DEBIAN_VERSION: $(DEBIAN_VERSION))


# 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)

$(info I: Initial settings of compiler flags)
$(info CPPFLAGS: $(CPPFLAGS))
$(info CXXFLAGS: $(CXXFLAGS))
$(info LDFLAGS: $(LDFLAGS))

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

# 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

CFLAGS_APPEND += -std=c++11 # because of UChar, may not be implicitly set for earlier compiler verions
CFLAGS_APPEND += $(CPPFLAGS)


ifeq (g++-5,$(CXX))
WHICH_GPP5 := $(shell which g++-5)
ifeq (,$(WHICH_GPP5))
$(info default g++-5 not available, falling back to g++)
CXX=g++
CC=gcc
CPP=cpp
endif
endif



# Append this to CFLAGS and CXXFLAGS.
CFLAGS_APPEND += -I/usr/include/boinc -I/usr/include/boinc/lib -I/usr/include/boinc/api
CXX_VERSION_LONG=$(shell $(CXX) --version|head -n 1)

ifneq (clang,$(CXX))
GCCVERSION=$(shell $(CXX) --version | tr " " "\n" | egrep '^[0-9]+\.[0-9]+\.[0-9]+$$' | cut -f1,2 -d.)
CFLAGS_APPEND += -O3
#CFLAGS_APPEND += -mno-avx
#CFLAGS_APPEND += -msse4.2
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,$(DEB_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_TARGET_GNU_CPU),i686)
  $(info W: Not supporting earlier systems than i686)
  # Demand i686 class system
  CFLAGS_APPEND += -march=i686 -mtune=i686
  CFLAGS_APPEND += -mfpmath=sse -msse
endif
ifeq ($(DEB_TARGET_GNU_CPU),x86_64)
  #CFLAGS_APPEND += -mavx -msse3 
  CFLAGS_APPEND += -mfpmath=sse -msse
endif
endif

ifeq (1,$(shell $(CXX) --version | egrep -c "g\+\+.* [5678]\.[0-9]\.[0-9]"))
#$(info I: Enabled link-time optimisation for g++ >= 5)
#CFLAGS_APPEND += -flto
endif

CFLAGS_APPEND += -DNOCHECKPOINTING

# 250fold reduction of progress communication, effectively every 2 minutes
CFLAGS_APPEND += -DCOMMUNICATIONREDUCTION=250


DEB_TARGET_ARCH_OS=$(shell dpkg-architecture -qDEB_TARGET_ARCH_OS)
CXXFLAGS += $(CFLAGS_APPEND)

CXXFLAGS += -DDEBIAN_VERSION="\"$(DEBIAN_VERSION)\""
CXXFLAGS += -DDEB_TARGET_ARCH_OS="\"$(DEB_TARGET_ARCH_OS)\""
CXXFLAGS += -DDEB_TARGET_GNU_CPU="\"$(DEB_TARGET_GNU_CPU)\""
CXXFLAGS += -DCXX_VERSION_LONG="\"$(CXX_VERSION_LONG)\""


ifeq (1,$(shell $(CXX) --version | egrep -c "g\+\+.* [5678]\.[0-9]\.[0-9]"))
# Completing LTO
#LDFLAGS += $(CFLAGS_APPEND)
endif

$(info I: Extended CXXFLAGS: $(CXXFLAGS))
$(info I:          LDFLAGS: $(LDFLAGS))


## 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))
# 56 was agreed with upstream
APP_INFO_VERSION = 56


%:
	dh $@
	
override_dh_auto_build: 
ifeq (1,$(shell grep -c "^Package: boinc-app-eah-brp-opencl" debian/control))
	@echo Peforming with Makefile.linux.opencl
	$(MAKE) EINSTEINBINARY_TARGET=$(EINSTEINBINARY_TARGET_OPENCL) -f Makefile.linux.opencl release
endif
	@echo Building with gprof parameters and running example workflow
	$(MAKE) profile
	@echo Compiling with runtime parameters learned
	$(MAKE) release

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* einsteinbinary_opencl*
	dh_clean results_profile.cand *.gcda *.o boinc_EinsteinRadio_0 boinc_finish_called
	dh_clean debian/extra/einstein_bench/boinc_EinsteinRadio_0 \
	         debian/extra/einstein_bench/boinc_finish_called \
	         debian/extra/einstein_bench/stderr.txt \
	         debian/extra/einstein_bench/testwu/stochastic_full.bank.*

test:	override_dh_auto_test
override_dh_auto_test:
	$(MAKE) TARGET=$(EINSTEINBINARY_TARGET) test
