| Directory: | ./ |
|---|---|
| File: | src/plugin.cc |
| Date: | 2025-03-10 11:18:21 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 14 | 35 | 40.0% |
| Branches: | 5 | 94 | 5.3% |
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // Copyright (c) 2019, Joseph Mirabel | ||
| 2 | // Authors: Joseph Mirabel (joseph.mirabel@laas.fr) | ||
| 3 | // | ||
| 4 | |||
| 5 | // Redistribution and use in source and binary forms, with or without | ||
| 6 | // modification, are permitted provided that the following conditions are | ||
| 7 | // met: | ||
| 8 | // | ||
| 9 | // 1. Redistributions of source code must retain the above copyright | ||
| 10 | // notice, this list of conditions and the following disclaimer. | ||
| 11 | // | ||
| 12 | // 2. Redistributions in binary form must reproduce the above copyright | ||
| 13 | // notice, this list of conditions and the following disclaimer in the | ||
| 14 | // documentation and/or other materials provided with the distribution. | ||
| 15 | // | ||
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
| 20 | // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH | ||
| 27 | // DAMAGE. | ||
| 28 | |||
| 29 | #include <dlfcn.h> | ||
| 30 | |||
| 31 | #include <boost/filesystem/operations.hpp> | ||
| 32 | #include <boost/filesystem/path.hpp> | ||
| 33 | #include <hpp/core/plugin.hh> | ||
| 34 | #include <hpp/util/debug.hh> | ||
| 35 | #include <hpp/util/exception-factory.hh> | ||
| 36 | #include <pinocchio/utils/file-explorer.hpp> | ||
| 37 | |||
| 38 | namespace fs = boost::filesystem; | ||
| 39 | |||
| 40 | namespace hpp { | ||
| 41 | namespace core { | ||
| 42 | namespace plugin { | ||
| 43 | ✗ | std::string findPluginLibrary(const std::string& name) { | |
| 44 | ✗ | if (fs::path(name).is_absolute()) return name; | |
| 45 | std::vector<std::string> ppaths = | ||
| 46 | ✗ | ::pinocchio::extractPathFromEnvVar("HPP_PLUGIN_DIRS", ":"); | |
| 47 | ✗ | for (std::size_t i = 0; i < ppaths.size(); ++i) { | |
| 48 | ✗ | fs::path lib(ppaths[i]); | |
| 49 | ✗ | lib /= "hppPlugins"; | |
| 50 | ✗ | lib /= name; | |
| 51 | ✗ | if (fs::is_regular_file(lib)) return lib.native(); | |
| 52 | } | ||
| 53 | |||
| 54 | std::vector<std::string> ldpaths = | ||
| 55 | ✗ | ::pinocchio::extractPathFromEnvVar("LD_LIBRARY_PATH", ":"); | |
| 56 | ✗ | for (std::size_t i = 0; i < ldpaths.size(); ++i) { | |
| 57 | ✗ | fs::path lib(ldpaths[i]); | |
| 58 | ✗ | lib /= "hppPlugins"; | |
| 59 | ✗ | lib /= name; | |
| 60 | ✗ | if (fs::is_regular_file(lib)) return lib.native(); | |
| 61 | } | ||
| 62 | ✗ | HPP_THROW(std::invalid_argument, "Could not find plugin " | |
| 63 | << name | ||
| 64 | << ". Check your LD_LIBRARY_PATH."); | ||
| 65 | } | ||
| 66 | |||
| 67 | 1 | bool loadPlugin(const std::string& lib, ProblemSolverPtr_t ps) { | |
| 68 | typedef ::hpp::core::ProblemSolverPlugin* (*PluginFunction_t)(); | ||
| 69 | |||
| 70 | // Clear old errors | ||
| 71 | 1 | const char* error = dlerror(); | |
| 72 | // void* library = dlopen(lib.c_str(), RTLD_NOW|RTLD_GLOBAL); | ||
| 73 | 1 | void* library = dlopen(lib.c_str(), RTLD_NOW); | |
| 74 | 1 | error = dlerror(); | |
| 75 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (error != NULL) { |
| 76 | ✗ | throw std::runtime_error("Error loading library " + lib + ": " + error); | |
| 77 | return false; | ||
| 78 | } | ||
| 79 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (library == NULL) { |
| 80 | // uncaught error ? | ||
| 81 | ✗ | return false; | |
| 82 | } | ||
| 83 | |||
| 84 | PluginFunction_t function = reinterpret_cast<PluginFunction_t>( | ||
| 85 | 1 | dlsym(library, "createProblemSolverPlugin")); | |
| 86 | 1 | error = dlerror(); | |
| 87 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (error != NULL) { |
| 88 | ✗ | throw std::runtime_error("Error loading library " + lib + ": " + error); | |
| 89 | return false; | ||
| 90 | } | ||
| 91 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (function == NULL) { |
| 92 | ✗ | throw std::runtime_error( | |
| 93 | "Symbol createProblemSolverPlugin of " | ||
| 94 | ✗ | "(correctly loaded) library " + | |
| 95 | ✗ | lib + " is NULL."); | |
| 96 | return false; | ||
| 97 | } | ||
| 98 | |||
| 99 | 1 | ProblemSolverPlugin* plugin = function(); | |
| 100 | 1 | bool success = plugin->initialize(ps); | |
| 101 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | delete plugin; |
| 102 | |||
| 103 | // I don't think we should do that because the symbols should not be | ||
| 104 | // removed... dlclose (library); | ||
| 105 | |||
| 106 | 1 | return success; | |
| 107 | } | ||
| 108 | } // namespace plugin | ||
| 109 | } // namespace core | ||
| 110 | } // namespace hpp | ||
| 111 |