#############################################################
# Makefile to build curl in toolchain
#
#

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


ifeq ($(BUILD),debug-gcov)
	EXTRA_CFLAGS=-g -O0
	RUNSTRIP=ls
else
	# to forcefully disable debug symbol generation, we feed -g0 as last option on the commandline
	EXTRA_CFLAGS=-g0 -O3
	RUNSTRIP=strip -S
endif

#
# Curl Version taken from Pulsar Docker File:
# pulsar-client-cpp/pulsar-2.6.2/pulsar-client-cpp/docker/Dockerfile
#
CURL_DIR_NAME=curl-7.61.0
CURL_TARBALL=curl-7.61.0.tar.gz
CURL_SOVER=4.5.0
CURL_DIR=$(realpath $(TOPDIR)/curl)

.PHONY: all untar config build  clean 

fulldist: all

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

untar:
	@echo ""
	@echo "=== Untarring $(LIBNAME)"
	@echo ""
	rm -rf $(CURL_DIR_NAME)
	mkdir -p $(PRECOMPILED_DIR) include
	tar -xvzf $(CURL_TARBALL)
	@echo ""
	@echo "=== Untarring $(LIBNAME): done!"
	@echo ""

build:
	@echo ""
	@echo "=== Building $(LIBNAME) and installing into $(PRECOMPILED_DIR)"
	@echo ""
	cd $(CURL_DIR_NAME) && \
		CFLAGS=-fPIC TARGET=CURL::libcurl \
		./configure --with-ssl --prefix=$(CURL_DIR)/tmp-install && \
			make -j$(NUM_PROCS) TARGET=CURL::libcurl  && \
			make install && \
			mkdir -p ../include/curl && \
			cp -fv ./include/curl/*.h ../include/curl && \
			cp -fv ./lib/.libs/*.so.$(CURL_SOVER) ../$(PRECOMPILED_DIR) && \
			cp -fv ./lib/.libs/*.a ../$(PRECOMPILED_DIR)
	@$(LIBLINKS) $(PRECOMPILED_DIR)/*.so*
	@echo ""
	@echo "=== Building and Installing $(LIBNAME): done!"
	@echo ""

clean_build_intermediate_artifacts:
	rm -rf $(CURL_DIR_NAME) tmp-install
	@echo ""
	@echo "Cleaning Build Intermediate Artifacts: done!"
	@echo ""

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

include $(TOPDIR)/Makefiles/ThirdPartyTargetHelpers.mk
