Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the C++ library relocatable when compiled as shared library via reloc-cpp #179

Merged
merged 1 commit into from
Jun 25, 2024
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
24 changes: 21 additions & 3 deletions cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@


set(ICUB_MODELS_MODELS_PATH ${CMAKE_INSTALL_PREFIX}/share/iCub/robots)
# Handle relocatability via reloc-cpp
option(ICUB_MODELS_USE_SYSTEM_RELOC_CPP "Use system reloc-cpp" OFF)
mark_as_advanced(ICUB_MODELS_USE_SYSTEM_RELOC_CPP)
if(ICUB_MODELS_USE_SYSTEM_RELOC_CPP)
find_package(reloc-cpp REQUIRED)
else()
include(FetchContent)
FetchContent_Declare(
reloc-cpp
GIT_REPOSITORY https://github.com/ami-iit/reloc-cpp.git
GIT_TAG v0.1.0
)
FetchContent_MakeAvailable(reloc-cpp)
endif()

set(ICUB_MODELS_MODELS_RELATIVE_PATH share/iCub/robots)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/src/iCubModels.cpp.in
${CMAKE_CURRENT_BINARY_DIR}/autogenerated/iCubModels.cpp
@ONLY)
Expand Down Expand Up @@ -30,6 +43,11 @@ target_include_directories(${LIBRARY_TARGET_NAME} PUBLIC "$<BUILD_INTERFACE:${CM

target_compile_features(${LIBRARY_TARGET_NAME} PUBLIC cxx_std_11)

reloc_cpp_generate(${LIBRARY_TARGET_NAME}
GENERATED_HEADER ${CMAKE_CURRENT_BINARY_DIR}/iCubModelsGetInstallPrefix.h
GENERATED_FUNCTION iCubModels::getInstallPrefix)
target_include_directories(${LIBRARY_TARGET_NAME} PRIVATE ${CMAKE_CURRENT_BINARY_DIR})

install(TARGETS ${LIBRARY_TARGET_NAME}
EXPORT ${PROJECT_NAME}
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" COMPONENT shlib
Expand Down
12 changes: 5 additions & 7 deletions cpp/src/iCubModels.cpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,17 @@

#include <iCubModels/iCubModels.h>

#include <iCubModelsGetInstallPrefix.h>
#include <filesystem>

#include <algorithm>

namespace iCubModels
{
std::string getModelsPath()
{
std::string retstr = "@ICUB_MODELS_MODELS_PATH@";
// Sometimes the relocation of the C++ library in conda or similar systems is not working correctly
// and is leaving at the end of the retstr many \0 characters. To be sure not to return a faulty
// string, we remove all the trailing \0
// See https://github.com/ami-iit/bipedal-locomotion-framework/pull/526
retstr.erase(std::find(retstr.begin(), retstr.end(), '\0'), retstr.end());
return retstr;
std::filesystem::path ret = std::filesystem::path(iCubModels::getInstallPrefix().value()) / std::filesystem::path("@ICUB_MODELS_MODELS_RELATIVE_PATH@");
return ret.string();
}

std::unordered_set<std::string> getRobotNames()
Expand Down