Skip to content
Merged
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
80 changes: 69 additions & 11 deletions src/SystemLoader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <optional>
#include <sstream>
#include <string>
#include <string_view>
#include <unordered_set>

#include <gz/sim/SystemLoader.hh>
Expand All @@ -39,6 +40,10 @@ using namespace gz::sim;

class gz::sim::SystemLoaderPrivate
{
//////////////////////////////////////////////////
public: static constexpr std::string_view kStaticPluginFilenamePrefix =
"static://";

//////////////////////////////////////////////////
public: explicit SystemLoaderPrivate() = default;

Expand All @@ -60,6 +65,62 @@ class gz::sim::SystemLoaderPrivate
return systemPaths.PluginPaths();
}

//////////////////////////////////////////////////
public: std::string FixDeprecatedPluginName(const std::string &_pluginName)
{
std::string newPluginName = _pluginName;
constexpr std::string_view deprecatedPluginNamePrefix{"ignition::gazebo"};
if (auto pos = _pluginName.find(deprecatedPluginNamePrefix);
pos != std::string::npos)
{
newPluginName.replace(pos, deprecatedPluginNamePrefix.size(), "gz::sim");
gzwarn << "Trying to load deprecated plugin name [" << _pluginName
<< "]. Using [" << newPluginName << "] instead."
<< std::endl;
}
return newPluginName;
}

//////////////////////////////////////////////////
public: bool InstantiateStaticSystemPlugin(const sdf::Plugin &_sdfPlugin,
gz::plugin::PluginPtr &_gzPlugin)
{
const size_t prefixLen = kStaticPluginFilenamePrefix.size();
const std::string filenameWoPrefix =
_sdfPlugin.Filename().substr(prefixLen);
std::string pluginToInstantiate =
this->FixDeprecatedPluginName(filenameWoPrefix);

_gzPlugin = this->loader.Instantiate(pluginToInstantiate);

if (!_gzPlugin)
{
gzerr << "Failed to load system plugin: "
<< "(Reason: static plugin registry does not contain the requested "
"plugin)\n"
<< "- Requested plugin name: [" << _sdfPlugin.Name() << "]\n"
<< "- Requested library name: [" << _sdfPlugin.Filename() << "]\n";
return false;
}

if (!_gzPlugin->HasInterface<System>())
{
std::stringstream ss;
ss << "Failed to load system plugin: "
<< "(Reason: plugin does not implement System interface)\n"
<< "- Requested plugin name: [" << _sdfPlugin.Name() << "]\n"
<< "- Requested library name: [" << _sdfPlugin.Filename() << "]\n"
<< "- Plugin Interfaces Implemented:\n";
for (const auto &interfaceIt : this->loader.InterfacesImplemented())
{
ss << " - " << interfaceIt << "\n";
}
return false;
}

return true;
}

//////////////////////////////////////////////////
public: bool InstantiateSystemPlugin(const sdf::Plugin &_sdfPlugin,
gz::plugin::PluginPtr &_gzPlugin)
Expand All @@ -75,6 +136,12 @@ class gz::sim::SystemLoaderPrivate
<< "]. Using [" << filename << "] instead." << std::endl;
}

if (filename.substr(0, kStaticPluginFilenamePrefix.size()) ==
kStaticPluginFilenamePrefix)
{
return this->InstantiateStaticSystemPlugin(_sdfPlugin, _gzPlugin);
}

const std::list<std::string> paths = this->PluginPaths();
common::SystemPaths systemPaths;
for (const auto &p : paths)
Expand Down Expand Up @@ -125,17 +192,8 @@ class gz::sim::SystemLoaderPrivate
pluginName : _sdfPlugin.Name();

// Deprecated: accept ignition plugins.
std::string deprecatedPluginNamePrefix{"ignition::gazebo"};
pos = pluginToInstantiate.find(deprecatedPluginNamePrefix);
if (pos != std::string::npos)
{
auto origPluginName = pluginToInstantiate;
pluginToInstantiate.replace(pos, deprecatedPluginNamePrefix.size(),
"gz::sim");
gzwarn << "Trying to load deprecated plugin name [" << origPluginName
<< "]. Using [" << pluginToInstantiate << "] instead."
<< std::endl;
}
pluginToInstantiate =
this->FixDeprecatedPluginName(pluginToInstantiate);

_gzPlugin = this->loader.Instantiate(pluginToInstantiate);
if (!_gzPlugin)
Expand Down
30 changes: 30 additions & 0 deletions test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,36 @@ cc_library(
],
)

cc_library(
name = "TestStaticModelSystem",
testonly = 1,
srcs = [
"plugins/TestModelSystem.hh",
"plugins/TestStaticModelSystem.cc",
],
deps = [
"//:gz-sim",
"@gz-msgs//:gzmsgs_cc_proto",
"@gz-plugin//:register",
"@gz-transport",
],
alwayslink = 1,
)

cc_test(
name = "INTEGRATION_load_system_static_registry",
srcs = ["integration/load_system_static_registry.cc"],
deps = [
":Helpers",
":MockSystem",
":TestStaticModelSystem",
"//:gz-sim",
"@googletest//:gtest",
"@googletest//:gtest_main",
"@gz-common",
],
)

filegroup(
name = "worlds",
srcs = glob(["worlds/**"]),
Expand Down
24 changes: 24 additions & 0 deletions test/integration/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ set(tests
light.cc
lighter_than_air_dynamics.cc
link.cc
load_system_static_registry.cc
logical_camera_system.cc
logical_audio_sensor_plugin.cc
magnetometer_system.cc
Expand Down Expand Up @@ -249,3 +250,26 @@ if (TARGET INTEGRATION_python_system_loader)
set_tests_properties(INTEGRATION_python_system_loader PROPERTIES
ENVIRONMENT "PYTHONPATH=${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}/python/:$ENV{PYTHONPATH}")
endif()

if (TARGET INTEGRATION_load_system_static_registry)
# The linker option -force_load (for Clang) or --whole-archive (for GNU)
# or /WHOLEARCHIVE: (for MSVC) needs to be specified to ensure that the
# global structs specified in the static plugin module get loaded even
# without any explicit reference to the loaded symbols
# (only the interfaces are referenced).
if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
target_link_libraries(INTEGRATION_load_system_static_registry
-WHOLEARCHIVE:$<TARGET_FILE:TestStaticModelSystem>)

# The whole-archive invocation doesn't correctly compute dependencies,
# so explicitly require the plugin before the test can build.
add_dependencies(INTEGRATION_load_system_static_registry
TestStaticModelSystem)
else()
target_link_libraries(INTEGRATION_load_system_static_registry
$<$<CXX_COMPILER_ID:GNU>:-Wl,--whole-archive>
$<$<CXX_COMPILER_ID:Clang>:-force_load>
$<$<CXX_COMPILER_ID:AppleClang>:-force_load> TestStaticModelSystem
$<$<CXX_COMPILER_ID:GNU>:-Wl,--no-whole-archive>)
endif()
endif()
88 changes: 88 additions & 0 deletions test/integration/load_system_static_registry.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright (C) 2025 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

#include <gtest/gtest.h>

#include <gz/common/Util.hh>

#include "../helpers/EnvTestFixture.hh"
#include "gz/sim/Server.hh"
#include "plugins/MockSystem.hh"

using namespace gz;
using namespace sim;

/// \brief Test loading system from static plugin registry
class LoadSystemStaticRegistryTest : public InternalFixture<::testing::Test> {};

TEST_F(LoadSystemStaticRegistryTest, LoadWorks)
{
// Start server
ServerConfig serverConfig;
serverConfig.SetSdfString(R"(
<?xml version="1.0"?>
<sdf version="1.12">
<world name="default">
<model name="test_model1">
<static>true</static>
<link name="link_1" />
<plugin filename="static://gz::sim::TestModelSystem"
name="gz::sim::TestModelSystem">
<model_key>98765</model_key>
</plugin>
</model>
<model name="test_model2">
<static>true</static>
<link name="link_1" />
<!-- Plugin filename with registered alias -->
<plugin filename="static://StaticTestModelSystem"
name="gz::sim::TestModelSystem">
<model_key>54321</model_key>
</plugin>
</model>
</world>
</sdf>)");

Server server(serverConfig);

// Verify that server was initialized correctly
auto iterationCount = server.IterationCount();
ASSERT_NE(iterationCount, std::nullopt);
ASSERT_EQ(*iterationCount, 0);

std::optional<Entity> model1Id = server.EntityByName("test_model1");
ASSERT_NE(model1Id, std::nullopt);
std::optional<Entity> model2Id = server.EntityByName("test_model2");
ASSERT_NE(model2Id, std::nullopt);

// Verify that the System was instantiated for both models by checking the ECM
// for the configured `ModelPluginComponent`.
auto mockSystem = std::make_shared<MockSystem>();
mockSystem->postUpdateCallback =
[model1Id, model2Id](const sim::UpdateInfo &,
const sim::EntityComponentManager &_ecm)
{
const std::string modelComponentName{"ModelPluginComponent"};
auto modelComponentId = common::hash64(modelComponentName);
EXPECT_TRUE(_ecm.HasComponentType(modelComponentId));
EXPECT_TRUE(_ecm.EntityHasComponentType(*model1Id, modelComponentId));
EXPECT_TRUE(_ecm.EntityHasComponentType(*model2Id, modelComponentId));
};
ASSERT_TRUE(server.AddSystem(mockSystem));
server.RunOnce();
EXPECT_EQ(1, mockSystem->postUpdateCallCount);
}
7 changes: 7 additions & 0 deletions test/plugins/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,10 @@ if(BUILD_TESTING)
endif()
endforeach (src)
endif()

# Statically linked test plugin
add_library(TestStaticModelSystem STATIC TestStaticModelSystem.cc)
target_link_libraries(TestStaticModelSystem PRIVATE
gz-plugin${GZ_PLUGIN_VER}::register
gz-transport${GZ_TRANSPORT_VER}::gz-transport${GZ_TRANSPORT_VER}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think gz-transport is not needed here

Copy link
Contributor Author

@shameekganguly shameekganguly Jun 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe it is needed since the plugin implementation in the header file test/plugins/TestModelSystem.hh exposes a gz transport service.

gz-sim${PROJECT_VERSION_MAJOR})
25 changes: 25 additions & 0 deletions test/plugins/TestStaticModelSystem.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (C) 2025 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <gz/plugin/RegisterStatic.hh>

#include "TestModelSystem.hh"

GZ_ADD_STATIC_PLUGIN(gz::sim::TestModelSystem,
gz::sim::System,
gz::sim::TestModelSystem::ISystemConfigure)

GZ_ADD_STATIC_PLUGIN_ALIAS(gz::sim::TestModelSystem, "StaticTestModelSystem")
Loading