MODULES+=	opentracing

MODULE_SUMMARY_opentracing=	3rd-party OpenTracing dynamic module

include $(CONTRIB)/src/opentracing-cpp/version
include $(CONTRIB)/src/nginx-opentracing/version
include $(CONTRIB)/src/thrift/version
include $(CONTRIB)/src/jaeger-client-cpp/version

MODULE_VERSION_opentracing=	$(NGINX_OPENTRACING_VERSION)
MODULE_RELEASE_opentracing=	1

MODULE_VERSION_PREFIX_opentracing=$(MODULE_TARGET_PREFIX)

MODULE_SOURCES_opentracing=	opentracing-cpp-$(OPENTRACING_CPP_VERSION).tar.gz \
				nginx-opentracing-$(NGINX_OPENTRACING_VERSION).tar.gz \
				thrift-$(THRIFT_VERSION).tar.gz \
				jaeger-client-cpp-$(JAEGER_CLIENT_CPP_VERSION).tar.gz

MODULE_PATCHES_opentracing=	$(CONTRIB)/src/opentracing-cpp/CMakeLists.txt.patch \
				$(CONTRIB)/src/opentracing-cpp/fix-ftbfs-glibc-2.34.patch \
				$(CONTRIB)/src/jaeger-client-cpp/CMakeLists.txt.patch \
				$(CONTRIB)/src/thrift/CMakeLists.txt.patch \
				$(CONTRIB)/src/thrift/build-cmake-Config.cmake.in.patch \
				$(CONTRIB)/src/thrift/build-cmake-ThriftMacros.cmake.patch \
				$(CONTRIB)/src/thrift/lib-cpp-CMakeLists.txt.patch

MODULE_CONFARGS_opentracing=	--add-dynamic-module=$(MODSRC_PREFIX)nginx-opentracing-$(NGINX_OPENTRACING_VERSION)/opentracing

.deps-module-opentracing:
	cd $(CONTRIB) && make \
		.sum-opentracing-cpp \
		.sum-nginx-opentracing \
		.sum-thrift \
		.sum-jaeger-client-cpp
	touch $@

MODULE_BUILD_DEPENDS_opentracing=,cmake,libyaml-cpp-dev,libboost-dev

define MODULE_PREBUILD_opentracing
	cd $$(BUILDDIR_nginx)/extra/opentracing-cpp-$(OPENTRACING_CPP_VERSION) \&\& \
		mkdir .build \&\& \
		cd .build \&\& \
		cmake ../ \&\& \
		DESTDIR=$$(BUILDDIR_nginx)/extra/opentracing-cpp-$(OPENTRACING_CPP_VERSION) \
		$$(MAKE) -j$$(NUMJOBS) install
	cd $$(BUILDDIR_nginx)/extra/thrift-$(THRIFT_VERSION) \&\& \
		mkdir .build \&\& \
		cd .build \&\& \
		cmake \
			-DBUILD_TESTING=OFF \
			-DBUILD_TUTORIALS=OFF \
			-DBUILD_COMPILER=OFF \
			-DBUILD_C_GLIB=OFF \
			-DBUILD_HASKELL=OFF \
			-DBUILD_JAVA=OFF \
			-DBUILD_PYTHON=OFF \
			-DWITH_QT4=OFF \
			-DWITH_QT5=OFF \
			-DWITH_SHARED_LIB=OFF \
			-DWITH_PLUGIN=OFF \
			-DWITH_LIBEVENT=OFF \
			-DWITH_OPENSSL=OFF \
			-DWITH_ZLIB=OFF \
			-DBUILD_SHARED_LIBS=ON \
			-DCMAKE_INSTALL_LIBDIR=lib \
		../ \&\& \
		DESTDIR=$$(BUILDDIR_nginx)/extra/thrift-$(THRIFT_VERSION) \
		$$(MAKE) -j$$(NUMJOBS) install
	cd $$(BUILDDIR_nginx)/extra/jaeger-client-cpp-$(JAEGER_CLIENT_CPP_VERSION) \&\& \
		mkdir .build \&\& \
		cd .build \&\& \
		OpenTracing_DIR=$$(BUILDDIR_nginx)/extra/opentracing-cpp-$(OPENTRACING_CPP_VERSION)/usr/local/lib/cmake/OpenTracing \
		thrift_DIR=$$(BUILDDIR_nginx)/extra/thrift-$(THRIFT_VERSION)/usr/local/lib/cmake/thrift \
		cmake \
			-DJAEGERTRACING_BUILD_EXAMPLES=OFF \
			-DBUILD_TESTING=OFF \
			-DJAEGERTRACING_PLUGIN=ON \
			-DCMAKE_BUILD_TYPE=Release \
			-DHUNTER_CONFIGURATION_TYPES=Release \
			-DOPENTRACING_INC=$$(BUILDDIR_nginx)/extra/opentracing-cpp-$(OPENTRACING_CPP_VERSION)/usr/local/include \
		../ \&\& \
		$$(MAKE) -j$$(NUMJOBS)
endef
export MODULE_PREBUILD_opentracing

MODULE_CC_OPT_opentracing=-I$$\(BUILDDIR_nginx\)/extra/opentracing-cpp-$(OPENTRACING_CPP_VERSION)/usr/local/include
MODULE_CC_OPT_DEBUG_opentracing=$(MODULE_CC_OPT_opentracing)
MODULE_LD_OPT_opentracing=-L$$\(BUILDDIR_nginx\)/extra/opentracing-cpp-$(OPENTRACING_CPP_VERSION)/usr/local/lib
MODULE_LD_OPT_DEBUG_opentracing=$(MODULE_LD_OPT_opentracing)

define MODULE_PREINSTALL_opentracing
	mkdir -p $$(INSTALLDIR)/usr/lib/$$(DEB_HOST_MULTIARCH)
	install -m755 $$(BUILDDIR_nginx)/extra/opentracing-cpp-$(OPENTRACING_CPP_VERSION)/usr/local/lib/libopentracing.so.$(OPENTRACING_CPP_VERSION) $$(INSTALLDIR)/usr/lib/$$(DEB_HOST_MULTIARCH)
	ln -fs libopentracing.so.$(OPENTRACING_CPP_VERSION) $$(INSTALLDIR)/usr/lib/$$(DEB_HOST_MULTIARCH)/libopentracing.so.1
	ln -fs libopentracing.so.1 $$(INSTALLDIR)/usr/lib/$$(DEB_HOST_MULTIARCH)/libopentracing.so
	install -m755 $$(BUILDDIR_nginx)/extra/opentracing-cpp-$(OPENTRACING_CPP_VERSION)/usr/local/lib/libopentracing_mocktracer.so.$(OPENTRACING_CPP_VERSION) $$(INSTALLDIR)/usr/lib/$$(DEB_HOST_MULTIARCH)
	ln -fs libopentracing_mocktracer.so.$(OPENTRACING_CPP_VERSION) $$(INSTALLDIR)/usr/lib/$$(DEB_HOST_MULTIARCH)/libopentracing_mocktracer.so.1
	ln -fs libopentracing_mocktracer.so.1 $$(INSTALLDIR)/usr/lib/$$(DEB_HOST_MULTIARCH)/libopentracing_mocktracer.so
	mkdir -p $$(INSTALLDIR)/usr/include
	cp -pr $$(BUILDDIR_nginx)/extra/opentracing-cpp-$(OPENTRACING_CPP_VERSION)/usr/local/include/opentracing $$(INSTALLDIR)/usr/include/
	mkdir -p $$(INSTALLDIR)/usr/libexec/opentracing
	install -m755 $$(BUILDDIR_nginx)/extra/jaeger-client-cpp-$(JAEGER_CLIENT_CPP_VERSION)/.build/libjaegertracing_plugin.so $$(INSTALLDIR)/usr/libexec/opentracing/
endef
export MODULE_PREINSTALL_opentracing

define MODULE_POST_opentracing
cat <<BANNER
----------------------------------------------------------------------

The $(MODULE_SUMMARY_opentracing) for $(MODULE_SUMMARY_PREFIX) has been installed.
To enable this module, add the following to /etc/nginx/nginx.conf
and reload nginx:

    load_module modules/ngx_http_opentracing_module.so;

Note that additional tracer plugins are required in order
to communicate with corresponding services (e.g. Zipkin, Jaeger).
This package includes the Jaeger dynamic tracer plugin:

    /usr/libexec/opentracing/libjaegertracing_plugin.so

Please refer to the module documentation for further details:
https://github.com/opentracing-contrib/nginx-opentracing
https://github.com/opentracing-contrib/nginx-opentracing/blob/master/doc/Reference.md

OpenTracing project site (general info and documentation):
https://opentracing.io/

----------------------------------------------------------------------
BANNER
endef
export MODULE_POST_opentracing
