Skip to content

Commit

Permalink
fix: cmake & noexcept (AimRT#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
wtudio authored Dec 13, 2024
1 parent 86be305 commit b4cdb65
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 33 deletions.
25 changes: 11 additions & 14 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ 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
# See: https://cmake.org/cmake/help/latest/policy/CMP0077.html
set(CMAKE_POLICY_DEFAULT_CMP0077 NEW)

# Set cmake path
Expand All @@ -17,13 +17,16 @@ include(CMakeDependentOption)

# Some option
option(AIMRT_BUILD_TESTS "AimRT build tests." OFF)
option(AIMRT_BUILD_EXAMPLES "AimRT build examples." OFF)
option(AIMRT_BUILD_DOCUMENT "AimRT build document." OFF)
option(AIMRT_BUILD_RUNTIME "AimRT build runtime." ON)
option(AIMRT_BUILD_CLI_TOOLS "AimRT build aimrt command line tools." OFF)
option(AIMRT_BUILD_WITH_PROTOBUF "AimRT build with protobuf." ON)
option(AIMRT_BUILD_WITH_ROS2 "AimRT build with ros2." OFF)

option(AIMRT_INSTALL "Enable installation of AimRT." ON)

cmake_dependent_option(AIMRT_BUILD_EXAMPLES "AimRT build examples." OFF "AIMRT_BUILD_RUNTIME" OFF)

cmake_dependent_option(AIMRT_BUILD_PYTHON_RUNTIME "AimRT build python runtime." OFF "AIMRT_BUILD_RUNTIME" OFF)
cmake_dependent_option(AIMRT_BUILD_PYTHON_PACKAGE "AimRT build python package." OFF "AIMRT_BUILD_PYTHON_RUNTIME;AIMRT_INSTALL" OFF)

Expand All @@ -46,8 +49,6 @@ cmake_dependent_option(AIMRT_BUILD_LOG_CONTROL_PLUGIN "AimRT build log control p
cmake_dependent_option(AIMRT_BUILD_GRPC_PLUGIN "AimRT build grpc plugin." OFF "AIMRT_BUILD_RUNTIME" OFF)
cmake_dependent_option(AIMRT_BUILD_PROXY_PLUGIN "AimRT build proxy plugin." OFF "AIMRT_BUILD_RUNTIME" OFF)

option(AIMRT_INSTALL "Enable installation of AimRT." ON)

option(AIMRT_EXECUTOR_USE_STDEXEC "AimRT use stdexec as executor impl. (Experimental)" OFF)
option(AIMRT_BUILD_WITH_WERROR "AimRT build with -Werror option. (Experimental)" OFF)

Expand Down Expand Up @@ -95,12 +96,10 @@ if(AIMRT_MASTER_PROJECT)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
endif()

if(UNIX)
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")
endif()

if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
elseif(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
add_compile_options(/utf-8 /wd4819)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Zc:__cplusplus")
endif()
Expand Down Expand Up @@ -180,7 +179,7 @@ if(AIMRT_BUILD_WITH_PROTOBUF)
endif()

if(AIMRT_BUILD_WITH_ROS2)
# fix cmake policy for using FindPythonInterp and FindPythonLibs (ros2)
# Fix cmake policy for using FindPythonInterp and FindPythonLibs (ros2)
if(POLICY CMP0148)
cmake_policy(SET CMP0148 OLD)
endif()
Expand Down Expand Up @@ -227,7 +226,7 @@ if(AIMRT_BUILD_RUNTIME)
endif()

if(AIMRT_BUILD_ICEORYX_PLUGIN)
# try to find libacl
# Try to find libacl
if(CMAKE_SYSTEM_NAME MATCHES "Linux|QNX")
find_library(ACL_LIB acl)
if(NOT ACL_LIB)
Expand All @@ -247,7 +246,7 @@ if(AIMRT_BUILD_RUNTIME)
endif()

if(AIMRT_BUILD_ZENOH_PLUGIN)
# find Rust compiler
# Find Rust compiler
execute_process(
COMMAND rustc --version
RESULT_VARIABLE rustc_result
Expand Down Expand Up @@ -293,9 +292,7 @@ if(AIMRT_INSTALL)
endif()

# Put at last to ensure ros2 installation is done
if(AIMRT_INSTALL
AND AIMRT_BUILD_PYTHON_RUNTIME
AND AIMRT_BUILD_PYTHON_PACKAGE)
if(AIMRT_BUILD_PYTHON_PACKAGE)
install(CODE "execute_process(COMMAND \"${CMAKE_COMMAND}\" --build \"${CMAKE_BINARY_DIR}\" --config ${CMAKE_BUILD_TYPE} --target create_python_pkg)")
endif()

Expand Down
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ if(AIMRT_BUILD_RUNTIME)
add_subdirectory(plugins)
endif()

if(AIMRT_BUILD_RUNTIME AND AIMRT_BUILD_EXAMPLES)
if(AIMRT_BUILD_EXAMPLES)
add_subdirectory(examples)
endif()
6 changes: 2 additions & 4 deletions src/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ if(AIMRT_BUILD_WITH_ROS2)
add_subdirectory(ros2_util)
endif()

if(AIMRT_BUILD_RUNTIME)
if(AIMRT_BUILD_NET_PLUGIN OR AIMRT_BUILD_GRPC_PLUGIN)
add_subdirectory(net)
endif()
if(AIMRT_BUILD_NET_PLUGIN OR AIMRT_BUILD_GRPC_PLUGIN)
add_subdirectory(net)
endif()
21 changes: 8 additions & 13 deletions src/interface/aimrt_module_cpp_interface/util/function.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,6 @@ concept FunctionCStyleOps =
std::is_same_v<void (*)(void*), decltype(T::destroyer)> &&
DecayedType<typename InvokerTraitsHelper<decltype(T::invoker)>::ReturnType>;

/*
TODO:
1. noexcept类型
2. operator()方法在加上Args的类型限制
*/

/**
* @brief Function
* @note 由定义好的C Style类型ops构造,可以直接使用NativeHandle与C互调
Expand Down Expand Up @@ -131,8 +125,9 @@ class Function<Ops> {
return *this;
}

// TODO:加上Args的类型限制
template <typename... Args>
R operator()(Args... args) const {
R operator()(Args... args) const noexcept {
return static_cast<const OpsType*>(base_.ops)->invoker(&(base_.object_buf), args...);
}

Expand All @@ -147,33 +142,33 @@ class Function<Ops> {

if constexpr (sizeof(Decayed) <= sizeof(base_.object_buf)) {
static constexpr OpsType kOps = {
.invoker = [](void* object, std::tuple_element_t<Idx, ArgsTuple>... args) -> R {
.invoker = [](void* object, std::tuple_element_t<Idx, ArgsTuple>... args) noexcept -> R {
if constexpr (std::is_void_v<R>) {
std::invoke(*static_cast<Decayed*>(object), args...);
} else {
return std::invoke(*static_cast<Decayed*>(object), args...);
}
},
.relocator = [](void* from, void* to) {
.relocator = [](void* from, void* to) noexcept {
new (to) Decayed(std::move(*static_cast<Decayed*>(from)));
static_cast<Decayed*>(from)->~Decayed(); },
.destroyer = [](void* object) { static_cast<Decayed*>(object)->~Decayed(); }};
.destroyer = [](void* object) noexcept { static_cast<Decayed*>(object)->~Decayed(); }};

base_.ops = &kOps;
new (&(base_.object_buf)) Decayed(std::forward<T>(action));
} else {
using Stored = Decayed*;

static constexpr OpsType kOps = {
.invoker = [](void* object, std::tuple_element_t<Idx, ArgsTuple>... args) -> R {
.invoker = [](void* object, std::tuple_element_t<Idx, ArgsTuple>... args) noexcept -> R {
if constexpr (std::is_void_v<R>) {
std::invoke(**static_cast<Stored*>(object), args...);
} else {
return std::invoke(**static_cast<Stored*>(object), args...);
}
},
.relocator = [](void* from, void* to) { new (to) Stored(*static_cast<Stored*>(from)); },
.destroyer = [](void* object) { delete *static_cast<Stored*>(object); }};
.relocator = [](void* from, void* to) noexcept { new (to) Stored(*static_cast<Stored*>(from)); },
.destroyer = [](void* object) noexcept { delete *static_cast<Stored*>(object); }};

base_.ops = &kOps;
new (&(base_.object_buf)) Stored(new Decayed(std::forward<T>(action)));
Expand Down
4 changes: 4 additions & 0 deletions src/runtime/core/aimrt_core.cc
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ void AimRTCore::StartImpl() {
void AimRTCore::ShutdownImpl() {
if (std::atomic_exchange(&shutdown_impl_flag_, true)) return;

AIMRT_INFO("AimRT start shutdown.");

EnterState(State::kPreShutdown);

EnterState(State::kPreShutdownModules);
Expand Down Expand Up @@ -271,6 +273,8 @@ std::future<void> AimRTCore::AsyncStart() {
void AimRTCore::Shutdown() {
if (std::atomic_exchange(&shutdown_flag_, true)) return;

AIMRT_INFO("Received shutdown signal.");

shutdown_promise_.set_value();
}

Expand Down
2 changes: 1 addition & 1 deletion src/tools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ if(AIMRT_BUILD_CLI_TOOLS)
add_subdirectory(aimrt_cli)
endif()

if(AIMRT_BUILD_PYTHON_RUNTIME AND AIMRT_BUILD_PYTHON_PACKAGE)
if(AIMRT_BUILD_PYTHON_PACKAGE)
add_subdirectory(package_aimrt_py)
endif()

0 comments on commit b4cdb65

Please sign in to comment.