From 925bdbaf7400c5e1a2d9fc4fb5a5791d832ac7ff Mon Sep 17 00:00:00 2001 From: Johnson Shih Date: Thu, 30 Aug 2018 11:54:19 -0700 Subject: [PATCH] Strip the 'lib' prefix when load library --- pluginlib/CMakeLists.txt | 5 +++ pluginlib/include/pluginlib/class_loader.hpp | 6 ++++ .../include/pluginlib/class_loader_imp.hpp | 36 ++++++++++++++++++- 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/pluginlib/CMakeLists.txt b/pluginlib/CMakeLists.txt index 4d6f672d..99542532 100644 --- a/pluginlib/CMakeLists.txt +++ b/pluginlib/CMakeLists.txt @@ -16,6 +16,11 @@ if(CATKIN_ENABLE_TESTING) add_library(test_plugins EXCLUDE_FROM_ALL SHARED test/test_plugins.cpp) target_link_libraries(test_plugins ${class_loader_LIBRARIES}) + if(WIN32) + # On Windows, default library runtime output set to CATKIN_GLOBAL_BIN_DESTINATION, + # change it back to CATKIN_PACKAGE_LIB_DESTINATION to match the library path described in plugin description file + set_target_properties(test_plugins PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CATKIN_DEVEL_PREFIX}/${CATKIN_PACKAGE_LIB_DESTINATION}) + endif() catkin_add_gtest(${PROJECT_NAME}_utest test/utest.cpp) if(TARGET ${PROJECT_NAME}_utest) diff --git a/pluginlib/include/pluginlib/class_loader.hpp b/pluginlib/include/pluginlib/class_loader.hpp index 7aaa0441..ab18b724 100644 --- a/pluginlib/include/pluginlib/class_loader.hpp +++ b/pluginlib/include/pluginlib/class_loader.hpp @@ -288,6 +288,12 @@ class ClassLoader : public ClassLoaderBase const std::string & library_name, const std::string & exporting_package_name); +#ifdef _WIN32 + std::string getClassLibraryPathFromLibraryName( + const std::string & library_name, + const std::string & package_name); +#endif + /// Return the paths where libraries are installed according to the Catkin build system. std::vector getCatkinLibraryPaths(); diff --git a/pluginlib/include/pluginlib/class_loader_imp.hpp b/pluginlib/include/pluginlib/class_loader_imp.hpp index f0a2c834..c75cb12e 100644 --- a/pluginlib/include/pluginlib/class_loader_imp.hpp +++ b/pluginlib/include/pluginlib/class_loader_imp.hpp @@ -413,8 +413,42 @@ std::string ClassLoader::getClassLibraryPath(const std::string & lookup_name) ROS_DEBUG_NAMED("pluginlib.ClassLoader", "Class %s maps to library %s in classes_available_.", lookup_name.c_str(), library_name.c_str()); + std::string library_path = getClassLibraryPathFromLibraryName(library_name, it->second.package_); + +#ifdef _WIN32 + // Windows does not add 'lib' prefix to libary name, remove the prefix and try again + if ("" == library_path) { + std::string only_path; + std::string only_file; + size_t c = library_name.find_last_of(getPathSeparator()); + if (std::string::npos == c) { + only_path = ""; + only_file = library_name; + } else { + only_path = library_name.substr(0, c + 1); + only_file = library_name.substr(c + 1, library_name.size()); + } + + const std::string lib_suffix = "lib"; + if (boost::starts_with(only_file, lib_suffix)) { + only_file = only_file.substr(lib_suffix.length()); + if (!only_file.empty()) { + const std::string new_library_name = only_path + only_file; + library_path = getClassLibraryPathFromLibraryName(new_library_name, it->second.package_); + } + } + } +#endif + + return library_path; +} + +template +std::string ClassLoader::getClassLibraryPathFromLibraryName(const std::string & library_name, const std::string & package_name) +/***************************************************************************/ +{ std::vector paths_to_try = - getAllLibraryPathsToTry(library_name, it->second.package_); + getAllLibraryPathsToTry(library_name, package_name); ROS_DEBUG_NAMED("pluginlib.ClassLoader", "Iterating through all possible paths where %s could be located...",