Skip to content

Commit

Permalink
Add rcpputils for find_library
Browse files Browse the repository at this point in the history
Signed-off-by: Eric Cousineau <[email protected]>
  • Loading branch information
EricCousineau-TRI committed Mar 25, 2019
1 parent bda9bb4 commit 2e50501
Show file tree
Hide file tree
Showing 8 changed files with 10 additions and 195 deletions.
3 changes: 2 additions & 1 deletion rosidl_typesupport_c/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down Expand Up @@ -47,7 +48,7 @@ target_include_directories(${PROJECT_NAME}
include ${Poco_INCLUDE_DIRS}
)
target_link_libraries(${PROJECT_NAME} ${Poco_LIBRARIES})
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")
Expand Down
1 change: 1 addition & 0 deletions rosidl_typesupport_c/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

<buildtool_depend>ament_cmake_ros</buildtool_depend>

<build_depend>rcpputils</build_depend>
<build_depend>libpoco-dev</build_depend>
<build_depend>poco_vendor</build_depend>
<build_depend>rosidl_generator_c</build_depend>
Expand Down
87 changes: 0 additions & 87 deletions rosidl_typesupport_c/src/type_support_dispatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,90 +13,3 @@
// limitations under the License.

#include "type_support_dispatch.hpp"

#include <list>
#include <string>

#include <fstream>
#include <sstream>

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<std::string> 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<std::string> split(const std::string & value, const char delimiter)
{
std::list<std::string> 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
11 changes: 2 additions & 9 deletions rosidl_typesupport_c/src/type_support_dispatch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> split(const std::string & value, const char delimiter);

bool is_file_exist(const char * filename);

extern const char * typesupport_identifier;

template<typename TypeSupport>
Expand All @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion rosidl_typesupport_cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down Expand Up @@ -44,7 +45,7 @@ target_include_directories(${PROJECT_NAME}
include ${Poco_INCLUDE_DIRS}
)
target_link_libraries(${PROJECT_NAME} ${Poco_LIBRARIES})
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")
Expand Down
1 change: 1 addition & 0 deletions rosidl_typesupport_cpp/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

<buildtool_depend>ament_cmake_ros</buildtool_depend>

<build_depend>rcpputils</build_depend>
<build_depend>libpoco-dev</build_depend>
<build_depend>poco_vendor</build_depend>
<build_depend>rosidl_generator_c</build_depend>
Expand Down
87 changes: 0 additions & 87 deletions rosidl_typesupport_cpp/src/type_support_dispatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,90 +13,3 @@
// limitations under the License.

#include "type_support_dispatch.hpp"

#include <list>
#include <string>

#include <fstream>
#include <sstream>

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<std::string> 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<std::string> split(const std::string & value, const char delimiter)
{
std::list<std::string> 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
12 changes: 2 additions & 10 deletions rosidl_typesupport_cpp/src/type_support_dispatch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,18 @@
#include <cstdio>
#include <cstring>

#include <list>
#include <string>

#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<std::string> split(const std::string & value, const char delimiter);

bool is_file_exist(const char * filename);

extern const char * typesupport_identifier;

template<typename TypeSupport>
Expand All @@ -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;
Expand Down

0 comments on commit 2e50501

Please sign in to comment.