#!/usr/bin/make -f
# See debhelper(7) (uncomment to enable)
# output every command that modifies files on the build system.
#export DH_VERBOSE = 1


# see FEATURE AREAS in dpkg-buildflags(1)
#export DEB_BUILD_MAINT_OPTIONS = hardening=+all

# see ENVIRONMENT in dpkg-buildflags(1)
# package maintainers to append CFLAGS
#export DEB_CFLAGS_MAINT_APPEND  = -Wall -pedantic
# package maintainers to append LDFLAGS
#export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed

ROOT_DIR:=$(shell pwd)
export DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
export DEB_HOST_ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)
export BUILDDIR=build
export NNSTREAMER_SOURCE_ROOT_PATH=$(ROOT_DIR)
export NNSTREAMER_BUILD_ROOT_PATH=${NNSTREAMER_SOURCE_ROOT_PATH}/${BUILDDIR}
export GST_PLUGIN_PATH=${NNSTREAMER_BUILD_ROOT_PATH}/gst:${NNSTREAMER_BUILD_ROOT_PATH}/ext
export NNSTREAMER_CONF=${NNSTREAMER_BUILD_ROOT_PATH}/nnstreamer-test.ini
export NNSTREAMER_FILTERS=${NNSTREAMER_BUILD_ROOT_PATH}/ext/nnstreamer/tensor_filter
export NNSTREAMER_DECODERS=${NNSTREAMER_BUILD_ROOT_PATH}/ext/nnstreamer/tensor_decoder
export NNSTREAMER_CONVERTERS=${NNSTREAMER_BUILD_ROOT_PATH}/ext/nnstreamer/tensor_converter
export NNSTREAMER_TRAINERS=${NNSTREAMER_BUILD_ROOT_PATH}/ext/nnstreamer/tensor_trainer
export PYTHONIOENCODING=utf-8
ifeq ($(DEB_BUILD_ARCH_CPU), arm)
FLOAT16 := -Denable-float16=true
endif
ifeq ($(DEB_BUILD_ARCH_CPU), arm64)
FLOAT16 := -Denable-float16=true
endif

%:
	dh $@ --parallel

override_dh_auto_clean:
	rm -rf ${BUILDDIR}

override_dh_auto_configure:
	mkdir -p ${BUILDDIR}
ifeq ($(shell dpkg-vendor --derives-from Ubuntu && echo yes), yes)
	meson --buildtype=plain --prefix=/usr --sysconfdir=/etc --libdir=lib/$(DEB_HOST_MULTIARCH) --bindir=lib/nnstreamer/bin --includedir=include \
	-Denable-edgetpu=true -Denable-openvino=true -Dgrpc-support=disabled -Dmqtt-support=enabled \
	-Ddatarepo-support=enabled -Donnxruntime-support=enabled \
	-Denable-tizen=false -Denable-test=true -Dinstall-test=true -Dsubplugindir=/usr/lib/nnstreamer $(FLOAT16) ${BUILDDIR}
else
	# Debian has less packages ready.
	meson --buildtype=plain --prefix=/usr --sysconfdir=/etc --libdir=lib/$(DEB_HOST_MULTIARCH) --bindir=lib/nnstreamer/bin --includedir=include \
	-Denable-edgetpu=false -Denable-openvino=false -Dgrpc-support=disabled -Dmqtt-support=enabled \
	-Denable-tizen=false -Denable-test=true -Dinstall-test=true -Dsubplugindir=/usr/lib/nnstreamer ${BUILDDIR}
endif

override_dh_auto_build:
	ninja -C ${BUILDDIR}
	# A few modules are not available in Ubuntu 22.04. Don't try to create them if not available.
	if [ -f './build/ext/nnstreamer/tensor_filter/libnnstreamer_filter_nnfw.so' ]; then echo "NNFW exists" ; else rm debian/nnstreamer-nnfw.install; fi
	if [ -f './build/ext/nnstreamer/tensor_filter/libnnstreamer_filter_pytorch.so' ]; then echo "pytorch exists" ; else rm debian/nnstreamer-pytorch.install; fi
	if [ -f './build/ext/nnstreamer/tensor_filter/libnnstreamer_filter_caffe2.so' ]; then echo "caffe2 exists" ; else rm debian/nnstreamer-caffe2.install; fi

override_dh_auto_test:
	./packaging/run_unittests_binaries.sh ./tests
	LD_LIBRARY_PATH=${NNSTREAMER_BUILD_ROOT_PATH}/tests/nnstreamer_edge ./packaging/run_unittests_binaries.sh ./tests/nnstreamer_edge
	./packaging/run_unittests_binaries.sh ./tests/nnstreamer_datarepo
ifeq ($(shell dpkg-vendor --derives-from Ubuntu && echo yes), yes)
ifeq ($(DEB_HOST_ARCH), amd64)
	if [ -f './tests/tizen_nnfw_runtime/unittest_nnfw_runtime_raw' ]; then ./packaging/run_unittests_binaries.sh ./tests/tizen_nnfw_runtime/unittest_nnfw_runtime_raw; fi
endif
	cd tests && ssat -n -p=1 && cd ..
else
	echo "Skipping SSAT test because it's not in Debian."
endif

override_dh_auto_install:
	DESTDIR=$(CURDIR)/debian/tmp ninja -C ${BUILDDIR} install

override_dh_install:
	dh_install --sourcedir=debian/tmp --list-missing
# Add --fail-missing option after adding *.install files for all subpackages.


override_dh_clean:
	dh_clean $@
# Prepare SUBSTVARS for Linux distro differences
ifeq ($(shell dpkg-vendor --derives-from Ubuntu && echo yes), yes)
	# Default debian/control targets Xenial.
	# If you want different control for Bionic or later,
	# you may create another control and copy it here.
	cp debian/control.ubuntu.ppa debian/control
else
	cp debian/control.debian debian/control
endif

override_dh_shlibdeps:
	dh_shlibdeps --exclude=libtensorflow2-lite-custom.so
