Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 54 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,69 @@ jobs:
NAME: Thread Sanitizer
- TARGET_CMAKE_ARGS: -DENABLE_SANITIZER_UNDEFINED_BEHAVIOR=ON -DCMAKE_BUILD_TYPE=Debug
NAME: Undefined Behavior Sanitizer
- TARGET_CMAKE_ARGS: -DCLASS_LOADER_USE_RCPPUTILS=OFF
NAME: Without rcpputils
env:
ROS_DISTRO: rolling
runs-on: ubuntu-latest
name: ${{ matrix.env.NAME }}
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v7
# ThreadSanitizer fails with "incompatible memory layout" under the
# runner kernel's default ASLR entropy (vm.mmap_rnd_bits=32). The
# industrial_ci container shares the host kernel, so lower it here.
- name: Lower ASLR entropy for ThreadSanitizer
run: sudo sysctl vm.mmap_rnd_bits=28
- uses: 'ros-industrial/industrial_ci@master'
env: ${{matrix.env}}

# Build and test without ament and ROS (CLASS_LOADER_IGNORE_AMENT=ON, dependencies from the
# system's package manager), install, and check the installed library via CMake and
# pkg-config (not on Windows).
standalone:
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.os }}
name: Standalone (without ROS, ${{ matrix.os }})
defaults:
run:
shell: bash
steps:
- uses: actions/checkout@v7
- name: Install dependencies (Linux)
if: runner.os == 'Linux'
run: |
sudo apt-get update
sudo apt-get install -y libconsole-bridge-dev libgtest-dev pkg-config
echo "INSTALL_PREFIX=$GITHUB_WORKSPACE/install" >> "$GITHUB_ENV"
- name: Install dependencies (macOS)
if: runner.os == 'macOS'
run: |
brew install console_bridge googletest pkgconf
echo "INSTALL_PREFIX=$GITHUB_WORKSPACE/install" >> "$GITHUB_ENV"
- name: Install dependencies (Windows)
if: runner.os == 'Windows'
run: |
vcpkg install console-bridge gtest --triplet x64-windows
vcpkg_root=$(cygpath -m "$VCPKG_INSTALLATION_ROOT")
echo "CMAKE_TOOLCHAIN_FILE=$vcpkg_root/scripts/buildsystems/vcpkg.cmake" >> "$GITHUB_ENV"
cygpath -w "$vcpkg_root/installed/x64-windows/bin" >> "$GITHUB_PATH"
echo "INSTALL_PREFIX=$(cygpath -m "$GITHUB_WORKSPACE/install")" >> "$GITHUB_ENV"
- name: Build
run: |
cmake -S . -B build -DCLASS_LOADER_IGNORE_AMENT=ON -DBUILD_TESTING=ON \
-DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="$INSTALL_PREFIX"
cmake --build build --parallel --config Release
- name: Test
run: ctest --test-dir build --output-on-failure --build-config Release
- name: Install
run: cmake --install build --config Release
- name: Test installed library
run: |
export PKG_CONFIG_PATH="$INSTALL_PREFIX/lib/pkgconfig"
cmake -S test/standalone -B build_test -DCMAKE_BUILD_TYPE=Release \
-DCMAKE_PREFIX_PATH="$INSTALL_PREFIX"
cmake --build build_test --parallel --config Release
ctest --test-dir build_test --output-on-failure --build-config Release
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
Changelog for package class_loader
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Forthcoming
-----------
* Make rcpputils optional (new CMake option ``CLASS_LOADER_USE_RCPPUTILS``), so class_loader can be built without any ROS packages (`#251 <https://github.com/ros/class_loader/issues/251>`_)
* Builds without ament (``CLASS_LOADER_IGNORE_AMENT``): don't require console_bridge_vendor, fix the pkg-config file, install a CMake package configuration, and allow running the tests (`#251 <https://github.com/ros/class_loader/issues/251>`_)
* Contributors: Christoph Hertzberg

3.0.3 (2026-10-22)
------------------
* Include missing dependency in package.xml (`#235 <https://github.com/ros/class_loader/issues/235>`_)
Expand Down
100 changes: 84 additions & 16 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,30 @@ else()
set(explicit_library_type "SHARED")
endif()

find_package(console_bridge_vendor REQUIRED) # Provides console_bridge 0.4.0 on platforms without it.
if(CLASS_LOADER_IGNORE_AMENT)
set(_use_rcpputils_default OFF)
else()
set(_use_rcpputils_default ON)
endif()
option(CLASS_LOADER_USE_RCPPUTILS
Comment thread
mjcarroll marked this conversation as resolved.
Resolved
"Load libraries with rcpputils (otherwise with a minimal built-in replacement)"
${_use_rcpputils_default})

if(NOT CLASS_LOADER_IGNORE_AMENT)
# Provides console_bridge 0.4.0 on platforms without it.
find_package(console_bridge_vendor REQUIRED)
endif()
find_package(console_bridge REQUIRED)

find_package(rcpputils REQUIRED)
if(CLASS_LOADER_USE_RCPPUTILS)
find_package(rcpputils REQUIRED)
endif()

if(ament_cmake_FOUND)
if(NOT CLASS_LOADER_IGNORE_AMENT)
ament_export_dependencies(console_bridge)
ament_export_dependencies(rcpputils)
if(CLASS_LOADER_USE_RCPPUTILS)
ament_export_dependencies(rcpputils)
endif()
endif()

add_library(${PROJECT_NAME} ${explicit_library_type}
Expand All @@ -42,21 +58,37 @@ add_library(${PROJECT_NAME} ${explicit_library_type}
target_include_directories(${PROJECT_NAME} PUBLIC
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
"$<INSTALL_INTERFACE:include/${PROJECT_NAME}>")
# The public headers use C++20 (e.g. concepts), so users need it as well.
# (Previously, this was only propagated through rcpputils.)
target_compile_features(${PROJECT_NAME} INTERFACE cxx_std_20)
# dlopen() is used directly (and by the built-in shared library wrapper).
target_link_libraries(${PROJECT_NAME} PRIVATE ${CMAKE_DL_LIBS})
if(CLASS_LOADER_USE_RCPPUTILS)
target_link_libraries(${PROJECT_NAME} PUBLIC rcpputils::rcpputils)
else()
target_sources(${PROJECT_NAME} PRIVATE src/shared_library.cpp)
# Also needed by users, as the public headers select the shared library wrapper with it.
target_compile_definitions(${PROJECT_NAME} PUBLIC CLASS_LOADER_NO_RCPPUTILS)
endif()

if(ament_cmake_FOUND)
if(NOT CLASS_LOADER_IGNORE_AMENT)
target_link_libraries(${PROJECT_NAME} PUBLIC
console_bridge::console_bridge
rcpputils::rcpputils)
console_bridge::console_bridge)
# ament_ros_defaults is an INTERFACE target providing cxx_std_20 / c_std_17.
# Linked PRIVATE so the standard applies to our own sources without leaking
# into the exported interface (a SHARED library does not propagate PRIVATE
# link requirements to consumers).
target_link_libraries(${PROJECT_NAME} PRIVATE ament_cmake_ros_core::ament_ros_defaults)
ament_export_targets(${PROJECT_NAME})
else()
target_include_directories(${PROJECT_NAME}
PUBLIC ${console_bridge_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} ${console_bridge_LIBRARIES})
if(TARGET console_bridge::console_bridge)
target_link_libraries(${PROJECT_NAME} PUBLIC console_bridge::console_bridge)
else()
# Older console_bridge versions do not provide an imported target.
target_include_directories(${PROJECT_NAME}
PUBLIC ${console_bridge_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} PUBLIC ${console_bridge_LIBRARIES})
endif()
# Standalone (non-ament) build: request C++20 for our own translation units.
target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_20)
endif()
Expand All @@ -67,7 +99,11 @@ if(WIN32)
endif()

if(BUILD_TESTING)
find_package(ament_lint_auto QUIET)
if(CLASS_LOADER_IGNORE_AMENT)
enable_testing()
else()
find_package(ament_lint_auto QUIET)
endif()
if(ament_lint_auto_FOUND)
# TODO(mikaelarguedas) add copyright test once the linter accepts /** block comments
list(APPEND AMENT_LINT_AUTO_EXCLUDE
Expand All @@ -85,15 +121,27 @@ if(BUILD_TESTING)
add_subdirectory(test)
endif()

if(ament_cmake_FOUND)
if(NOT CLASS_LOADER_IGNORE_AMENT)
ament_package(
CONFIG_EXTRAS "class_loader-extras.cmake"
)
else()
set(TARGET_NAME ${PROJECT_NAME})
set(PKGCONFIG_LIBS
${console_bridge_LIBRARIES}
)
file(READ "${CMAKE_CURRENT_SOURCE_DIR}/package.xml" _package_xml)
string(REGEX MATCH "<version[^>]*>([^<]*)</version>" _ "${_package_xml}")
set(PROJECT_VERSION "${CMAKE_MATCH_1}")
if(NOT PROJECT_VERSION)
message(FATAL_ERROR "Could not read the version from package.xml")
endif()
set(PROJECT_DESCRIPTION "Library for loading plugins at runtime")
set(PKGCONFIG_REQUIRES console_bridge)
if(CLASS_LOADER_USE_RCPPUTILS)
message(WARNING "The pkg-config file of class_loader does not provide the flags for "
"rcpputils (which does not provide a pkg-config file). Use the CMake package "
"configuration instead, or build with CLASS_LOADER_USE_RCPPUTILS=OFF.")
else()
set(PKGCONFIG_CFLAGS "-DCLASS_LOADER_NO_RCPPUTILS")
endif()

# Prepare and install necessary files to support finding of the library
# using pkg-config
Expand All @@ -102,7 +150,27 @@ else()
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc"
@ONLY)

install(FILES "${CMAKE_BINARY_DIR}/${PROJECT_NAME}.pc" DESTINATION lib/pkgconfig)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc" DESTINATION lib/pkgconfig)

# Prepare and install a CMake package configuration, providing the target
# class_loader::class_loader as in ament builds
install(EXPORT ${PROJECT_NAME}
NAMESPACE ${PROJECT_NAME}::
FILE ${PROJECT_NAME}Targets.cmake
DESTINATION share/${PROJECT_NAME}/cmake)
include(CMakePackageConfigHelpers)
configure_package_config_file(
"${CMAKE_CURRENT_SOURCE_DIR}/src/${PROJECT_NAME}Config.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
INSTALL_DESTINATION share/${PROJECT_NAME}/cmake)
write_basic_package_version_file(
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
VERSION ${PROJECT_VERSION}
COMPATIBILITY AnyNewerVersion) # as in ament builds
install(FILES
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
DESTINATION share/${PROJECT_NAME}/cmake)
endif()

install(TARGETS ${PROJECT_NAME} EXPORT ${PROJECT_NAME}
Expand Down
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,21 @@ int main()
```

Visit the [class_loader](https://docs.ros.org/en/rolling/p/class_loader/) API documentation for a complete list of its main components.

# Building without ROS

class_loader can also be built and used without ament and other ROS packages; only [console_bridge](https://github.com/ros/console_bridge) is required (and GoogleTest for the tests). For example, with plain CMake:
```
cmake -S . -B build -DCLASS_LOADER_IGNORE_AMENT=ON
cmake --build build
cmake --install build
```

The following CMake options control this:

- `CLASS_LOADER_IGNORE_AMENT` (default: `OFF`): Build without ament. Instead of the ament package, a CMake package configuration (`find_package(class_loader)`, providing the target `class_loader::class_loader`) and a pkg-config file (`class_loader.pc`) are installed. With `BUILD_TESTING=ON`, the tests use GoogleTest from the system.
- `CLASS_LOADER_USE_RCPPUTILS` (default: `ON`, or `OFF` if `CLASS_LOADER_IGNORE_AMENT` is `ON`): Load libraries with `rcpputils::SharedLibrary`. If `OFF`, a minimal built-in replacement is used, and rcpputils (and rcutils) are not required.

With `CLASS_LOADER_USE_RCPPUTILS=OFF`, the preprocessor symbol `CLASS_LOADER_NO_RCPPUTILS` is defined publicly, i.e., for class_loader and for all its users (via the CMake target and the pkg-config file), because it changes types used in the public headers. All code using class_loader must be compiled with the same setting as class_loader itself; anything else violates the one definition rule (ODR).

The public headers require C++20. The CMake target provides this requirement, users of the pkg-config file need to enable C++20 themselves. The pkg-config file does not provide the flags for rcpputils, so with `CLASS_LOADER_USE_RCPPUTILS=ON` (and without ament), use the CMake package configuration.
5 changes: 2 additions & 3 deletions include/class_loader/class_loader_core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,9 @@
#include "class_loader/exceptions.hpp"
#include "class_loader/interface_traits.hpp"
#include "class_loader/meta_object.hpp"
#include "class_loader/shared_library.hpp"
#include "class_loader/visibility_control.hpp"

#include "rcpputils/shared_library.hpp"

/**
* @note This header file is the internal implementation of the plugin system which is exposed
* via the ClassLoader class
Expand All @@ -81,7 +80,7 @@ using ClassName = std::string;
using BaseClassName = std::string;
using FactoryMap = std::map<ClassName, impl::AbstractMetaObjectBase *>;
using BaseToFactoryMapMap = std::map<BaseClassName, FactoryMap>;
using LibraryPair = std::pair<LibraryPath, std::shared_ptr<rcpputils::SharedLibrary>>;
using LibraryPair = std::pair<LibraryPath, std::shared_ptr<SharedLibrary>>;
using LibraryVector = std::vector<LibraryPair>;
using MetaObjectVector = std::vector<AbstractMetaObjectBase *>;
class MetaObjectGraveyardVector : public MetaObjectVector
Expand Down
113 changes: 113 additions & 0 deletions include/class_loader/shared_library.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* Copyright (c) 2026, DFKI GmbH
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the copyright holders nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef CLASS_LOADER__SHARED_LIBRARY_HPP_
#define CLASS_LOADER__SHARED_LIBRARY_HPP_

/**
* @note This header is part of the internal implementation. It selects the shared library
* wrapper used by class_loader: rcpputils::SharedLibrary by default, or a minimal built-in
* replacement if class_loader was built without rcpputils (CMake option
* CLASS_LOADER_USE_RCPPUTILS=OFF), in which case CLASS_LOADER_NO_RCPPUTILS is defined for the
* library and all its users.
*/

#include <string>

#ifdef CLASS_LOADER_NO_RCPPUTILS

#include "class_loader/visibility_control.hpp"

namespace class_loader
{
namespace impl
{

/// Minimal replacement for the subset of rcpputils::SharedLibrary used by class_loader.
class CLASS_LOADER_PUBLIC SharedLibrary
{
public:
/// Loads the library at the given path (or found by the system's library search).
/**
* \throws std::runtime_error if the library cannot be loaded
*/
explicit SharedLibrary(const std::string & library_path);

/// Unloads the library, if it has not been unloaded with unload_library() before.
~SharedLibrary();

SharedLibrary(const SharedLibrary &) = delete;
SharedLibrary & operator=(const SharedLibrary &) = delete;

/// Unloads the library.
/**
* \throws std::runtime_error if the library is not loaded or cannot be unloaded
*/
void unload_library();

/// Returns the path of the loaded library (resolved by the system loader where supported).
/**
* \throws std::runtime_error if the library is not loaded
*/
std::string get_library_path() const;

private:
void * handle_ = nullptr;
std::string library_path_;
};

/// Same as rcpputils::get_platform_library_name(): adds the platform's prefix and suffix.
/**
* E.g. "foo" becomes "libfoo.so" on Linux, "libfoo.dylib" on macOS and "foo.dll" on Windows.
* With debug=true, a "d" is appended to the library name ("libfood.so").
*/
CLASS_LOADER_PUBLIC
std::string get_platform_library_name(std::string library_name, bool debug = false);

} // namespace impl
} // namespace class_loader

#else // CLASS_LOADER_NO_RCPPUTILS

#include "rcpputils/shared_library.hpp"

namespace class_loader
{
namespace impl
{

using SharedLibrary = rcpputils::SharedLibrary;
using rcpputils::get_platform_library_name;

} // namespace impl
} // namespace class_loader

#endif // CLASS_LOADER_NO_RCPPUTILS

#endif // CLASS_LOADER__SHARED_LIBRARY_HPP_
Loading
Loading