g2o
Loading...
Searching...
No Matches
g2o_common.cpp
Go to the documentation of this file.
1// g2o - General Graph Optimization
2// Copyright (C) 2011 R. Kuemmerle, G. Grisetti, W. Burgard
3// All rights reserved.
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// * Redistributions of source code must retain the above copyright notice,
10// this list of conditions and the following disclaimer.
11// * Redistributions in binary form must reproduce the above copyright
12// notice, this list of conditions and the following disclaimer in the
13// documentation and/or other materials provided with the distribution.
14//
15// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
16// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
18// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27#include "g2o_common.h"
28
29#include <cstdlib>
30#include <iostream>
31#include <vector>
32
33#include "dl_wrapper.h"
36using namespace ::std;
37
38/*
39 * setting up the library filename patterns for the different OS
40 */
41#ifdef __APPLE__
42#define SO_EXT "dylib"
43#elif defined(WINDOWS) || defined(CYGWIN)
44#define SO_EXT "dll"
45#else // Linux
46#define SO_EXT "so"
47#endif
48
49// This is used to determine where this library is
50#if defined(UNIX) || defined(CYGWIN)
51#if (defined UNIX)
52// dladdr is not available on a recent installation of Cygwin
53#ifndef _GNU_SOURCE
54#define _GNU_SOURCE
55#endif
56#include <dlfcn.h>
57static Dl_info info;
58#endif
59#define PATH_SEPARATOR ":"
60#else // WINDOWS
61#define PATH_SEPARATOR ";"
62
63static void fakeFunctionForWindows() {}
64
65HMODULE getMyInstance() {
66 MEMORY_BASIC_INFORMATION mbi;
67 if (VirtualQuery((const void*)&fakeFunctionForWindows, &mbi, sizeof(mbi))) {
68 return (HMODULE)mbi.AllocationBase;
69 }
70 return NULL;
71}
72#endif
73
74// This can occur if we are doing a release build, and the release
75// postfix is empty
76#ifndef G2O_LIBRARY_POSTFIX
77#define G2O_LIBRARY_POSTFIX ""
78#endif
79
80static const string TYPES_PATTERN = string("*_types_*") +
81 string(G2O_LIBRARY_POSTFIX) + string(".") +
82 string(SO_EXT);
83static const string SOLVERS_PATTERN = string("*_solver_*") +
84 string(G2O_LIBRARY_POSTFIX) +
85 string(".") + string(SO_EXT);
86
87namespace g2o {
88
89void findArguments(const std::string& option, vector<string>& args, int argc,
90 char** argv) {
91 args.clear();
92 for (int i = 0; i < argc; ++i) {
93 if (argv[i] == option && i + 1 < argc) {
94 args.push_back(argv[i + 1]);
95 }
96 }
97}
98
99void loadStandardTypes(DlWrapper& dlTypesWrapper, int argc, char** argv) {
100 char* envTypesPath = getenv("G2O_TYPES_DIR");
101 string typesPath;
102
103 if (envTypesPath != NULL) {
104 typesPath = envTypesPath;
105 } else {
106 typesPath = G2O_DEFAULT_TYPES_DIR_;
107#if (defined UNIX)
108 if (dladdr(&info, &info) != 0) {
109 typesPath = getDirname(info.dli_fname);
110 }
111#elif (defined WINDOWS)
112 char libFilename[MAX_PATH + 1];
113 HMODULE instance = getMyInstance();
114 if (instance && GetModuleFileName(instance, libFilename, MAX_PATH) > 0) {
115 typesPath = getDirname(libFilename);
116 }
117#endif
118 }
119
120 vector<string> paths = strSplit(typesPath, PATH_SEPARATOR);
121 for (vector<string>::const_iterator it = paths.begin(); it != paths.end();
122 ++it) {
123 if (it->size() > 0) dlTypesWrapper.openLibraries(*it, TYPES_PATTERN);
124 }
125
126 vector<string> libs;
127 if (argc > 0 && argv != 0) findArguments("-typeslib", libs, argc, argv);
128 for (vector<string>::const_iterator it = libs.begin(); it != libs.end();
129 ++it) {
130 cerr << "Loading types " << *it << endl;
131 dlTypesWrapper.openLibrary(*it);
132 }
133}
134
135void loadStandardSolver(DlWrapper& dlSolverWrapper, int argc, char** argv) {
136 char* envSolversPath = getenv("G2O_SOLVERS_DIR");
137 string solversPath = G2O_DEFAULT_SOLVERS_DIR_;
138
139 if (envSolversPath != NULL) {
140 solversPath = envSolversPath;
141 } else {
142#if (defined UNIX)
143 if (dladdr(&info, &info) != 0) {
144 solversPath = getDirname(info.dli_fname);
145 }
146#elif (defined WINDOWS)
147 char libFilename[MAX_PATH + 1];
148 HMODULE instance = getMyInstance();
149 if (instance && GetModuleFileName(instance, libFilename, MAX_PATH) > 0) {
150 solversPath = getDirname(libFilename);
151 }
152#endif
153 }
154
155 vector<string> paths = strSplit(solversPath, PATH_SEPARATOR);
156 for (vector<string>::const_iterator it = paths.begin(); it != paths.end();
157 ++it) {
158 if (it->size() > 0) dlSolverWrapper.openLibraries(*it, SOLVERS_PATTERN);
159 }
160
161 vector<string> libs;
162 if (argc > 0 && argv != 0) findArguments("-solverlib", libs, argc, argv);
163 for (vector<string>::const_iterator it = libs.begin(); it != libs.end();
164 ++it) {
165 cerr << "Loading solver " << *it << endl;
166 dlSolverWrapper.openLibrary(*it);
167 }
168}
169
170} // namespace g2o
Loading libraries during run-time.
Definition dl_wrapper.h:46
bool openLibrary(const std::string &filename)
int openLibraries(const std::string &directory, const std::string &pattern="")
#define PATH_SEPARATOR
static const string TYPES_PATTERN
#define SO_EXT
static void fakeFunctionForWindows()
#define G2O_LIBRARY_POSTFIX
HMODULE getMyInstance()
static const string SOLVERS_PATTERN
void findArguments(const std::string &option, vector< string > &args, int argc, char **argv)
void loadStandardTypes(DlWrapper &dlTypesWrapper, int argc, char **argv)
std::vector< std::string > strSplit(const std::string &str, const std::string &delimiters)
void loadStandardSolver(DlWrapper &dlSolverWrapper, int argc, char **argv)
std::string getDirname(const std::string &filename)
Definition jet.h:876