Skip to content

Commit

Permalink
Add support for searching the local directory
Browse files Browse the repository at this point in the history
  • Loading branch information
ooeygui committed Jun 18, 2020
1 parent 33e2edb commit 8455464
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/find_library.cpp
Expand Up @@ -21,6 +21,7 @@
#include <fstream>
#include <sstream>
#include <string>
#include <filesystem>

#include "rcutils/filesystem.h"
#include "rcutils/get_env.h"
Expand All @@ -32,6 +33,7 @@ namespace
{

#ifdef _WIN32
#include <windows.h>
static constexpr char kPathVar[] = "PATH";
static constexpr char kPathSeparator = ';';
static constexpr char kSolibPrefix[] = "";
Expand Down Expand Up @@ -76,6 +78,19 @@ std::string find_library_path(const std::string & library_name)
std::string search_path = get_env_var(kPathVar);
std::list<std::string> search_paths = split(search_path, kPathSeparator);

#ifdef _WIN32
// Support current directory to enable containerized deployments, like Hololens.
DWORD len = GetCurrentDirectoryA(0, NULL) + 1; // How much space is needed for current path?
char* currentDir = new char[len];
if (currentDir) {
if (GetCurrentDirectoryA(len, currentDir) > 0) {
search_paths.push_front(currentDir);
}

delete [] currentDir; // no longer needed.
}
#endif

std::string filename = kSolibPrefix;
filename += library_name + kSolibExtension;

Expand Down
37 changes: 37 additions & 0 deletions test/test_find_library.cpp
Expand Up @@ -19,15 +19,22 @@
#include "gtest/gtest.h"

#include "rcutils/get_env.h"
#include "rcutils/filesystem.h"
#include "rcpputils/find_library.hpp"

#ifdef _WIN32
#include <windows.h>
#endif

namespace rcpputils
{
namespace
{

TEST(test_find_library, find_library)
{
rcutils_allocator_t allocator = rcutils_get_default_allocator();

// Get ground-truth values from CTest properties.
std::string expected_library_path;
{
Expand Down Expand Up @@ -67,6 +74,36 @@ TEST(test_find_library, find_library)
"this_is_a_junk_libray_name_please_dont_define_this_if_you_do_then_"
"you_are_really_naughty");
EXPECT_EQ(bad_path, "");

#ifdef _WIN32
// On Windows, test find library in the current directory, to support containerized deployments

const char* currentDir = rcutils_join_path(test_lib_dir, "test_current_path", allocator);
EXPECT_NE(currentDir, nullptr);

const char* testFile = rcutils_join_path(currentDir, "test_win_current_dir.dll", allocator);
EXPECT_NE(testFile, nullptr);

// move a file into the non-current path
EXPECT_NE(rcutils_mkdir(currentDir), false);
EXPECT_NE(CopyFile(test_lib_actual.c_str(), currentDir, FALSE), FALSE);

const std::string test_current_dir_fail = find_library_path("test_win_current_dir");
EXPECT_EQ(test_current_dir_fail.empty(), true); // shouldn't be found until we set the current directory.

DWORD len = GetCurrentDirectoryA(0, NULL) + 1; // How much space is needed for current path?
char* originalCurrentDir = new char[len];
GetCurrentDirectoryA(len, originalCurrentDir);

SetCurrentDirectory(currentDir);

const std::string test_current_dir_succeed = find_library_path("test_win_current_dir");
EXPECT_EQ(test_current_dir_succeed.empty(), false);

SetCurrentDirectory(currentDir);

delete [] originalCurrentDir;
#endif
}

} // namespace
Expand Down

0 comments on commit 8455464

Please sign in to comment.