#!/usr/bin/make -f

CPPFLAGS:=$(shell dpkg-buildflags --get CPPFLAGS)
CFLAGS:=$(shell dpkg-buildflags --get CFLAGS) $(CPPFLAGS)
CXXFLAGS:=$(shell dpkg-buildflags --get CXXFLAGS) $(CPPFLAGS)
LDFLAGS:=$(shell dpkg-buildflags --get LDFLAGS) -Wl,--as-needed

DEB_HOST_ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
DEB_BUILD_OPTIONS += nocheck
export DEB_BUILD_MAINT_OPTIONS = hardening=+bindnow

ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
	NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
	MAKEFLAGS += -j$(NUMJOBS)
endif

# These don't need to be exported and if they are we'll get the flags
# duplicated in the command line.
#unexport CFLAGS
#unexport CXXFLAGS
#unexport LDFLAGS

CMAKE_FLAGS = \
        -DCMAKE_INSTALL_PREFIX=/usr \
        -DCMAKE_VERBOSE_MAKEFILE=ON \
        -DCMAKE_C_FLAGS_RELEASE="$(CFLAGS)" \
        -DCMAKE_CXX_FLAGS_RELEASE="$(CXXFLAGS)" \
        -DCMAKE_SHARED_LINKER_FLAGS_RELEASE="$(LDFLAGS)" \
        -DCMAKE_BUILD_TYPE=RelWithDebInfo \
	-DCMAKE_SKIP_INSTALL_RPATH=TRUE \
	-DOMPL_ODESOLVER=ON \
        -DOMPL_REGISTRATION=OFF \
	-DOMPL_BUILD_PYBINDINGS=ON \
	-DOMPL_BUILD_PYTESTS=OFF \
	-DPYTHON_EXEC=/usr/bin/python3

%:
	dh $@ --builddirectory=build --buildsystem=cmake

override_dh_clean:
ifneq ("$(wildcard $(build/bindings_generator.py))","")
	rm -rf py-bindings/ompl/bindings_generator.py*
	mv build/bindings_generator.py py-bindings/ompl/
endif
	rm -rf build

	find py-bindings/ -name *.pypp.*  | xargs -n1 rm -rf
	find py-bindings/ -name *.pyc  | xargs -n1 rm -rf
	find py-bindings/ -name *.so  | xargs -n1 rm -rf
	rm -rf doc/markdown/download.md doc/markdown/mainpage.md tests/resources/config.h CMakeModules/ompl.pc src/ompl/config.h
	dh_clean


override_dh_auto_configure:
	mkdir -p build
	dh_auto_configure --builddirectory=build -- $(CMAKE_FLAGS) $(CMAKE_ARCH_FLAGS)

override_dh_auto_build:
# Uncomment this part to prepare the python bindings
#	cd build && $(MAKE) update_bindings
	cd build && $(MAKE)

override_dh_auto_install:
	dh_auto_install --builddirectory=build

override_dh_auto_test:
	echo "Supressing upstream tests"
