Skip to content

Commit

Permalink
Fix: make hephaestus build statically (#159)
Browse files Browse the repository at this point in the history
# Description
Few fixes to make hephaestus build statically. 
In details:
* Properly set options for external dependencies
* Add missing link libraries
* Add option to disable install of third party dependency. 
* This is useful when building statically to avoid exposing 3rd party
dependency.

Other minor changes:
* Avoid installing google test if tests are not enabled
* Fix compilation with GCC

---------

Co-authored-by: Lorenz Hruby <[email protected]>
  • Loading branch information
filippobrizzi and lhruby authored Aug 30, 2024
1 parent 793115b commit 76d596a
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 61 deletions.
100 changes: 61 additions & 39 deletions cmake/05_modules.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ macro(configure_modules)
get_property(_enabled_modules_list GLOBAL PROPERTY ENABLED_MODULES)

# Add each marked module into the build
message("\n\n${_enabled_modules_list}\n\n")
foreach(_module IN LISTS _enabled_modules_list)
message(VERBOSE "Adding subdirectory ${MODULE_${_module}_PATH} for module ${_module}")
add_subdirectory(${MODULE_${_module}_PATH})
Expand All @@ -141,7 +140,10 @@ macro(configure_modules)
configure_file(${CMAKE_TEMPLATES_DIR}/doxyfile.in ${CMAKE_BINARY_DIR}/doxyfile @ONLY)

# Define installation rules for module targets
install_modules()
option(DISABLE_MODULES_INSTALL "Disable installtion of modules, this is usefull when building statically" OFF)
if(NOT DISABLE_MODULES_INSTALL)
install_modules()
endif()
endmacro()

# ==================================================================================================
Expand Down Expand Up @@ -563,30 +565,32 @@ endmacro()

# ==================================================================================================
# Setup tests target
if(BUILD_TESTING)
FetchContent_Declare(
googletest URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt
ON
CACHE BOOL "" FORCE
)
FetchContent_MakeAvailable(googletest)
FetchContent_GetProperties(googletest)
if(NOT googletest_POPULATED)
FetchContent_Populate(googletest)
add_subdirectory(${googletest_SOURCE_DIR} ${googletest_BINARY_DIR} EXCLUDE_FROM_ALL)
endif()

FetchContent_Declare(
googletest URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt
ON
CACHE BOOL "" FORCE
)
FetchContent_MakeAvailable(googletest)
FetchContent_GetProperties(googletest)
if(NOT googletest_POPULATED)
FetchContent_Populate(googletest)
add_subdirectory(${googletest_SOURCE_DIR} ${googletest_BINARY_DIR} EXCLUDE_FROM_ALL)
# use leaner set of compiler warnings for sources we have no control over
set_target_properties(gtest PROPERTIES COMPILE_OPTIONS "${THIRD_PARTY_COMPILER_WARNINGS}")
set_target_properties(gtest PROPERTIES CXX_CLANG_TIDY "")
set_target_properties(gtest_main PROPERTIES CXX_CLANG_TIDY "")
set_target_properties(gmock PROPERTIES COMPILE_OPTIONS "${THIRD_PARTY_COMPILER_WARNINGS}")
set_target_properties(gmock PROPERTIES CXX_CLANG_TIDY "")
set_target_properties(gmock_main PROPERTIES CXX_CLANG_TIDY "")
else()
message(STATUS "Testing is disabled")
endif()

# use leaner set of compiler warnings for sources we have no control over
set_target_properties(gtest PROPERTIES COMPILE_OPTIONS "${THIRD_PARTY_COMPILER_WARNINGS}")
set_target_properties(gtest PROPERTIES CXX_CLANG_TIDY "")
set_target_properties(gtest_main PROPERTIES CXX_CLANG_TIDY "")
set_target_properties(gmock PROPERTIES COMPILE_OPTIONS "${THIRD_PARTY_COMPILER_WARNINGS}")
set_target_properties(gmock PROPERTIES CXX_CLANG_TIDY "")
set_target_properties(gmock_main PROPERTIES CXX_CLANG_TIDY "")

# ==================================================================================================
# Adds a custom target to group all test programs built on call to `make tests`
set(TESTS_BUILD_TARGET tests_build)
Expand All @@ -610,20 +614,30 @@ add_custom_target(
add_dependencies(${CHECK_TARGET} ${TESTS_BUILD_TARGET}) # `check` depends on `tests` target

# ==================================================================================================
# ~~~
# macro: define_module_test
#
# Description: Macro to define a 'test' program Note: - A test program belongs to the enclosing module - A module can
# have multiple tests - Executables are not installed on call to `make install`
#
# Parameters: SOURCES : (list) Source files to compile [PUBLIC_INCLUDE_PATHS] : (list, optional) Publicly included
# directories. See cmake documentation for 'PUBLIC' keyword in `target_include_directories` [PRIVATE_INCLUDE_PATHS] :
# (list, optional) Privately included directories. See cmake documentation for 'PRIVATE' keyword in
# `target_include_directories` [SYSTEM_PUBLIC_INCLUDE_PATHS] : (list, optional) Publicly included system directories on
# some platforms. See 'SYSTEM' keyword in `target_include_directories` [SYSTEM_PRIVATE_INCLUDE_PATHS] : (list, optional)
# Privately included system directories on some platforms. See 'SYSTEM' keyword in `target_include_directories`
# [PUBLIC_LINK_LIBS] : (list, optional) Public link dependencies. See 'PUBLIC' keyword in `target_link_libraries`
# [PRIVATE_LINK_LIBS]: (list, optional) Private link dependencies. See 'PRIVATE' keyword in `target_link_libraries`
# Description: Macro to define a 'test' program
# Note:
# - A test program belongs to the enclosing module
# - A module can have multiple tests
# - Examples are not installed on call to `make install`
#
# Parameters:
# SOURCES : (list) Source files to compile
# [PUBLIC_INCLUDE_PATHS] : (list, optional) Publicly included directories.
# See cmake documentation for 'PUBLIC' keyword in `target_include_directories`
# [PRIVATE_INCLUDE_PATHS] : (list, optional) Privately included directories.
# See cmake documentation for 'PRIVATE' keyword in `target_include_directories`
# [SYSTEM_PUBLIC_INCLUDE_PATHS] : (list, optional) Publicly included system directories on some platforms.
# See 'SYSTEM' keyword in `target_include_directories`
# [SYSTEM_PRIVATE_INCLUDE_PATHS] : (list, optional) Privately included system directories on some platforms.
# See 'SYSTEM' keyword in `target_include_directories`
# [PUBLIC_LINK_LIBS] : (list, optional) Public link dependencies.
# See 'PUBLIC' keyword in `target_link_libraries`
# [PRIVATE_LINK_LIBS]: (list, optional) Private link dependencies.
# See 'PRIVATE' keyword in `target_link_libraries`
# ~~~
include(GoogleTest)
macro(define_module_test)
set(flags "")
Expand All @@ -632,6 +646,9 @@ macro(define_module_test)

include(CMakeParseArguments)
cmake_parse_arguments(TARGET_ARG "${flags}" "${single_opts}" "${multi_opts}" ${ARGN})
if(NOT BUILD_TESTING)
return()
endif()

if(TARGET_ARG_UNPARSED_ARGUMENTS)
message(FATAL_ERROR "Unparsed arguments: ${TARGET_ARG_UNPARSED_ARGUMENTS}")
Expand Down Expand Up @@ -680,10 +697,15 @@ macro(define_module_test)
endmacro()

# ==================================================================================================
# (for internal use) Create installation rules for library and executable targets in enabled modules Description:
# Installs CMake config files to support calling find_package() in downstream projects. The following files are
# configured and installed in <installdir>/lib/cmake/<project>_<module>: - <project>_<module>-config.cmake -
# <project>_<module>-config-version.cmake - <project>_<module>-targets.cmake
# ~~~
# (for internal use) Create installation rules for library and executable targets in enabled modules
# Description:
# Installs CMake config files to support calling find_package() in downstream projects.
# The following files are configured and installed in <installdir>/lib/cmake/<project>_<module>:
# - <project>_<module>-config.cmake
# - <project>_<module>-config-version.cmake
# - <project>_<module>-targets.cmake
# ~~~
function(install_modules)
get_property(_enabled_modules_list GLOBAL PROPERTY ENABLED_MODULES)
foreach(_module IN LISTS _enabled_modules_list)
Expand Down
15 changes: 10 additions & 5 deletions cmake/06_external_dependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,16 @@ macro(build_external_dependencies)
endif()

list(PREPEND CMAKE_PREFIX_PATH ${EP_DEPLOY_DIR}/)
install(
DIRECTORY ${EP_DEPLOY_DIR}/
DESTINATION ${CMAKE_INSTALL_PREFIX}
USE_SOURCE_PERMISSIONS
)

option(INSTALL_EXTERNAL_DEPENDENCIES "Install external dependencies" ON)
if(INSTALL_EXTERNAL_DEPENDENCIES)
install(
DIRECTORY ${EP_DEPLOY_DIR}/
DESTINATION ${CMAKE_INSTALL_PREFIX}
USE_SOURCE_PERMISSIONS
)
endif()

if(CMAKE_CROSSCOMPILING)
list(APPEND CMAKE_FIND_ROOT_PATH ${EP_DEPLOY_DIR})
endif()
Expand Down
6 changes: 4 additions & 2 deletions cmake/build.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ enumerate_modules(ROOT_PATH ${PROJECT_SOURCE_DIR}/modules)
build_external_dependencies(FOLDER ${PROJECT_SOURCE_DIR}/external)

# -------------------------------------------------------------------------------------------------
# Configure modules enabled with -DBUILD_MODULES="semi-colon;separated;list". - Special value "all" will build all
# modules found
# ~~~
# Configure modules enabled with -DBUILD_MODULES="semi-colon;separated;list".
# - Special value "all" will build all modules found
# ~~~
configure_modules()
1 change: 1 addition & 0 deletions cmake/external.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ set(EP_CMAKE_EXTRA_ARGS
-DCMAKE_CXX_COMPILER_LAUNCHER=${CMAKE_CXX_COMPILER_LAUNCHER}
-DCMAKE_C_COMPILER_LAUNCHER=${CMAKE_C_COMPILER_LAUNCHER}
-DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD}
-DBUILD_TESTING=OFF
)
list(PREPEND CMAKE_PREFIX_PATH ${CMAKE_INSTALL_PREFIX})

Expand Down
7 changes: 5 additions & 2 deletions external/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,14 @@ endif()
# --------------------------------------------------------------------------------------------------
# zenoh

set(ZENOHC_CMAKE_ARGS -DZENOHC_CARGO_CHANNEL=+stable -DZENOHC_BUILD_WITH_SHARED_MEMORY=TRUE)
if(NOT BUILD_SHARED_LIBS)
list(APPEND ZENOHC_CMAKE_ARGS "-DZENOHC_LIB_STATIC=TRUE" "-DZENOHC_INSTALL_STATIC_LIBRARY=TRUE")
endif()
# NOTE: There is a bug in the current release, so for now we are pointing to our fix. set(ZENOH_VERSION "1.0.0.6")
set(ZENOH_VERSION "5a82ecfd6154adf30ff96efe9b7838018134a528") # from zenohcxx dev/1.0.0
add_cmake_dependency(
NAME zenohc URL https://github.com/eclipse-zenoh/zenoh-c/archive/${ZENOH_VERSION}.zip
CMAKE_ARGS -DZENOHC_CARGO_CHANNEL=+stable
NAME zenohc URL https://github.com/eclipse-zenoh/zenoh-c/archive/${ZENOH_VERSION}.zip CMAKE_ARGS ${ZENOHC_CMAKE_ARGS}
)

# set(ZENOHCXX_VERSION ${ZENOH_VERSION})
Expand Down
2 changes: 2 additions & 0 deletions modules/ipc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ define_module_library(
NAME ipc
PUBLIC_LINK_LIBS
absl::base
absl::raw_hash_set
absl::status
absl::statusor
absl::log
hephaestus::concurrency
hephaestus::containers
Expand Down
2 changes: 1 addition & 1 deletion modules/ipc/include/hephaestus/ipc/zenoh/subscriber.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Subscriber {
void callback(const ::zenoh::Sample& sample);

private:
using Message = std::pair<MessageMetadata, std::vector<const std::byte>>;
using Message = std::pair<MessageMetadata, std::vector<std::byte>>;

SessionPtr session_;
TopicConfig topic_config_;
Expand Down
26 changes: 16 additions & 10 deletions modules/ipc/include/hephaestus/ipc/zenoh/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ static constexpr auto TEXT_PLAIN_ENCODING = "text/plain";
static constexpr auto PUBLISHER_ATTACHMENT_MESSAGE_COUNTER_KEY = "0";
static constexpr auto PUBLISHER_ATTACHMENT_MESSAGE_SESSION_ID_KEY = "1";

[[nodiscard]] static inline auto toByteVector(const ::zenoh::Bytes& bytes) -> std::vector<const std::byte> {
[[nodiscard]] static inline auto toByteVector(const ::zenoh::Bytes& bytes) -> std::vector<std::byte> {
auto reader = bytes.reader();
std::vector<const std::byte> vec(bytes.size());
std::vector<std::byte> vec(bytes.size());
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast,cppcoreguidelines-pro-type-const-cast)
reader.read(reinterpret_cast<uint8_t*>(const_cast<std::byte*>(vec.data())), vec.size());
reader.read(reinterpret_cast<uint8_t*>(vec.data()), vec.size());
return vec;
}

Expand Down Expand Up @@ -86,15 +86,21 @@ inline auto toString(const std::vector<std::string>& vec) -> std::string {
return str;
}

inline auto toChrono(uint64_t ts) -> std::chrono::nanoseconds {
const auto seconds = std::chrono::seconds{ static_cast<std::uint32_t>(ts >> 32U) };
const auto fraction = std::chrono::nanoseconds{ static_cast<std::uint32_t>(ts & 0xFFFFFFFF) };

return seconds + fraction;
inline auto toChrono(uint64_t timestamp) -> std::chrono::nanoseconds {
// For details see https://zenoh.io/docs/manual/abstractions/#timestamp
const auto seconds = std::chrono::seconds{ static_cast<std::uint32_t>(timestamp >> 32U) };
static constexpr auto FRACTION_MASK = 0xFFFFFFF0;
auto fraction = static_cast<uint32_t>(timestamp & FRACTION_MASK); //
// Convert fraction to nanoseconds
// The fraction is in units of 2^-32 seconds, so we multiply by 10^9 / 2^32
auto nanoseconds =
std::chrono::nanoseconds{ static_cast<uint64_t>(fraction) * 1'000'000'000 / 0x100000000 }; // NOLINT

return seconds + nanoseconds;
}

inline auto toChrono(const ::zenoh::Timestamp& ts) -> std::chrono::nanoseconds {
return toChrono(ts.get_time());
inline auto toChrono(const ::zenoh::Timestamp& timestamp) -> std::chrono::nanoseconds {
return toChrono(timestamp.get_time());
}

[[nodiscard]] auto createZenohConfig(const Config& config) -> ::zenoh::Config;
Expand Down
13 changes: 11 additions & 2 deletions modules/serdes/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ declare_module(
find_package(absl REQUIRED)
find_package(magic_enum REQUIRED)
find_package(Protobuf REQUIRED)
find_package(utf8_range REQUIRED)
find_package(range-v3 REQUIRED)

# library sources
Expand All @@ -36,8 +37,16 @@ set(SOURCES
# library target
define_module_library(
NAME serdes
PUBLIC_LINK_LIBS absl::log_internal_check_op absl::status hephaestus::utils magic_enum::magic_enum
protobuf::libprotobuf range-v3::range-v3
PUBLIC_LINK_LIBS
absl::log_internal_check_op
absl::raw_hash_set
absl::status
absl::statusor
hephaestus::utils
magic_enum::magic_enum
protobuf::libprotobuf
range-v3::range-v3
utf8_range::utf8_validity
PRIVATE_LINK_LIBS ""
SOURCES ${SOURCES}
PUBLIC_INCLUDE_PATHS $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> $<INSTALL_INTERFACE:include>
Expand Down

0 comments on commit 76d596a

Please sign in to comment.