Skip to content

Commit

Permalink
Add rcpputils for find_library (#57)
Browse files Browse the repository at this point in the history
* Add `rcpputils` for `find_library`

Signed-off-by: Eric Cousineau <[email protected]>

* Address review

Signed-off-by: Eric Cousineau <[email protected]>

* Fix dependency declarations

Signed-off-by: Eric Cousineau <[email protected]>

* Fix errors

Signed-off-by: Eric Cousineau <[email protected]>

* fix CMake ordering

Signed-off-by: Eric Cousineau <[email protected]>

* functions: Revert back to custom get_env_var

Signed-off-by: Eric Cousineau <[email protected]>

* use rcutils_get_env, remove added namespace

Signed-off-by: Dirk Thomas <[email protected]>

* export new dependencies when necessary

Signed-off-by: Dirk Thomas <[email protected]>

Co-authored-by: Dirk Thomas <[email protected]>
  • Loading branch information
EricCousineau-TRI and dirk-thomas committed Jan 22, 2020
1 parent f3def1a commit 2d5e1e1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 84 deletions.
7 changes: 5 additions & 2 deletions rmw_implementation/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ endif()
find_package(ament_cmake REQUIRED)
find_package(ament_cmake_python REQUIRED)

find_package(rcutils REQUIRED)

find_package(rmw_implementation_cmake REQUIRED)

ament_python_install_package(${PROJECT_NAME})
Expand Down Expand Up @@ -55,18 +53,23 @@ else()
# provides FindPoco.cmake and Poco on platforms without it
find_package(poco_vendor REQUIRED)
find_package(Poco REQUIRED COMPONENTS Foundation)
find_package(rcpputils REQUIRED)
find_package(rcutils REQUIRED)
find_package(rmw REQUIRED)

add_library(${PROJECT_NAME} SHARED
src/functions.cpp)
ament_target_dependencies(${PROJECT_NAME}
"Poco"
"rcpputils"
"rcutils"
"rmw")
target_compile_definitions(${PROJECT_NAME}
PUBLIC "DEFAULT_RMW_IMPLEMENTATION=${RMW_IMPLEMENTATION}")
configure_rmw_library(${PROJECT_NAME})

ament_export_libraries(${PROJECT_NAME})
ament_export_dependencies(rcpputils rcutils)

install(
TARGETS ${PROJECT_NAME}
Expand Down
3 changes: 2 additions & 1 deletion rmw_implementation/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
<buildtool_depend>ament_cmake</buildtool_depend>
<buildtool_depend>ament_cmake_python</buildtool_depend>

<build_depend>rcutils</build_depend>
<depend>rcpputils</depend>
<depend>rcutils</depend>
<build_depend>rmw</build_depend>
<!--
Bloom does not support group_depend so entries below duplicate the group rmw_implementation_packages.
Expand Down
92 changes: 11 additions & 81 deletions rmw_implementation/src/functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,17 @@
// 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/get_env.h"
#include "rcutils/types/string_array.h"

#include "Poco/SharedLibrary.h"

#include "rcpputils/find_library.hpp"
#include "rmw/error_handling.h"
#include "rmw/event.h"
#include "rmw/names_and_types.h"
Expand All @@ -37,85 +33,19 @@
#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();
}
#define STRINGIFY_(s) #s
#define STRINGIFY(s) STRINGIFY_(s)

std::string find_library_path(const std::string & library_name)
std::string get_env_var(const char * env_var)
{
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;
}
const char * value{};
const char * err = rcutils_get_env(env_var, &value);
if (err) {
throw std::runtime_error(err);
}
return "";
return value ? value : "";
}

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

Poco::SharedLibrary *
get_library()
{
Expand All @@ -125,7 +55,7 @@ get_library()
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 2d5e1e1

Please sign in to comment.