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

include /usr/share/dpkg/default.mk

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

ifneq (,$(filter $(DEB_BUILD_ARCH), armhf armel))
  # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77728
  DEB_CXXFLAGS_MAINT_APPEND += -Wno-psabi
endif

VER_FULL = $(DEB_VERSION_UPSTREAM)
VER_SONAME = $(shell echo $(DEB_VERSION_UPSTREAM) | cut -f1-2 -d.)

# technically the following should reduce the amount of symbols being exported,
# it turns out this does not work in practice:
# https://bugs.debian.org/997080
#DEB_CFLAGS_MAINT_APPEND += -fvisibility=hidden -fvisibility-inlines-hidden
#DEB_CXXFLAGS_MAINT_APPEND += -fvisibility=hidden -fvisibility-inlines-hidden

# Disable optimization on mipsel because the compiler is running out of memory
# see #847752 / #879636
ifneq (,$(filter $(DEB_BUILD_ARCH), armel armhf i386 mipsel hppa riscv64 sh4 x32))
  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 mipsel. -g1 seems to be solve symptoms, eg. #890237
ifneq (,$(filter $(DEB_BUILD_ARCH), mipsel))
  DEB_CXXFLAGS_MAINT_APPEND += -g1
endif

export DEB_CXXFLAGS_MAINT_APPEND

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

# remove test if specified:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
  BUILDTEST = ON
else
  BUILDTEST = OFF
endif

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

MPL: LICENSE
	cp -f $< $@

PYVERS := $(shell py3versions -rd)

# notes:
# - https://github.com/AcademySoftwareFoundation/openvdb/issues/144#issuecomment-508984426
# - Force tbbmalloc for simplicity since jemalloc breaks python plugins. (#951704)
CMAKE_EXTRA_FLAGS = -DCMAKE_SKIP_RPATH:BOOL=ON \
	-DOPENVDB_ENABLE_RPATH:BOOL=OFF \
	-DOPENVDB_CORE_STATIC:BOOL=OFF \
	-DCMAKE_BUILD_TYPE:STRING=None \
	-DCONCURRENT_MALLOC:STRING=Tbbmalloc \
	-DUSE_LOG4CPLUS:BOOL=ON \
	-DCMAKE_NO_SYSTEM_FROM_IMPORTED:BOOL=TRUE \
	-DOPENVDB_BUILD_PYTHON_MODULE:BOOL=ON \
	-DPython_EXECUTABLE:FILEPATH=/usr/bin/$(PYVERS) \
	-DOPENVDB_BUILD_DOCS:BOOL=$(BUILDDOC) \
	-DOPENVDB_BUILD_UNITTESTS:BOOL=$(BUILDTEST) \
	-DOPENVDB_BUILD_VDB_LOD:BOOL=TRUE \
	-DOPENVDB_BUILD_VDB_RENDER:BOOL=TRUE \
	-DOPENVDB_BUILD_VDB_VIEW:BOOL=TRUE \
	-DUSE_EXPLICIT_INSTANTIATION:BOOL=FALSE \
	-DUSE_BLOSC:BOOL=TRUE \
	-DUSE_EXR:BOOL=TRUE \
	-DUSE_AX:BOOL=FALSE \
	-DUSE_NANOVDB:BOOL=FALSE \
	-DUSE_PNG:BOOL=TRUE \
	-DUSE_IMATH_HALF:BOOL=TRUE \
	-DUSE_NUMPY:BOOL=TRUE

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

override_dh_auto_configure:
	dh_auto_configure -- $(CMAKE_EXTRA_FLAGS)
override_dh_installdocs: MPL
	dh_installdocs -A $^
override_dh_clean:
	dh_clean doc/doxygen_sqlite3.db
	dh_clean debian/*.1

override_dh_auto_test-arch:
	timeout 2h dh_auto_test -- $(TESTOPS) || true
override_dh_install-arch: manpages
	dh_numpy3 --package=$(pkg_py)
	dh_install -p$(pkg_run) debian/tmp/usr/bin
	dh_install -p$(pkg_lib) debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/libopenvdb.so.$(VER_SONAME)
	dh_install -p$(pkg_lib) debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/libopenvdb.so.$(VER_FULL)
	dh_install -p$(pkg_dev) debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/libopenvdb.so
	dh_install -p$(pkg_dev) debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/cmake
	dh_install -p$(pkg_dev) debian/tmp/usr/include
	dh_install -p$(pkg_py)  debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/$(PYVERS)/dist-packages/pyopenvdb.so      usr/lib/$(PYVERS)/dist-packages/

override_dh_auto_build-indep:
	dh_auto_build -- doc
override_dh_auto_test-indep:
	# No tests needed for docs
override_dh_auto_install-indep:
	dh_auto_install -- -C doc
override_dh_install-indep:
	dh_install -p$(pkg_doc) debian/tmp/usr/share/doc/OpenVDB/html usr/share/doc/libopenvdb-doc/

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

debian/%.1: debian/%.1.in
	LD_PRELOAD= LD_LIBRARY_PATH=./debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH) help2man --version-string=$(VER_FULL) --include=$< --output=$@ --no-info --no-discard-stderr ./debian/tmp/usr/bin/`basename $@ .1`
	echo "Done generation $@"

.PHONY: manpages

manpages: debian/vdb_print.1 debian/vdb_view.1 debian/vdb_render.1 debian/vdb_lod.1

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