#!/usr/bin/make -f

# Uncomment to view all compilation commands and fix W-compiler-flags-hidden
export VERBOSE=1
DH_VERBOSE=1
export DH_VERBOSE=1


#CMake silently ignores CPPFLAGS. Below will properly set CFLAGS/CXXFLAGS
#https://wiki.debian.org/Hardening#Notes_for_packages_using_CMake
include /usr/share/dpkg/architecture.mk
ifeq ($(origin CC),default)
	CC = $(DEB_HOST_GNU_TYPE)-gcc
endif

# For multiarch
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

CFLAGSX = -Wall $(shell dpkg-buildflags --get CPPFLAGS 2>/dev/null) $(shell dpkg-buildflags --get CFLAGS 2>/dev/null | sed -e 's/-g\|-O2//g') -fomit-frame-pointer -mno-sse4 -mno-sse3 -mno-sse2 

DEB_BUILD_ARCH ?= $(shell dpkg-architecture -qDEB_BUILD_ARCH)


ifneq (,$(findstring amd64,$(DEB_BUILD_ARCH)))
export DEB_BUILD_MAINT_OPTIONS = hardening=+fortify,-stackprotector,+relro,+bindnow,-pie
else
export DEB_BUILD_MAINT_OPTIONS = hardening=+fortify,-stackprotector,+relro,+bindnow,+pie
endif

devi := $(shell lsb_release -r -s | cut -d '.'  -f1)
ifeq ($(shell if [ $(devi) -gt 7 ] ; then echo gt ; else echo lt ; fi),gt)
   DEBVE2=yes
else
   DEBVE2=no
endif

DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
DEB_HOST_ARCH = $(shell dpkg-architecture -qDEB_HOST_ARCH)

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
    CFLAGS += -O0
else

OPTI_FLAGS_LIBONLY := -funswitch-loops -fpredictive-commoning -fomit-frame-pointer
ifeq ($(DEB_HOST_ARCH),i386)
ifeq ($(DEBVE2),yes)
    OPTI_FLAGS_LIBONLY += -march=i686 -mtune=generic -mmmx -msse -mfpmath=sse -fexcess-precision=fast 
else
    OPTI_FLAGS_LIBONLY += -march=i586 -mtune=generic -mmmx -fexcess-precision=fast 
endif
else
ifeq ($(DEB_HOST_ARCH),i486)
ifeq ($(DEBVE2),yes)
    OPTI_FLAGS_LIBONLY += -march=i686 -mtune=generic -mmmx -msse -mfpmath=sse -fexcess-precision=fast 
else
    OPTI_FLAGS_LIBONLY += -march=i586 -mtune=generic -mmmx -fexcess-precision=fast 
endif
endif
endif

ifeq ($(DEB_HOST_ARCH),amd64)
    OPTI_FLAGS_LIBONLY += -mmmx -msse -msse2 -mfpmath=sse -fexcess-precision=fast 
endif

CFLAGS= $(CFLAGSX) -O3 $(OPTI_FLAGS_LIBONLY)
export CFLAGS= $(CFLAGSX) -O3 $(OPTI_FLAGS_LIBONLY)
CXXFLAGS= -O3 $(OPTI_FLAGS_LIBONLY)
export CXXFLAGS= -O3 $(OPTI_FLAGS_LIBONLY)

endif

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


# Use this variable to allow options passed to cmake to be overridable
DEB_CMAKE_OPTIONS ?= -DCMAKE_VERBOSE_MAKEFILE=ON \-DCMAKE_BUILD_TYPE=RelWithDebInfo \
		-DCMAKE_INSTALL_PREFIX="/usr" \
		-DCMAKE_C_FLAGS="$(CFLAGS)" \
		-DCMAKE_CPP_FLAGS="$(CFLAGS)" \
		-DCMAKE_CXX_FLAGS="$(CXXFLAGS) $(CFLAGS)" \
		-DLIB_SUFFIX="/$(DEB_HOST_MULTIARCH)" \
		-DEXAMPLES=OFF  \
		..

BUILD_TREE=$(CURDIR)/build-tree

DLOPENED_RECOMMENDS_LIBS = -lpulse
ifeq ($(DEB_HOST_ARCH_OS),linux)
DLOPENED_RECOMMENDS_LIBS += -lasound
endif
DLOPENED_SUGGESTS_LIBS = -lportaudio -lsndio

.PHONY: build
%:
	dh $@ --builddirectory=$(BUILD_TREE) --parallel

override_dh_auto_clean:
	rm -rf $(BUILD_TREE)

override_dh_auto_configure:
	dh_auto_configure -- $(DEB_CMAKE_OPTIONS)

override_dh_installchangelogs:
	dh_installchangelogs ChangeLog 

override_dh_strip:
	dh_strip -plibopenal1 --dbg-package=libopenal1-dbg
	dh_strip

copyright_check:
	${MAKE} -f /usr/share/cdbs/1/rules/utils.mk debian/stamp-copyright-check
	rm debian/stamp-copyright-check

override_dh_auto_install:
	dh_auto_install
	install -d debian/tmp/etc/openal
	install -m644 \
		debian/tmp/usr/share/openal/alsoftrc.sample \
		debian/tmp/etc/openal/alsoft.conf


override_dh_shlibdeps: debian/tmp/openal-soft-Recommends-dummy.so debian/tmp/openal-soft-Suggests-dummy.so
	dh_shlibdeps -O--parallel
	# Need to manually add dependencies for dlopened libs.
	dpkg-shlibdeps -dRecommends -edebian/tmp/openal-soft-Recommends-dummy.so -xlibc6 -O >>debian/libopenal1.substvars
	dpkg-shlibdeps -dSuggests -edebian/tmp/openal-soft-Suggests-dummy.so -xlibc6 -O >>debian/libopenal1.substvars

debian/tmp/openal-soft-Recommends-dummy.so:
	mkdir -p debian/tmp
	$(CC) -xc -shared -Wl,--no-as-needed -o $@ /dev/null $(DLOPENED_RECOMMENDS_LIBS)

debian/tmp/openal-soft-Suggests-dummy.so:
	mkdir -p debian/tmp
	$(CC) -xc -shared -Wl,--no-as-needed -o $@ /dev/null $(DLOPENED_SUGGESTS_LIBS)

get-orig-source:
	$(dir $_)openal-soft-get-orig-source
