From cdd22a141c85585e4208e6a6c5c48e7bb2d1373e Mon Sep 17 00:00:00 2001
From: Julien Wadel <julien.wadel@belledonne-communications.com>
Date: Tue, 20 Apr 2021 19:37:50 +0200
Subject: [PATCH] Prepare the CMakeLists.txt for new Flexisp build and
 packaging scripts

* Require CMake 3.13 or later
* Adding DISABLE_BC_PACKAGE_SEARCH hidden variable
* Export linphone and linphone++ targets on configure stage only when LINPHONE_BUILDER_GROUP feature is on.

(cherry picked from commit a84369490945186fcdf0825fee9937039cb7defb)
(cherry picked from commit 2066e4d4c22c2a94510178e95da2f77825ae0f09)
---
 CMakeLists.txt                           | 49 +++++++++++++++++-------
 coreapi/CMakeLists.txt                   |  3 +-
 daemon/CMakeLists.txt                    |  4 +-
 src/CMakeLists.txt                       | 16 ++++++--
 src/account_creator/connector_xmlrpc.cpp |  3 ++
 wrappers/cpp/CMakeLists.txt              | 25 ++++++------
 6 files changed, 68 insertions(+), 32 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 863b405fb4..f1b137d697 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,6 +1,6 @@
 ############################################################################
 # CMakeLists.txt
-# Copyright (C) 2010-2019  Belledonne Communications, Grenoble France
+# Copyright (C) 2010-2021 Belledonne Communications, Grenoble France
 #
 ############################################################################
 #
@@ -20,9 +20,14 @@
 #
 ############################################################################
 
-cmake_minimum_required(VERSION 3.1)
+
+# CMake 3.13 required for new Flexisip build process.
+# See [CMP077](https://cmake.org/cmake/help/v3.13/policy/CMP0077.html).
+cmake_minimum_required(VERSION 3.13)
+
 project(linphone VERSION 5.1.0 LANGUAGES C CXX)
 
+
 set(LINPHONE_MAJOR_VERSION ${PROJECT_VERSION_MAJOR})
 set(LINPHONE_MINOR_VERSION ${PROJECT_VERSION_MINOR})
 set(LINPHONE_MICRO_VERSION ${PROJECT_VERSION_PATCH})
@@ -73,6 +78,10 @@ option(ENABLE_PACKAGE_SOURCE "Create 'package_source' target for source archive
 cmake_dependent_option(ENABLE_NOTIFY "Enable libnotify support." YES "ENABLE_GTK_UI;NOT APPLE" NO)
 cmake_dependent_option(ENABLE_ASSISTANT "Turn on assistant compiling." YES "ENABLE_GTK_UI" NO)
 
+# Hidden non-cache options:
+# * DISABLE_BC_PACKAGE_SEARCH: skip find_package() for every BC package (bctoolbox, ortp, etc.)
+# * DISABLE_SOCI_PACKAGE_SEARCH: skip find_package() for Soci.
+
 set(CMAKE_CXX_STANDARD 14)
 set(CMAKE_CXX_EXTENSIONS NO)
 
@@ -123,11 +132,13 @@ if (ENABLE_VCARD)
 	endif()
 endif()
 
-find_package(BelleSIP CONFIG REQUIRED)
-find_package(Mediastreamer2 CONFIG REQUIRED)
-find_package(ortp CONFIG REQUIRED)
-find_package(bctoolbox 0.0.3 CONFIG REQUIRED OPTIONAL_COMPONENTS tester)
-find_package(belr CONFIG REQUIRED)
+if(NOT DISABLE_BC_PACKAGE_SEARCH)
+	find_package(BelleSIP CONFIG REQUIRED)
+	find_package(Mediastreamer2 CONFIG REQUIRED)
+	find_package(ortp CONFIG REQUIRED)
+	find_package(bctoolbox 0.0.3 CONFIG REQUIRED OPTIONAL_COMPONENTS tester)
+	find_package(belr CONFIG REQUIRED)
+endif()
 
 if(ENABLE_ADVANCED_IM)
 	find_package(XercesC REQUIRED)
@@ -140,11 +151,19 @@ find_package(Sqlite3 REQUIRED)
 find_package(XML2 REQUIRED)
 
 if (ENABLE_DB_STORAGE)
-	#APPLE platform does not use dlopen for soci backend
-	if (APPLE OR ANDROID)
-		find_package(Soci REQUIRED COMPONENTS sqlite3)
+	if(NOT DISABLE_SOCI_PACKAGE_SEARCH)
+		#APPLE platform does not use dlopen for soci backend
+		if (APPLE OR ANDROID)
+			find_package(Soci REQUIRED COMPONENTS sqlite3)
+		else()
+			find_package(Soci REQUIRED)
+		endif()
 	else()
-		find_package(Soci REQUIRED)
+		set(SOCI_FOUND YES)
+		set(SOCI_LIBRARIES soci_core)
+		if (APPLE OR ANDROID)
+			set(SOCI_sqlite3_PLUGIN soci_sqlite3)
+		endif()
 	endif()
 	set(HAVE_DB_STORAGE 1)
 endif()
@@ -412,9 +431,11 @@ write_basic_package_version_file(
 	VERSION ${LINPHONE_VERSION}
 	COMPATIBILITY AnyNewerVersion
 )
-export(EXPORT ${EXPORT_TARGETS_NAME}Targets
-	FILE "${CMAKE_CURRENT_BINARY_DIR}/LinphoneTargets.cmake"
-)
+if(LINPHONE_BUILDER_GROUP_EXTERNAL_SOURCE_PATH_BUILDERS)
+	export(EXPORT ${EXPORT_TARGETS_NAME}Targets
+		FILE "${CMAKE_CURRENT_BINARY_DIR}/LinphoneTargets.cmake"
+	)
+endif()
 configure_package_config_file(cmake/LinphoneConfig.cmake.in
 	"${CMAKE_CURRENT_BINARY_DIR}/LinphoneConfig.cmake"
 	INSTALL_DESTINATION ${CONFIG_PACKAGE_LOCATION}
diff --git a/coreapi/CMakeLists.txt b/coreapi/CMakeLists.txt
index 4ddd4fb98a..6da488ea53 100644
--- a/coreapi/CMakeLists.txt
+++ b/coreapi/CMakeLists.txt
@@ -1,6 +1,6 @@
 ############################################################################
 # CMakeLists.txt
-# Copyright (C) 2014  Belledonne Communications, Grenoble France
+# Copyright (C) 2010-2021 Belledonne Communications, Grenoble France
 #
 ############################################################################
 #
@@ -152,6 +152,7 @@ if (ENABLE_SHARED)
 		${LINPHONE_PRIVATE_HEADER_FILES} ${LINPHONE_SOURCE_FILES_C} ${LINPHONE_SOURCE_FILES_CXX} ${LINPHONE_SOURCE_FILES_OBJC}
 	)
 	target_include_directories(linphone-coreapi PRIVATE ${LINPHONE_INCLUDE_DIRS})
+	target_link_libraries(linphone-coreapi PRIVATE ortp mediastreamer bellesip belr)
 	target_compile_options(linphone-coreapi PRIVATE "-fPIC")
 	add_dependencies(linphone-coreapi liblinphone-git-version)
 	if (ENABLE_JAVA_WRAPPER)
diff --git a/daemon/CMakeLists.txt b/daemon/CMakeLists.txt
index 544d93f59b..4450afecb2 100644
--- a/daemon/CMakeLists.txt
+++ b/daemon/CMakeLists.txt
@@ -1,6 +1,6 @@
 ############################################################################
 # CMakeLists.txt
-# Copyright (C) 2016  Belledonne Communications, Grenoble France
+# Copyright (C) 2010-2021 Belledonne Communications, Grenoble France
 #
 ############################################################################
 #
@@ -123,7 +123,7 @@ bc_apply_compile_flags(DAEMON_PIPETEST_SOURCE_FILES STRICT_OPTIONS_CPP STRICT_OP
 bc_apply_compile_flags(DAEMON_SOURCE_FILES_OBJC STRICT_OPTIONS_CPP STRICT_OPTIONS_OBJC)
 add_executable(linphone-daemon ${DAEMON_SOURCE_FILES} ${DAEMON_SOURCE_FILES_OBJC})
 target_include_directories(linphone-daemon PRIVATE ${CMAKE_CURRENT_LIST_DIR} ${LINPHONE_INCLUDE_DIRS})
-target_link_libraries(linphone-daemon ${LINPHONE_LIBS_FOR_TOOLS} mediastreamer ortp bctoolbox ${XSD_LIBRARIES})
+target_link_libraries(linphone-daemon ${LINPHONE_LIBS_FOR_TOOLS} mediastreamer ortp bctoolbox bellesip ${XSD_LIBRARIES})
 set_target_properties(linphone-daemon PROPERTIES LINK_FLAGS "${LINPHONE_LDFLAGS}")
 set_target_properties(linphone-daemon PROPERTIES LINKER_LANGUAGE CXX)
 
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 95817f4d9c..d1e0c0fbad 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -1,6 +1,6 @@
 ############################################################################
 # CMakeLists.txt
-# Copyright (C) 2017  Belledonne Communications, Grenoble France
+# Copyright (C) 2010-2021 Belledonne Communications, Grenoble France
 #
 ############################################################################
 #
@@ -24,8 +24,10 @@ set(LINK_LIBS
 	bctoolbox
 	${BELLESIP_TARGETNAME}
 	belr
-	mediastreamer
+)
+set(PUBLIC_LINK_LIBS
 	ortp
+	mediastreamer
 )
 
 if (ENABLE_FLEXIAPI)
@@ -625,7 +627,10 @@ if(ENABLE_STATIC)
 		$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/src/>
 		PRIVATE ${LINPHONE_INCLUDE_DIRS} ${LINPHONE_CXX_OBJECTS_INCLUDE_DIRS}
 	)
-	target_link_libraries(linphone-static PRIVATE ${LINK_LIBS})
+	target_link_libraries(linphone-static
+		PUBLIC ${PUBLIC_LINK_LIBS}
+		PRIVATE ${LINK_LIBS}
+	)
 
 	if(APPLE)
 		target_link_libraries(linphone-static PUBLIC ${APPLE_LIBS})
@@ -706,7 +711,10 @@ if(ENABLE_SHARED)
 		$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/src/>
 		PRIVATE ${LINPHONE_INCLUDE_DIRS} ${LINPHONE_CXX_OBJECTS_INCLUDE_DIRS}
 	)
-	target_link_libraries(linphone PRIVATE ${LINK_LIBS})
+	target_link_libraries(linphone
+		PUBLIC ${PUBLIC_LINK_LIBS}
+		PRIVATE ${LINK_LIBS}
+	)
 
 	if(APPLE)
 		target_link_libraries(linphone PUBLIC ${APPLE_LIBS})
diff --git a/src/account_creator/connector_xmlrpc.cpp b/src/account_creator/connector_xmlrpc.cpp
index 244db40a2c..61a1a2f752 100644
--- a/src/account_creator/connector_xmlrpc.cpp
+++ b/src/account_creator/connector_xmlrpc.cpp
@@ -23,7 +23,10 @@
 
 #include "c-wrapper/c-wrapper.h"
 #include "dial-plan/dial-plan.h"
+
+#ifdef HAVE_FLEXIAPI
 #include "FlexiAPIClient.hh"
+#endif
 
 #include "bctoolbox/crypto.h"
 #include "bctoolbox/regex.h"
diff --git a/wrappers/cpp/CMakeLists.txt b/wrappers/cpp/CMakeLists.txt
index b245d77231..f645b3ac95 100644
--- a/wrappers/cpp/CMakeLists.txt
+++ b/wrappers/cpp/CMakeLists.txt
@@ -1,6 +1,6 @@
 ############################################################################
 # CMakeLists.txt
-# Copyright (C) 2017  Belledonne Communications, Grenoble France
+# Copyright (C) 2010-2021 Belledonne Communications, Grenoble France
 #
 ############################################################################
 #
@@ -56,7 +56,7 @@ if(MSVC)
 endif()
 target_compile_definitions(linphone++ PRIVATE "-DLINPHONECXX_EXPORTS")
 target_link_libraries(linphone++
-	PRIVATE ${BCTOOLBOX_CORE_LIBRARIES} ${BELLESIP_LIBRARIES} linphone
+	PRIVATE bctoolbox bellesip linphone
 )
 
 set_target_properties(linphone++ PROPERTIES
@@ -66,12 +66,13 @@ set_target_properties(linphone++ PROPERTIES
 )
 
 target_include_directories(linphone++
-	PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/include
-	PRIVATE ${PROJECT_BINARY_DIR}/include
-	PRIVATE ${PROJECT_SOURCE_DIR}/include
-	PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}
-	PRIVATE ${BCTOOLBOX_INCLUDE_DIRS}
-	PRIVATE ${BELLESIP_INCLUDE_DIRS}
+	PUBLIC
+		$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include>
+		$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
+		$<INSTALL_INTERFACE:include>
+	PRIVATE
+		${PROJECT_BINARY_DIR}/include
+		${PROJECT_SOURCE_DIR}/include
 )
 set_target_properties(linphone++ PROPERTIES SOVERSION ${LINPHONE_SO_VERSION})
 
@@ -94,9 +95,11 @@ write_basic_package_version_file(
 	VERSION ${PROJECT_VERSION}
 	COMPATIBILITY AnyNewerVersion
 )
-export(EXPORT LinphoneCxxTargets
-	FILE "${CMAKE_CURRENT_BINARY_DIR}/LinphoneCxxTargets.cmake"
-)
+if(LINPHONE_BUILDER_GROUP_EXTERNAL_SOURCE_PATH_BUILDERS)
+	export(EXPORT LinphoneCxxTargets
+		FILE "${CMAKE_CURRENT_BINARY_DIR}/LinphoneCxxTargets.cmake"
+	)
+endif()
 configure_file(LinphoneCxxConfig.cmake.in LinphoneCxxConfig.cmake @ONLY)
 
 install(EXPORT LinphoneCxxTargets
-- 
GitLab