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

export PREFIX := /opt/mellanox/dpdk

# see EXAMPLES in dpkg-buildflags(1) and read /usr/share/dpkg/*
DPKG_EXPORT_BUILDFLAGS = 1
export DEB_BUILD_MAINT_OPTIONS = hardening=+all
include /usr/share/dpkg/default.mk

DPDK_ABI := $(shell echo $(DEB_VERSION_UPSTREAM) | cut -d '-' -f1 | cut -d '.'  -f1-2 | cut -d '~' -f1)
DPDK_DRIVER_DIR ?= dpdk-$(DPDK_ABI)-drivers

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

# see ENVIRONMENT in dpkg-buildflags(1)
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk

# Support backporting to jessie
dpkg_version_lt = $(shell dpkg --compare-versions $$(dpkg --status dpkg-dev | grep Version | cut -d' ' -f2) lt-nl "1.18.11" && echo yes)
ifeq (yes, $(dpkg_version_lt))
	# package maintainers to append CFLAGS
	# For Debian, dpkg-dev >= 1.18.11 and gcc 6.x, dpkg-buildflags and gcc
	# handle -fPIC and related flags relying on specs files
	# (/usr/share/dpkg/*specs)
	export DEB_CFLAGS_MAINT_APPEND  = -fPIC
	export EXTRA_CFLAGS=$(CFLAGS)
	export EXTRA_CPPFLAGS=$(CPPFLAGS)
	export HOST_EXTRA_CFLAGS=$(CFLAGS)
	export HOST_EXTRA_CPPFLAGS=$(CPPFLAGS)
	# need to be stripped as DPDK build system adds them and it would be -Wl,-Wl,opt
	export EXTRA_LDFLAGS=$(shell echo $(LDFLAGS) | sed 's/-Wl,//g')
	# HOST_CC build system does not add -Wl ...
	export EXTRA_HOST_LDFLAGS=$(LDFLAGS)
endif

export RTE_DEVEL_BUILD=n
export EXTRA_CFLAGS+=-g

# People rebuilding this package can overwrite DPDK_CONFIG, RTE_MACHINE and
# RTE_TARGET via DEB_BUILD_OPTIONS if they like
ifneq (,$(filter dpdk_config=%,$(DEB_BUILD_OPTIONS)))
    DPDK_CONFIG ?= $(patsubst dpdk_config=%,%,$(filter dpdk_config=%,$(DEB_BUILD_OPTIONS)))
endif

ifeq (,$(findstring terse,$(DEB_BUILD_OPTIONS)))
	export DH_VERBOSE=1
	export DH_OPTIONS=-v
endif

# People rebuilding this package can overwrite RTE_MACHINE
# via DEB_BUILD_OPTIONS if they like
ifneq (,$(filter rte_machine=%,$(DEB_BUILD_OPTIONS)))
    RTE_MACHINE ?= $(patsubst rte_machine=%,%,$(filter rte_machine=%,$(DEB_BUILD_OPTIONS)))
endif
ifneq (,$(filter rte_target=%,$(DEB_BUILD_OPTIONS)))
    RTE_TARGET ?= $(patsubst rte_target=%,%,$(filter rte_target=%,$(DEB_BUILD_OPTIONS)))
endif

ifneq (,$(filter $(DEB_HOST_ARCH), arm64))
DPDK_CONFIG ?= "arm64-bluefield-linux-gcc"
RTE_MACHINE ?= "armv8a"
RTE_TARGET ?= "arm64-bluefield-linux-gcc"
INCLUDE_ARCH := arm
else
ifneq (,$(filter $(DEB_HOST_ARCH), ppc64el))
DPDK_CONFIG ?= "ppc_64-power8-linux-gcc"
RTE_MACHINE ?= "power8"
RTE_TARGET ?= "ppc_64-power8-linux-gcc"
INCLUDE_ARCH := ppc_64
else
DPDK_CONFIG ?= "$(DEB_HOST_GNU_CPU)-native-linux-gcc"
RTE_MACHINE ?= "default"
RTE_TARGET ?= "$(DEB_HOST_GNU_CPU)-native-linux-gcc"
INCLUDE_ARCH := x86
endif
endif
DPDK_STATIC_DIR = "debian/build/static-root"
DPDK_SHARED_DIR = "debian/build/shared-root"

# now stable with parallel comilation, so support -j
ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
    PAR := $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
    MAKEFLAGS += -j$(PAR)
endif

ifneq (,$(findstring nodoc,$(DEB_BUILD_PROFILES)))
BUILD_DOCS=n
else
ifneq (,$(findstring nodoc,$(DEB_BUILD_OPTIONS)))
BUILD_DOCS=n
# to stop dh_installdoc from running
export DEB_BUILD_PROFILES += nodoc
else
BUILD_DOCS=y
endif
endif

ifneq (,$(findstring nostatic,$(DEB_BUILD_OPTIONS)))
CONFIG_RTE_BUILD_SHARED_LIB=n
TARGET := $(DPDK_STATIC_DIR)
else
CONFIG_RTE_BUILD_SHARED_LIB=y
TARGET := $(DPDK_SHARED_DIR)
endif
CONFIG_RTE_MAX_ETHPORTS=256

%:
	dh $@ --with python3,systemd

override_dh_auto_clean:
	rm -rf debian/build debian/tmp debian/dpdk-modules-* \
		debian/control.modules debian/VERSION

override_dh_auto_configure:
	# Add support for a custom defconfig file in the debian directory.
ifneq (,$(filter dpdk_config=%,$(DEB_BUILD_OPTIONS)))
	test -e config/defconfig_$(DPDK_CONFIG) || ln -fs ../debian/defconfig_$(DPDK_CONFIG) config/defconfig_$(DPDK_CONFIG)
endif
	# report dpkg-buildflags status to build log
	dpkg-buildflags --status
	echo EXTRA_CFLAGS $$EXTRA_CFLAGS
	echo EXTRA_LDFLAGS: $$EXTRA_LDFLAGS
	$(MAKE) V=$(DH_VERBOSE) O=$(TARGET) T=$(DPDK_CONFIG) config
ifeq (,$(filter dpdk_config=%,$(DEB_BUILD_OPTIONS)))
	sed -ri -e 's,(RTE_MACHINE=).*,\1$(RTE_MACHINE),' \
		-e 's,(CONFIG_RTE_LIBRTE_MLX5_PMD=).*,\1y,' \
		-e 's,(RTE_NEXT_ABI=).*,\1n,' \
		-e 's,(CONFIG_RTE_EAL_IGB_UIO=).*,\1n,' \
		-e 's,(CONFIG_RTE_LIBRTE_KNI=).*,\1n,' \
		-e 's,(CONFIG_RTE_KNI_KMOD=).*,\1n,' \
		-e 's,(LIBRTE_PMD_PCAP=).*,\1y,' \
		-e 's,(CONFIG_RTE_EAL_PMD_PATH=).*,\1"$(PREFIX)/lib/$(DPDK_DRIVER_DIR)/",' \
		-e 's,(CONFIG_RTE_BUILD_SHARED_LIB=).*,\1$(CONFIG_RTE_BUILD_SHARED_LIB),' \
		-e 's,(CONFIG_RTE_MAX_ETHPORTS=).*,\1$(CONFIG_RTE_MAX_ETHPORTS),' \
		$(TARGET)/.config
endif
	echo "CONFIG_RTE_MAJOR_ABI=\"$(DPDK_ABI)\"" >> \
		$(TARGET)/.config
	cp $(TARGET)/.config .config
#	dh_auto_configure

override_dh_auto_build-indep:
ifeq (y,$(BUILD_DOCS))
	$(MAKE) V=$(DH_VERBOSE) O=$(TARGET) doc-api-html doc-guides-html
	# NINJA DOC INSTALL DOES NOT WORK - .buildinfo present, css missing
	rm -f obj-$(DEB_HOST_MULTIARCH)/doc/guides/guides/.buildinfo
endif

override_dh_auto_install-indep:
	# Package: mlnx-dpdk-doc
	# All files based on the install-doc rule (includes examples)
ifeq (y,$(BUILD_DOCS))
	$(MAKE) V=$(DH_VERBOSE) O=$(TARGET) prefix=$(PREFIX) \
		DESTDIR=debian/mlnx-dpdk-doc install-doc
endif

override_dh_auto_build-arch:

	echo TARGET = $(TARGET)
	$(MAKE) V=$(DH_VERBOSE) O=$(TARGET)
#ifeq (y,$(BUILD_DOCS))
 	# need to be around for dh_installman to be picked up
# 	$(MAKE) V=$(DH_VERBOSE) O=$(TARGET) doc-guides-man
#endif
	$(MAKE) V=$(DH_VERBOSE) O=$(TARGET) test-build

override_dh_auto_install-arch: LIBDIR=$(PREFIX)/lib
override_dh_auto_install-arch:

	# Package: dpdk (runtime)
	$(MAKE) V=$(DH_VERBOSE) O=$(TARGET) prefix=$(PREFIX) libdir=$(LIBDIR) \
		DESTDIR=debian/mlnx-dpdk install-runtime

	$(MAKE) V=$(DH_VERBOSE) O=$(TARGET) prefix=$(PREFIX) libdir=$(LIBDIR) \
		DESTDIR=debian/mlnx-dpdk install
	mkdir -p debian/mlnx-dpdk/etc/mlnx-dpdk
	cp debian/mlnx-dpdk.interfaces debian/mlnx-dpdk/etc/mlnx-dpdk/interfaces
	mkdir -p debian/mlnx-dpdk$(PREFIX)/sbin
	rm -f debian/mlnx-dpdk$(PREFIX)/sbin/dpdk-devbind
	ln -s $(PREFIX)/share/dpdk/usertools/dpdk-devbind.py \
		debian/mlnx-dpdk$(PREFIX)/sbin/dpdk-devbind
	if [ -d debian/mlnx-dpdk/usr/share ] ; then \
		mkdir -p debian/mlnx-dpdk$(PREFIX)/share; \
		rsync -av debian/mlnx-dpdk/usr/share/* debian/mlnx-dpdk$(PREFIX)/share; \
		rm -rf debian/mlnx-dpdk/usr; \
	fi
	mkdir -p debian/mlnx-dpdk/etc/ld.so.conf.d
	echo "$(PREFIX)/lib" > debian/mlnx-dpdk/etc/ld.so.conf.d/mlnx-dpdk.conf

	# Package: mlnx-dpdk-dev (build environment)
	# workaround to fix symbolic link creation
	mkdir -p debian/mlnx-dpdk-dev$(PREFIX)/share/dpdk
	mkdir -p debian/mlnx-dpdk-dev/$(LIBDIR)
	$(MAKE) V=$(DH_VERBOSE) O=$(TARGET) prefix=/usr libdir=$(LIBDIR) \
		DESTDIR=debian/mlnx-dpdk-dev install-sdk
	sed -e 's/SDK_TARGET/$(RTE_TARGET)/' debian/dpdk-sdk-env.sh.in > \
		debian/mlnx-dpdk-dev$(PREFIX)/share/dpdk/dpdk-sdk-env.sh
	# include the bundled autotest suite to be usable from the dpdk-dev package
	if test -e $(TARGET)/.config; then \
	if grep -qs 'CONFIG_RTE_APP_TEST=y' $(TARGET)/.config; then \
		mkdir -p debian/mlnx-dpdk-dev$(PREFIX)/share/dpdk/test; \
		cp -a test/test/autotest* debian/mlnx-dpdk-dev$(PREFIX)/share/dpdk/test; \
		cp $(TARGET)/app/test debian/mlnx-dpdk-dev$(PREFIX)/share/dpdk/test/; \
		cp $(TARGET)/app/testacl debian/mlnx-dpdk-dev$(PREFIX)/share/dpdk/test/; \
		cp $(TARGET)/app/testpipeline debian/mlnx-dpdk-dev$(PREFIX)/share/dpdk/test/; \
	fi; \
	fi
	rm -rf debian/mlnx-dpdk-dev$(PREFIX)/include
	mkdir -p debian/mlnx-dpdk-dev$(PREFIX)/include
	rsync -av debian/mlnx-dpdk$(PREFIX)/include/* debian/mlnx-dpdk-dev$(PREFIX)/include/
	rm -rf debian/mlnx-dpdk$(PREFIX)/include
	rsync -av debian/mlnx-dpdk$(PREFIX)/share/* debian/mlnx-dpdk-dev$(PREFIX)/share/
	rm -rf debian/mlnx-dpdk$(PREFIX)/share
	mkdir -p debian/mlnx-dpdk-dev/$(LIBDIR)
	mv debian/mlnx-dpdk/$(LIBDIR)/libdpdk.so debian/mlnx-dpdk-dev/$(LIBDIR)/
	mv $(TARGET)/lib/*.so debian/mlnx-dpdk-dev/$(LIBDIR)/
	rm -f debian/mlnx-dpdk/$(LIBDIR)/*.so
	# genereate the preinst to deal with the symlink -> directory transition smoothly
	sed "s/@@RTE_TARGET@@/$(RTE_TARGET)/g" debian/mlnx-dpdk-dev.preinst.in > debian/mlnx-dpdk-dev.preinst
	if [ -d debian/mlnx-dpdk-dev/usr/share ] ; then \
		mkdir -p debian/mlnx-dpdk-dev$(PREFIX)/share; \
		rsync -av debian/mlnx-dpdk-dev/usr/share/* debian/mlnx-dpdk-dev$(PREFIX)/share; \
		rm -rf debian/mlnx-dpdk-dev/usr; \
	fi
	ln -snf ../../../lib debian/mlnx-dpdk-dev$(PREFIX)/share/dpdk/*linux-gcc/lib
	mkdir -p debian/mlnx-dpdk-doc$(PREFIX)/share/doc
	rsync -av debian/mlnx-dpdk-dev$(PREFIX)/share/doc/* debian/mlnx-dpdk-doc$(PREFIX)/share/doc/
	rm -rf debian/mlnx-dpdk-dev$(PREFIX)/share/doc
	mkdir -p debian/mlnx-dpdk-doc$(PREFIX)/share/dpdk/examples
	rsync -av debian/mlnx-dpdk-dev$(PREFIX)/share/dpdk/examples/* debian/mlnx-dpdk-doc$(PREFIX)/share/dpdk/examples/
	rm -rf debian/mlnx-dpdk-dev$(PREFIX)/share/dpdk/examples

override_dh_shlibdeps:
	dh_shlibdeps -l$(PREFIX)/lib

override_dh_installinit:
	dh_installinit --no-start --no-stop-on-upgrade

override_dh_systemd_start:
	dh_systemd_start --no-start --no-stop-on-upgrade

override_dh_auto_test:

override_dh_python3:
	# dh_python only looks in /usr/share/package_name but dpdk-doc installs in
	# /usr/share/dpdk, so pass /usr to catch all
	# dh_python3 --shebang=$(PREFIX)/bin/python3 $(PREFIX)
