#!/usr/bin/make -f
#export DH_VERBOSE=1

ifneq (,$(filter $(DEB_HOST_ARCH), arm64 ppc64el))
  DEB_BUILD_OPTIONS := $(strip $(filter-out parallel=%, $(DEB_BUILD_OPTIONS)) parallel=2)
  export DEB_BUILD_OPTIONS
endif

export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed

# as per upstream request:
#export DEB_CFLAGS_MAINT_APPEND = -fvisibility=hidden

DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
VER_FULL = $(shell dpkg-parsechangelog | grep '^Version' | cut -d' ' -f2 | cut -f1 -d-)
VER_SONAME = $(shell dpkg-parsechangelog | grep '^Version' | cut -d' ' -f2 | cut -f1 -d- | cut -f1-2 -d.)

#
CFLAGS+=$(CPPFLAGS) -fvisibility=hidden -fvisibility-inlines-hidden
CXXFLAGS+=$(CPPFLAGS) -fvisibility=hidden -fvisibility-inlines-hidden

# tbb issue:
DEB_BUILD_ARCH_CPU := $(shell dpkg-architecture -qDEB_BUILD_ARCH_CPU 2>/dev/null)
ifeq ($(DEB_BUILD_ARCH_CPU), arm)
CXXFLAGS+=-Wa,-mimplicit-it=thumb
endif

# Disable optimization on mips* because the compiler is running out of memory
# see #847752 / #879636
ifneq (,$(filter $(DEB_BUILD_ARCH_CPU), mips mipsel sh4))
CXXFLAGS:=$(filter-out -O2,$(CXXFLAGS)) --param ggc-min-expand=10 -O1
endif
# -g is already passed through different mechanism, simply set OPTIMIZE make
#  variable on mips/el. -g1 seems to be solve symptoms, eg. #890237
ifneq (,$(filter $(DEB_BUILD_ARCH_CPU), mips mipsel))
OPTIMIZE=-g1
endif

# deduce documentation option (build-indep target)
ifeq "" "$(filter %-doc,$(shell dh_listpackages))"
  BUILDDOC = OFF
else
  BUILDDOC = ON
endif

%:
	dh $@ --parallel --with python3 --buildsystem=cmake

MPL: LICENSE
	cp -f $< $@

# https://github.com/AcademySoftwareFoundation/openvdb/issues/144#issuecomment-508984426
CMAKE_EXTRA_FLAGS = -DCMAKE_SKIP_RPATH=ON \
	-DCMAKE_BUILD_TYPE:STRING=None \
	-DCMAKE_NO_SYSTEM_FROM_IMPORTED:BOOL=TRUE \
	-DPYTHON_DEFAULT_EXECUTABLE:PATH=/usr/bin/python3.7 \
	-D_Python_CONFIG:FILEPATH=/usr/bin/x86_64-linux-gnu-python3.7-config \
	-DPython_EXECUTABLE:FILEPATH=/usr/bin/python3.7 \
	-DOPENVDB_BUILD_DOCS:BOOL=$(BUILDDOC) \
	-DOPENVDB_BUILD_UNITTESTS:BOOL=TRUE \
	-DOPENVDB_BUILD_VDB_LOD:BOOL=TRUE \
	-DOPENVDB_BUILD_VDB_RENDER:BOOL=TRUE \
	-DOPENVDB_BUILD_VDB_VIEW:BOOL=TRUE \
	-DUSE_BLOSC:BOOL=TRUE \
	-DUSE_EXR:BOOL=TRUE \
	-DUSE_NUMPY:BOOL=TRUE

override_dh_auto_configure:
	dh_auto_configure -- $(CMAKE_EXTRA_FLAGS)

override_dh_auto_test-arch:
	timeout 2h dh_auto_test -- $(TESTOPS) || true

override_dh_auto_build-indep:
	dh_auto_build -- doc

# No tests needed for docs
override_dh_auto_test-indep:

pkg_run = libopenvdb-tools
pkg_lib = libopenvdb$(VER_SONAME)
pkg_dev = libopenvdb-dev
pkg_doc = libopenvdb-doc
pkg_py  = python3-openvdb

PYMODDIR := /usr/lib/$(shell py3versions -r)

override_dh_install-indep:
	dh_install -p$(pkg_doc) debian/tmp/usr/doc/html usr/share/doc/libopenvdb-doc/

override_dh_install-arch:
	dh_numpy3 --package=$(pkg_py)
	dh_install -p$(pkg_run) debian/tmp/usr/bin
	dh_install -p$(pkg_lib) debian/tmp/usr/lib/libopenvdb.so.$(VER_SONAME) usr/lib/$(DEB_HOST_MULTIARCH)
	dh_install -p$(pkg_lib) debian/tmp/usr/lib/libopenvdb.so.$(VER_FULL)   usr/lib/$(DEB_HOST_MULTIARCH)
	dh_install -p$(pkg_dev) debian/tmp/usr/lib/libopenvdb.so               usr/lib/$(DEB_HOST_MULTIARCH)
	dh_install -p$(pkg_dev) debian/tmp/usr/include
	dh_install -p$(pkg_py)  debian/tmp/usr/lib/python3.7/pyopenvdb.so      $(PYMODDIR)/dist-packages/

override_dh_installdocs: MPL
	dh_installdocs -A $^

#override_dh_shlibdeps:
#	dh_shlibdeps --dpkg-shlibdeps-params=--ignore-missing-info

override_dh_clean:
	#test -h openvdb || { unlink openvdb; }
	dh_clean doc/doxygen_sqlite3.db

# man page cannot be automatically generated since we need to cleanup help2man output
debian/vdb_print.1: debian/vdb_print.1.in
	help2man --version-string=$(VER_FULL) --include=$< --output=$@ --no-info --no-discard-stderr vdb_print

debian/vdb_view.1: debian/vdb_view.1.in
	help2man --version-string=$(VER_FULL) --include=$< --output=$@ --no-info --no-discard-stderr vdb_view

debian/vdb_render.1: debian/vdb_render.1.in
	help2man --version-string=$(VER_FULL) --include=$< --output=$@ --no-info --no-discard-stderr vdb_render

debian/vdb_lod.1: debian/vdb_lod.1.in
	help2man --version-string=$(VER_FULL) --include=$< --output=$@ --no-info --no-discard-stderr vdb_lod

get-orig-source:
	uscan --verbose --force-download --rename --repack
