From a61945e625385893343c544feb53db731e9bca6c Mon Sep 17 00:00:00 2001 From: Silvio Traversaro Date: Tue, 22 Nov 2022 00:06:45 +0100 Subject: [PATCH] Enable binary relocatability Signed-off-by: Silvio Traversaro --- CMakeLists.txt | 10 ++-- .../gz/rendering/InstallationDirectories.hh | 46 +++++++++++++++++++ include/gz/rendering/config.hh.in | 4 +- ogre/src/CMakeLists.txt | 1 - ogre/src/OgreCamera.cc | 1 + ogre/src/OgreDepthCamera.cc | 3 +- ogre/src/OgreMaterial.cc | 3 +- ogre/src/OgreRTShaderSystem.cc | 4 +- ogre/src/OgreRenderEngine.cc | 3 +- ogre2/src/CMakeLists.txt | 1 - ogre2/src/Ogre2RenderEngine.cc | 6 ++- optix/src/CMakeLists.txt | 2 - optix/src/OptixRenderEngine.cc | 2 +- src/CMakeLists.txt | 13 +++++- src/InstallationDirectories.cc | 41 +++++++++++++++++ src/RenderEngineManager.cc | 4 +- src/base/BaseScene.cc | 3 +- 17 files changed, 126 insertions(+), 21 deletions(-) create mode 100644 include/gz/rendering/InstallationDirectories.hh create mode 100644 src/InstallationDirectories.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index dcef901d5..947bf32a8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -125,9 +125,12 @@ if (OGRE2_FOUND) endif() # Plugin install dirs -set(GZ_RENDERING_ENGINE_INSTALL_DIR - ${CMAKE_INSTALL_PREFIX}/${GZ_LIB_INSTALL_DIR}/gz-${GZ_DESIGNATION}-${PROJECT_VERSION_MAJOR}/engine-plugins +set(GZ_RENDERING_ENGINE_RELATIVE_INSTALL_DIR + ${GZ_LIB_INSTALL_DIR}/gz-${GZ_DESIGNATION}-${PROJECT_VERSION_MAJOR}/engine-plugins ) +set(GZ_RENDERING_ENGINE_INSTALL_DIR + ${CMAKE_INSTALL_PREFIX}/${GZ_RENDERING_ENGINE_RELATIVE_INSTALL_DIR}) + #-------------------------------------- # Find dependencies that we ignore for Visual Studio @@ -155,7 +158,8 @@ else() set(GZ_RENDERING_PLUGIN_PATH ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}) endif() -set(GZ_RENDERING_RESOURCE_PATH ${CMAKE_INSTALL_PREFIX}/${GZ_DATA_INSTALL_DIR}) +set(GZ_RENDERING_RELATIVE_RESOURCE_PATH ${GZ_DATA_INSTALL_DIR}) +set(GZ_RENDERING_RESOURCE_PATH ${CMAKE_INSTALL_PREFIX}/${GZ_RENDERING_RELATIVE_RESOURCE_PATH}) #============================================================================ # Configure the build diff --git a/include/gz/rendering/InstallationDirectories.hh b/include/gz/rendering/InstallationDirectories.hh new file mode 100644 index 000000000..60f2a7f2a --- /dev/null +++ b/include/gz/rendering/InstallationDirectories.hh @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2023 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. + * + */ + +#ifndef GZ_RENDERING_INSTALLATION_DIRECTORIES_HH_ +#define GZ_RENDERING_INSTALLATION_DIRECTORIES_HH_ + +#include + +#include +#include + +namespace gz +{ + namespace rendering + { + inline namespace GZ_RENDERING_VERSION_NAMESPACE { + + /// \brief getInstallPrefix return the install prefix of the library + /// i.e. CMAKE_INSTALL_PREFIX unless the library has been moved + GZ_RENDERING_VISIBLE std::string getInstallPrefix(); + + /// \brief getResourcePath return the resource path + GZ_RENDERING_VISIBLE std::string getResourcePath(); + + /// \brief getEngineInstallDir return the install directory of the engines + GZ_RENDERING_VISIBLE std::string getEngineInstallDir(); + + } + } +} + +#endif diff --git a/include/gz/rendering/config.hh.in b/include/gz/rendering/config.hh.in index 948032d94..d852dfbb7 100644 --- a/include/gz/rendering/config.hh.in +++ b/include/gz/rendering/config.hh.in @@ -32,9 +32,9 @@ #define GZ_RENDERING_VERSION_HEADER "Gazebo Rendering, version ${PROJECT_VERSION_FULL}\nCopyright (C) 2014 Open Source Robotics Foundation.\nReleased under the Apache 2.0 License.\n\n" -#define GZ_RENDERING_RESOURCE_PATH "${GZ_RENDERING_RESOURCE_PATH}" +#define GZ_RENDERING_RESOURCE_PATH _Pragma ("GCC warning \"'GZ_RENDERING_RESOURCE_PATH' macro is deprecated, use gz::rendering::getResourcePath() function instead. \"") "${GZ_RENDERING_RESOURCE_PATH}" -#define GZ_RENDERING_ENGINE_INSTALL_DIR "${GZ_RENDERING_ENGINE_INSTALL_DIR}" +#define GZ_RENDERING_ENGINE_INSTALL_DIR _Pragma ("GCC warning \"'GZ_RENDERING_ENGINE_INSTALL_DIR' macro is deprecated, use gz::rendering::getEngineInstallDir() function instead. \"") "${GZ_RENDERING_RESOURCE_PATH}" "${GZ_RENDERING_ENGINE_INSTALL_DIR}" #cmakedefine HAVE_OGRE 1 #cmakedefine HAVE_OGRE2 1 diff --git a/ogre/src/CMakeLists.txt b/ogre/src/CMakeLists.txt index 093301af3..1b68649f6 100644 --- a/ogre/src/CMakeLists.txt +++ b/ogre/src/CMakeLists.txt @@ -27,7 +27,6 @@ find_package(OpenGL) set_property( SOURCE OgreRenderEngine.cc OgreRTShaderSystem.cc OgreGpuRays.cc OgreMaterial.cc OgreDepthCamera.cc OgreThermalCamera.cc PROPERTY COMPILE_DEFINITIONS - GZ_RENDERING_RESOURCE_PATH="${GZ_RENDERING_RESOURCE_PATH}" OGRE_RESOURCE_PATH="${OGRE_RESOURCE_PATH}" ) diff --git a/ogre/src/OgreCamera.cc b/ogre/src/OgreCamera.cc index 53c6a6606..da9587e5f 100644 --- a/ogre/src/OgreCamera.cc +++ b/ogre/src/OgreCamera.cc @@ -15,6 +15,7 @@ * */ +#include "gz/rendering/InstallationDirectories.hh" #include "gz/rendering/ogre/OgreCamera.hh" #include "gz/rendering/ogre/OgreConversions.hh" #include "gz/rendering/ogre/OgreIncludes.hh" diff --git a/ogre/src/OgreDepthCamera.cc b/ogre/src/OgreDepthCamera.cc index 23ea75049..285df433e 100644 --- a/ogre/src/OgreDepthCamera.cc +++ b/ogre/src/OgreDepthCamera.cc @@ -23,6 +23,7 @@ #include #endif #include +#include "gz/rendering/InstallationDirectories.hh" #include "gz/rendering/ogre/OgreDepthCamera.hh" #include "gz/rendering/ogre/OgreMaterial.hh" @@ -235,7 +236,7 @@ void OgreDepthCamera::CreatePointCloudTexture() } std::string resourcePath = (env) ? std::string(env) : - GZ_RENDERING_RESOURCE_PATH; + gz::rendering::getResourcePath(); // path to look for vertex and fragment shader parameters std::string pcdVSPath = common::joinPaths( diff --git a/ogre/src/OgreMaterial.cc b/ogre/src/OgreMaterial.cc index f7ab57b68..4f2c2fc32 100644 --- a/ogre/src/OgreMaterial.cc +++ b/ogre/src/OgreMaterial.cc @@ -18,6 +18,7 @@ #include #include +#include "gz/rendering/InstallationDirectories.hh" #include "gz/rendering/ShaderParams.hh" #include "gz/rendering/ogre/OgreMaterial.hh" #include "gz/rendering/ogre/OgreConversions.hh" @@ -737,7 +738,7 @@ void OgreMaterial::SetDepthMaterial(const double _far, } std::string resourcePath = (env) ? std::string(env) : - GZ_RENDERING_RESOURCE_PATH; + gz::rendering::getResourcePath(); // path to look for vertex and fragment shader parameters std::string depth_vertex_shader_path = common::joinPaths( diff --git a/ogre/src/OgreRTShaderSystem.cc b/ogre/src/OgreRTShaderSystem.cc index e03358c8a..0034207aa 100644 --- a/ogre/src/OgreRTShaderSystem.cc +++ b/ogre/src/OgreRTShaderSystem.cc @@ -28,7 +28,7 @@ #include #include -#include "gz/rendering/config.hh" +#include "gz/rendering/InstallationDirectories.hh" #include "gz/rendering/ogre/OgreRenderEngine.hh" #include "gz/rendering/ogre/OgreScene.hh" #include "gz/rendering/ogre/OgreMaterial.hh" @@ -501,7 +501,7 @@ bool OgreRTShaderSystem::Paths(std::string &coreLibsPath, } std::string resourcePath = (env) ? std::string(env) : - GZ_RENDERING_RESOURCE_PATH; + gz::rendering::getResourcePath(); // path to look for ogre media files std::vector paths; diff --git a/ogre/src/OgreRenderEngine.cc b/ogre/src/OgreRenderEngine.cc index 96351ec45..7a335c52b 100644 --- a/ogre/src/OgreRenderEngine.cc +++ b/ogre/src/OgreRenderEngine.cc @@ -36,6 +36,7 @@ #include #include +#include "gz/rendering/InstallationDirectories.hh" #include "gz/rendering/RenderEngineManager.hh" #include "gz/rendering/ogre/OgreIncludes.hh" #include "gz/rendering/ogre/OgreRenderEngine.hh" @@ -582,7 +583,7 @@ void OgreRenderEngine::CreateResources() } std::string resourcePath = (env) ? std::string(env) : - GZ_RENDERING_RESOURCE_PATH; + gz::rendering::getResourcePath(); // install path std::string mediaPath = common::joinPaths(resourcePath, "ogre", "media"); paths.push_back(mediaPath); diff --git a/ogre2/src/CMakeLists.txt b/ogre2/src/CMakeLists.txt index 7792efae7..796ac6860 100644 --- a/ogre2/src/CMakeLists.txt +++ b/ogre2/src/CMakeLists.txt @@ -18,7 +18,6 @@ gz_add_component(${engine_name} SOURCES ${sources} GET_TARGET_NAME ogre2_target) set_property( SOURCE Ogre2RenderEngine.cc PROPERTY COMPILE_DEFINITIONS - GZ_RENDERING_RESOURCE_PATH="${GZ_RENDERING_RESOURCE_PATH}" OGRE2_RESOURCE_PATH="${OGRE2_RESOURCE_PATH}" OGRE2_VERSION="${OGRE2_VERSION}" ) diff --git a/ogre2/src/Ogre2RenderEngine.cc b/ogre2/src/Ogre2RenderEngine.cc index 4b30f60bd..049781b48 100644 --- a/ogre2/src/Ogre2RenderEngine.cc +++ b/ogre2/src/Ogre2RenderEngine.cc @@ -27,6 +27,7 @@ #include #include "gz/rendering/GraphicsAPI.hh" +#include "gz/rendering/InstallationDirectories.hh" #include "gz/rendering/RenderEngineManager.hh" #include "gz/rendering/ogre2/Ogre2Includes.hh" #include "gz/rendering/ogre2/Ogre2RenderEngine.hh" @@ -766,7 +767,8 @@ void Ogre2RenderEngine::RegisterHlms() } std::string resourcePath = (env) ? std::string(env) : - GZ_RENDERING_RESOURCE_PATH; + gz::rendering::getResourcePath(); + // install path std::string mediaPath = common::joinPaths(resourcePath, "ogre2", "media"); if (!common::exists(mediaPath)) @@ -969,7 +971,7 @@ void Ogre2RenderEngine::CreateResources() } std::string resourcePath = (env) ? std::string(env) : - GZ_RENDERING_RESOURCE_PATH; + gz::rendering::getResourcePath(); // install path std::string mediaPath = common::joinPaths(resourcePath, "ogre2", "media"); if (!common::exists(mediaPath)) diff --git a/optix/src/CMakeLists.txt b/optix/src/CMakeLists.txt index c813f0ba2..d0204ab9b 100644 --- a/optix/src/CMakeLists.txt +++ b/optix/src/CMakeLists.txt @@ -29,8 +29,6 @@ set(cuda_sources set_property( SOURCE OptixRenderEngine.cc - PROPERTY COMPILE_DEFINITIONS - GZ_RENDERING_RESOURCE_PATH="${GZ_RENDERING_RESOURCE_PATH}" ) target_link_libraries(${optix_target} diff --git a/optix/src/OptixRenderEngine.cc b/optix/src/OptixRenderEngine.cc index 9bb862f5d..041aa3508 100644 --- a/optix/src/OptixRenderEngine.cc +++ b/optix/src/OptixRenderEngine.cc @@ -96,7 +96,7 @@ std::string OptixRenderEngine::PtxFile(const std::string& _fileBase) const } std::string resourcePath = (env) ? std::string(env) : - GZ_RENDERING_RESOURCE_PATH; + gz::rendering::getResourcePath(); resourcePath = common::joinPaths(resourcePath, "optix"); folders.push_back(resourcePath); diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c47093632..7acffcb04 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -15,6 +15,9 @@ endif() # Create the library target. gz_create_core_library(SOURCES ${sources}) +gz_add_get_install_prefix_impl(GET_INSTALL_PREFIX_FUNCTION gz::rendering::getInstallPrefix + GET_INSTALL_PREFIX_HEADER gz/rendering/InstallationDirectories.hh + OVERRIDE_INSTALL_PREFIX_ENV_VARIABLE GZ_RENDERING_INSTALL_PREFIX) # Include the interface directories that we always need. #gz_target_interface_include_directories(${PROJECT_LIBRARY_TARGET_NAME} @@ -26,6 +29,13 @@ set_property( GZ_RENDERING_PLUGIN_PATH="${GZ_RENDERING_PLUGIN_PATH}" ) +set_property( + SOURCE InstallationDirectories.cc + PROPERTY COMPILE_DEFINITIONS + GZ_RENDERING_RELATIVE_RESOURCE_PATH="${GZ_RENDERING_RELATIVE_RESOURCE_PATH}" + GZ_RENDERING_ENGINE_RELATIVE_INSTALL_DIR="${GZ_RENDERING_ENGINE_RELATIVE_INSTALL_DIR}" +) + target_link_libraries(${PROJECT_LIBRARY_TARGET_NAME} PUBLIC gz-common${GZ_COMMON_VER}::requested @@ -38,4 +48,5 @@ if (UNIX AND NOT APPLE) endif() # Build the unit tests. -gz_build_tests(TYPE UNIT SOURCES ${gtest_sources}) +gz_build_tests(TYPE UNIT SOURCES ${gtest_sources} + ENVIRONMENT GZ_RENDERING_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}) diff --git a/src/InstallationDirectories.cc b/src/InstallationDirectories.cc new file mode 100644 index 000000000..c846f1cb3 --- /dev/null +++ b/src/InstallationDirectories.cc @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2023 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 + +#include + +namespace gz +{ +namespace rendering +{ +inline namespace GZ_RENDERING_VERSION_NAMESPACE { + +std::string getResourcePath() +{ + return gz::common::joinPaths(getInstallPrefix(), GZ_RENDERING_RELATIVE_RESOURCE_PATH); +} + +std::string getEngineInstallDir() +{ + return gz::common::joinPaths(getInstallPrefix(), GZ_RENDERING_ENGINE_RELATIVE_INSTALL_DIR); +} + +} +} +} diff --git a/src/RenderEngineManager.cc b/src/RenderEngineManager.cc index e8bab0bc8..f9e657792 100644 --- a/src/RenderEngineManager.cc +++ b/src/RenderEngineManager.cc @@ -23,7 +23,7 @@ #include -#include "gz/rendering/config.hh" +#include "gz/rendering/InstallationDirectories.hh" #include "gz/rendering/RenderEngine.hh" #include "gz/rendering/RenderEngineManager.hh" #include "gz/rendering/RenderEnginePlugin.hh" @@ -496,7 +496,7 @@ bool RenderEngineManagerPrivate::LoadEnginePlugin( // Add default install folder. systemPaths.AddPluginPaths(std::string(GZ_RENDERING_PLUGIN_PATH)); - systemPaths.AddPluginPaths({GZ_RENDERING_ENGINE_INSTALL_DIR}); + systemPaths.AddPluginPaths(gz::rendering::getEngineInstallDir()); // Add any preset plugin paths. for (const auto &path : this->pluginPaths) diff --git a/src/base/BaseScene.cc b/src/base/BaseScene.cc index 4d66a36ef..fd4906a20 100644 --- a/src/base/BaseScene.cc +++ b/src/base/BaseScene.cc @@ -28,6 +28,7 @@ #include "gz/rendering/BoundingBoxCamera.hh" #include "gz/rendering/COMVisual.hh" #include "gz/rendering/InertiaVisual.hh" +#include "gz/rendering/InstallationDirectories.hh" #include "gz/rendering/JointVisual.hh" #include "gz/rendering/LidarVisual.hh" #include "gz/rendering/LightVisual.hh" @@ -1569,7 +1570,7 @@ void BaseScene::CreateMaterials() } std::string resourcePath = (env) ? std::string(env) : - GZ_RENDERING_RESOURCE_PATH; + gz::rendering::getResourcePath(); // path to look for CoM material texture std::string com_material_texture_path = common::joinPaths(