#!/usr/bin/make -f

export DH_OPTIONS
export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export LDFLAGS = $(shell dpkg-buildflags --get LDFLAGS)
CXXFLAGS = $(shell dpkg-buildflags --get CXXFLAGS)
CPPFLAGS = $(shell dpkg-buildflags --get CPPFLAGS)
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
BLDDIR = debian/cmake
INSTDIR = debian/tmp

# Helper variables
PY3VERS = $(shell py3versions -vr)
PY3DEF = $(shell py3versions -vd)
# All supported python3 versions except the default one
PY3EXTRA = $(filter-out $(PY3DEF),$(PY3VERS))

%:
	dh $@ -Scmake -B$(BLDDIR) --with python3

# without the following, we have seen been failure since ocio 2.5+
ifeq ($(DEB_TARGET_ARCH),i386)
    export DEB_CXXFLAGS_MAINT_APPEND += -msse2
endif

override_dh_auto_configure:
	dh_auto_configure -- \
		-DCMAKE_BUILD_TYPE=Release \
		-DCMAKE_CXX_FLAGS="$(CXXFLAGS) $(CPPFLAGS)" \
		-DCMAKE_CXX_STANDARD=17 \
		-DCMAKE_INSTALL_MESSAGE="ALWAYS" \
		-DCMAKE_SKIP_RPATH=ON \
		-DOCIO_BUILD_NUKE=OFF \
		-DOCIO_BUILD_STATIC=OFF \
		-DOCIO_BUILD_TESTS=OFF \
		-DOCIO_INSTALL_EXT_PACKAGES=NONE \
		-DOCIO_USE_SSE=OFF \
		-Dpystring_INCLUDE_DIR:PATH="/usr/include"

		# One additional CMake build dir per extra python3 version.
		set -e; for v in $(PY3EXTRA); do \
			dh_auto_configure --builddirectory=build-py$$v -- \
				-DCMAKE_BUILD_TYPE=Release \
				-DCMAKE_CXX_FLAGS="$(CXXFLAGS) $(CPPFLAGS)" \
				-DCMAKE_CXX_STANDARD=17 \
				-DCMAKE_INSTALL_MESSAGE="ALWAYS" \
				-DCMAKE_SKIP_RPATH=ON \
				-DOCIO_BUILD_NUKE=OFF \
				-DOCIO_BUILD_STATIC=OFF \
				-DOCIO_BUILD_TESTS=OFF \
				-DOCIO_INSTALL_EXT_PACKAGES=NONE \
				-DOCIO_USE_SSE=OFF \
				-Dpystring_INCLUDE_DIR:PATH="/usr/include" \
				-DPython_EXECUTABLE=/usr/bin/python$$v \
				-DPython3_EXECUTABLE=/usr/bin/python$$v ; \
		done

override_dh_auto_build-arch:
	dh_auto_build
	set -e; for v in $(PY3EXTRA); do \
		dh_auto_build --builddirectory=build-py$$v ; \
	done

override_dh_auto_install:
	dh_auto_install
	set -e; for v in $(PY3EXTRA); do \
		dh_auto_install --builddirectory=build-py$$v ; \
	done
	# CMake installs every python3 version's module as PyOpenColorIO.so under
	# a version-specific site-packages dir. Rename each one to its proper
	# ABI-tagged name so that, once dh_install merges them into a single
	# dist-packages/PyOpenColorIO/, the modules do not collide and dh_python3
	# leaves the already-tagged extensions untouched.
	# For each version we find the suffix of the form .cpython-XY*.so and move
	# the .so to a path of the form PyOpenColorIO.cpython-XY*.so.
	set -e; for v in $(PY3VERS); do \
		so=$(INSTDIR)/usr/lib/$(DEB_HOST_MULTIARCH)/python$$v/site-packages/PyOpenColorIO/PyOpenColorIO.so; \
		if [ -f "$$so" ]; then \
			suffix=$$(python$$v -c 'import sysconfig; print(sysconfig.get_config_var("EXT_SUFFIX"))'); \
			mv "$$so" "$$(dirname "$$so")/PyOpenColorIO$$suffix"; \
		fi; \
	done

	rm -f $(INSTDIR)/usr/share/ocio/setup_ocio.sh
	rm -f $(INSTDIR)/usr/lib/$(DEB_HOST_MULTIARCH)/lib*.a

override_dh_install:
	mkdir -p debian/tmp/usr/include/PyOpenColorIO
	cp src/bindings/python/PyOpenColorIO.h debian/tmp/usr/include/PyOpenColorIO/
	dh_install

override_dh_auto_test:

override_dh_auto_clean:
	dh_auto_clean
	set -e; for v in $(PY3EXTRA); do \
		rm -rf build-py$$v ; \
	done
