forked from FreeFem/FreeFem-sources
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload.cpp
195 lines (167 loc) · 5.1 KB
/
load.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// -*- Mode : c++ -*-
//
// SUMMARY :
// USAGE :
// ORG :
// AUTHOR : Frederic Hecht
// E-MAIL : [email protected]
//
/*
This file is part of Freefem++
Freefem++ is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
Freefem++ is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with Freefem++; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "config-wrapper.h" // needed for HAVE_DLFCN_H
#include <iostream>
#include <map>
#include <set>
#include "AFunction.hpp"
#include "environment.hpp"
#include "InitFunct.hpp"
using namespace std;
#include "lex.hpp"
#define LOAD 1
// ALH - 11/3/15 - added NOLOAD macro option because no dynamic load available in javascript
#if defined(__INTEL__) || defined(__MWERKS__) || !defined(HAVE_DLFCN_H) || defined(NOLOAD)
#undef LOAD
#endif
#ifdef LOAD
#include <dlfcn.h>
#elif _WIN32
#include <windows.h>
#endif
#include "ffapi.hpp"
set<string> SetLoadFile;
bool load(string ss)
{
// FFCS - do not allow potentially dangerous commands from remote anonymous clients
if(ffapi::protectedservermode() && (ss=="pipe" || ss=="shell")){
cerr<<"library "<<ss<<" not allowed in server environment"<<endl;
CompileError("Error load");
return 0;
}
if(SetLoadFile.find(ss) != SetLoadFile.end())
{
if( (mpirank==0)&& verbosity)
cout << " (already loaded : " << ss << " ) " ;
}
else
{
SetLoadFile.insert(ss);
bool ret=false;
void * handle = 0;
const int /*nbprefix=2,*/nbsuffix=2;
list<string> prefix(ffenvironment["loadpath"]);
if(prefix.empty())
{
prefix.push_back("");
prefix.push_back("./");
}
string suffix[nbsuffix] ;
suffix[0]="";
suffix[1]=".so";
#ifdef __APPLE__
suffix[1]=".dylib";
#endif
#ifdef _WIN32
suffix[1]=".dll";
#endif
int j;
for (list<string>::const_iterator i= prefix.begin();i !=prefix.end();++i)
for ( j= 0;j< nbsuffix;++j)
{
string s= *i+ss+suffix[j];
#ifdef LOAD
handle = dlopen (s.c_str(), RTLD_LAZY );
if (verbosity>9) cout << " test dlopen(" << s << ")= " << handle << endl;
// FFCS - 20/9/11 - print explanation for load errors
if(verbosity>9 && !handle){
cout<<"load error was: "<<dlerror()<<endl;
}
ret= handle !=0;
if ( ret )
{
if(verbosity > 1 && (mpirank ==0))
cout << " (load: dlopen " << s << " " << handle << ") ";
callInitsFunct() ; // [[file:InitFunct.cpp::callInitsFunct]]
return handle;
}
#elif _WIN32
{
HINSTANCE mod= LoadLibrary(s.c_str());
if (verbosity>9) cout << " test LoadLibrary(" << s << ")= " << mod << endl;
if(mod==0)
{
DWORD merr = GetLastError();
if(verbosity>19)
cerr << "\n try loadLibary : " <<s << "\n \t fail : " << merr << endl;
}
else
{
if(verbosity&& (mpirank ==0))
cout << "(load: loadLibary " << s << " = " << handle << ")";
callInitsFunct() ; // [[file:InitFunct.cpp::callInitsFunct]]
return mod;
}
}
#elif STATIC_LINKING
// <<STATIC_LINKING>> Enable statically linked libraries for [[file:~/fflib/Makefile::STATIC_LINKING]] - ALH
bool ok=false;
// <<static_load_msh3>> [[file:~/ff/examples++-load/msh3.cpp::dynamic_loading]]
if(ss=="msh3"){
// [[file:~/ff/examples++-load/msh3.cpp::msh3_Load_Init]]
void msh3_Load_Init();
msh3_Load_Init();
ok=true;
}
// <<static_load_medit>> [[file:~/ff/examples++-load/medit.cpp::dynamic_loading]]
if(ss=="medit"){
// [[file:~/ff/examples++-load/medit.cpp::medit_Load_Init]]
void medit_Load_Init();
medit_Load_Init();
ok=true;
}
if(ok && verbosity && (mpirank ==0)) cout << " (static load: " << ss << " " << ") ";
return ok;
#else
if(mpirank ==0)
{
cout << "------------------------------------ \n" ;
cout << " load: sorry no dlopen on this system " << s << " \n" ;
cout << "------------------------------------ \n" ;
}
CompileError("Error load");
return 0;
#endif
}
if(mpirank ==0)
{
cerr << "\nload error : " << ss << "\n \t fail : " << endl;
char *error=0;
#ifndef _WIN32
#ifdef LOAD
error= dlerror();
if ( error != NULL) {
cerr << " dlerror : " << error << endl;
}
#endif
#endif
cerr << "list prefix: " ;
for (list<string>::const_iterator i= prefix.begin();i !=prefix.end();++i)
cerr <<"'"<<*i<<"' ";
cerr << "list suffix : '"<< suffix[0] << "' , '" << suffix[1] << "' ";
cerr << endl;
}
CompileError("Error load");
}
return 0 ;
}