From b4cdb6527ee34f3020c0d0beddcc5e8712352832 Mon Sep 17 00:00:00 2001 From: wtudio <95963900+wtudio@users.noreply.github.com> Date: Fri, 13 Dec 2024 16:15:37 +0800 Subject: [PATCH] fix: cmake & noexcept (#126) --- CMakeLists.txt | 25 ++++++++----------- src/CMakeLists.txt | 2 +- src/common/CMakeLists.txt | 6 ++--- .../util/function.h | 21 ++++++---------- src/runtime/core/aimrt_core.cc | 4 +++ src/tools/CMakeLists.txt | 2 +- 6 files changed, 27 insertions(+), 33 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c2803404f..d61c4c193 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 @@ -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) @@ -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) @@ -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() @@ -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() @@ -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) @@ -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 @@ -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() diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 9f2c91242..57c4e0a9e 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -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() diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 0612aba31..0d5e9e088 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -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() diff --git a/src/interface/aimrt_module_cpp_interface/util/function.h b/src/interface/aimrt_module_cpp_interface/util/function.h index e499a9550..298afa966 100644 --- a/src/interface/aimrt_module_cpp_interface/util/function.h +++ b/src/interface/aimrt_module_cpp_interface/util/function.h @@ -36,12 +36,6 @@ concept FunctionCStyleOps = std::is_same_v && DecayedType::ReturnType>; -/* -TODO: -1. noexcept类型 -2. operator()方法在加上Args的类型限制 -*/ - /** * @brief Function * @note 由定义好的C Style类型ops构造,可以直接使用NativeHandle与C互调 @@ -131,8 +125,9 @@ class Function { return *this; } + // TODO:加上Args的类型限制 template - R operator()(Args... args) const { + R operator()(Args... args) const noexcept { return static_cast(base_.ops)->invoker(&(base_.object_buf), args...); } @@ -147,17 +142,17 @@ class Function { if constexpr (sizeof(Decayed) <= sizeof(base_.object_buf)) { static constexpr OpsType kOps = { - .invoker = [](void* object, std::tuple_element_t... args) -> R { + .invoker = [](void* object, std::tuple_element_t... args) noexcept -> R { if constexpr (std::is_void_v) { std::invoke(*static_cast(object), args...); } else { return std::invoke(*static_cast(object), args...); } }, - .relocator = [](void* from, void* to) { + .relocator = [](void* from, void* to) noexcept { new (to) Decayed(std::move(*static_cast(from))); static_cast(from)->~Decayed(); }, - .destroyer = [](void* object) { static_cast(object)->~Decayed(); }}; + .destroyer = [](void* object) noexcept { static_cast(object)->~Decayed(); }}; base_.ops = &kOps; new (&(base_.object_buf)) Decayed(std::forward(action)); @@ -165,15 +160,15 @@ class Function { using Stored = Decayed*; static constexpr OpsType kOps = { - .invoker = [](void* object, std::tuple_element_t... args) -> R { + .invoker = [](void* object, std::tuple_element_t... args) noexcept -> R { if constexpr (std::is_void_v) { std::invoke(**static_cast(object), args...); } else { return std::invoke(**static_cast(object), args...); } }, - .relocator = [](void* from, void* to) { new (to) Stored(*static_cast(from)); }, - .destroyer = [](void* object) { delete *static_cast(object); }}; + .relocator = [](void* from, void* to) noexcept { new (to) Stored(*static_cast(from)); }, + .destroyer = [](void* object) noexcept { delete *static_cast(object); }}; base_.ops = &kOps; new (&(base_.object_buf)) Stored(new Decayed(std::forward(action))); diff --git a/src/runtime/core/aimrt_core.cc b/src/runtime/core/aimrt_core.cc index 26c42da06..62b17349a 100644 --- a/src/runtime/core/aimrt_core.cc +++ b/src/runtime/core/aimrt_core.cc @@ -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); @@ -271,6 +273,8 @@ std::future AimRTCore::AsyncStart() { void AimRTCore::Shutdown() { if (std::atomic_exchange(&shutdown_flag_, true)) return; + AIMRT_INFO("Received shutdown signal."); + shutdown_promise_.set_value(); } diff --git a/src/tools/CMakeLists.txt b/src/tools/CMakeLists.txt index 4b7fde3d8..07c3c843e 100644 --- a/src/tools/CMakeLists.txt +++ b/src/tools/CMakeLists.txt @@ -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()