build: refactor CMake scripts to use functions for variable scope (#87)

* build: refactor CMake scripts to use functions for variable scope

Wrap multiple CMake scripts in functions to restrict variable scope and prevent unintended resets. This ensures better modularity and maintainability in the build process while adhering to modern CMake best practices.

* refactor: simplify opentelemetry fetch logic

Streamline the handling of OpenTelemetry dependencies by removing unnecessary function wrappers and directly implementing the fetching logic. This improves readability and maintainability while ensuring that relevant variables are correctly set within the proper scope.

* refactor: encapsulate OpenTelemetry configuration in a function

Wrap the OpenTelemetry setup in a function to limit variable scope, improving code organization and maintainability.
This commit is contained in:
zhangyi1357 2024-11-07 21:17:34 +08:00 committed by GitHub
parent 8b6283e684
commit 0a63cb9fe1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
20 changed files with 397 additions and 374 deletions

View File

@ -5,6 +5,11 @@ cmake_minimum_required(VERSION 3.24)
project(aimrt LANGUAGES C CXX) project(aimrt LANGUAGES C CXX)
# Prevent variables from being reset by option
# This setting allows predefined variables to take precedence for FetchContent_MakeAvailable()
# see: https://cmake.org/cmake/help/latest/policy/CMP0077.html
set(CMAKE_POLICY_DEFAULT_CMP0077 NEW)
# Set cmake path # Set cmake path
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake) list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)

View File

@ -22,6 +22,8 @@ else()
OVERRIDE_FIND_PACKAGE) OVERRIDE_FIND_PACKAGE)
endif() endif()
# Wrap it in a function to restrict the scope of the variables
function(get_asio)
FetchContent_GetProperties(asio) FetchContent_GetProperties(asio)
if(NOT asio_POPULATED) if(NOT asio_POPULATED)
FetchContent_Populate(asio) FetchContent_Populate(asio)
@ -77,6 +79,9 @@ if(NOT asio_POPULATED)
install(EXPORT asio-config DESTINATION lib/cmake/asio) install(EXPORT asio-config DESTINATION lib/cmake/asio)
endif() endif()
endfunction()
get_asio()
# import targets # import targets
# asio::asio # asio::asio

View File

@ -22,13 +22,16 @@ else()
OVERRIDE_FIND_PACKAGE) OVERRIDE_FIND_PACKAGE)
endif() endif()
# Wrap it in a function to restrict the scope of the variables
function(get_boost)
FetchContent_GetProperties(boost) FetchContent_GetProperties(boost)
if(NOT boost_POPULATED) if(NOT boost_POPULATED)
set(BOOST_INCLUDE_LIBRARIES asio beast) set(BOOST_INCLUDE_LIBRARIES asio beast)
set(Boost_USE_STATIC_LIBS set(Boost_USE_STATIC_LIBS ON)
ON
CACHE BOOL "")
FetchContent_MakeAvailable(boost) FetchContent_MakeAvailable(boost)
endif() endif()
endfunction()
get_boost()

View File

@ -23,6 +23,8 @@ else()
OVERRIDE_FIND_PACKAGE) OVERRIDE_FIND_PACKAGE)
endif() endif()
# Wrap it in a function to restrict the scope of the variables
function(get_cpptoml)
FetchContent_GetProperties(cpptoml) FetchContent_GetProperties(cpptoml)
if(NOT cpptoml_POPULATED) if(NOT cpptoml_POPULATED)
FetchContent_Populate(cpptoml) FetchContent_Populate(cpptoml)
@ -47,3 +49,6 @@ if(NOT cpptoml_POPULATED)
add_subdirectory(${cpptoml_SOURCE_DIR} ${cpptoml_BINARY_DIR}) add_subdirectory(${cpptoml_SOURCE_DIR} ${cpptoml_BINARY_DIR})
endif() endif()
endfunction()
get_cpptoml()

View File

@ -22,17 +22,19 @@ else()
OVERRIDE_FIND_PACKAGE) OVERRIDE_FIND_PACKAGE)
endif() endif()
# Wrap it in a function to restrict the scope of the variables
function(get_fmt)
FetchContent_GetProperties(fmt) FetchContent_GetProperties(fmt)
if(NOT fmt_POPULATED) if(NOT fmt_POPULATED)
set(FMT_MASTER_PROJECT set(FMT_MASTER_PROJECT OFF)
OFF
CACHE BOOL "") set(FMT_INSTALL ON)
set(FMT_INSTALL
${AIMRT_INSTALL}
CACHE BOOL "")
FetchContent_MakeAvailable(fmt) FetchContent_MakeAvailable(fmt)
endif() endif()
endfunction()
get_fmt()
# import targets # import targets
# fmt::fmt # fmt::fmt

View File

@ -22,13 +22,13 @@ else()
OVERRIDE_FIND_PACKAGE) OVERRIDE_FIND_PACKAGE)
endif() endif()
# Wrap it in a function to restrict the scope of the variables
function(get_gflags)
FetchContent_GetProperties(gflags) FetchContent_GetProperties(gflags)
if(NOT gflags_POPULATED) if(NOT gflags_POPULATED)
FetchContent_Populate(gflags) FetchContent_Populate(gflags)
set(BUILD_TESTING set(BUILD_TESTING OFF)
OFF
CACHE BOOL "")
file(READ ${gflags_SOURCE_DIR}/CMakeLists.txt TMP_VAR) file(READ ${gflags_SOURCE_DIR}/CMakeLists.txt TMP_VAR)
string(REPLACE " set (PKGCONFIG_INSTALL_DIR " "# set (PKGCONFIG_INSTALL_DIR " TMP_VAR "${TMP_VAR}") string(REPLACE " set (PKGCONFIG_INSTALL_DIR " "# set (PKGCONFIG_INSTALL_DIR " TMP_VAR "${TMP_VAR}")
@ -37,6 +37,9 @@ if(NOT gflags_POPULATED)
add_subdirectory(${gflags_SOURCE_DIR} ${gflags_BINARY_DIR}) add_subdirectory(${gflags_SOURCE_DIR} ${gflags_BINARY_DIR})
endif() endif()
endfunction()
get_gflags()
# import targets: # import targets:
# gflags::gflags # gflags::gflags

View File

@ -22,19 +22,20 @@ else()
OVERRIDE_FIND_PACKAGE) OVERRIDE_FIND_PACKAGE)
endif() endif()
# Wrap it in a function to restrict the scope of the variables
function(get_googletest)
FetchContent_GetProperties(googletest) FetchContent_GetProperties(googletest)
if(NOT googletest_POPULATED) if(NOT googletest_POPULATED)
if(WIN32) if(WIN32)
set(gtest_force_shared_crt set(gtest_force_shared_crt ON)
ON
CACHE BOOL "")
endif() endif()
set(INSTALL_GTEST set(INSTALL_GTEST OFF)
OFF
CACHE BOOL "")
FetchContent_MakeAvailable(googletest) FetchContent_MakeAvailable(googletest)
endif() endif()
endfunction()
get_googletest()
# import targets: # import targets:
# GTest::gtest # GTest::gtest

View File

@ -25,6 +25,8 @@ else()
OVERRIDE_FIND_PACKAGE) OVERRIDE_FIND_PACKAGE)
endif() endif()
# Wrap it in a function to restrict the scope of the variables
function(get_iceoryx)
FetchContent_GetProperties(iceoryx) FetchContent_GetProperties(iceoryx)
if(NOT iceoryx_POPULATED) if(NOT iceoryx_POPULATED)
@ -34,3 +36,6 @@ if(NOT iceoryx_POPULATED)
add_subdirectory(${iceoryx_SOURCE_DIR}/iceoryx_meta ${iceoryx_BINARY_DIR}) add_subdirectory(${iceoryx_SOURCE_DIR}/iceoryx_meta ${iceoryx_BINARY_DIR})
endif() endif()
endfunction()
get_iceoryx()

View File

@ -22,33 +22,22 @@ else()
OVERRIDE_FIND_PACKAGE) OVERRIDE_FIND_PACKAGE)
endif() endif()
# Wrap it in a function to restrict the scope of the variables
function(get_jsoncpp)
FetchContent_GetProperties(jsoncpp) FetchContent_GetProperties(jsoncpp)
if(NOT jsoncpp_POPULATED) if(NOT jsoncpp_POPULATED)
set(JSONCPP_WITH_TESTS set(JSONCPP_WITH_TESTS OFF)
OFF
CACHE BOOL "")
set(JSONCPP_WITH_POST_BUILD_UNITTEST
OFF
CACHE BOOL "")
set(BUILD_OBJECT_LIBS set(JSONCPP_WITH_POST_BUILD_UNITTEST OFF)
OFF
CACHE BOOL "") set(BUILD_OBJECT_LIBS OFF)
if(BUILD_SHARED_LIBS) if(BUILD_SHARED_LIBS)
set(BUILD_SHARED_LIBS set(BUILD_SHARED_LIBS ON)
ON set(BUILD_STATIC_LIBS OFF)
CACHE BOOL "")
set(BUILD_STATIC_LIBS
OFF
CACHE BOOL "")
else() else()
set(BUILD_SHARED_LIBS set(BUILD_SHARED_LIBS OFF)
OFF set(BUILD_STATIC_LIBS ON)
CACHE BOOL "")
set(BUILD_STATIC_LIBS
ON
CACHE BOOL "")
endif() endif()
FetchContent_MakeAvailable(jsoncpp) FetchContent_MakeAvailable(jsoncpp)
@ -59,6 +48,9 @@ if(NOT jsoncpp_POPULATED)
add_library(jsoncpp::jsoncpp ALIAS jsoncpp_lib) add_library(jsoncpp::jsoncpp ALIAS jsoncpp_lib)
endif() endif()
endif() endif()
endfunction()
get_jsoncpp()
# import targets: # import targets:
# jsoncpp::jsoncpp # jsoncpp::jsoncpp

View File

@ -22,11 +22,11 @@ else()
OVERRIDE_FIND_PACKAGE) OVERRIDE_FIND_PACKAGE)
endif() endif()
# Wrap it in a function to restrict the scope of the variables
function(get_libunifex)
FetchContent_GetProperties(libunifex) FetchContent_GetProperties(libunifex)
if(NOT libunifex_POPULATED) if(NOT libunifex_POPULATED)
set(UNIFEX_BUILD_EXAMPLES set(UNIFEX_BUILD_EXAMPLES OFF)
OFF
CACHE BOOL "")
FetchContent_MakeAvailable(libunifex) FetchContent_MakeAvailable(libunifex)
@ -36,6 +36,9 @@ if(NOT libunifex_POPULATED)
add_library(unifex::unifex ALIAS unifex) add_library(unifex::unifex ALIAS unifex)
endif() endif()
endfunction()
get_libunifex()
# import targets: # import targets:
# unifex::unifex # unifex::unifex

View File

@ -22,6 +22,8 @@ else()
OVERRIDE_FIND_PACKAGE) OVERRIDE_FIND_PACKAGE)
endif() endif()
# Wrap it in a function to restrict the scope of the variables
function(get_nghttp2)
FetchContent_GetProperties(nghttp2) FetchContent_GetProperties(nghttp2)
if(NOT nghttp2_POPULATED) if(NOT nghttp2_POPULATED)
FetchContent_Populate(nghttp2) FetchContent_Populate(nghttp2)
@ -38,6 +40,9 @@ if(NOT nghttp2_POPULATED)
add_subdirectory(${nghttp2_SOURCE_DIR} ${nghttp2_BINARY_DIR} EXCLUDE_FROM_ALL) add_subdirectory(${nghttp2_SOURCE_DIR} ${nghttp2_BINARY_DIR} EXCLUDE_FROM_ALL)
endif() endif()
endfunction()
get_nghttp2()
# import targets: # import targets:
# nghttp2::nghttp2 # nghttp2::nghttp2

View File

@ -22,10 +22,15 @@ else()
OVERRIDE_FIND_PACKAGE) OVERRIDE_FIND_PACKAGE)
endif() endif()
# Wrap it in a function to restrict the scope of the variables
function(get_nlohmann_json)
FetchContent_GetProperties(nlohmann_json) FetchContent_GetProperties(nlohmann_json)
if(NOT nlohmann_json_POPULATED) if(NOT nlohmann_json_POPULATED)
FetchContent_MakeAvailable(nlohmann_json) FetchContent_MakeAvailable(nlohmann_json)
endif() endif()
endfunction()
get_nlohmann_json()
# import targets: # import targets:
# nlohmann_json::nlohmann_json # nlohmann_json::nlohmann_json

View File

@ -18,35 +18,23 @@ else()
DOWNLOAD_EXTRACT_TIMESTAMP TRUE) DOWNLOAD_EXTRACT_TIMESTAMP TRUE)
endif() endif()
# Wrap it in a function to restrict the scope of the variables
function(get_opentelemetry_cpp)
FetchContent_GetProperties(opentelemetry_cpp) FetchContent_GetProperties(opentelemetry_cpp)
if(NOT opentelemetry_cpp_POPULATED) if(NOT opentelemetry_cpp_POPULATED)
set(BUILD_TESTING set(BUILD_TESTING OFF)
OFF
CACHE BOOL "")
set(WITH_BENCHMARK set(WITH_BENCHMARK OFF)
OFF
CACHE BOOL "")
set(WITH_EXAMPLES set(WITH_EXAMPLES OFF)
OFF
CACHE BOOL "")
set(WITH_FUNC_TESTS set(WITH_FUNC_TESTS OFF)
OFF
CACHE BOOL "")
set(WITH_NO_DEPRECATED_CODE set(WITH_NO_DEPRECATED_CODE ON)
ON
CACHE BOOL "")
set(WITH_DEPRECATED_SDK_FACTORY set(WITH_DEPRECATED_SDK_FACTORY OFF)
OFF
CACHE BOOL "")
set(WITH_OTLP_HTTP set(WITH_OTLP_HTTP ON)
ON
CACHE BOOL "")
set(WITH_STL set(WITH_STL
"CXX20" "CXX20"
@ -56,13 +44,9 @@ if(NOT opentelemetry_cpp_POPULATED)
${opentelemetry_proto_SOURCE_DIR} ${opentelemetry_proto_SOURCE_DIR}
CACHE PATH "") CACHE PATH "")
set(PROTOBUF_PROTOC_EXECUTABLE set(PROTOBUF_PROTOC_EXECUTABLE ${Protobuf_PROTOC_EXECUTABLE})
${Protobuf_PROTOC_EXECUTABLE}
CACHE STRING "")
set(BUILD_SHARED_LIBS set(BUILD_SHARED_LIBS OFF)
OFF
CACHE BOOL "")
FetchContent_MakeAvailable(opentelemetry_cpp) FetchContent_MakeAvailable(opentelemetry_cpp)
@ -135,6 +119,9 @@ if(NOT opentelemetry_cpp_POPULATED)
endif() endif()
endif() endif()
endfunction()
get_opentelemetry_cpp()
# import targets: # import targets:
# opentelemetry-cpp::api # opentelemetry-cpp::api

View File

@ -22,35 +22,29 @@ else()
OVERRIDE_FIND_PACKAGE) OVERRIDE_FIND_PACKAGE)
endif() endif()
# Wrap it in a function to restrict the scope of the variables
function(get_protobuf)
FetchContent_GetProperties(protobuf) FetchContent_GetProperties(protobuf)
if(NOT protobuf_POPULATED) if(NOT protobuf_POPULATED)
set(protobuf_BUILD_TESTS set(protobuf_BUILD_TESTS OFF)
OFF
CACHE BOOL "") set(protobuf_BUILD_CONFORMANCE OFF)
set(protobuf_BUILD_CONFORMANCE
OFF set(protobuf_DISABLE_RTTI OFF)
CACHE BOOL "")
set(protobuf_BUILD_EXAMPLES set(protobuf_WITH_ZLIB OFF)
OFF
CACHE BOOL "") set(protobuf_MSVC_STATIC_RUNTIME OFF)
set(protobuf_DISABLE_RTTI
OFF set(protobuf_INSTALL ${AIMRT_INSTALL})
CACHE BOOL "")
set(protobuf_WITH_ZLIB set(protobuf_VERBOSE ON)
OFF
CACHE BOOL "")
set(protobuf_MSVC_STATIC_RUNTIME
OFF
CACHE BOOL "")
set(protobuf_INSTALL
${AIMRT_INSTALL}
CACHE BOOL "")
set(protobuf_VERBOSE
ON
CACHE BOOL "")
FetchContent_MakeAvailable(protobuf) FetchContent_MakeAvailable(protobuf)
endif() endif()
endfunction()
get_protobuf()
# import targets: # import targets:
# protobuf::libprotobuf # protobuf::libprotobuf

View File

@ -22,7 +22,12 @@ else()
OVERRIDE_FIND_PACKAGE) OVERRIDE_FIND_PACKAGE)
endif() endif()
# Wrap it in a function to restrict the scope of the variables
function(get_pybind11)
FetchContent_GetProperties(pybind11) FetchContent_GetProperties(pybind11)
if(NOT pybind11_POPULATED) if(NOT pybind11_POPULATED)
FetchContent_MakeAvailable(pybind11) FetchContent_MakeAvailable(pybind11)
endif() endif()
endfunction()
get_pybind11()

View File

@ -23,6 +23,8 @@ else()
OVERRIDE_FIND_PACKAGE) OVERRIDE_FIND_PACKAGE)
endif() endif()
# Wrap it in a function to restrict the scope of the variables
function(get_sqlite)
FetchContent_GetProperties(sqlite) FetchContent_GetProperties(sqlite)
if(NOT sqlite_POPULATED) if(NOT sqlite_POPULATED)
FetchContent_Populate(sqlite) FetchContent_Populate(sqlite)
@ -41,6 +43,9 @@ if(NOT sqlite_POPULATED)
target_link_libraries(libsqlite PUBLIC pthread dl) target_link_libraries(libsqlite PUBLIC pthread dl)
endif() endif()
endif() endif()
endfunction()
get_sqlite()
# import targets: # import targets:
# sqlite::libsqlite # sqlite::libsqlite

View File

@ -22,22 +22,19 @@ else()
OVERRIDE_FIND_PACKAGE) OVERRIDE_FIND_PACKAGE)
endif() endif()
# Wrap it in a function to restrict the scope of the variables
function(get_stdexec)
FetchContent_GetProperties(stdexec) FetchContent_GetProperties(stdexec)
if(NOT stdexec_POPULATED) if(NOT stdexec_POPULATED)
set(STDEXEC_ENABLE_IO_URING_TESTS set(STDEXEC_ENABLE_IO_URING_TESTS OFF)
OFF set(STDEXEC_BUILD_EXAMPLES OFF)
CACHE BOOL "") set(STDEXEC_BUILD_TESTS OFF)
set(STDEXEC_BUILD_EXAMPLES
OFF
CACHE BOOL "")
set(STDEXEC_BUILD_TESTS
OFF
CACHE BOOL "")
FetchContent_MakeAvailable(stdexec) FetchContent_MakeAvailable(stdexec)
endif() endif()
endfunction()
get_stdexec()
# import targets: # import targets:
# STDEXEC::stdexec # STDEXEC::stdexec

View File

@ -22,26 +22,23 @@ else()
OVERRIDE_FIND_PACKAGE) OVERRIDE_FIND_PACKAGE)
endif() endif()
# Wrap it in a function to restrict the scope of the variables
function(get_tbb)
FetchContent_GetProperties(tbb) FetchContent_GetProperties(tbb)
if(NOT tbb_POPULATED) if(NOT tbb_POPULATED)
set(TBB_TEST set(TBB_TEST OFF)
OFF
CACHE BOOL "")
set(TBB_DIR set(TBB_DIR "")
""
CACHE STRING "" FORCE)
set(TBB_INSTALL set(TBB_INSTALL ON)
ON
CACHE BOOL "")
set(TBB_STRICT set(TBB_STRICT OFF)
OFF
CACHE BOOL "")
FetchContent_MakeAvailable(tbb) FetchContent_MakeAvailable(tbb)
endif() endif()
endfunction()
get_tbb()
# import targets: # import targets:
# TBB::tbb # TBB::tbb

View File

@ -22,28 +22,27 @@ else()
OVERRIDE_FIND_PACKAGE) OVERRIDE_FIND_PACKAGE)
endif() endif()
# Wrap it in a function to restrict the scope of the variables
function(get_yaml_cpp)
FetchContent_GetProperties(yaml-cpp) FetchContent_GetProperties(yaml-cpp)
if(NOT yaml-cpp_POPULATED) if(NOT yaml-cpp_POPULATED)
set(BUILD_TESTING set(BUILD_TESTING OFF)
OFF
CACHE BOOL "") set(YAML_CPP_BUILD_TESTS OFF)
set(YAML_CPP_BUILD_TESTS
OFF set(YAML_CPP_BUILD_TOOLS OFF)
CACHE BOOL "")
set(YAML_CPP_BUILD_TOOLS set(YAML_CPP_INSTALL ON)
OFF
CACHE BOOL "") set(YAML_CPP_FORMAT_SOURCE OFF)
set(YAML_CPP_INSTALL
ON set(YAML_CPP_BUILD_CONTRIB OFF)
CACHE BOOL "")
set(YAML_CPP_FORMAT_SOURCE
OFF
CACHE BOOL "")
set(YAML_CPP_BUILD_CONTRIB
OFF
CACHE BOOL "")
FetchContent_MakeAvailable(yaml-cpp) FetchContent_MakeAvailable(yaml-cpp)
endif() endif()
endfunction()
get_yaml_cpp()
# import targets: # import targets:
# yaml-cpp::yaml-cpp # yaml-cpp::yaml-cpp

View File

@ -23,7 +23,12 @@ else()
OVERRIDE_FIND_PACKAGE) OVERRIDE_FIND_PACKAGE)
endif() endif()
# Wrap it in a function to restrict the scope of the variables
function(get_zenohc)
FetchContent_GetProperties(zenohc) FetchContent_GetProperties(zenohc)
if(NOT zenohc_POPULATED) if(NOT zenohc_POPULATED)
FetchContent_MakeAvailable(zenohc) FetchContent_MakeAvailable(zenohc)
endif() endif()
endfunction()
get_zenohc()