cmake: rewrite custom FetchContent
note: before cmake 3.18 FetchContent does not support SOURCE_SUBDIR which is needed for protobuf
This commit is contained in:
@@ -16,6 +16,9 @@ set(CMAKE_FIND_PACKAGE_PREFER_CONFIG TRUE)
|
||||
if(NOT BUILD_ZLIB)
|
||||
find_package(ZLIB REQUIRED)
|
||||
endif()
|
||||
if(NOT TARGET ZLIB::ZLIB)
|
||||
message(FATAL_ERROR "Target ZLIB::ZLIB not available.")
|
||||
endif()
|
||||
|
||||
if(NOT BUILD_absl)
|
||||
find_package(absl REQUIRED)
|
||||
@@ -39,21 +42,22 @@ set(GFLAGS_USE_TARGET_NAMESPACE TRUE)
|
||||
if(NOT BUILD_gflags)
|
||||
find_package(gflags REQUIRED)
|
||||
endif()
|
||||
if(NOT TARGET gflags::gflags)
|
||||
message(FATAL_ERROR "Target gflags::gflags not available.")
|
||||
endif()
|
||||
|
||||
if(NOT BUILD_glog)
|
||||
find_package(glog REQUIRED)
|
||||
endif()
|
||||
if(NOT TARGET glog::glog)
|
||||
message(FATAL_ERROR "Target glog::glog not available.")
|
||||
endif()
|
||||
|
||||
if(NOT BUILD_Protobuf)
|
||||
find_package(Protobuf REQUIRED)
|
||||
else()
|
||||
if(${CMAKE_VERSION} VERSION_LESS "3.18")
|
||||
find_package(Protobuf REQUIRED CONFIG)
|
||||
endif()
|
||||
|
||||
if(NOT TARGET protobuf::libprotobuf)
|
||||
message(FATAL_ERROR "protobuf not builded")
|
||||
endif()
|
||||
endif()
|
||||
if(NOT TARGET protobuf::libprotobuf)
|
||||
message(FATAL_ERROR "Target protobuf::libprotobuf not available.")
|
||||
endif()
|
||||
|
||||
if(USE_SCIP)
|
||||
@@ -246,7 +250,7 @@ get_target_property(protobuf_dirs protobuf::libprotobuf INTERFACE_INCLUDE_DIRECT
|
||||
foreach(dir IN LISTS protobuf_dirs)
|
||||
if ("${dir}" MATCHES "BUILD_INTERFACE")
|
||||
message(STATUS "Adding proto path: ${dir}")
|
||||
list(APPEND PROTO_DIRS "\"--proto_path=${dir}\"")
|
||||
list(APPEND PROTO_DIRS "--proto_path=${dir}")
|
||||
endif()
|
||||
endforeach()
|
||||
|
||||
|
||||
@@ -142,10 +142,11 @@ endif()
|
||||
if(BUILD_Protobuf)
|
||||
message(CHECK_START "Fetching Protobuf")
|
||||
list(APPEND CMAKE_MESSAGE_INDENT " ")
|
||||
set(protobuf_BUILD_TESTS OFF)
|
||||
set(protobuf_BUILD_EXPORT OFF)
|
||||
set(protobuf_MSVC_STATIC_RUNTIME OFF)
|
||||
# FetchContent_Declare(SOURCE_SUBDIR) was introduced in 3.18
|
||||
if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.18")
|
||||
set(protobuf_BUILD_TESTS OFF)
|
||||
set(protobuf_MSVC_STATIC_RUNTIME OFF)
|
||||
FetchContent_Declare(
|
||||
protobuf
|
||||
GIT_REPOSITORY "https://github.com/protocolbuffers/protobuf.git"
|
||||
@@ -154,15 +155,12 @@ if(BUILD_Protobuf)
|
||||
SOURCE_SUBDIR cmake)
|
||||
FetchContent_MakeAvailable(protobuf)
|
||||
else()
|
||||
build_git_dependency(
|
||||
fetch_git_dependency(
|
||||
NAME Protobuf
|
||||
REPOSITORY "https://github.com/protocolbuffers/protobuf.git"
|
||||
TAG "v3.12.2"
|
||||
APPLY_PATCH "${CMAKE_CURRENT_LIST_DIR}/../../patches/protobuf-v3.12.2.patch"
|
||||
CMAKE_ARGS
|
||||
-Dprotobuf_MSVC_STATIC_RUNTIME:BOOL=OFF
|
||||
-Dprotobuf_BUILD_TESTS:BOOL=OFF
|
||||
"SOURCE_SUBDIR cmake"
|
||||
PATCH_COMMAND "git apply \"${CMAKE_CURRENT_LIST_DIR}/../../patches/protobuf-v3.12.2.patch\""
|
||||
SOURCE_SUBDIR cmake
|
||||
)
|
||||
endif()
|
||||
list(POP_BACK CMAKE_MESSAGE_INDENT)
|
||||
|
||||
@@ -1,43 +1,26 @@
|
||||
cmake_minimum_required(VERSION 3.5)
|
||||
cmake_minimum_required(VERSION @CMAKE_VERSION@)
|
||||
|
||||
project(@GIT_DEP_NAME@ NONE)
|
||||
|
||||
include(ExternalProject)
|
||||
|
||||
set(FETCH_BASE_DIR "@CMAKE_BINARY_DIR@/_deps" CACHE PATH "Directory under which to collect all populated content")
|
||||
|
||||
ExternalProject_Add(${PROJECT_NAME}_project
|
||||
TMP_DIR "@CMAKE_CURRENT_BINARY_DIR@/${PROJECT_NAME}/tmp"
|
||||
STAMP_DIR "@CMAKE_CURRENT_BINARY_DIR@/${PROJECT_NAME}/stamp"
|
||||
DOWNLOAD_DIR "@CMAKE_CURRENT_BINARY_DIR@/${PROJECT_NAME}/download"
|
||||
SOURCE_DIR "@CMAKE_CURRENT_BINARY_DIR@/${PROJECT_NAME}/source"
|
||||
BINARY_DIR "@CMAKE_CURRENT_BINARY_DIR@/${PROJECT_NAME}/build"
|
||||
#TMP_DIR "${FETCH_BASE_DIR}/@NAME_LOWER@-tmp"
|
||||
#STAMP_DIR "${FETCH_BASE_DIR}/@NAME_LOWER@-stamp"
|
||||
#DOWNLOAD_DIR "${FETCH_BASE_DIR}/@NAME_LOWER@-download"
|
||||
SOURCE_DIR "${FETCH_BASE_DIR}/@NAME_LOWER@-src"
|
||||
BINARY_DIR "${FETCH_BASE_DIR}/@NAME_LOWER@-build"
|
||||
|
||||
GIT_REPOSITORY "@GIT_DEP_REPOSITORY@"
|
||||
GIT_TAG "@GIT_DEP_TAG@"
|
||||
LOG_DOWNLOAD FALSE
|
||||
|
||||
# UPDATE_COMMAND ""
|
||||
LOG_UPDATE FALSE
|
||||
|
||||
PATCH_COMMAND @PATCH_CMD@
|
||||
#LOG_PATCH FALSE
|
||||
|
||||
# CONFIGURE_COMMAND ""
|
||||
CMAKE_ARGS
|
||||
-DCMAKE_PREFIX_PATH=@CMAKE_CURRENT_BINARY_DIR@/install
|
||||
-DCMAKE_INSTALL_PREFIX=@CMAKE_CURRENT_BINARY_DIR@/install
|
||||
-DCMAKE_BUILD_TYPE=@CMAKE_BUILD_TYPE@
|
||||
-DBUILD_SHARED_LIBS=OFF
|
||||
-DCMAKE_POSITION_INDEPENDENT_CODE:BOOL=ON
|
||||
-DBUILD_TESTING=OFF
|
||||
@GIT_DEP_CMAKE_ARGS@
|
||||
LOG_CONFIGURE FALSE
|
||||
|
||||
# BUILD_COMMAND ""
|
||||
LOG_BUILD FALSE
|
||||
|
||||
# INSTALL_COMMAND ""
|
||||
LOG_INSTALL FALSE
|
||||
PATCH_COMMAND @GIT_DEP_PATCH_COMMAND@
|
||||
|
||||
CONFIGURE_COMMAND ""
|
||||
BUILD_COMMAND ""
|
||||
INSTALL_COMMAND ""
|
||||
TEST_COMMAND ""
|
||||
LOG_TEST FALSE
|
||||
USES_TERMINAL_DOWNLOAD YES
|
||||
USES_TERMINAL_UPDATE YES
|
||||
)
|
||||
|
||||
@@ -63,7 +63,7 @@ function(check_target my_target)
|
||||
endfunction()
|
||||
|
||||
|
||||
# build_git_dependency()
|
||||
# fetch_git_dependency()
|
||||
#
|
||||
# CMake function to download, build and install (in staging area) a dependency at configure
|
||||
# time.
|
||||
@@ -75,7 +75,7 @@ endfunction()
|
||||
# APPLY_PATCH: apply patch
|
||||
# CMAKE_ARGS: List of specific CMake args to add
|
||||
#
|
||||
# build_dependency(
|
||||
# fetch_git_dependency(
|
||||
# NAME
|
||||
# abseil-cpp
|
||||
# URL
|
||||
@@ -83,11 +83,11 @@ endfunction()
|
||||
# TAG
|
||||
# master
|
||||
# APPLY_PATCH
|
||||
# ${CMAKE_SOURCE_DIR}/patches/abseil-cpp.patch
|
||||
# "git apply ${CMAKE_SOURCE_DIR}/patches/abseil-cpp.patch"
|
||||
# )
|
||||
function(build_git_dependency)
|
||||
function(fetch_git_dependency)
|
||||
set(options "")
|
||||
set(oneValueArgs NAME REPOSITORY TAG APPLY_PATCH)
|
||||
set(oneValueArgs NAME REPOSITORY TAG PATCH_COMMAND SOURCE_SUBDIR)
|
||||
set(multiValueArgs CMAKE_ARGS)
|
||||
cmake_parse_arguments(GIT_DEP
|
||||
"${options}"
|
||||
@@ -96,20 +96,21 @@ function(build_git_dependency)
|
||||
${ARGN}
|
||||
)
|
||||
message(STATUS "Building ${GIT_DEP_NAME}: ...")
|
||||
string(TOLOWER ${GIT_DEP_NAME} NAME_LOWER)
|
||||
|
||||
if(GIT_DEP_APPLY_PATCH)
|
||||
set(PATCH_CMD "git apply \"${GIT_DEP_APPLY_PATCH}\"")
|
||||
if(GIT_DEP_PATCH_COMMAND)
|
||||
set(PATCH_CMD "${GIT_DEP_PATCH_COMMAND}")
|
||||
else()
|
||||
set(PATCH_CMD "\"\"")
|
||||
set(PATCH_CMD "")
|
||||
endif()
|
||||
configure_file(
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt.in
|
||||
${CMAKE_CURRENT_BINARY_DIR}/${GIT_DEP_NAME}/CMakeLists.txt @ONLY)
|
||||
${CMAKE_CURRENT_LIST_DIR}/CMakeLists.txt.in
|
||||
${CMAKE_BINARY_DIR}/_deps/${NAME_LOWER}-subbuild/CMakeLists.txt @ONLY)
|
||||
|
||||
execute_process(
|
||||
COMMAND ${CMAKE_COMMAND} -H. -Bproject_build -G "${CMAKE_GENERATOR}"
|
||||
COMMAND ${CMAKE_COMMAND} -S. -Bproject_build -G "${CMAKE_GENERATOR}"
|
||||
RESULT_VARIABLE result
|
||||
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${GIT_DEP_NAME})
|
||||
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/_deps/${NAME_LOWER}-subbuild)
|
||||
if(result)
|
||||
message(FATAL_ERROR "CMake step for ${GIT_DEP_NAME} failed: ${result}")
|
||||
endif()
|
||||
@@ -117,11 +118,21 @@ function(build_git_dependency)
|
||||
execute_process(
|
||||
COMMAND ${CMAKE_COMMAND} --build project_build --config ${CMAKE_BUILD_TYPE}
|
||||
RESULT_VARIABLE result
|
||||
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${GIT_DEP_NAME})
|
||||
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/_deps/${NAME_LOWER}-subbuild)
|
||||
if(result)
|
||||
message(FATAL_ERROR "Build step for ${GIT_DEP_NAME} failed: ${result}")
|
||||
endif()
|
||||
|
||||
if(GIT_DEP_SOURCE_SUBDIR)
|
||||
add_subdirectory(
|
||||
${CMAKE_BINARY_DIR}/_deps/${NAME_LOWER}-src/${GIT_DEP_SOURCE_SUBDIR}
|
||||
${CMAKE_BINARY_DIR}/_deps/${NAME_LOWER}-build)
|
||||
else()
|
||||
add_subdirectory(
|
||||
${CMAKE_BINARY_DIR}/_deps/${NAME_LOWER}-src
|
||||
${CMAKE_BINARY_DIR}/_deps/${NAME_LOWER}-build)
|
||||
endif()
|
||||
|
||||
message(STATUS "Building ${GIT_DEP_NAME}: ...DONE")
|
||||
endfunction()
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt
|
||||
index 849679995..1efc6141c 100644
|
||||
index 849679995..63456ebcf 100644
|
||||
--- a/cmake/CMakeLists.txt
|
||||
+++ b/cmake/CMakeLists.txt
|
||||
@@ -15,6 +15,10 @@ endif ()
|
||||
@@ -13,7 +13,15 @@ index 849679995..1efc6141c 100644
|
||||
|
||||
# Project
|
||||
project(protobuf C CXX)
|
||||
@@ -48,7 +52,7 @@ else (BUILD_SHARED_LIBS)
|
||||
@@ -41,6 +45,7 @@ option(protobuf_BUILD_TESTS "Build tests" ON)
|
||||
option(protobuf_BUILD_CONFORMANCE "Build conformance tests" OFF)
|
||||
option(protobuf_BUILD_EXAMPLES "Build examples" OFF)
|
||||
option(protobuf_BUILD_PROTOC_BINARIES "Build libprotoc and protoc compiler" ON)
|
||||
+option(protobuf_BUILD_EXPORT "Build export to use build directory" ON)
|
||||
if (BUILD_SHARED_LIBS)
|
||||
set(protobuf_BUILD_SHARED_LIBS_DEFAULT ON)
|
||||
else (BUILD_SHARED_LIBS)
|
||||
@@ -48,7 +53,7 @@ else (BUILD_SHARED_LIBS)
|
||||
endif (BUILD_SHARED_LIBS)
|
||||
option(protobuf_BUILD_SHARED_LIBS "Build Shared Libraries" ${protobuf_BUILD_SHARED_LIBS_DEFAULT})
|
||||
include(CMakeDependentOption)
|
||||
@@ -22,35 +30,91 @@ index 849679995..1efc6141c 100644
|
||||
"NOT protobuf_BUILD_SHARED_LIBS" OFF)
|
||||
set(protobuf_WITH_ZLIB_DEFAULT ON)
|
||||
option(protobuf_WITH_ZLIB "Build with zlib support" ${protobuf_WITH_ZLIB_DEFAULT})
|
||||
@@ -116,8 +120,10 @@ endif (CMAKE_USE_PTHREADS_INIT)
|
||||
@@ -116,24 +121,12 @@ endif (CMAKE_USE_PTHREADS_INIT)
|
||||
|
||||
set(_protobuf_FIND_ZLIB)
|
||||
if (protobuf_WITH_ZLIB)
|
||||
- find_package(ZLIB)
|
||||
- if (ZLIB_FOUND)
|
||||
+ if (NOT TARGET ZLIB::ZLIB)
|
||||
+ find_package(ZLIB)
|
||||
+ endif()
|
||||
+ if (ZLIB_FOUND OR TARGET ZLIB::ZLIB)
|
||||
set(HAVE_ZLIB 1)
|
||||
# FindZLIB module define ZLIB_INCLUDE_DIRS variable
|
||||
# Set ZLIB_INCLUDE_DIRECTORIES for compatible
|
||||
@@ -125,15 +131,15 @@ if (protobuf_WITH_ZLIB)
|
||||
# Using imported target if exists
|
||||
if (TARGET ZLIB::ZLIB)
|
||||
set(ZLIB_LIBRARIES ZLIB::ZLIB)
|
||||
- set(HAVE_ZLIB 1)
|
||||
- # FindZLIB module define ZLIB_INCLUDE_DIRS variable
|
||||
- # Set ZLIB_INCLUDE_DIRECTORIES for compatible
|
||||
- set(ZLIB_INCLUDE_DIRECTORIES ${ZLIB_INCLUDE_DIRECTORIES} ${ZLIB_INCLUDE_DIRS})
|
||||
- # Using imported target if exists
|
||||
- if (TARGET ZLIB::ZLIB)
|
||||
- set(ZLIB_LIBRARIES ZLIB::ZLIB)
|
||||
- set(_protobuf_FIND_ZLIB "if(NOT ZLIB_FOUND)\n find_package(ZLIB)\nendif()")
|
||||
+ set(_protobuf_FIND_ZLIB "if(NOT ZLIB_FOUND AND NOT TARGET ZLIB::ZLIB)\n find_package(ZLIB)\nendif()")
|
||||
endif (TARGET ZLIB::ZLIB)
|
||||
- endif (TARGET ZLIB::ZLIB)
|
||||
- else (ZLIB_FOUND)
|
||||
+ else (ZLIB_FOUND OR TARGET ZLIB::ZLIB)
|
||||
set(HAVE_ZLIB 0)
|
||||
# Explicitly set these to empty (override NOT_FOUND) so cmake doesn't
|
||||
# complain when we use them later.
|
||||
set(ZLIB_INCLUDE_DIRECTORIES)
|
||||
set(ZLIB_LIBRARIES)
|
||||
- set(HAVE_ZLIB 0)
|
||||
- # Explicitly set these to empty (override NOT_FOUND) so cmake doesn't
|
||||
- # complain when we use them later.
|
||||
- set(ZLIB_INCLUDE_DIRECTORIES)
|
||||
- set(ZLIB_LIBRARIES)
|
||||
- endif (ZLIB_FOUND)
|
||||
+ endif (ZLIB_FOUND OR TARGET ZLIB::ZLIB)
|
||||
+ if (NOT TARGET ZLIB::ZLIB)
|
||||
+ find_package(ZLIB REQUIRED)
|
||||
+ endif()
|
||||
+ set(HAVE_ZLIB 1)
|
||||
+ set(_protobuf_FIND_ZLIB "if(NOT ZLIB_FOUND AND NOT TARGET ZLIB::ZLIB)\n
|
||||
+ find_package(ZLIB REQUIRED)\nendif()")
|
||||
endif (protobuf_WITH_ZLIB)
|
||||
|
||||
if (HAVE_ZLIB)
|
||||
@@ -223,7 +216,6 @@ endif (MSVC)
|
||||
get_filename_component(protobuf_source_dir ${protobuf_SOURCE_DIR} PATH)
|
||||
|
||||
include_directories(
|
||||
- ${ZLIB_INCLUDE_DIRECTORIES}
|
||||
${protobuf_BINARY_DIR}
|
||||
${protobuf_source_dir}/src)
|
||||
|
||||
diff --git a/cmake/install.cmake b/cmake/install.cmake
|
||||
index be47c54a1..846e6dd60 100644
|
||||
--- a/cmake/install.cmake
|
||||
+++ b/cmake/install.cmake
|
||||
@@ -119,18 +119,19 @@ configure_file(protobuf-options.cmake
|
||||
${CMAKE_INSTALL_CMAKEDIR}/protobuf-options.cmake @ONLY)
|
||||
|
||||
# Allows the build directory to be used as a find directory.
|
||||
-
|
||||
-if (protobuf_BUILD_PROTOC_BINARIES)
|
||||
- export(TARGETS libprotobuf-lite libprotobuf libprotoc protoc
|
||||
- NAMESPACE protobuf::
|
||||
- FILE ${CMAKE_INSTALL_CMAKEDIR}/protobuf-targets.cmake
|
||||
- )
|
||||
-else (protobuf_BUILD_PROTOC_BINARIES)
|
||||
- export(TARGETS libprotobuf-lite libprotobuf
|
||||
- NAMESPACE protobuf::
|
||||
- FILE ${CMAKE_INSTALL_CMAKEDIR}/protobuf-targets.cmake
|
||||
- )
|
||||
-endif (protobuf_BUILD_PROTOC_BINARIES)
|
||||
+if(protobuf_BUILD_EXPORT)
|
||||
+ if (protobuf_BUILD_PROTOC_BINARIES)
|
||||
+ export(TARGETS libprotobuf-lite libprotobuf libprotoc protoc
|
||||
+ NAMESPACE protobuf::
|
||||
+ FILE ${CMAKE_INSTALL_CMAKEDIR}/protobuf-targets.cmake
|
||||
+ )
|
||||
+ else (protobuf_BUILD_PROTOC_BINARIES)
|
||||
+ export(TARGETS libprotobuf-lite libprotobuf
|
||||
+ NAMESPACE protobuf::
|
||||
+ FILE ${CMAKE_INSTALL_CMAKEDIR}/protobuf-targets.cmake
|
||||
+ )
|
||||
+ endif (protobuf_BUILD_PROTOC_BINARIES)
|
||||
+endif()
|
||||
|
||||
install(EXPORT protobuf-targets
|
||||
DESTINATION "${CMAKE_INSTALL_CMAKEDIR}"
|
||||
diff --git a/cmake/libprotobuf.cmake b/cmake/libprotobuf.cmake
|
||||
index 0c12596c2..ac119a84d 100644
|
||||
--- a/cmake/libprotobuf.cmake
|
||||
+++ b/cmake/libprotobuf.cmake
|
||||
@@ -116,7 +116,7 @@ add_library(libprotobuf ${protobuf_SHARED_OR_STATIC}
|
||||
${libprotobuf_lite_files} ${libprotobuf_files} ${libprotobuf_includes} ${libprotobuf_rc_files})
|
||||
target_link_libraries(libprotobuf ${CMAKE_THREAD_LIBS_INIT})
|
||||
if(protobuf_WITH_ZLIB)
|
||||
- target_link_libraries(libprotobuf ${ZLIB_LIBRARIES})
|
||||
+ target_link_libraries(libprotobuf ZLIB::ZLIB)
|
||||
endif()
|
||||
if(protobuf_LINK_LIBATOMIC)
|
||||
target_link_libraries(libprotobuf atomic)
|
||||
|
||||
Reference in New Issue
Block a user