Skip to content

Commit

Permalink
Add rcpputils for find_library
Browse files Browse the repository at this point in the history
  • Loading branch information
EricCousineau-TRI committed Mar 25, 2019
1 parent 32c3de1 commit 422e172
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 84 deletions.
2 changes: 2 additions & 0 deletions rmw_implementation/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ endif()

find_package(ament_cmake REQUIRED)

find_package(rcpputils REQUIRED)
find_package(rcutils REQUIRED)

# provides FindPoco.cmake and Poco on platforms without it
Expand Down Expand Up @@ -54,6 +55,7 @@ else()
src/functions.cpp)
ament_target_dependencies(${PROJECT_NAME}
"Poco"
"rcpputils"
"rmw")
target_compile_definitions(${PROJECT_NAME}
PUBLIC "DEFAULT_RMW_IMPLEMENTATION=${RMW_IMPLEMENTATION}")
Expand Down
1 change: 1 addition & 0 deletions rmw_implementation/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

<buildtool_depend>ament_cmake</buildtool_depend>

<build_depend>rcpputils</build_depend>
<build_depend>rcutils</build_depend>
<build_depend>rmw</build_depend>
<!--
Expand Down
88 changes: 4 additions & 84 deletions rmw_implementation/src/functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,104 +13,23 @@
// limitations under the License.

#include <cstddef>
#include <cstdio>
#include <cstring>

#include <list>
#include <string>

#include <fstream>
#include <sstream>

#include "rcutils/allocator.h"
#include "rcutils/format_string.h"
#include "rcutils/types/string_array.h"

#include "Poco/SharedLibrary.h"

#include "rcpputils/find_library.hpp"
#include "rmw/error_handling.h"
#include "rmw/names_and_types.h"
#include "rmw/get_node_info_and_types.h"
#include "rmw/get_service_names_and_types.h"
#include "rmw/get_topic_names_and_types.h"
#include "rmw/rmw.h"

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();
}

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 "";
}

#define STRINGIFY_(s) #s
#define STRINGIFY(s) STRINGIFY_(s)

Expand All @@ -119,11 +38,12 @@ get_library()
{
static Poco::SharedLibrary * lib = nullptr;
if (!lib) {
std::string env_var = get_env_var("RMW_IMPLEMENTATION");
const char * cenv_var = getenv("RMW_IMPLEMENTATION");
std::string env_var = cenv_var ? cenv_var : "";
if (env_var.empty()) {
env_var = STRINGIFY(DEFAULT_RMW_IMPLEMENTATION);
}
std::string library_path = find_library_path(env_var);
std::string library_path = rcpputils::find_library_path(env_var);
if (library_path.empty()) {
RMW_SET_ERROR_MSG(
("failed to find shared library of rmw implementation. Searched " + env_var).c_str());
Expand Down

0 comments on commit 422e172

Please sign in to comment.