#! /usr/bin/make -f

include /usr/share/dpkg/architecture.mk

ifeq ("$(DEB_HOST_ARCH_CPU)","ppc64el")
	DEB_CFLAGS_MAINT_APPEND = -mno-altivec -mno-vsx
	DEB_CXXFLAGS_MAINT_APPEND = -mno-altivec -mno-vsx
endif

DPKG_EXPORT_BUILDFLAGS = 1
DEB_BUILD_MAINT_OPTIONS=hardening=+all
include /usr/share/dpkg/default.mk

%:
	dh $@ --buildsystem=cmake+ninja


# NATIVE needs to be off to build for generic machines
# SSE can be off always: amd64 always enables it, i386
# cannot enable it

DEB_CMAKE_EXTRA_FLAGS = \
	-DCMAKE_BUILD_TYPE=RelWithDebInfo \
	-DCMAKE_SKIP_RPATH=on \
	-DNATIVE=off \
	-DSSE=off \
	-DSSE2=off \
	-DDSO_VISIBILITY=on \
	-DSYSTEM_BOOST=on \
	-DSYSTEM_YAMLCPP=on \
	-DENABLE_TESTSUITE=off \
	-DLIBSCSYNTH=on \
	$(empty)

binaries := $(shell dh_listpackages)

# supernova (optional alternative to scsynth) uses fancy simd things
# which fail to build on non-target architectures
ifneq (,$(filter supercollider-supernova,$(binaries)))
DEB_CMAKE_EXTRA_FLAGS += -DSUPERNOVA=on
else
DEB_CMAKE_EXTRA_FLAGS += -DSUPERNOVA=off
endif

ifneq (,$(filter supercollider-emacs,$(binaries)))
DEB_CMAKE_EXTRA_FLAGS += -DSC_EL=on -DSC_EL_BYTECOMPILE=off
else
DEB_CMAKE_EXTRA_FLAGS += -DSC_EL=off
endif

ifneq (,$(filter supercollider-vim,$(binaries)))
DEB_CMAKE_EXTRA_FLAGS += -DSC_VIM=on
else
DEB_CMAKE_EXTRA_FLAGS += -DSC_VIM=off
endif

# plenty of architectures do not ship qt6-webengine-dev
$(shell dpkg -s qt6-webengine-dev > /dev/null)
ifneq ($(.SHELLSTATUS),0)
DEB_CMAKE_EXTRA_FLAGS += -DSC_USE_QTWEBENGINE=off
endif


override_dh_auto_configure:
	dh_auto_configure -- $(DEB_CMAKE_EXTRA_FLAGS)


# Do not enable tests, they only test the supernova component, which only works on x86
override_dh_auto_test:
	@echo "Tests disabled"

execute_after_dh_auto_install-indep:
	install -m 644 debian/README.extensions \
		$(CURDIR)/debian/tmp/usr/share/SuperCollider/Extensions/README
# this needs to be an absolute not relative softlink,
# because supercollider copies it around. hence not using dh_link for this.
	install -d $(CURDIR)/debian/tmp/usr/share/SuperCollider/HelpSource/lib
	ln -sf /usr/share/javascript/prettify/prettify.js $(CURDIR)/debian/tmp/usr/share/SuperCollider/HelpSource/prettify.js
	ln -sf /usr/share/javascript/codemirror/codemirror.js  $(CURDIR)/debian/tmp/usr/share/SuperCollider/HelpSource/lib/codemirror-5.39.2.min.js
	ln -sf /usr/share/javascript/codemirror/addon/mode/simple.js $(CURDIR)/debian/tmp/usr/share/SuperCollider/HelpSource/lib/codemirror-addon-simple-5.39.2.min.js
	ln -sf /usr/share/javascript/jquery/jquery.min.js $(CURDIR)/debian/tmp/usr/share/SuperCollider/HelpSource/lib/jquery.min.js
