#############################################################
# Makefile to build PROMETHEUS in toolchain
#
#

TOPDIR=..
LIBNAME=prometheus-cpp
include $(TOPDIR)/Makefiles/Repo-defines.mk

#
# Prometheus-cpp Version taken from CMakeList.txt File:
# 
#
PROMETHEUS_DIR_NAME=prometheus-cpp-1.1.0
PROMETHEUS_TARBALL=prometheus-cpp-1.1.0.tar.gz
PROMETHEUS_SOVER=1.1.0
PROMETHEUS_DIR=$(realpath $(TOPDIR)/prometheus-cpp)

.PHONY: all untar config build  clean 

fulldist: all

all:
	$(MKDIR) $(PRECOMPILED_DIR)
	$(MAKE) untar
	$(MAKE) patch
	$(MAKE) config
	$(MAKE) build

untar:
	@echo ""
	@echo "=== Untarring $(LIBNAME)"
	@echo ""
	rm -rf $(PROMETHEUS_DIR_NAME)
	mkdir -p $(PRECOMPILED_DIR) include
	tar -xvzf $(PROMETHEUS_TARBALL)
	mv prometheus-cpp-with-submodules $(PROMETHEUS_DIR_NAME)
	@echo ""
	@echo "=== Untarring $(LIBNAME): done!"
	@echo ""

patch:
	@echo "=== Applying patches to $(LIBNAME)"
	patch -p0 < ./patches/prometheus_cpp_1.1.0_change_thread_name.patch

config:
	@echo ""
	@echo "=== Configuring $(LIBNAME)"
	@echo ""
	cd $(PROMETHEUS_DIR_NAME) && \
	    mkdir _build && \
	    cmake3 -B_build -DBUILD_SHARED_LIBS=ON  -DENABLE_PUSH=OFF -DENABLE_TESTING=OFF -DENABLE_COMPRESSION=OFF
	@echo ""
	@echo "=== Configuring $(LIBNAME): done!"
	@echo ""	
    

build:
	@echo ""
	@echo "=== Building $(LIBNAME) and installing into $(PRECOMPILED_DIR)"
	@echo ""
	cd $(PROMETHEUS_DIR_NAME) && \
		cmake3 --build _build --target package --parallel $(NUM_PROCS) && \
		mkdir -p ../include/prometheus
	cp -fv $(PROMETHEUS_DIR_NAME)/_build/lib/libprometheus-cpp-core.so*      ./$(PRECOMPILED_DIR)
	cp -fv $(PROMETHEUS_DIR_NAME)/_build/lib/libprometheus-cpp-pull.so*      ./$(PRECOMPILED_DIR)
	cp -fvr $(PROMETHEUS_DIR_NAME)/core/include/prometheus/*                 ./include/prometheus
	cp -fvr $(PROMETHEUS_DIR_NAME)/pull/include/prometheus/*                 ./include/prometheus
	cp -fvr $(PROMETHEUS_DIR_NAME)/_build/core/include/prometheus/detail/*   ./include/prometheus/detail
	cp -fvr $(PROMETHEUS_DIR_NAME)/_build/pull/include/prometheus/detail/*   ./include/prometheus/detail
	@$(LIBLINKS) $(PRECOMPILED_DIR)/*.so*
	@echo ""
	@echo "=== Building and Installing $(LIBNAME): done!"
	@echo ""

clean_build_intermediate_artifacts:
	rm -rf $(PROMETHEUS_DIR_NAME)/_build
	@echo ""
	@echo "Cleaning Build Intermediate Artifacts: done!"
	@echo ""

clean_build_outputs: clean
	rm -f $(PRECOMPILED_DIR)/*.so*
	rm -rf include/*
	@echo ""
	@echo "=== Cleaning Build Outputs: done!"
	@echo ""
	

include $(TOPDIR)/Makefiles/ThirdPartyTargetHelpers.mk
