From 0ac1a4ebb61bdae808e7eb02312d7d1403b5c91a Mon Sep 17 00:00:00 2001 From: Silvio Traversaro Date: Tue, 22 Nov 2022 00:18:05 +0100 Subject: [PATCH] Fix for binary relocatability --- include/ignition/gazebo/config.hh.in | 48 +++++++++++++ src/CMakeLists.txt | 3 + src/ServerConfig.cc | 2 +- src/SystemLoader.cc | 2 +- src/Util.cc | 2 +- src/config.cc | 76 ++++++++++++++++++++ src/gui/Gui.cc | 7 +- src/gui/QuickStartHandler.hh | 2 +- src/ign.cc | 3 +- src/systems/physics/EntityFeatureMap_TEST.cc | 2 +- src/systems/physics/Physics.cc | 2 +- test/integration/tracked_vehicle_system.cc | 2 +- 12 files changed, 140 insertions(+), 11 deletions(-) create mode 100644 src/config.cc diff --git a/include/ignition/gazebo/config.hh.in b/include/ignition/gazebo/config.hh.in index 8a817d1687e..a46069cec87 100644 --- a/include/ignition/gazebo/config.hh.in +++ b/include/ignition/gazebo/config.hh.in @@ -1,5 +1,12 @@ /* Config.hh. Generated by CMake for @PROJECT_NAME_NO_VERSION@. */ +#ifndef IGNITION_${GZ_DESIGNATION_UPPER}_CONFIG_HH_ +#define IGNITION_${GZ_DESIGNATION_UPPER}_CONFIG_HH_ + +#include + +#include + /* Version number */ #define IGNITION_GAZEBO_MAJOR_VERSION ${PROJECT_VERSION_MAJOR} #define IGNITION_GAZEBO_MINOR_VERSION ${PROJECT_VERSION_MINOR} @@ -19,8 +26,49 @@ #define IGN_GAZEBO_PLUGIN_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/${IGN_LIB_INSTALL_DIR}/ign-${IGN_DESIGNATION}-${PROJECT_VERSION_MAJOR}/plugins" #define IGN_GAZEBO_GUI_PLUGIN_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/${IGN_LIB_INSTALL_DIR}/ign-${IGN_DESIGNATION}-${PROJECT_VERSION_MAJOR}/plugins/gui" #define IGN_GAZEBO_WORLD_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/${IGN_DATA_INSTALL_DIR}/worlds" + +#define IGNITION_GAZEBO_GUI_CONFIG_RELATIVE_PATH "${IGN_DATA_INSTALL_DIR}/gui" +#define IGNITION_GAZEBO_SYSTEM_CONFIG_RELATIVE_PATH "${IGN_DATA_INSTALL_DIR}/systems" +#define IGNITION_GAZEBO_SERVER_CONFIG_RELATIVE_PATH "${IGN_DATA_INSTALL_DIR}" +#define IGN_GAZEBO_PLUGIN_RELATIVE_INSTALL_DIR "${IGN_LIB_INSTALL_DIR}/ign-${IGN_DESIGNATION}-${PROJECT_VERSION_MAJOR}/plugins" +#define IGN_GAZEBO_GUI_PLUGIN_RELATIVE_INSTALL_DIR "${IGN_LIB_INSTALL_DIR}/ign-${IGN_DESIGNATION}-${PROJECT_VERSION_MAJOR}/plugins/gui" +#define IGN_GAZEBO_WORLD_RELATIVE_INSTALL_DIR "${IGN_DATA_INSTALL_DIR}/worlds" + + + #define GZ_DISTRIBUTION "${GZ_DISTRIBUTION}" #cmakedefine IGNITION_GAZEBO_BUILD_TYPE_PROFILE 1 #cmakedefine IGNITION_GAZEBO_BUILD_TYPE_DEBUG 1 #cmakedefine IGNITION_GAZEBO_BUILD_TYPE_RELEASE 1 + +namespace ignition +{ + namespace gazebo + { + /// \brief getInstallPrefix return the install prefix of the library + /// i.e. CMAKE_INSTALL_PREFIX unless the library has been moved + IGNITION_GAZEBO_VISIBLE std::string getInstallPrefix(); + + /// \brief getGUIConfigPath return the GUI config path + IGNITION_GAZEBO_VISIBLE std::string getGUIConfigPath(); + + /// \brief getSystemConfigPath return the system config path + IGNITION_GAZEBO_VISIBLE std::string getSystemConfigPath(); + + /// \brief getServerConfigPath return the server config path + IGNITION_GAZEBO_VISIBLE std::string getServerConfigPath(); + + /// \brief getPluginInstallDir return the plugin install dir + IGNITION_GAZEBO_VISIBLE std::string getPluginInstallDir(); + + /// \brief getGUIPluginInstallDir return the GUI plugin install dir + IGNITION_GAZEBO_VISIBLE std::string getGUIPluginInstallDir(); + + /// \brief getWorldInstallDir return the world install dir + IGNITION_GAZEBO_VISIBLE std::string getWorldInstallDir(); + } +} + + +#endif diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index d4ea8bc4028..9957edfccfa 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -64,6 +64,7 @@ set (sources View.cc World.cc cmd/ModelCommandAPI.cc + config.cc ${PROTO_PRIVATE_SRC} ${network_sources} ${comms_sources} @@ -152,6 +153,8 @@ target_link_libraries(${ign_lib_target} # Create the library target ign_create_core_library(SOURCES ${sources} CXX_STANDARD 17) +ign_add_get_install_prefix_impl(GET_INSTALL_PREFIX_FUNCTION ignition::gazebo::getInstallPrefix) + target_link_libraries(${PROJECT_LIBRARY_TARGET_NAME} PUBLIC ignition-math${IGN_MATH_VER} diff --git a/src/ServerConfig.cc b/src/ServerConfig.cc index d5c6d027bbc..555e5354de0 100644 --- a/src/ServerConfig.cc +++ b/src/ServerConfig.cc @@ -982,7 +982,7 @@ gazebo::loadPluginInfo(bool _isPlayback) if (!common::exists(defaultConfig)) { auto installedConfig = common::joinPaths( - IGNITION_GAZEBO_SERVER_CONFIG_PATH, + getServerConfigPath(), configFilename); if (!common::createDirectories(defaultConfigDir)) diff --git a/src/SystemLoader.cc b/src/SystemLoader.cc index fd8556cb7fb..10faa152788 100644 --- a/src/SystemLoader.cc +++ b/src/SystemLoader.cc @@ -53,7 +53,7 @@ class ignition::gazebo::SystemLoaderPrivate ignition::common::env(IGN_HOMEDIR, homePath); systemPaths.AddPluginPaths(common::joinPaths( homePath, ".ignition", "gazebo", "plugins")); - systemPaths.AddPluginPaths(IGN_GAZEBO_PLUGIN_INSTALL_DIR); + systemPaths.AddPluginPaths(getPluginInstallDir()); return systemPaths.PluginPaths(); } diff --git a/src/Util.cc b/src/Util.cc index 1b1706c4532..885371796a1 100644 --- a/src/Util.cc +++ b/src/Util.cc @@ -729,7 +729,7 @@ std::string resolveSdfWorldFile(const std::string &_sdfFile, systemPaths.SetFilePathEnv(kResourcePathEnv); // Worlds installed with ign-gazebo - systemPaths.AddFilePaths(IGN_GAZEBO_WORLD_INSTALL_DIR); + systemPaths.AddFilePaths(getWorldInstallDir()); filePath = systemPaths.FindFile(_sdfFile); } diff --git a/src/config.cc b/src/config.cc new file mode 100644 index 00000000000..158d4e5768f --- /dev/null +++ b/src/config.cc @@ -0,0 +1,76 @@ +/* + * Copyright (C) 2022 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 + +#include + +namespace ignition +{ + namespace gazebo + { + + std::string getGUIConfigPath() + { + std::filesystem::path relative_ret_path(IGNITION_GAZEBO_GUI_CONFIG_RELATIVE_PATH); + std::filesystem::path install_prefix(getInstallPrefix()); + std::filesystem::path ret_path = install_prefix / relative_ret_path; + return ret_path.string(); + } + + std::string getSystemConfigPath() + { + std::filesystem::path relative_ret_path(IGNITION_GAZEBO_SYSTEM_CONFIG_RELATIVE_PATH); + std::filesystem::path install_prefix(getInstallPrefix()); + std::filesystem::path ret_path = install_prefix / relative_ret_path; + return ret_path.string(); + } + + std::string getServerConfigPath() + { + std::filesystem::path relative_ret_path(IGNITION_GAZEBO_SERVER_CONFIG_RELATIVE_PATH); + std::filesystem::path install_prefix(getInstallPrefix()); + std::filesystem::path ret_path = install_prefix / relative_ret_path; + return ret_path.string(); + } + + std::string getPluginInstallDir() + { + std::filesystem::path relative_ret_path(IGN_GAZEBO_PLUGIN_RELATIVE_INSTALL_DIR); + std::filesystem::path install_prefix(getInstallPrefix()); + std::filesystem::path ret_path = install_prefix / relative_ret_path; + return ret_path.string(); + } + + std::string getGUIPluginInstallDir() + { + std::filesystem::path relative_ret_path(IGN_GAZEBO_GUI_PLUGIN_RELATIVE_INSTALL_DIR); + std::filesystem::path install_prefix(getInstallPrefix()); + std::filesystem::path ret_path = install_prefix / relative_ret_path; + return ret_path.string(); + } + + std::string getWorldInstallDir() + { + std::filesystem::path relative_ret_path(IGN_GAZEBO_WORLD_RELATIVE_INSTALL_DIR); + std::filesystem::path install_prefix(getInstallPrefix()); + std::filesystem::path ret_path = install_prefix / relative_ret_path; + return ret_path.string(); + } + + } +} diff --git a/src/gui/Gui.cc b/src/gui/Gui.cc index a0723855367..bdb0304cc2c 100644 --- a/src/gui/Gui.cc +++ b/src/gui/Gui.cc @@ -90,7 +90,7 @@ std::string defaultGuiConfigFile(bool _isPlayback, } auto installedConfig = common::joinPaths( - IGNITION_GAZEBO_GUI_CONFIG_PATH, defaultGuiConfigName); + getGUIConfigPath(), defaultGuiConfigName); if (!common::copyFile(installedConfig, defaultConfig)) { ignerr << "Failed to copy installed config [" << installedConfig @@ -268,7 +268,7 @@ std::unique_ptr createGui( auto app = std::make_unique( _argc, _argv, ignition::gui::WindowType::kMainWindow); - app->AddPluginPath(IGN_GAZEBO_GUI_PLUGIN_INSTALL_DIR); + app->AddPluginPath(getGUIPluginInstallDir()); auto aboutDialogHandler = new ignition::gazebo::gui::AboutDialogHandler(); aboutDialogHandler->setParent(app->Engine()); @@ -280,7 +280,8 @@ std::unique_ptr createGui( pathManager->setParent(app->Engine()); // add import path so we can load custom modules - app->Engine()->addImportPath(IGN_GAZEBO_GUI_PLUGIN_INSTALL_DIR); + std::string gui_plugin_install_dir = getGUIPluginInstallDir(); + app->Engine()->addImportPath(gui_plugin_install_dir.c_str()); app->SetDefaultConfigPath(defaultConfig); diff --git a/src/gui/QuickStartHandler.hh b/src/gui/QuickStartHandler.hh index 6b0d921eb2c..0b8a40ab9cd 100644 --- a/src/gui/QuickStartHandler.hh +++ b/src/gui/QuickStartHandler.hh @@ -71,7 +71,7 @@ class IGNITION_GAZEBO_GUI_VISIBLE QuickStartHandler : public QObject private: std::string worldsPath{IGN_GAZEBO_WORLD_INSTALL_DIR}; /// \brief Get starting world url. - private: std::string startingWorld{""}; + private: std::string startingWorld{getWorldInstallDir()}; }; } } diff --git a/src/ign.cc b/src/ign.cc index c846aacecee..c4a41b1f5c6 100644 --- a/src/ign.cc +++ b/src/ign.cc @@ -69,7 +69,8 @@ extern "C" void cmdVerbosity( ////////////////////////////////////////////////// extern "C" const char *worldInstallDir() { - return IGN_GAZEBO_WORLD_INSTALL_DIR; + static std::string worldInstallDir = getWorldInstallDir(); + return worldInstallDir.c_str(); } ////////////////////////////////////////////////// diff --git a/src/systems/physics/EntityFeatureMap_TEST.cc b/src/systems/physics/EntityFeatureMap_TEST.cc index 510b5d38669..249195d73d1 100644 --- a/src/systems/physics/EntityFeatureMap_TEST.cc +++ b/src/systems/physics/EntityFeatureMap_TEST.cc @@ -57,7 +57,7 @@ class EntityFeatureMapFixture: public InternalFixture<::testing::Test> const std::string pluginLib = "libignition-physics-dartsim-plugin.so"; common::SystemPaths systemPaths; - systemPaths.AddPluginPaths({IGNITION_PHYSICS_ENGINE_INSTALL_DIR}); + systemPaths.AddPluginPaths(ignition::physics::getEngineInstallDir()); auto pathToLib = systemPaths.FindSharedLibrary(pluginLib); ASSERT_FALSE(pathToLib.empty()) diff --git a/src/systems/physics/Physics.cc b/src/systems/physics/Physics.cc index 33eb83abdf2..b42cce443a6 100644 --- a/src/systems/physics/Physics.cc +++ b/src/systems/physics/Physics.cc @@ -752,7 +752,7 @@ void Physics::Configure(const Entity &_entity, // * Engines installed with ign-physics common::SystemPaths systemPaths; systemPaths.SetPluginPathEnv(this->dataPtr->pluginPathEnv); - systemPaths.AddPluginPaths({IGNITION_PHYSICS_ENGINE_INSTALL_DIR}); + systemPaths.AddPluginPaths(ignition::physics::getEngineInstallDir()); auto pathToLib = systemPaths.FindSharedLibrary(pluginLib); if (pathToLib.empty()) diff --git a/test/integration/tracked_vehicle_system.cc b/test/integration/tracked_vehicle_system.cc index 2d8180bf29b..6fbdbc8a327 100644 --- a/test/integration/tracked_vehicle_system.cc +++ b/test/integration/tracked_vehicle_system.cc @@ -83,7 +83,7 @@ class TrackedVehicleTest : public InternalFixture<::testing::Test> // modifications) common::SystemPaths systemPaths; systemPaths.SetPluginPathEnv("IGN_GAZEBO_PHYSICS_ENGINE_PATH"); - systemPaths.AddPluginPaths({IGNITION_PHYSICS_ENGINE_INSTALL_DIR}); + systemPaths.AddPluginPaths(ignition::physics::getEngineInstallDir()); auto pathToLib = systemPaths.FindSharedLibrary(*pluginLib); ASSERT_FALSE(pathToLib.empty())