Skip to content

Commit

Permalink
Simplify dl_wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
RainerKuemmerle committed Nov 12, 2023
1 parent 9a12d62 commit c422dcc
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 75 deletions.
11 changes: 0 additions & 11 deletions g2o/apps/g2o_cli/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,6 @@ add_executable(g2o_cli_application

target_link_libraries(g2o_cli_application g2o_cli_library)

set_property(TARGET g2o_cli_library APPEND PROPERTY COMPILE_DEFINITIONS $<$<CONFIG:Debug>:G2O_LIBRARY_POSTFIX="${CMAKE_DEBUG_POSTFIX}">)
set_property(TARGET g2o_cli_library APPEND PROPERTY COMPILE_DEFINITIONS $<$<CONFIG:RelWithDebInfo>:G2O_LIBRARY_POSTFIX="${CMAKE_RELWITHDEBINFO_POSTFIX}">)
set_property(TARGET g2o_cli_library APPEND PROPERTY COMPILE_DEFINITIONS $<$<CONFIG:MinSizeRel>:G2O_LIBRARY_POSTFIX="${CMAKE_MINSIZEREL_POSTFIX}">)

#if(POLICY CMP0043)
#cmake_policy(SET CMP0043 OLD)
#endif()
#set_property(TARGET g2o_cli_library APPEND PROPERTY COMPILE_DEFINITIONS_DEBUG G2O_LIBRARY_POSTFIX="${CMAKE_DEBUG_POSTFIX}")
#set_property(TARGET g2o_cli_library APPEND PROPERTY COMPILE_DEFINITIONS_RELWITHDEBINFO G2O_LIBRARY_POSTFIX="${CMAKE_RELWITHDEBINFO_POSTFIX}")
#set_property(TARGET g2o_cli_library APPEND PROPERTY COMPILE_DEFINITIONS_MINSIZEREL G2O_LIBRARY_POSTFIX="${CMAKE_MINSIZEREL_POSTFIX}")

set_target_properties(g2o_cli_application PROPERTIES OUTPUT_NAME g2o)

install(TARGETS g2o_cli_library g2o_cli_application
Expand Down
29 changes: 8 additions & 21 deletions g2o/apps/g2o_cli/dl_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,9 @@

#include <algorithm>
#include <cstdio>
#include <iostream>

#include "g2o/stuff/filesys_tools.h"
#include "g2o/stuff/macros.h"
#include "g2o/stuff/logger.h"

#if defined(UNIX) || defined(CYGWIN)
#include <dlfcn.h>
Expand Down Expand Up @@ -62,6 +61,7 @@ DlWrapper::~DlWrapper() {

int DlWrapper::openLibraries(const std::string& directory,
const std::string& pattern) {
G2O_TRACE("Loading libraries from {} pattern {}", directory, pattern);
// cerr << "# loading libraries from " << directory << "\t pattern: " <<
// pattern << endl;
string searchPattern = directory + "/" + pattern;
Expand All @@ -75,23 +75,8 @@ int DlWrapper::openLibraries(const std::string& directory,
_filenames.end())
continue;

// If we are doing a release build, the wildcards will pick up the
// suffixes; unfortunately the "_rd" extension means that we
// don't seem to be able to filter out the incompatible files using a
// wildcard expansion to wordexp.

#ifndef G2O_LIBRARY_POSTFIX
if ((filename.rfind(string("_d.") + SO_EXT) ==
filename.length() - 3 - SO_EXT_LEN) ||
(filename.rfind(string("_rd.") + SO_EXT) ==
filename.length() - 4 - SO_EXT_LEN) ||
(filename.rfind(string("_s.") + SO_EXT) ==
filename.length() - 3 - SO_EXT_LEN))
continue;
#endif

// open the lib
// cerr << "loading " << filename << endl;
G2O_TRACE("Loading {}", filename);
if (openLibrary(filename)) numLibs++;
}

Expand All @@ -116,16 +101,18 @@ bool DlWrapper::openLibrary(const std::string& filename) {
#if defined(UNIX) || defined(CYGWIN)
void* handle = dlopen(filename.c_str(), RTLD_LAZY);
if (!handle) {
cerr << __PRETTY_FUNCTION__ << " Cannot open library: " << dlerror()
<< '\n';
G2O_ERROR("Cannot open library: {} Error: {}", filename, dlerror());
return false;
}
#elif defined(WINDOWS)
HMODULE handle = LoadLibrary(filename.c_str());
if (!handle) {
cerr << __PRETTY_FUNCTION__ << " Cannot open library." << endl;
G2O_ERROR("Cannot open library: {}", filename);
return false;
}
#else
#warning "No implementation for openLibrary found"
return false;
#endif

// cerr << "loaded " << filename << endl;
Expand Down
13 changes: 6 additions & 7 deletions g2o/apps/g2o_cli/dl_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ class G2O_CLI_API DlWrapper {
DlWrapper();
virtual ~DlWrapper();

/**
* it's not allowed to draw a copy of the wrapper
*/
DlWrapper(const DlWrapper&) = delete;
DlWrapper& operator=(const DlWrapper&) = delete;

/**
* open all libs from a directory matching a specific pattern.
* @return number of loaded libs
Expand All @@ -72,13 +78,6 @@ class G2O_CLI_API DlWrapper {
std::vector<HMODULE> _handles;
#endif
std::vector<std::string> _filenames;

private:
/**
* it's not allowed to draw a copy of the wrapper
*/
DlWrapper(const DlWrapper&);
DlWrapper& operator=(const DlWrapper&);
};

} // namespace g2o
Expand Down
60 changes: 24 additions & 36 deletions g2o/apps/g2o_cli/g2o_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@
#include "g2o_common.h"

#include <cstdlib>
#include <iostream>
#include <string_view>
#include <vector>

#include "dl_wrapper.h"
#include "g2o/stuff/filesys_tools.h"
#include "g2o/stuff/logger.h"
#include "g2o/stuff/string_tools.h"
using namespace ::std;

/*
* setting up the library filename patterns for the different OS
Expand Down Expand Up @@ -71,23 +71,13 @@ HMODULE getMyInstance() {
}
#endif

// This can occur if we are doing a release build, and the release
// postfix is empty
#ifndef G2O_LIBRARY_POSTFIX
#define G2O_LIBRARY_POSTFIX ""
#endif

static const string TYPES_PATTERN = string("*_types_*") +
string(G2O_LIBRARY_POSTFIX) + string(".") +
string(SO_EXT);
static const string SOLVERS_PATTERN = string("*_solver_*") +
string(G2O_LIBRARY_POSTFIX) +
string(".") + string(SO_EXT);
static constexpr std::string_view TYPES_PATTERN = "*_types_*." SO_EXT;
static constexpr std::string_view SOLVERS_PATTERN = "*_solver_*." SO_EXT;

namespace g2o {

void findArguments(const std::string& option, vector<string>& args, int argc,
char** argv) {
void findArguments(const std::string& option, std::vector<std::string>& args,
int argc, char** argv) {
args.clear();
for (int i = 0; i < argc; ++i) {
if (argv[i] == option && i + 1 < argc) {
Expand All @@ -98,7 +88,7 @@ void findArguments(const std::string& option, vector<string>& args, int argc,

void loadStandardTypes(DlWrapper& dlTypesWrapper, int argc, char** argv) {
char* envTypesPath = getenv("G2O_TYPES_DIR");
string typesPath;
std::string typesPath;

if (envTypesPath != NULL) {
typesPath = envTypesPath;
Expand All @@ -117,24 +107,23 @@ void loadStandardTypes(DlWrapper& dlTypesWrapper, int argc, char** argv) {
#endif
}

vector<string> paths = strSplit(typesPath, PATH_SEPARATOR);
for (vector<string>::const_iterator it = paths.begin(); it != paths.end();
++it) {
if (it->size() > 0) dlTypesWrapper.openLibraries(*it, TYPES_PATTERN);
std::vector<std::string> paths = strSplit(typesPath, PATH_SEPARATOR);
for (const auto& path : paths) {
if (!path.empty())
dlTypesWrapper.openLibraries(path, std::string(TYPES_PATTERN));
}

vector<string> libs;
std::vector<std::string> libs;
if (argc > 0 && argv != 0) findArguments("-typeslib", libs, argc, argv);
for (vector<string>::const_iterator it = libs.begin(); it != libs.end();
++it) {
cerr << "Loading types " << *it << endl;
dlTypesWrapper.openLibrary(*it);
for (const auto& lib : libs) {
G2O_INFO("Loading types {}", lib);
dlTypesWrapper.openLibrary(lib);
}
}

void loadStandardSolver(DlWrapper& dlSolverWrapper, int argc, char** argv) {
char* envSolversPath = getenv("G2O_SOLVERS_DIR");
string solversPath = G2O_DEFAULT_SOLVERS_DIR_;
std::string solversPath = G2O_DEFAULT_SOLVERS_DIR_;

if (envSolversPath != NULL) {
solversPath = envSolversPath;
Expand All @@ -152,18 +141,17 @@ void loadStandardSolver(DlWrapper& dlSolverWrapper, int argc, char** argv) {
#endif
}

vector<string> paths = strSplit(solversPath, PATH_SEPARATOR);
for (vector<string>::const_iterator it = paths.begin(); it != paths.end();
++it) {
if (it->size() > 0) dlSolverWrapper.openLibraries(*it, SOLVERS_PATTERN);
std::vector<std::string> paths = strSplit(solversPath, PATH_SEPARATOR);
for (const auto& path : paths) {
if (!path.empty())
dlSolverWrapper.openLibraries(path, std::string(SOLVERS_PATTERN));
}

vector<string> libs;
std::vector<std::string> libs;
if (argc > 0 && argv != 0) findArguments("-solverlib", libs, argc, argv);
for (vector<string>::const_iterator it = libs.begin(); it != libs.end();
++it) {
cerr << "Loading solver " << *it << endl;
dlSolverWrapper.openLibrary(*it);
for (const auto& lib : libs) {
G2O_INFO("Loading solver {}", lib);
dlSolverWrapper.openLibrary(lib);
}
}

Expand Down

0 comments on commit c422dcc

Please sign in to comment.