#!/usr/bin/make -f
#
# 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/cdbs/1/class/langcore.mk
include /usr/share/cdbs/1/rules/buildcore.mk
include /usr/share/cdbs/1/rules/utils.mk
include /usr/share/cdbs/1/rules/debhelper.mk
DEB_CLEAN_EXCLUDE=debian/tmp
DEB_DESTDIR = $(CURDIR)/debian/tmp/

ifneq (,$(findstring parallel,$(DEB_BUILD_OPTIONS)))
	PARALLEL_JOBS := $(shell echo $(DEB_BUILD_OPTIONS) | \
		sed -e 's/.*parallel=\([0-9]\+\).*/\1/')
	ifeq ($(DEB_BUILD_OPTIONS),$(PARALLEL_JOBS))
		PARALLEL_JOBS := $(shell if [ -f /proc/cpuinfo ]; \
			then echo `cat /proc/cpuinfo | grep 'processor' | wc -l`; \
			else echo 1; fi)
	endif
	NJOBS := -j$(PARALLEL_JOBS)
endif

ALTIVEC_OPT_FLAGS := -O3 -mcpu=7400 -maltivec -mabi=altivec -mhard-float -mpowerpc-gfxopt
I686_OPT_FLAGS := -O3 -march=i686 -mmmx

DEB_SCONS_EXTRA_FLAGS := \
	PREFIX=/usr \
	NLS=yes \
	FREEDESKTOP=no \
	$(NJOBS) \
	SYSLIBS=yes \
	FREESOUND=yes \
	VST=0

DEB_SCONS_NOOPT_FLAGS := DEBUG=no FPU_OPTIMIZATION=no
ifneq (,$(findstring amd64,$(DEB_BUILD_ARCH)))
DEB_SCONS_NOOPT_FLAGS := DEBUG=no FPU_OPTIMIZATION=yes
endif

ifneq (,$(findstring i386,$(DEB_BUILD_ARCH)))
DEB_SCONS_NOOPT_FLAGS += DIST_TARGET=i386
endif


DEB_DH_STRIP_ARGS := --dbg-package=ardour


DEB_SCONS_ENVVARS :=
DEB_SCONS_INVOKE = $(DEB_SCONS_ENVVARS) scons 

# For an out-of-tree build, we just cp -al all the needed files.
# It seems these are enough
BUILD_FILES = ardour.rc.in gtk2_ardour icons libs SConstruct templates tools vst

common-build-arch:: debian/stamp-scons-build
debian/stamp-scons-build:
	mkdir -p $(DEB_DESTDIR)/generic
	mkdir -p build-generic
	cp -alf $(BUILD_FILES) build-generic
	cd build-generic && $(DEB_SCONS_INVOKE) 'ARCH=$(CFLAGS)' \
		DESTDIR=$(DEB_DESTDIR)/generic \
		$(DEB_SCONS_EXTRA_FLAGS) $(DEB_SCONS_NOOPT_FLAGS)
ifneq (,$(findstring i386,$(DEB_BUILD_ARCH)))
	mkdir -p $(DEB_DESTDIR)/i686
	mkdir -p build-i686
	cp -alf $(BUILD_FILES) build-i686
	cd build-i686 && $(DEB_SCONS_INVOKE) 'ARCH=$(I686_OPT_FLAGS)' \
		DESTDIR=$(DEB_DESTDIR)/i686 \
		DEBUG=no $(DEB_SCONS_EXTRA_FLAGS) \
		FPU_OPTIMIZATION=yes DIST_TARGET=i686
endif
ifneq (,$(findstring powerpc,$(DEB_BUILD_ARCH)))
	mkdir -p $(DEB_DESTDIR)/altivec
	mkdir -p build-altivec
	cp -alf $(BUILD_FILES) build-altivec
	cd build-altivec && $(DEB_SCONS_INVOKE) 'ARCH=$(ALTIVEC_OPT_FLAGS)' \
		DESTDIR=$(DEB_DESTDIR)/altivec \
		DEBUG=no $(DEB_SCONS_EXTRA_FLAGS)
endif
	touch $@

install/ardour::
	cd build-generic && $(DEB_SCONS_INVOKE) 'ARCH=$(CFLAGS)' \
		DESTDIR=$(DEB_DESTDIR)/generic \
		$(DEB_SCONS_EXTRA_FLAGS) $(DEB_SCONS_NOOPT_FLAGS) \
		install

ifneq (,$(findstring i386,$(DEB_BUILD_ARCH)))
install/ardour-i686::
	cd build-i686 && $(DEB_SCONS_INVOKE) 'ARCH=$(I686_OPT_FLAGS)' \
		DESTDIR=$(DEB_DESTDIR)/i686 \
		DEBUG=no $(DEB_SCONS_EXTRA_FLAGS) \
		FPU_OPTIMIZATION=yes DIST_TARGET=i686 \
		install
endif

ifneq (,$(findstring powerpc,$(DEB_BUILD_ARCH)))
install/ardour-altivec::
	cd build-altivec && $(DEB_SCONS_INVOKE) 'ARCH=$(ALTIVEC_OPT_FLAGS)' \
		DESTDIR=$(DEB_DESTDIR)/altivec \
		DEBUG=no $(DEB_SCONS_EXTRA_FLAGS) install
endif

# this is bad but the only easy way to have ardour.rc generated from
# ardour.rc.in
common-install-indep:: debian/stamp-scons-build
common-install-arch:: debian/stamp-scons-build

clean:: scons-clean
scons-clean::
	$(MAKE) -f debian/rules reverse-config
	rm -rf build-generic build-i686 build-altivec
	rm -rf $(DEB_DESTDIR) debian/stamp-scons-build

	rm -rf debian/ardour-dbg
	rm -f gtk2_ardour/*.mo
