#!/usr/bin/make -f

# See debhelper(7) (uncomment to enable).
# Output every command that modifies files on the build system.

SOURCEPKG=$(shell dpkg-parsechangelog | sed  -n 's/^Source: \(.*\)/\1/p')
UPSTREAM=$(shell dpkg-parsechangelog |  sed -n 's/^Version: \(.*\)/\1/p')
SHA1=$(lastword $(subst ~g, ,$(UPSTREAM)))
ORIG=${SOURCEPKG}_${UPSTREAM}.orig.tar.gz

export CONFIG_SHELL=/bin/bash
# Tell to use gcc 12 for jammy
export CC=/usr/bin/gcc-12
export CXX=/usr/bin/g++-12

ifneq (,$(filter noopt,$(DEB_BUILD_OPTIONS)))
	export DEB_CFLAGS_MAINT_APPEND=-O0
	export DEB_CXXFLAGS_MAINT_APPEND=-O0
else
	export DEB_CFLAGS_MAINT_APPEND=-O3
	export DEB_CXXFLAGS_MAINT_APPEND=-O3
endif

# Disable OPENMP for arm64 builds. See LP: #2038351
ifeq ($(DEB_HOST_ARCH),arm64)
        
        DISABLE_OPENMP = -DUSE_OPENMP=OFF
endif

#export DH_VERBOSE = 1


# See FEATURE AREAS in dpkg-buildflags(1).
#export DEB_BUILD_MAINT_OPTIONS = hardening=+all

# See ENVIRONMENT in dpkg-buildflags(1).
# Package maintainers to append CFLAGS.
#export DEB_CFLAGS_MAINT_APPEND  = -Wall -pedantic
# Package maintainers to append LDFLAGS.
#export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed


%:
	dh $@ --parallel


# dh_make generated override targets.
# This is an example for Cmake (see <https://bugs.debian.org/641051>).
#override_dh_auto_configure:
#	dh_auto_configure -- \
#	-DCMAKE_LIBRARY_PATH=$(DEB_HOST_MULTIARCH)

override_dh_missing:
	dh_missing --fail-missing

override_dh_link:
	dh_link
	rm -rf debian/darktable-tools-basecurve/usr/share/doc/darktable-tools-basecurve
	rm -rf debian/darktable-tools-noise/usr/share/doc/darktable-tools-noise
	ln -s /usr/share/doc/darktable debian/darktable-tools-basecurve/usr/share/doc/darktable-tools-basecurve
	ln -s /usr/share/doc/darktable debian/darktable-tools-noise/usr/share/doc/darktable-tools-noise

override_dh_auto_configure: src/version_gen.c
	dh_auto_configure -- \
		-DCMAKE_INSTALL_DATAROOTDIR="share" \
		-DCMAKE_INSTALL_LIBEXECDIR="lib" \
		-DCMAKE_INSTALL_DOCDIR="share/doc/darktable" \
		-DCMAKE_SKIP_RPATH=OFF \
		-DCMAKE_BUILD_TYPE=Release \
		-DBINARY_PACKAGE_BUILD=1 \
		-DRAWSPEED_ENABLE_LTO=OFF \
		-DUSE_OPENCL=ON \
		-DBUILD_NOISE_TOOLS=ON \
		-DBUILD_CURVE_TOOLS=ON \
		-DDONT_USE_INTERNAL_LUA=Off \
		$(DISABLE_OPENMP)

describe-current-version:
	git describe --tags upstream | sed 's,^release-,,;s,-,+,;s,-,~,;'

get-orig-source:
	git archive --format=tar $(SHA1) | gzip -9 > ../$(ORIG)

src/version_gen.c:
	./tools/create_version_c.sh src/version_gen.c "${UPSTREAM}"

.PHONY: describe-current-version git-orig-source