#!/usr/bin/make -f
#
# Pieter Palmers <pieter.palmers@ffado.org>
#
# Robert Jordens <jordens@debian.org>
#
# This software may be used and distributed according to the terms
# of the GNU General Public License, incorporated herein by reference.
#

include /usr/share/dpkg/architecture.mk
DEB_BUILD_MAINT_OPTIONS := hardening=+all
DEB_CFLAGS_MAINT_APPEND := -fPIC
DEB_LDFLAGS_MAINT_APPEND := -Wl,--as-needed
include /usr/share/dpkg/buildflags.mk
include /usr/share/dpkg/pkg-info.mk
include /usr/share/cdbs/1/rules/debhelper.mk
include /usr/share/cdbs/1/rules/utils.mk

scons := \
  $(foreach v,CC CFLAGS CPPFLAGS CXXFLAGS LDFLAGS,$(v)="$($(v))") \
  scons \
  CUSTOM_ENV=True \
  PREFIX=/usr \
  DESTDIR=$(DEB_DESTDIR) \
  LIBDIR=/usr/lib/$(DEB_HOST_MULTIARCH) \
  SHAREDIR=/usr/share/libffado2/ \
  PYPKGDIR=/usr/share/ffado-mixer-qt4/ \
  PYTHON_INTERPRETER=/usr/bin/python3 \
  WILL_DEAL_WITH_XDG_MYSELF=1 \
  DETECT_USERSPACE_ENV=0 \
  $(patsubst parallel=%,-j%,$(filter parallel=%,$(DEB_BUILD_OPTIONS))) \
  ENABLE_OPTIMIZATION=no
common-build-arch common-build-indep::
	$(scons) DEBUG=0
common-install-arch common-install-indep::
	$(scons) DEBUG=0 install
clean::
	$(scons) --clean
	rm -rf cache
	rm -f .sconsign.dblite
	find . -name "*.pyc" -delete

binary-install/ffado-mixer-qt4 binary-install/ffado-tools:: binary-install/%:
	dh_python3 -p$*

# Two versions of the library are built and installed, see #601657.
# During architecture-dependent builds, build with debug information
# then clean so that the objects are never installed.
libdir_dbg := /usr/lib/$(DEB_HOST_MULTIARCH)/libffado2
dbg_so := debian/libffado.so.$(DEB_VERSION_UPSTREAM)

common-build-arch:: $(dbg_so)
$(dbg_so):
	$(scons) DEBUG=1
	mv src/libffado.so $(dbg_so)
	$(scons) --clean
binary-install/ffado-tools:: binary-install/%:
	sed 's|@@libdir_dbg@@|$(libdir_dbg)|' debian/ffado-debug > debian/$*/usr/bin/ffado-debug
	dh_install -p$* $(dbg_so) $(libdir_dbg)
clean::
	rm -f $(dbg_so)
