#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS = hardening=+bindnow
export QT_SELECT=qt5

# Use -mlongcalls for hppa
ifneq (,$(findstring $(DEB_HOST_ARCH),hppa))
export DEB_CXXFLAGS_MAINT_APPEND=-mlong-calls
endif

# Fix building on armhf
ifneq (,$(filter $(DEB_HOST_ARCH),armhf))
  export DEB_CXXFLAGS_MAINT_STRIP=-O2
  export DEB_CXXFLAGS_MAINT_APPEND=-O1
endif

PY2VERS := $(shell pyversions -vd)
PY3VERS := $(shell py3versions -vd)

%:
	dh $@

override_dh_auto_configure:
	dh_auto_configure --builddirectory=build-generator -- ../generator/generator.pro
	for i in $(PY2VERS) $(PY3VERS); do \
		export PYTHON_VERSION=$${i}; \
		dh_auto_configure --builddirectory=build-py$${i} -- LIBS+=-L$$PWD/build-py$${i}/lib ../PythonQt.pro; \
	done

override_dh_auto_build:
	# Build the generator
	dh_auto_build --builddirectory=build-generator

	# Run the generator to create wrapper code for Qt classes. The generator
	# will look for folders under QTDIR/include. To easily create this, symlink
	# the correct include folder there.
	cd generator; \
	mkdir -p qtdir; \
	ln -s `pkg-config --variable=includedir Qt5Core` qtdir/include; \
	QTDIR=qtdir ../build-generator/pythonqt_generator qtscript_masterinclude.h build_all.txt

	for i in $(PY2VERS) $(PY3VERS); do \
		export PYTHON_VERSION=$${i}; \
		dh_auto_build --builddirectory=build-py$${i} -- sub-src; \
		dh_auto_build --builddirectory=build-py$${i} -- sub-extensions; \
	done

override_dh_auto_clean:
	rm -rf build-py*
	rm -rf generated_cpp
	rm -rf generator/qtdir
	rm -f generator/.preprocessed.tmp
	rm -f generator/*.log
	dh_auto_clean
