Skip to content

Commit

Permalink
build: refactor CMake scripts to use functions for variable scope (Ai…
Browse files Browse the repository at this point in the history
…mRT#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.
  • Loading branch information
zhangyi1357 authored Nov 7, 2024
1 parent 8b6283e commit 0a63cb9
Show file tree
Hide file tree
Showing 20 changed files with 396 additions and 373 deletions.
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ cmake_minimum_required(VERSION 3.24)

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
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)

Expand Down
101 changes: 53 additions & 48 deletions cmake/GetAsio.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -22,61 +22,66 @@ else()
OVERRIDE_FIND_PACKAGE)
endif()

FetchContent_GetProperties(asio)
if(NOT asio_POPULATED)
FetchContent_Populate(asio)

add_library(asio INTERFACE)
add_library(asio::asio ALIAS asio)

target_include_directories(asio INTERFACE $<BUILD_INTERFACE:${asio_SOURCE_DIR}/asio/include> $<INSTALL_INTERFACE:include/asio>)
target_compile_definitions(asio INTERFACE ASIO_STANDALONE ASIO_NO_DEPRECATED)

file(GLOB_RECURSE head_files ${asio_SOURCE_DIR}/asio/include/*.hpp ${asio_SOURCE_DIR}/asio/include/*.ipp)
target_sources(asio INTERFACE FILE_SET HEADERS BASE_DIRS ${asio_SOURCE_DIR}/asio/include FILES ${head_files})

find_package(Threads REQUIRED)
target_link_libraries(asio INTERFACE Threads::Threads)

if(WIN32)
# macro see @ https://stackoverflow.com/a/40217291/1746503
macro(get_win32_winnt version)
if(CMAKE_SYSTEM_VERSION)
set(ver ${CMAKE_SYSTEM_VERSION})
string(REGEX MATCH "^([0-9]+).([0-9])" ver ${ver})
string(REGEX MATCH "^([0-9]+)" verMajor ${ver})
# Check for Windows 10, b/c we'll need to convert to hex 'A'.
if("${verMajor}" MATCHES "10")
set(verMajor "A")
string(REGEX REPLACE "^([0-9]+)" ${verMajor} ver ${ver})
endif("${verMajor}" MATCHES "10")
# Remove all remaining '.' characters.
string(REPLACE "." "" ver ${ver})
# Prepend each digit with a zero.
string(REGEX REPLACE "([0-9A-Z])" "0\\1" ver ${ver})
set(${version} "0x${ver}")
# Wrap it in a function to restrict the scope of the variables
function(get_asio)
FetchContent_GetProperties(asio)
if(NOT asio_POPULATED)
FetchContent_Populate(asio)

add_library(asio INTERFACE)
add_library(asio::asio ALIAS asio)

target_include_directories(asio INTERFACE $<BUILD_INTERFACE:${asio_SOURCE_DIR}/asio/include> $<INSTALL_INTERFACE:include/asio>)
target_compile_definitions(asio INTERFACE ASIO_STANDALONE ASIO_NO_DEPRECATED)

file(GLOB_RECURSE head_files ${asio_SOURCE_DIR}/asio/include/*.hpp ${asio_SOURCE_DIR}/asio/include/*.ipp)
target_sources(asio INTERFACE FILE_SET HEADERS BASE_DIRS ${asio_SOURCE_DIR}/asio/include FILES ${head_files})

find_package(Threads REQUIRED)
target_link_libraries(asio INTERFACE Threads::Threads)

if(WIN32)
# macro see @ https://stackoverflow.com/a/40217291/1746503
macro(get_win32_winnt version)
if(CMAKE_SYSTEM_VERSION)
set(ver ${CMAKE_SYSTEM_VERSION})
string(REGEX MATCH "^([0-9]+).([0-9])" ver ${ver})
string(REGEX MATCH "^([0-9]+)" verMajor ${ver})
# Check for Windows 10, b/c we'll need to convert to hex 'A'.
if("${verMajor}" MATCHES "10")
set(verMajor "A")
string(REGEX REPLACE "^([0-9]+)" ${verMajor} ver ${ver})
endif("${verMajor}" MATCHES "10")
# Remove all remaining '.' characters.
string(REPLACE "." "" ver ${ver})
# Prepend each digit with a zero.
string(REGEX REPLACE "([0-9A-Z])" "0\\1" ver ${ver})
set(${version} "0x${ver}")
endif()
endmacro()

if(NOT DEFINED _WIN32_WINNT)
get_win32_winnt(ver)
set(_WIN32_WINNT ${ver})
endif()
endmacro()

if(NOT DEFINED _WIN32_WINNT)
get_win32_winnt(ver)
set(_WIN32_WINNT ${ver})
message(STATUS "Set _WIN32_WINNET=${_WIN32_WINNT}")

target_compile_definitions(asio INTERFACE _WIN32_WINNT=${_WIN32_WINNT} WIN32_LEAN_AND_MEAN)
endif()

message(STATUS "Set _WIN32_WINNET=${_WIN32_WINNT}")
set_property(TARGET asio PROPERTY EXPORT_NAME asio::asio)
install(
TARGETS asio
EXPORT asio-config
FILE_SET HEADERS
DESTINATION include/asio)

target_compile_definitions(asio INTERFACE _WIN32_WINNT=${_WIN32_WINNT} WIN32_LEAN_AND_MEAN)
install(EXPORT asio-config DESTINATION lib/cmake/asio)
endif()
endfunction()

set_property(TARGET asio PROPERTY EXPORT_NAME asio::asio)
install(
TARGETS asio
EXPORT asio-config
FILE_SET HEADERS
DESTINATION include/asio)

install(EXPORT asio-config DESTINATION lib/cmake/asio)
endif()
get_asio()

# import targets:
# asio::asio
19 changes: 11 additions & 8 deletions cmake/GetBoost.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@ else()
OVERRIDE_FIND_PACKAGE)
endif()

FetchContent_GetProperties(boost)
if(NOT boost_POPULATED)
set(BOOST_INCLUDE_LIBRARIES asio beast)
# Wrap it in a function to restrict the scope of the variables
function(get_boost)
FetchContent_GetProperties(boost)
if(NOT boost_POPULATED)
set(BOOST_INCLUDE_LIBRARIES asio beast)

set(Boost_USE_STATIC_LIBS
ON
CACHE BOOL "")
set(Boost_USE_STATIC_LIBS ON)

FetchContent_MakeAvailable(boost)
endif()
FetchContent_MakeAvailable(boost)
endif()
endfunction()

get_boost()
49 changes: 27 additions & 22 deletions cmake/GetCppToml.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,32 @@ else()
OVERRIDE_FIND_PACKAGE)
endif()

FetchContent_GetProperties(cpptoml)
if(NOT cpptoml_POPULATED)
FetchContent_Populate(cpptoml)

file(READ ${cpptoml_SOURCE_DIR}/include/cpptoml.h CPPTOML_TMP_VAR)
string(REPLACE "#include <cstring>" "#include <limits>" CPPTOML_TMP_VAR "${CPPTOML_TMP_VAR}")
file(WRITE ${cpptoml_SOURCE_DIR}/include/cpptoml.h "${CPPTOML_TMP_VAR}")

file(READ ${cpptoml_SOURCE_DIR}/cmake/cpptomlConfig.cmake.in CPPTOML_TMP_VAR)
string(REPLACE "\n" ";" CPPTOML_TMP_VAR_LINES "${CPPTOML_TMP_VAR}")
list(LENGTH CPPTOML_TMP_VAR_LINES CPPTOML_TMP_VAR_LINES_LENGTH)
if(CPPTOML_TMP_VAR_LINES_LENGTH GREATER 1)
list(REMOVE_AT CPPTOML_TMP_VAR_LINES 0)
endif()
string(REPLACE ";" "\n" CPPTOML_TMP_VAR_LINES "${CPPTOML_TMP_VAR_LINES}")
file(WRITE ${cpptoml_SOURCE_DIR}/cmake/cpptomlConfig.cmake.in "${CPPTOML_TMP_VAR_LINES}")

file(READ ${cpptoml_SOURCE_DIR}/CMakeLists.txt CPPTOML_TMP_VAR)
string(REPLACE " ON" " OFF" CPPTOML_TMP_VAR "${CPPTOML_TMP_VAR}")
file(WRITE ${cpptoml_SOURCE_DIR}/CMakeLists.txt "${CPPTOML_TMP_VAR}")
# Wrap it in a function to restrict the scope of the variables
function(get_cpptoml)
FetchContent_GetProperties(cpptoml)
if(NOT cpptoml_POPULATED)
FetchContent_Populate(cpptoml)

file(READ ${cpptoml_SOURCE_DIR}/include/cpptoml.h CPPTOML_TMP_VAR)
string(REPLACE "#include <cstring>" "#include <limits>" CPPTOML_TMP_VAR "${CPPTOML_TMP_VAR}")
file(WRITE ${cpptoml_SOURCE_DIR}/include/cpptoml.h "${CPPTOML_TMP_VAR}")

file(READ ${cpptoml_SOURCE_DIR}/cmake/cpptomlConfig.cmake.in CPPTOML_TMP_VAR)
string(REPLACE "\n" ";" CPPTOML_TMP_VAR_LINES "${CPPTOML_TMP_VAR}")
list(LENGTH CPPTOML_TMP_VAR_LINES CPPTOML_TMP_VAR_LINES_LENGTH)
if(CPPTOML_TMP_VAR_LINES_LENGTH GREATER 1)
list(REMOVE_AT CPPTOML_TMP_VAR_LINES 0)
endif()
string(REPLACE ";" "\n" CPPTOML_TMP_VAR_LINES "${CPPTOML_TMP_VAR_LINES}")
file(WRITE ${cpptoml_SOURCE_DIR}/cmake/cpptomlConfig.cmake.in "${CPPTOML_TMP_VAR_LINES}")

file(READ ${cpptoml_SOURCE_DIR}/CMakeLists.txt CPPTOML_TMP_VAR)
string(REPLACE " ON" " OFF" CPPTOML_TMP_VAR "${CPPTOML_TMP_VAR}")
file(WRITE ${cpptoml_SOURCE_DIR}/CMakeLists.txt "${CPPTOML_TMP_VAR}")

add_subdirectory(${cpptoml_SOURCE_DIR} ${cpptoml_BINARY_DIR})

add_subdirectory(${cpptoml_SOURCE_DIR} ${cpptoml_BINARY_DIR})
endif()
endfunction()

endif()
get_cpptoml()
24 changes: 13 additions & 11 deletions cmake/GetFmt.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,19 @@ else()
OVERRIDE_FIND_PACKAGE)
endif()

FetchContent_GetProperties(fmt)
if(NOT fmt_POPULATED)
set(FMT_MASTER_PROJECT
OFF
CACHE BOOL "")
set(FMT_INSTALL
${AIMRT_INSTALL}
CACHE BOOL "")

FetchContent_MakeAvailable(fmt)
endif()
# Wrap it in a function to restrict the scope of the variables
function(get_fmt)
FetchContent_GetProperties(fmt)
if(NOT fmt_POPULATED)
set(FMT_MASTER_PROJECT OFF)

set(FMT_INSTALL ON)

FetchContent_MakeAvailable(fmt)
endif()
endfunction()

get_fmt()

# import targets:
# fmt::fmt
25 changes: 14 additions & 11 deletions cmake/GetGFlags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,24 @@ else()
OVERRIDE_FIND_PACKAGE)
endif()

FetchContent_GetProperties(gflags)
if(NOT gflags_POPULATED)
FetchContent_Populate(gflags)
# Wrap it in a function to restrict the scope of the variables
function(get_gflags)
FetchContent_GetProperties(gflags)
if(NOT gflags_POPULATED)
FetchContent_Populate(gflags)

set(BUILD_TESTING
OFF
CACHE BOOL "")
set(BUILD_TESTING OFF)

file(READ ${gflags_SOURCE_DIR}/CMakeLists.txt TMP_VAR)
string(REPLACE " set (PKGCONFIG_INSTALL_DIR " "# set (PKGCONFIG_INSTALL_DIR " TMP_VAR "${TMP_VAR}")
file(WRITE ${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}")
file(WRITE ${gflags_SOURCE_DIR}/CMakeLists.txt "${TMP_VAR}")

add_subdirectory(${gflags_SOURCE_DIR} ${gflags_BINARY_DIR})
add_subdirectory(${gflags_SOURCE_DIR} ${gflags_BINARY_DIR})

endif()
endif()
endfunction()

get_gflags()

# import targets:
# gflags::gflags
23 changes: 12 additions & 11 deletions cmake/GetGTest.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,20 @@ else()
OVERRIDE_FIND_PACKAGE)
endif()

FetchContent_GetProperties(googletest)
if(NOT googletest_POPULATED)
if(WIN32)
set(gtest_force_shared_crt
ON
CACHE BOOL "")
# Wrap it in a function to restrict the scope of the variables
function(get_googletest)
FetchContent_GetProperties(googletest)
if(NOT googletest_POPULATED)
if(WIN32)
set(gtest_force_shared_crt ON)
endif()
set(INSTALL_GTEST OFF)

FetchContent_MakeAvailable(googletest)
endif()
set(INSTALL_GTEST
OFF
CACHE BOOL "")
endfunction()

FetchContent_MakeAvailable(googletest)
endif()
get_googletest()

# import targets:
# GTest::gtest
Expand Down
17 changes: 11 additions & 6 deletions cmake/GetIceoryx.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,17 @@ else()
OVERRIDE_FIND_PACKAGE)
endif()

FetchContent_GetProperties(iceoryx)
if(NOT iceoryx_POPULATED)
# Wrap it in a function to restrict the scope of the variables
function(get_iceoryx)
FetchContent_GetProperties(iceoryx)
if(NOT iceoryx_POPULATED)

FetchContent_Populate(iceoryx)
FetchContent_Populate(iceoryx)

# iceoryx‘s cmake file in ./iceoryx_meta
add_subdirectory(${iceoryx_SOURCE_DIR}/iceoryx_meta ${iceoryx_BINARY_DIR})
# iceoryx‘s cmake file in ./iceoryx_meta
add_subdirectory(${iceoryx_SOURCE_DIR}/iceoryx_meta ${iceoryx_BINARY_DIR})

endif()
endif()
endfunction()

get_iceoryx()
Loading

0 comments on commit 0a63cb9

Please sign in to comment.