50 "codeTemplates/dynamicCode"
58 const char*
const Foam::dynamicCode::libTargetRoot
60 "LIB = $(PWD)/../platforms/$(WM_OPTIONS)/lib/lib"
66 void Foam::dynamicCode::addLineDirective
73 code =
"#line " +
Foam::name(lineNum) +
" \"" +
name +
"\"\n" + code;
77 void Foam::dynamicCode::copyAndFilter
81 const HashTable<string>& mapping
87 <<
"Failed opening for reading " << is.name()
94 <<
"Failed writing " << os.name()
103 is.getLine(line,
false);
109 os.writeQuoted(line,
false) <<
nl;
115 bool Foam::dynamicCode::resolveTemplates
118 DynamicList<fileName>& resolvedFiles,
119 DynamicList<fileName>& badFiles
123 forAll(templateNames, fileI)
125 const fileName& templateName = templateNames[fileI];
132 dynamicCode::codeTemplateDirName,
139 badFiles.append(templateName);
144 resolvedFiles.append(file);
152 bool Foam::dynamicCode::createMakeFiles()
const
155 if (compileFiles_.empty())
160 const fileName dstFile(codePath()/
"Make/files");
163 mkDir(dstFile.path());
165 OFstream os(dstFile);
170 <<
"Failed writing " << dstFile
175 forAll(compileFiles_, fileI)
177 os.writeQuoted(compileFiles_[fileI],
false) <<
nl;
180 os <<
nl << dynamicCode::libTargetRoot << codeSha1Name_ <<
nl;
186 bool Foam::dynamicCode::createMakeOptions()
const
188 if (compileFiles_.empty())
194 const fileName optionsFile
199 verbatimString options;
202 if (!optionsFileName_.empty())
204 const fileName optionsFile
209 if (!optionsFile.empty())
211 IFstream is(optionsFile);
215 <<
"Failed opening " << optionsFile
219 dictionary optionsDict(is);
221 options = optionsDict.lookupOrDefault<verbatimString>
227 libs = optionsDict.lookupOrDefault<verbatimString>
236 <<
"Cannot find options file " << optionsFileName_
245 if (options.empty() &&
libs.empty())
250 const fileName dstFile(codePath()/
"Make/options");
251 mkDir(dstFile.path());
253 OFstream os(dstFile);
258 <<
"Failed writing " << dstFile
262 os.writeQuoted(options +
"\n\n" +
libs,
false) <<
nl;
268 bool Foam::dynamicCode::writeDigest()
const
270 const fileName
file(digestFile());
275 os.writeQuoted(sha1_.str(),
false) <<
nl;
287 const word& codeName,
288 const word& codeDirName,
291 const word& optionsFileName,
300 libSubDir_(stringOps::
expandEnvVar(
"platforms/$WM_OPTIONS/lib")),
303 codeDictVars_(codeDictVars),
304 optionsFileName_(optionsFileName),
305 compileFiles_(compileFiles),
306 copyFiles_(copyFiles),
307 codeStrings_(codeKeys.size())
312 <<
"This code should not be executed by someone with administrator"
313 <<
" rights due to security reasons." <<
nl
314 <<
"(it writes a shared library which then gets loaded "
322 <<
"Loading a shared library using case-supplied code is not"
323 <<
" enabled by default" <<
nl
324 <<
"because of security issues. If you trust the code you can"
325 <<
" enable this" <<
nl
326 <<
"facility be adding to the InfoSwitches setting in the system"
327 <<
" controlDict:" <<
nl <<
nl
328 <<
" allowSystemOperations 1" <<
nl <<
nl
329 <<
"The system controlDict is either" <<
nl <<
nl
330 <<
" ~/.OpenFOAM/$WM_PROJECT_VERSION/controlDict" <<
nl <<
nl
332 <<
" $WM_PROJECT_DIR/etc/controlDict" <<
nl
337 read(contextDict, codeDict);
339 const word sha1Str(sha1_.
str());
341 codeSha1Name_ = codeName_ +
'_' + sha1Str;
346 ?
word(
'_' + sha1Str)
350 varSubstitutions_.set(
"typeName", codeName_);
351 varSubstitutions_.set(
"uniqueFunctionName", codeSha1Name_);
352 varSubstitutions_.set(
"SHA1sum", sha1Str);
359 const word& codeName,
360 const word& codeDirName,
363 const word& codeOptionsFileName,
395 codeKeySubstitutions_.clear();
398 const word& key = codeKeys_[i];
409 codeKeySubstitutions_.insert(key, codeStrings_[i]);
413 codeKeySubstitutions_.insert(key,
"");
418 const entry* optionsPtr =
452 os << options_ << libs_;
460 const word& key = codeKeys_[i];
463 codeKeySubstitutions_[key],
464 codePtrs[i]->startLineNumber(),
498 Info<<
"Creating new library in " << libRelPath() <<
endl;
506 filterVars.set(iter.key(), iter());
510 compileFiles_.size() + copyFiles_.size();
516 dynamicCode::resolveTemplates
522 dynamicCode::resolveTemplates
529 if (!badFiles.
empty())
532 <<
"Could not find the code template(s): "
538 const fileName outputDir(codePath());
544 forAll(resolvedFiles, fileI)
546 const fileName& srcFile = resolvedFiles[fileI];
551 Info <<
" Copying " << srcFile <<
" to " << dstFile <<
endl;
558 <<
"Failed opening " << srcFile
566 <<
"Failed writing " << dstFile
571 dynamicCode::copyAndFilter(is, os, filterVars);
587 const string wmakeCmd(
"wmake -s libso " + codePath());
624 return dlOpen(libPath,
false);
632 const bool masterOnlyRead
644 if (!copyOrCreateFiles(
true))
649 ) <<
"Failed writing files for" <<
nl
650 << libRelPath() <<
nl
660 ) <<
"Failed wmake " << libRelPath() <<
nl
673 const fileName libPath = this->libPath();
688 <<
" have masterSize:" << masterSize
689 <<
" and localSize:" << mySize
693 if (mySize < masterSize)
697 Pout<<
"Local file " << libPath
698 <<
" not of same size (" << mySize
700 << masterSize <<
"). Waiting for "
702 <<
" seconds." <<
endl;
709 if (mySize < masterSize)
714 ) <<
"Cannot read (NFS mounted) library " <<
nl
717 <<
" detected size " << mySize
718 <<
" whereas master size is " << masterSize
720 <<
"If your case is not NFS mounted"
721 <<
" (so distributed) set fileModificationSkew"
730 <<
" after waiting: have masterSize:" << masterSize
731 <<
" and localSize:" << mySize
740 read(contextDict, contextDict);
752 writeEntry(os, codeKeys_[i], codeStrings_[i]);
758 writeEntry(os,
"codeOptions", optionsString_);
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
#define forAll(list, i)
Loop across all elements in list.
#define forAllConstIter(Container, container, iter)
Iterate across all elements in the container object of type.
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
An STL-conforming hash table.
bool good() const
Return true if next operation might succeed.
bool good() const
Return true if next operation might succeed.
A Foam::OSstream for calculating SHA-1 digests.
SHA1Digest digest()
Return the SHA-1 digest for the data processed until now.
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
static void scatter(const List< commsStruct > &comms, T &Value, const int tag, const label comm)
Scatter data. Distribute without modification. Reverse of gather.
std::string str(const bool prefixed=false) const
Return (40-byte) text representation, optionally with '_' prefix.
bool empty() const
Return true if the UList is empty (ie, size() is zero)
static bool master(const label communicator=0)
Am I the master process.
static int myProcNo(const label communicator=0)
Number of this process (starting from masterNo() = 0)
const fileName & name() const
Return the dictionary name.
A list of keywords followed by any number of values (e.g. words and numbers) or sub-dictionaries.
const entry * lookupEntryPtr(const word &, bool recursive, bool patternMatch) const
Find and return an entry data stream pointer if present.
void * findLibrary(const fileName &libName)
Find the handle of the named library.
bool open(const fileName &libName, const bool verbose=true)
Open the named library, optionally with warnings if problems occur.
Encapsulation of dynamic code dictionaries and functionality.
dynamicCode(const dictionary &contextDict, const dictionary &codeDict, const word &codeName, const word &codeDirName, const wordList &codeKeys, const wordList &codeDictVars, const word &optionsFileName, const wordList &compileFiles, const wordList ©Files)
Construct from the context and code dictionaries.
static int allowSystemOperations
Flag if system operations are allowed.
bool upToDate() const
Verify if the copied code is up-to-date, based on Make/SHA1Digest.
bool copyOrCreateFiles(const bool verbose=false) const
Copy/create files prior to compilation.
void write(Ostream &os) const
Write the code for restart.
void * loadLibrary(const fileName &libPath) const
static word libraryBaseName(const fileName &libPath)
Return the library basename without leading 'lib' or trailing '.so'.
static fileName resolveTemplate(const fileName &templateName)
Resolve code-template via Foam::findConfigFile.
bool wmakeLibso() const
Compile a libso.
void createLibrary(const dictionary &dict, const bool masterOnlyRead=false) const
void read(const dictionary &contextDict, const dictionary &codeDict)
A keyword and a list of tokens is an 'entry'.
virtual ITstream & stream() const =0
Return token stream if this entry is a primitive entry.
A class for handling file names.
word name() const
Return file name (part beyond last /)
static float fileModificationSkew
A class for handling verbatimStrings, derived from string.
static const verbatimString null
An empty verbatimString.
A class for handling words, derived from string.
static const word null
An empty word.
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
int infoSwitch(const char *name, const int defaultValue=0)
Lookup info switch or add default value.
string & inplaceExpandCodeString(string &, const dictionary &dict, const word &dictVar="dict", const char sigil='$')
Inplace expand occurrences of variables according to the dictionary.
string trim(const string &)
Return string trimmed of leading and trailing whitespace.
string & inplaceExpandCodeTemplate(string &, const HashTable< string, word, string::hash > &mapping, const char sigil='$')
Inplace expand occurrences of variables according to the mapping.
string expandEnvVar(const string &, const bool allowEmpty=false)
Expand all occurrences of environment variables and paths.
errorManipArg< error, int > exit(error &err, const int errNo=1)
dlLibraryTable libs
Table of loaded dynamic libraries.
int system(const std::string &command)
Execute the specified command.
off_t fileSize(const fileName &, const bool checkVariants=true, const bool followLink=true)
Return size of file.
List< word > wordList
A List of words.
fileName findConfigFile(const word &configName, const fileName &configFilesPath, const word &configFilesDir, const word ®ion=word::null)
Search for configuration file for given region.
bool read(const char *, int32_t &)
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
bool mkDir(const fileName &, mode_t=0777)
Make a directory and return an error if it could not be created.
Ostream & endl(Ostream &os)
Add newline and flush stream.
bool isAdministrator()
Is user administrator.
void * dlOpen(const fileName &lib, const bool check=true)
Open a shared library. Return handle to library. Print error message.
bool exists(const fileName &, const bool checkVariants=true, const bool followLink=true)
Does the name exist (as directory or file) in the file system?
word name(const LagrangianState state)
Return a string representation of a Lagrangian state enumeration.
prefixOSstream Pout(cout, "Pout")
defineTypeNameAndDebug(atmosphericBoundaryLayer, 0)
unsigned int sleep(const unsigned int)
Sleep for the specified number of seconds.
void writeEntry(Ostream &os, const word &key, const DimensionedFieldFunction< DimensionedFieldType > &f)