diff --git a/rosidl_typesupport_c/CMakeLists.txt b/rosidl_typesupport_c/CMakeLists.txt index be0f5003..38e144cb 100644 --- a/rosidl_typesupport_c/CMakeLists.txt +++ b/rosidl_typesupport_c/CMakeLists.txt @@ -19,6 +19,7 @@ find_package(ament_cmake_ros REQUIRED) # provides FindPoco.cmake and Poco on platforms without it find_package(poco_vendor) find_package(Poco COMPONENTS Foundation) +find_package(rcpputils REQUIRED) find_package(rosidl_generator_c REQUIRED) link_directories(${Poco_LIBRARY_DIR}) @@ -50,7 +51,7 @@ target_include_directories(${PROJECT_NAME} if(Poco_FOUND) ament_target_dependencies(${PROJECT_NAME} "Poco") endif() -ament_target_dependencies(${PROJECT_NAME} "rosidl_generator_c") +ament_target_dependencies(${PROJECT_NAME} "rcpputils" "rosidl_generator_c") ament_export_libraries(${PROJECT_NAME}) ament_index_register_resource("rosidl_runtime_packages") diff --git a/rosidl_typesupport_c/package.xml b/rosidl_typesupport_c/package.xml index 9948026a..39d43199 100644 --- a/rosidl_typesupport_c/package.xml +++ b/rosidl_typesupport_c/package.xml @@ -9,6 +9,7 @@ ament_cmake_ros + rcpputils libpoco-dev poco_vendor rosidl_generator_c diff --git a/rosidl_typesupport_c/src/type_support_dispatch.cpp b/rosidl_typesupport_c/src/type_support_dispatch.cpp index 055aba05..b58f0236 100644 --- a/rosidl_typesupport_c/src/type_support_dispatch.cpp +++ b/rosidl_typesupport_c/src/type_support_dispatch.cpp @@ -13,90 +13,3 @@ // limitations under the License. #include "type_support_dispatch.hpp" - -#include -#include - -#include -#include - -namespace rosidl_typesupport_c -{ - -std::string find_library_path(const std::string & library_name) -{ - const char * env_var; - char separator; - const char * filename_prefix; - const char * filename_extension; -#ifdef _WIN32 - env_var = "PATH"; - separator = ';'; - filename_prefix = ""; - filename_extension = ".dll"; -#elif __APPLE__ - env_var = "DYLD_LIBRARY_PATH"; - separator = ':'; - filename_prefix = "lib"; - filename_extension = ".dylib"; -#else - env_var = "LD_LIBRARY_PATH"; - separator = ':'; - filename_prefix = "lib"; - filename_extension = ".so"; -#endif - std::string search_path = get_env_var(env_var); - std::list search_paths = split(search_path, separator); - - std::string filename = filename_prefix; - filename += library_name + filename_extension; - - for (auto it : search_paths) { - std::string path = it + "/" + filename; - if (is_file_exist(path.c_str())) { - return path; - } - } - return ""; -} - -std::string get_env_var(const char * env_var) -{ - char * value = nullptr; -#ifndef _WIN32 - value = getenv(env_var); -#else - size_t value_size; - _dupenv_s(&value, &value_size, env_var); -#endif - std::string value_str = ""; - if (value) { - value_str = value; -#ifdef _WIN32 - free(value); -#endif - } - // printf("get_env_var(%s) = %s\n", env_var, value_str.c_str()); - return value_str; -} - -std::list split(const std::string & value, const char delimiter) -{ - std::list list; - std::istringstream ss(value); - std::string s; - while (std::getline(ss, s, delimiter)) { - list.push_back(s); - } - // printf("split(%s) = %zu\n", value.c_str(), list.size()); - return list; -} - -bool is_file_exist(const char * filename) -{ - std::ifstream h(filename); - // printf("is_file_exist(%s) = %s\n", filename, h.good() ? "true" : "false"); - return h.good(); -} - -} // namespace rosidl_typesupport_c diff --git a/rosidl_typesupport_c/src/type_support_dispatch.hpp b/rosidl_typesupport_c/src/type_support_dispatch.hpp index f1f168ec..8b90af92 100644 --- a/rosidl_typesupport_c/src/type_support_dispatch.hpp +++ b/rosidl_typesupport_c/src/type_support_dispatch.hpp @@ -26,20 +26,13 @@ #include "Poco/SharedLibrary.h" #endif +#include "rcpputils/find_library.hpp" #include "rosidl_typesupport_c/identifier.h" #include "rosidl_typesupport_c/type_support_map.h" namespace rosidl_typesupport_c { -std::string find_library_path(const std::string & library_name); - -std::string get_env_var(const char * env_var); - -std::list split(const std::string & value, const char delimiter); - -bool is_file_exist(const char * filename); - extern const char * typesupport_identifier; template @@ -65,7 +58,7 @@ get_typesupport_handle_function( snprintf( library_name, 1023, "%s__%s", map->package_name, identifier); - std::string library_path = find_library_path(library_name); + std::string library_path = rcpputils::find_library_path(library_name); if (library_path.empty()) { fprintf(stderr, "Failed to find library '%s'\n", library_name); return nullptr; diff --git a/rosidl_typesupport_cpp/CMakeLists.txt b/rosidl_typesupport_cpp/CMakeLists.txt index 92386632..ff1e3d6b 100644 --- a/rosidl_typesupport_cpp/CMakeLists.txt +++ b/rosidl_typesupport_cpp/CMakeLists.txt @@ -15,6 +15,7 @@ find_package(ament_cmake_ros REQUIRED) # provides FindPoco.cmake and Poco on platforms without it find_package(poco_vendor) find_package(Poco COMPONENTS Foundation) +find_package(rcpputils REQUIRED) find_package(rosidl_generator_c REQUIRED) link_directories(${Poco_LIBRARY_DIR}) @@ -47,7 +48,7 @@ target_include_directories(${PROJECT_NAME} if(Poco_FOUND) ament_target_dependencies(${PROJECT_NAME} "Poco") endif() -ament_target_dependencies(${PROJECT_NAME} "rosidl_generator_c") +ament_target_dependencies(${PROJECT_NAME} "rcpputils" "rosidl_generator_c") ament_export_libraries(${PROJECT_NAME}) ament_index_register_resource("rosidl_runtime_packages") diff --git a/rosidl_typesupport_cpp/package.xml b/rosidl_typesupport_cpp/package.xml index 47d89aff..07a9e08b 100644 --- a/rosidl_typesupport_cpp/package.xml +++ b/rosidl_typesupport_cpp/package.xml @@ -9,6 +9,7 @@ ament_cmake_ros + rcpputils libpoco-dev poco_vendor rosidl_generator_c diff --git a/rosidl_typesupport_cpp/src/type_support_dispatch.cpp b/rosidl_typesupport_cpp/src/type_support_dispatch.cpp index 4ae04e1c..b58f0236 100644 --- a/rosidl_typesupport_cpp/src/type_support_dispatch.cpp +++ b/rosidl_typesupport_cpp/src/type_support_dispatch.cpp @@ -13,90 +13,3 @@ // limitations under the License. #include "type_support_dispatch.hpp" - -#include -#include - -#include -#include - -namespace rosidl_typesupport_cpp -{ - -std::string find_library_path(const std::string & library_name) -{ - const char * env_var; - char separator; - const char * filename_prefix; - const char * filename_extension; -#ifdef _WIN32 - env_var = "PATH"; - separator = ';'; - filename_prefix = ""; - filename_extension = ".dll"; -#elif __APPLE__ - env_var = "DYLD_LIBRARY_PATH"; - separator = ':'; - filename_prefix = "lib"; - filename_extension = ".dylib"; -#else - env_var = "LD_LIBRARY_PATH"; - separator = ':'; - filename_prefix = "lib"; - filename_extension = ".so"; -#endif - std::string search_path = get_env_var(env_var); - std::list search_paths = split(search_path, separator); - - std::string filename = filename_prefix; - filename += library_name + filename_extension; - - for (auto it : search_paths) { - std::string path = it + "/" + filename; - if (is_file_exist(path.c_str())) { - return path; - } - } - return ""; -} - -std::string get_env_var(const char * env_var) -{ - char * value = nullptr; -#ifndef _WIN32 - value = getenv(env_var); -#else - size_t value_size; - _dupenv_s(&value, &value_size, env_var); -#endif - std::string value_str = ""; - if (value) { - value_str = value; -#ifdef _WIN32 - free(value); -#endif - } - // printf("get_env_var(%s) = %s\n", env_var, value_str.c_str()); - return value_str; -} - -std::list split(const std::string & value, const char delimiter) -{ - std::list list; - std::istringstream ss(value); - std::string s; - while (std::getline(ss, s, delimiter)) { - list.push_back(s); - } - // printf("split(%s) = %zu\n", value.c_str(), list.size()); - return list; -} - -bool is_file_exist(const char * filename) -{ - std::ifstream h(filename); - // printf("is_file_exist(%s) = %s\n", filename, h.good() ? "true" : "false"); - return h.good(); -} - -} // namespace rosidl_typesupport_cpp diff --git a/rosidl_typesupport_cpp/src/type_support_dispatch.hpp b/rosidl_typesupport_cpp/src/type_support_dispatch.hpp index 4941b3ed..c9f54245 100644 --- a/rosidl_typesupport_cpp/src/type_support_dispatch.hpp +++ b/rosidl_typesupport_cpp/src/type_support_dispatch.hpp @@ -19,26 +19,18 @@ #include #include -#include #include #ifdef ROSIDL_TYPESUPPORT_CPP_USE_POCO #include "Poco/SharedLibrary.h" #endif +#include "rcpputils/find_library.hpp" #include "rosidl_typesupport_cpp/type_support_map.h" namespace rosidl_typesupport_cpp { -std::string find_library_path(const std::string & library_name); - -std::string get_env_var(const char * env_var); - -std::list split(const std::string & value, const char delimiter); - -bool is_file_exist(const char * filename); - extern const char * typesupport_identifier; template @@ -64,7 +56,7 @@ get_typesupport_handle_function( snprintf( library_name, 1023, "%s__%s", map->package_name, identifier); - std::string library_path = find_library_path(library_name); + std::string library_path = rcpputils::find_library_path(library_name); if (library_path.empty()) { fprintf(stderr, "Failed to find library '%s'\n", library_name); return nullptr;