From c25f13adb4d13e63c2811894f387a81d7016c5ed Mon Sep 17 00:00:00 2001 From: insleker Date: Wed, 20 May 2026 20:14:51 +0800 Subject: [PATCH] feat: add cpp controller build support --- CMakeLists.txt | 2 +- conanfile.py | 2 +- src/controller/CMakeLists.txt | 1 + src/controller/cpp/CMakeLists.txt | 57 +++++++++++++++++++++++++++++++ test_package/CMakeLists.txt | 46 +++++++++++++++++++++++++ test_package/conanfile.py | 28 +++++++++++++++ test_package/test_package.cpp | 12 +++++++ 7 files changed, 146 insertions(+), 2 deletions(-) create mode 100644 src/controller/cpp/CMakeLists.txt create mode 100644 test_package/test_package.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 2579c84..cbcba07 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.21) -project(webots_controller VERSION 0.1.0 LANGUAGES C) +project(webots_controller VERSION 0.1.0 LANGUAGES C CXX) option(WEBOTS_CONTROLLER_NO_PLUGINS "Disable runtime plugin loading (dlopen/LoadLibrary paths)." ON) diff --git a/conanfile.py b/conanfile.py index 4fa7566..24720d4 100644 --- a/conanfile.py +++ b/conanfile.py @@ -49,4 +49,4 @@ class WebotsControllerConan(ConanFile): copy(self, "LICENSE", src=self.source_folder, dst=f"{self.package_folder}/licenses") def package_info(self): - self.cpp_info.libs = ["webots_controller"] + self.cpp_info.libs = ["webots_cpp_controller", "webots_controller"] diff --git a/src/controller/CMakeLists.txt b/src/controller/CMakeLists.txt index 20bc4cc..8d173f5 100644 --- a/src/controller/CMakeLists.txt +++ b/src/controller/CMakeLists.txt @@ -1 +1,2 @@ add_subdirectory(c) +add_subdirectory(cpp) diff --git a/src/controller/cpp/CMakeLists.txt b/src/controller/cpp/CMakeLists.txt new file mode 100644 index 0000000..041bdf4 --- /dev/null +++ b/src/controller/cpp/CMakeLists.txt @@ -0,0 +1,57 @@ +set(WEBOTS_CONTROLLER_CPP_SOURCES + Accelerometer.cpp + Altimeter.cpp + Brake.cpp + Camera.cpp + Compass.cpp + Connector.cpp + Device.cpp + Display.cpp + DistanceSensor.cpp + Emitter.cpp + Field.cpp + GPS.cpp + Gyro.cpp + InertialUnit.cpp + Joystick.cpp + Keyboard.cpp + LED.cpp + Lidar.cpp + LightSensor.cpp + Motion.cpp + Motor.cpp + Mouse.cpp + Node.cpp + Pen.cpp + PositionSensor.cpp + Proto.cpp + Radar.cpp + RangeFinder.cpp + Receiver.cpp + Robot.cpp + Skin.cpp + Speaker.cpp + Supervisor.cpp + TouchSensor.cpp + VacuumGripper.cpp +) + +add_library(webots_cpp_controller ${WEBOTS_CONTROLLER_CPP_SOURCES}) +add_library(webots-controller::webots_cpp_controller ALIAS webots_cpp_controller) + +if(WIN32) + set_target_properties(webots_cpp_controller PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS ON) +endif() + +target_include_directories(webots_cpp_controller + PRIVATE + ${CMAKE_SOURCE_DIR}/include/controller/c + ${CMAKE_SOURCE_DIR}/include/controller/cpp +) + +target_link_libraries(webots_cpp_controller + PUBLIC + webots_controller +) + +install(TARGETS webots_cpp_controller) diff --git a/test_package/CMakeLists.txt b/test_package/CMakeLists.txt index e69de29..3da4349 100644 --- a/test_package/CMakeLists.txt +++ b/test_package/CMakeLists.txt @@ -0,0 +1,46 @@ +cmake_minimum_required(VERSION 3.15) +project(test_package CXX) + +find_package(webots-controller REQUIRED CONFIG) + +add_executable(test_package test_package.cpp) +target_link_libraries(test_package PRIVATE Webots::CppController Webots::Controller) + +if(POLICY CMP0207) + cmake_policy(SET CMP0207 NEW) +endif() + +# Webots controllers rely on seeing shared deps in the same directory as the executable. +install(TARGETS test_package + RUNTIME_DEPENDENCY_SET test_package_runtime_deps + RUNTIME DESTINATION bin + LIBRARY DESTINATION lib + ARCHIVE DESTINATION lib) + +if(WIN32) + # for single-config generators. + set(_runtime_dependency_directories) + string(TOUPPER "${CMAKE_BUILD_TYPE}" _conan_build_type) + set(_webots_bin_dirs_var "webots-controller_BIN_DIRS_${_conan_build_type}") + if(DEFINED ${_webots_bin_dirs_var}) + list(APPEND _runtime_dependency_directories ${${_webots_bin_dirs_var}}) + endif() + if(DEFINED ENV{MINGW_HOME}) + list(APPEND _runtime_dependency_directories "$ENV{MINGW_HOME}/bin") + endif() + install( + RUNTIME_DEPENDENCY_SET test_package_runtime_deps + DIRECTORIES ${_runtime_dependency_directories} + PRE_EXCLUDE_REGEXES + "^api-ms-win-.*" + "^ext-ms-.*" + POST_EXCLUDE_REGEXES + ".*/system32/.*" + ".*/Windows/.*" + ".*/SysWOW64/.*" + DESTINATION bin) +else() + install( + RUNTIME_DEPENDENCY_SET test_package_runtime_deps + DESTINATION bin) +endif() \ No newline at end of file diff --git a/test_package/conanfile.py b/test_package/conanfile.py index e69de29..16aa76e 100644 --- a/test_package/conanfile.py +++ b/test_package/conanfile.py @@ -0,0 +1,28 @@ +from conan import ConanFile +from conan.tools.build import can_run +from conan.tools.cmake import CMake, cmake_layout +import os + + +class TestPackageConan(ConanFile): + test_type = "explicit" + settings = "os", "arch", "compiler", "build_type" + generators = "CMakeDeps", "CMakeToolchain" + + def requirements(self): + self.requires(self.tested_reference_str) + + def layout(self): + cmake_layout(self) + + def build(self): + cmake = CMake(self) + cmake.configure() + cmake.build() + + def test(self): + cmake = CMake(self) + cmake.install() + if can_run(self): + exe = os.path.join(self.source_folder, "bin", "test_package") + self.run(exe, env="conanrun") \ No newline at end of file diff --git a/test_package/test_package.cpp b/test_package/test_package.cpp new file mode 100644 index 0000000..b9cc1cb --- /dev/null +++ b/test_package/test_package.cpp @@ -0,0 +1,12 @@ +#include +#include +#include + +int main() { + auto cpp_symbol = &webots::Robot::getBasicTimeStep; + auto c_symbol = &wb_robot_get_time; + (void)cpp_symbol; + (void)c_symbol; + std::cout << "Success!" << std::endl; + return 0; +} \ No newline at end of file