feat: separate pure C lib to a standalone cmake target

This commit is contained in:
insleker 2026-05-21 13:51:37 +08:00
parent 0ec592a2be
commit dbd6f6c6b8
7 changed files with 47 additions and 11 deletions

View File

@ -11,6 +11,7 @@ By default, headers are read from `./include/controller/c` and stb from `./src/s
## Example ## Example
```bash ```bash
git submodule update --init --recursive
conan create . \ conan create . \
-o webots-controller/*:no_plugins=True -o webots-controller/*:no_plugins=True
``` ```

View File

@ -61,5 +61,17 @@ class WebotsControllerConan(ConanFile):
copy(self, "LICENSE", src=self.source_folder, dst=f"{self.package_folder}/licenses") copy(self, "LICENSE", src=self.source_folder, dst=f"{self.package_folder}/licenses")
def package_info(self): def package_info(self):
self.cpp_info.libs = ["webots_cpp_controller", "webots_controller"] self.cpp_info.set_property("cmake_file_name", "webots-controller")
self.cpp_info.includedirs = ["include", "include/controller/cpp", "include/controller/c"] self.cpp_info.set_property("cmake_target_name", "webots-controller::webots-controller")
self.cpp_info.requires = ["CppController"]
controller = self.cpp_info.components["Controller"]
controller.libs = ["webots_controller"]
controller.includedirs = ["include", "include/controller/c"]
controller.set_property("cmake_target_name", "webots-controller::Controller")
cpp_controller = self.cpp_info.components["CppController"]
cpp_controller.libs = ["webots_cpp_controller"]
cpp_controller.includedirs = ["include", "include/controller/cpp", "include/controller/c"]
cpp_controller.requires = ["Controller"]
cpp_controller.set_property("cmake_target_name", "webots-controller::CppController")

View File

@ -85,6 +85,7 @@ endif()
# cmake. # cmake.
add_library(webots_controller ${WEBOTS_CONTROLLER_C_SOURCES}) add_library(webots_controller ${WEBOTS_CONTROLLER_C_SOURCES})
add_library(webots-controller::webots_controller ALIAS webots_controller) add_library(webots-controller::webots_controller ALIAS webots_controller)
add_library(webots-controller::Controller ALIAS webots_controller)
target_sources( target_sources(
webots_controller webots_controller
@ -136,7 +137,11 @@ if(UNIX)
endif() endif()
install( install(
TARGETS webots_controller FILE_SET webots_c_headers TARGETS webots_controller
RUNTIME DESTINATION "bin"
LIBRARY DESTINATION "lib"
ARCHIVE DESTINATION "lib"
FILE_SET webots_c_headers
DESTINATION "include/controller/c" DESTINATION "include/controller/c"
FILE_SET webots_plugin_headers FILE_SET webots_plugin_headers
DESTINATION "include") DESTINATION "include")

View File

@ -41,6 +41,8 @@ file(GLOB_RECURSE WEBOTS_CONTROLLER_CPP_PUBLIC_HEADERS CONFIGURE_DEPENDS
add_library(webots_cpp_controller ${WEBOTS_CONTROLLER_CPP_SOURCES}) add_library(webots_cpp_controller ${WEBOTS_CONTROLLER_CPP_SOURCES})
add_library(webots-controller::webots_cpp_controller ALIAS add_library(webots-controller::webots_cpp_controller ALIAS
webots_cpp_controller) webots_cpp_controller)
add_library(webots-controller::CppController ALIAS webots_cpp_controller)
add_library(webots-controller::webots-controller ALIAS webots_cpp_controller)
target_sources( target_sources(
webots_cpp_controller webots_cpp_controller
@ -67,5 +69,9 @@ target_link_libraries(webots_cpp_controller PUBLIC webots_controller)
install( install(
TARGETS webots_cpp_controller TARGETS webots_cpp_controller
RUNTIME DESTINATION "bin"
LIBRARY DESTINATION "lib"
ARCHIVE DESTINATION "lib"
# `include "../../c/webots/types.h"` in `Robot.hpp` # `include "../../c/webots/types.h"` in `Robot.hpp`
FILE_SET webots_cpp_headers DESTINATION "include/controller/cpp") FILE_SET webots_cpp_headers
DESTINATION "include/controller/cpp")

View File

@ -1,10 +1,13 @@
cmake_minimum_required(VERSION 3.21) cmake_minimum_required(VERSION 3.21)
project(test_package CXX) project(test_package C CXX)
find_package(webots-controller REQUIRED CONFIG) find_package(webots-controller REQUIRED CONFIG)
add_executable(test_package test_package.cpp) add_executable(test_package test_package.cpp)
target_link_libraries(test_package PRIVATE webots-controller::webots-controller) target_link_libraries(test_package PRIVATE webots-controller::CppController)
add_executable(test_package_c test_package.c)
target_link_libraries(test_package_c PRIVATE webots-controller::Controller)
if(POLICY CMP0207) if(POLICY CMP0207)
cmake_policy(SET CMP0207 NEW) cmake_policy(SET CMP0207 NEW)
@ -23,7 +26,8 @@ endif()
# Webots controllers rely on seeing shared deps in the same directory as the # Webots controllers rely on seeing shared deps in the same directory as the
# executable. Destination therefore change to "." # executable. Destination therefore change to "."
install( install(
TARGETS test_package RUNTIME_DEPENDENCY_SET test_package_runtime_deps TARGETS test_package test_package_c RUNTIME_DEPENDENCY_SET
test_package_runtime_deps
RUNTIME DESTINATION . RUNTIME DESTINATION .
LIBRARY DESTINATION . LIBRARY DESTINATION .
ARCHIVE DESTINATION .) ARCHIVE DESTINATION .)

View File

@ -27,4 +27,5 @@ class TestPackageConan(ConanFile):
if can_run(self): if can_run(self):
exe = os.path.join(self.source_folder, "test_package") exe = os.path.join(self.source_folder, "test_package")
self.run(exe, env="conanrun") self.run(exe, env="conanrun")
exe = os.path.join(self.source_folder, "test_package_c")
self.run(exe, env="conanrun")

View File

@ -0,0 +1,7 @@
#include <webots/robot.h>
int main(void) {
double (*c_symbol)(void) = &wb_robot_get_time;
(void)c_symbol;
return 0;
}