-
Notifications
You must be signed in to change notification settings - Fork 54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
find_library: Centralize functionality here #4
Closed
+343
−3
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9ba2300
find_library: Centralize functionality here
EricCousineau-TRI 0b21550
fix
EricCousineau-TRI a983b44
fix for win32
EricCousineau-TRI aff2a87
Ensure test_library exports symbols
EricCousineau-TRI ce50d37
Fix missing header
EricCousineau-TRI ea92951
fix visibility macro
dirk-thomas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright 2019 Open Source Robotics Foundation, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#ifndef RCPPUTILS__FIND_LIBRARY_HPP_ | ||
#define RCPPUTILS__FIND_LIBRARY_HPP_ | ||
|
||
#include <string> | ||
|
||
#include "rcpputils/visibility_control.hpp" | ||
|
||
namespace rcpputils | ||
{ | ||
|
||
/// Finds a library located in the OS's specified environment variable for | ||
/// library paths and returns the absolute filesystem path, including the | ||
/// appropriate prefix and extension. | ||
/** | ||
* The environment variable and file format per platform: | ||
* * Linux: `${LD_LIBRARY_PATH}`, `lib{}.so` | ||
* * Apple: `${DYLD_LIBRARY_PATH}`, `lib{}.dyld` | ||
* * Windows: `%PATH%`, `{}.dll` | ||
* | ||
* \param[in] library_name Name of the library to find. | ||
* \return Absolute path of library. | ||
* \throws std::runtime_error if an error is encountered when accessing | ||
* environment variables. | ||
*/ | ||
EricCousineau-TRI marked this conversation as resolved.
Show resolved
Hide resolved
|
||
RCPPUTILS_PUBLIC | ||
std::string find_library_path(const std::string & library_name); | ||
|
||
} // namespace rcpputils | ||
|
||
#endif // RCPPUTILS__FIND_LIBRARY_HPP_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright (c) 2019, Open Source Robotics Foundation, Inc. | ||
// All rights reserved. | ||
// | ||
// Software License Agreement (BSD License 2.0) | ||
// | ||
// Redistribution and use in source and binary forms, with or without | ||
// modification, are permitted provided that the following conditions | ||
// are met: | ||
// | ||
// * Redistributions of source code must retain the above copyright | ||
// notice, this list of conditions and the following disclaimer. | ||
// * Redistributions in binary form must reproduce the above | ||
// copyright notice, this list of conditions and the following | ||
// disclaimer in the documentation and/or other materials provided | ||
// with the distribution. | ||
// * Neither the name of the copyright holders nor the names of its | ||
// contributors may be used to endorse or promote products derived | ||
// from this software without specific prior written permission. | ||
// | ||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | ||
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | ||
// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | ||
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN | ||
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
// POSSIBILITY OF SUCH DAMAGE. | ||
|
||
#ifndef RCPPUTILS__VISIBILITY_CONTROL_HPP_ | ||
#define RCPPUTILS__VISIBILITY_CONTROL_HPP_ | ||
|
||
// This logic was borrowed (then namespaced) from the examples on the gcc wiki: | ||
// https://gcc.gnu.org/wiki/Visibility | ||
|
||
#if defined _WIN32 || defined __CYGWIN__ | ||
#ifdef __GNUC__ | ||
#define RCPPUTILS_EXPORT __attribute__ ((dllexport)) | ||
#define RCPPUTILS_IMPORT __attribute__ ((dllimport)) | ||
#else | ||
#define RCPPUTILS_EXPORT __declspec(dllexport) | ||
#define RCPPUTILS_IMPORT __declspec(dllimport) | ||
#endif | ||
#ifdef RCPPUTILS_BUILDING_LIBRARY | ||
#define RCPPUTILS_PUBLIC RCPPUTILS_EXPORT | ||
#else | ||
#define RCPPUTILS_PUBLIC RCPPUTILS_IMPORT | ||
#endif | ||
#define RCPPUTILS_PUBLIC_TYPE RCPPUTILS_PUBLIC | ||
#define RCPPUTILS_LOCAL | ||
#else | ||
#define RCPPUTILS_EXPORT __attribute__ ((visibility("default"))) | ||
#define RCPPUTILS_IMPORT | ||
#if __GNUC__ >= 4 | ||
#define RCPPUTILS_PUBLIC __attribute__ ((visibility("default"))) | ||
#define RCPPUTILS_LOCAL __attribute__ ((visibility("hidden"))) | ||
#else | ||
#define RCPPUTILS_PUBLIC | ||
#define RCPPUTILS_LOCAL | ||
#endif | ||
#define RCPPUTILS_PUBLIC_TYPE | ||
#endif | ||
|
||
#endif // RCPPUTILS__VISIBILITY_CONTROL_HPP_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Copyright 2019 Open Source Robotics Foundation, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include "rcpputils/find_library.hpp" | ||
|
||
#include <cassert> | ||
#include <cstddef> | ||
|
||
#include <list> | ||
#include <fstream> | ||
#include <sstream> | ||
EricCousineau-TRI marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#include <string> | ||
|
||
#include "rcutils/filesystem.h" | ||
#include "rcutils/get_env.h" | ||
|
||
namespace rcpputils | ||
{ | ||
|
||
namespace | ||
{ | ||
|
||
#ifdef _WIN32 | ||
static constexpr char kPathVar[] = "PATH"; | ||
static constexpr char kPathSeparator = ';'; | ||
static constexpr char kSolibPrefix[] = ""; | ||
static constexpr char kSolibExtension[] = ".dll"; | ||
#elif __APPLE__ | ||
static constexpr char kPathVar[] = "DYLD_LIBRARY_PATH"; | ||
static constexpr char kPathSeparator = ':'; | ||
static constexpr char kSolibPrefix[] = "lib"; | ||
static constexpr char kSolibExtension[] = ".dylib"; | ||
#else | ||
static constexpr char kPathVar[] = "LD_LIBRARY_PATH"; | ||
static constexpr char kPathSeparator = ':'; | ||
static constexpr char kSolibPrefix[] = "lib"; | ||
static constexpr char kSolibExtension[] = ".so"; | ||
#endif | ||
|
||
std::string get_env_var(const char * kPathVar) | ||
{ | ||
const char * value{}; | ||
const char * err = rcutils_get_env(kPathVar, &value); | ||
if (err) { | ||
throw std::runtime_error(err); | ||
} | ||
return value ? value : ""; | ||
} | ||
|
||
std::list<std::string> split(const std::string & value, const char delimiter) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would need a test ideally. |
||
{ | ||
std::list<std::string> list; | ||
std::istringstream ss(value); | ||
std::string s; | ||
while (std::getline(ss, s, delimiter)) { | ||
list.push_back(s); | ||
} | ||
return list; | ||
} | ||
|
||
} // namespace | ||
|
||
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); | ||
|
||
std::string filename = kSolibPrefix; | ||
filename += library_name + kSolibExtension; | ||
|
||
for (const auto & search_path : search_paths) { | ||
std::string path = search_path + "/" + filename; | ||
if (rcutils_is_file(path.c_str())) { | ||
return path; | ||
} | ||
} | ||
return ""; | ||
} | ||
|
||
} // namespace rcpputils |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright 2019 Open Source Robotics Foundation, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include <stdlib.h> | ||
|
||
#include <string> | ||
|
||
#include "gtest/gtest.h" | ||
|
||
#include "rcutils/get_env.h" | ||
#include "rcpputils/find_library.hpp" | ||
|
||
namespace rcpputils | ||
{ | ||
namespace | ||
{ | ||
|
||
TEST(test_find_library, find_library) | ||
{ | ||
// Get ground-truth values from CTest properties. | ||
const char * test_lib_expected{}; | ||
EXPECT_EQ(rcutils_get_env("_TEST_LIBRARY", &test_lib_expected), nullptr); | ||
EXPECT_NE(test_lib_expected, nullptr); | ||
const char * test_lib_dir{}; | ||
EXPECT_EQ(rcutils_get_env("_TEST_LIBRARY_DIR", &test_lib_dir), nullptr); | ||
EXPECT_NE(test_lib_dir, nullptr); | ||
|
||
// Set our relevant path variable. | ||
const char * env_var{}; | ||
#ifdef _WIN32 | ||
EricCousineau-TRI marked this conversation as resolved.
Show resolved
Hide resolved
|
||
env_var = "PATH"; | ||
#elif __APPLE__ | ||
env_var = "DYLD_LIBRARY_PATH"; | ||
#else | ||
env_var = "LD_LIBRARY_PATH"; | ||
#endif | ||
|
||
#ifdef _WIN32 | ||
EXPECT_EQ(_putenv_s(env_var, test_lib_dir), 0); | ||
#else | ||
const int override = 1; | ||
EXPECT_EQ(setenv(env_var, test_lib_dir, override), 0); | ||
#endif | ||
|
||
// Positive test. | ||
const std::string test_lib_actual = find_library_path("test_library"); | ||
EXPECT_EQ(test_lib_actual, test_lib_expected); | ||
|
||
// (Hopefully) Negative test. | ||
const std::string bad_path = find_library_path( | ||
"this_is_a_junk_libray_name_please_dont_define_this_if_you_do_then_" | ||
"you_are_really_naughty"); | ||
EXPECT_EQ(bad_path, ""); | ||
} | ||
|
||
} // namespace | ||
} // namespace rcpputils |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright 2019 Open Source Robotics Foundation, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
/// @file | ||
/// Trivial library to ensure we have some linking present for | ||
/// `test_find_library`. | ||
|
||
#include "rcpputils/visibility_control.hpp" | ||
|
||
namespace test_library | ||
{ | ||
|
||
RCPPUTILS_EXPORT | ||
int add_one(int x) | ||
{ | ||
return x + 1; | ||
} | ||
|
||
} // namespace test_library |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sigh... I'm thinking that Windows CMake or something else is maybe failing here? (in that it only passes the directly, not the full path?)
I've recently gotten a Windows machine (for another purpose), but will see if I can dig in a bit more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That being said, thank you for your patience thus far!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trying to get this setup on Windows, but not being a Windows developer, to try and reproduce this bug is very time consuming and frustrating.
Is there some free Azure container / VM service that has ROS 2 provisioned, that I could possible connect to so that I can debug from there? (For me and other developers, who do not wish to spend time / money setting up Windows environments?)
And can you point me to a unittest that does this sort of thing successfully?
(Something more explicit than saying "Hey, it loaded the library, cool.")
If there isn't one already, I may just shortcut this for now and special-case the Windows logic to pass on CI by acknowledging the current bug (which I'm still confident is CMake, pending any other evidence to the contrary).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently the test fails with:
Your test code retrieves two environment variables: first the library path then the library dir. But you are missing the fact that the second call overwrites the first variable. See the API documentation of that function: https://github.com/ros2/rcutils/blob/9af59e871e0bfaadb0cbb32014a7167efb9760c0/include/rcutils/get_env.h#L27-L29
So the value of the first environment variable must be copied to its own memory before fetching the second. See ed7b9fa for the fix.