g2o
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 "dl_wrapper.h"
30 #include "g2o/stuff/string_tools.h"
32 
33 #include <vector>
34 #include <iostream>
35 #include <cstdlib>
36 using 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>
57  static Dl_info info;
58 # endif
59 # define PATH_SEPARATOR ":"
60 #else // WINDOWS
61 #define PATH_SEPARATOR ";"
62 
63 static void fakeFunctionForWindows() {}
64 
65 HMODULE getMyInstance()
66 {
67  MEMORY_BASIC_INFORMATION mbi;
68  if (VirtualQuery((const void *)&fakeFunctionForWindows, &mbi, sizeof(mbi))) {
69  return (HMODULE) mbi.AllocationBase;
70  }
71  return NULL;
72 }
73 #endif
74 
75 // This can occur if we are doing a release build, and the release
76 // postfix is empty
77 #ifndef G2O_LIBRARY_POSTFIX
78 #define G2O_LIBRARY_POSTFIX ""
79 #endif
80 
81 static const string TYPES_PATTERN=string("*_types_*")+string(G2O_LIBRARY_POSTFIX)+string(".")+string(SO_EXT);
82 static const string SOLVERS_PATTERN=string("*_solver_*")+string(G2O_LIBRARY_POSTFIX)+string(".")+string(SO_EXT);
83 
84 namespace g2o {
85 
86 void findArguments(const std::string& option, vector<string>& args, int argc, char** argv)
87 {
88  args.clear();
89  for (int i = 0; i < argc; ++i) {
90  if (argv[i] == option && i + 1 < argc) {
91  args.push_back(argv[i+1]);
92  }
93  }
94 }
95 
96 void loadStandardTypes(DlWrapper& dlTypesWrapper, int argc, char** argv)
97 {
98  char * envTypesPath = getenv("G2O_TYPES_DIR");
99  string typesPath;
100 
101  if (envTypesPath != NULL) {
102  typesPath = envTypesPath;
103  } else {
104  typesPath = G2O_DEFAULT_TYPES_DIR_;
105 #if (defined UNIX)
106  if (dladdr(&info, &info) != 0) {
107  typesPath = getDirname(info.dli_fname);
108  }
109 #elif (defined WINDOWS)
110  char libFilename[MAX_PATH + 1];
111  HMODULE instance = getMyInstance();
112  if (instance && GetModuleFileName(instance, libFilename, MAX_PATH) > 0) {
113  typesPath = getDirname(libFilename);
114  }
115 #endif
116  }
117 
118  vector<string> paths = strSplit(typesPath, PATH_SEPARATOR);
119  for (vector<string>::const_iterator it = paths.begin(); it != paths.end(); ++it) {
120  if (it->size() > 0)
121  dlTypesWrapper.openLibraries(*it, TYPES_PATTERN);
122  }
123 
124  vector<string> libs;
125  if (argc > 0 && argv != 0)
126  findArguments("-typeslib", libs, argc, argv);
127  for (vector<string>::const_iterator it = libs.begin(); it != libs.end(); ++it) {
128  cerr << "Loading types " << *it << endl;
129  dlTypesWrapper.openLibrary(*it);
130  }
131 }
132 
133 void loadStandardSolver(DlWrapper& dlSolverWrapper, int argc, char** argv)
134 {
135  char * envSolversPath = getenv("G2O_SOLVERS_DIR");
136  string solversPath = G2O_DEFAULT_SOLVERS_DIR_;
137 
138  if (envSolversPath != NULL) {
139  solversPath = envSolversPath;
140  } else {
141 #if (defined UNIX)
142  if (dladdr(&info, &info) != 0) {
143  solversPath = getDirname(info.dli_fname);
144  }
145 #elif (defined WINDOWS)
146  char libFilename[MAX_PATH + 1];
147  HMODULE instance = getMyInstance();
148  if (instance && GetModuleFileName(instance, libFilename, MAX_PATH) > 0) {
149  solversPath = getDirname(libFilename);
150  }
151 #endif
152  }
153 
154  vector<string> paths = strSplit(solversPath, PATH_SEPARATOR);
155  for (vector<string>::const_iterator it = paths.begin(); it != paths.end(); ++it) {
156  if (it->size() > 0)
157  dlSolverWrapper.openLibraries(*it, SOLVERS_PATTERN);
158  }
159 
160  vector<string> libs;
161  if (argc > 0 && argv != 0)
162  findArguments("-solverlib", libs, argc, argv);
163  for (vector<string>::const_iterator it = libs.begin(); it != libs.end(); ++it) {
164  cerr << "Loading solver " << *it << endl;
165  dlSolverWrapper.openLibrary(*it);
166  }
167 }
168 
169 } // end namespace
#define G2O_LIBRARY_POSTFIX
Definition: g2o_common.cpp:78
int openLibraries(const std::string &directory, const std::string &pattern="")
Definition: dl_wrapper.cpp:65
void loadStandardTypes(DlWrapper &dlTypesWrapper, int argc, char **argv)
Definition: g2o_common.cpp:96
static const string SOLVERS_PATTERN
Definition: g2o_common.cpp:82
#define PATH_SEPARATOR
Definition: g2o_common.cpp:61
HMODULE getMyInstance()
Definition: g2o_common.cpp:65
bool openLibrary(const std::string &filename)
Definition: dl_wrapper.cpp:115
void findArguments(const std::string &option, vector< string > &args, int argc, char **argv)
Definition: g2o_common.cpp:86
static void fakeFunctionForWindows()
Definition: g2o_common.cpp:63
#define SO_EXT
Definition: g2o_common.cpp:46
std::string getDirname(const std::string &filename)
std::vector< std::string > strSplit(const std::string &str, const std::string &delimiters)
void loadStandardSolver(DlWrapper &dlSolverWrapper, int argc, char **argv)
Definition: g2o_common.cpp:133
static const string TYPES_PATTERN
Definition: g2o_common.cpp:81
Loading libraries during run-time.
Definition: dl_wrapper.h:44