#############################################################
# Makefile to
# - extract sources out of the 6WindGate tarball
# - apply some additional configuration
# - build whole DPDK framework
# - get libraries ready for Conan packaging

TOPDIR=..
REALTOP=$(shell readlink -f $(TOPDIR))
LIB_NAME=6windgate-x86_64-redhat-8-empirix-v
LIB_VER=5.6.0.ga
LIBNAME=6windgate-dpdk
LIB_NAME_MELLANOX_RDMA=6windgate-dpdk-pmd-mellanox-rdma-core
USERTOOLS=${LIB_NAME}${LIB_VER}/tools-build-framework/sources/dpdk-21.11/usertools/


include $(TOPDIR)/Makefiles/Repo-defines.mk

# required for RPM deployment in Nexus repository during TeamCity building
include ${EMPIRIX_PIPELINE_FRAMEWORK_MAKEFILE_SUPPORT_DIR}/Version-defines.mk
include ${EMPIRIX_PIPELINE_FRAMEWORK_MAKEFILE_SUPPORT_DIR}/Deploy-defines.mk

.PHONY: clean distclean


all: 
	@echo "Nothing to do. Everything is built offline/precompiled for 6wind"
	# now take the full list of DPDK linker flags and move them into our support makefile:
	__DPDK_DYNAMIC_LIBS_LIST__="$$(cd docker-release && ls -1 librte_*.so | sed 's@lib@@g' | sed 's@.so@@g' | tr '\n' ' ')" \
		j2 6WindGateDpdk-support.mk.j2 -o 6WindGateDpdk-support.mk

# these targets are currently run manually because they take a lot of disk (around 1.5GB) and a lot of time (around 45min)

VOLUMES_AND_ENV:= \
	--volume $(REALTOP):/source \
	--volume /opt/empirix/conan:/opt/empirix/conan \
	--volume $(REALTOP)/6wind/include:/out-headers/ \
	--volume $(REALTOP)/6wind/docker-release/:/out-libraries \
	--env NUM_CPUS=$(NUM_CPUS) \
	--workdir /source/6wind

rebuild_6windgate_dpdk_binaries:
	# Libraries will end up inside the standard 'docker-release' folder
	docker run \
		$(VOLUMES_AND_ENV) \
		--entrypoint=/source/6wind/rebuild-dpdk-binaries-docker-bootstrap.sh \
		$(BUILDER_DOCKER_IMAGE_FOR_RUN)
	# IMPORTANT: some DPDK shared libraries need to load other .so files (e.g. librte_ethdev.so.22 depends from librte_kvargs.so.22)
	#            and since Fedora35 the dynamic linker wants every .so location explicitly mentioned as RPATH... so to simplify
	#            deployments, we just tell the linker to look in the same folder where
	#            e.g. librte_ethdev.so.22 is present to find the associated file librte_kvargs.so.22
	for thelib in $$(ls docker-release/*.so); do \
		echo "Patching $$thelib by setting as RPATH the special ORIGIN path" ; \
		patchelf --set-rpath '$$ORIGIN' $$thelib ; \
	done

bash_in_6windgate_dpdk_builder:
	docker run \
		-it \
		$(VOLUMES_AND_ENV) \
		$(BUILDER_DOCKER_IMAGE_FOR_RUN)

clean_build_intermediate_artifacts:
	rm -rf ./${LIB_NAME}$(LIB_VER)/

clean_build_outputs:
	rm -rf include/*
	rm -f $(PRECOMPILED_DIR)/*.a

HAS_INSTALL_SUPPORT=2
include $(TOPDIR)/Makefiles/ThirdPartyTargetHelpers.mk
include ${EMPIRIX_PIPELINE_FRAMEWORK_MAKEFILE_SUPPORT_DIR}/BuilderDocker-targets.mk

install_libs::
ifndef DESTDIR
	@echo "*** ERROR: please call this makefile supplying explicitly the DESTDIR variable"
	@exit 1
endif
	@echo "Installing $(LIBNAME) third party library to $(DESTDIR)"
	@mkdir --parents										  $(DESTDIR)
	@cp -afv $(VERBOSE_FLAG) docker-release/lib*.so*      $(DESTDIR)

install_headers::
ifndef DESTDIR
	@echo "*** ERROR: please call this makefile supplying explicitly the DESTDIR variable"
	@exit 1
endif
	@echo "Installing $(LIBNAME) third party library header files to $(DESTDIR)"
	@mkdir --parents                        $(DESTDIR)/dpdk
	@cp -afr $(VERBOSE_FLAG) include/*      $(DESTDIR)/dpdk/
