Go to the documentation of this file.
43 =
"FOAM_CODE_TEMPLATES";
46 =
"codeTemplates/dynamicCode";
49 "LIB = $(PWD)/../platforms/$(WM_OPTIONS)/lib/lib";
65 <<
"This code should not be executed by someone with administrator"
66 <<
" rights due to security reasons." <<
nl
67 <<
"(it writes a shared library which then gets loaded "
72 if (!allowSystemOperations)
75 <<
"Loading a shared library using case-supplied code is not"
76 <<
" enabled by default" <<
nl
77 <<
"because of security issues. If you trust the code you can"
78 <<
" enable this" <<
nl
79 <<
"facility be adding to the InfoSwitches setting in the system"
80 <<
" controlDict:" <<
nl <<
nl
81 <<
" allowSystemOperations 1" <<
nl <<
nl
82 <<
"The system controlDict is either" <<
nl <<
nl
83 <<
" ~/.OpenFOAM/$WM_PROJECT_VERSION/controlDict" <<
nl <<
nl
85 <<
" $WM_PROJECT_DIR/etc/controlDict" <<
nl
113 <<
"Failed opening for reading " << is.
name()
120 <<
"Failed writing " << os.
name()
151 forAll(templateNames, fileI)
153 const fileName& templateName = templateNames[fileI];
156 if (!templateDir.empty() &&
isDir(templateDir))
158 file = templateDir/templateName;
168 file =
findEtcFile(codeTemplateDirName/templateName);
173 badFiles.
append(templateName);
178 resolvedFiles.
append(file);
188 const bool hasSHA1 = filterVars_.found(
"SHA1sum");
192 os <<
"/* dynamicCode:\n * SHA1 = ";
193 os.
writeQuoted(filterVars_[
"SHA1sum"],
false) <<
"\n */\n";
203 if (compileFiles_.empty())
208 const fileName dstFile(this->codePath()/
"Make/files");
218 <<
"Failed writing " << dstFile
222 writeCommentSHA1(os);
225 forAll(compileFiles_, fileI)
231 << libTargetRoot << codeName_.c_str() <<
nl;
240 if (compileFiles_.empty() || makeOptions_.empty())
245 const fileName dstFile(this->codePath()/
"Make/options");
255 <<
"Failed writing " << dstFile
259 writeCommentSHA1(os);
295 codeRoot_(stringOps::
expand(
"$FOAM_CASE")/topDirName),
296 libSubDir_(stringOps::
expand(
"platforms/$WM_OPTIONS/lib")),
298 codeDirName_(codeDirName)
313 return topDirName/codeDirName_;
319 return codeRelPath()/libSubDir_/
"lib" + codeName_ +
".so";
325 compileFiles_.clear();
327 createFiles_.clear();
329 filterVars_.set(
"typeName", codeName_);
330 filterVars_.set(
"SHA1sum",
SHA1Digest().str());
345 setFilterContext(context);
351 compileFiles_.append(
name);
357 copyFiles_.append(
name);
364 const string& contents
376 filterVars_.set(
"localCode", context.
localCode());
377 filterVars_.set(
"code", context.
code());
378 filterVars_.set(
"codeInclude", context.
include());
379 filterVars_.set(
"SHA1sum", context.
sha1().
str());
386 const std::string& value
389 filterVars_.set(key, value);
395 makeOptions_ = content;
403 Info<<
"Creating new library in " << this->libRelPath() <<
endl;
406 const label nFiles = compileFiles_.size() + copyFiles_.size();
412 resolveTemplates(compileFiles_, resolvedFiles, badFiles);
413 resolveTemplates(copyFiles_, resolvedFiles, badFiles);
415 if (!badFiles.empty())
418 <<
"Could not find the code template(s): "
420 <<
"Under the $" << codeTemplateEnvName
421 <<
" directory or via via the ~OpenFOAM/"
422 << codeTemplateDirName <<
" expansion"
429 const fileName outputDir = this->codePath();
435 forAll(resolvedFiles, fileI)
437 const fileName& srcFile = resolvedFiles[fileI];
445 <<
"Failed opening " << srcFile
454 <<
"Failed writing " << dstFile
459 copyAndFilter(is, os, filterVars_);
464 forAll(createFiles_, fileI)
477 <<
"Failed writing " << dstFile
488 writeDigest(filterVars_[
"SHA1sum"]);
496 const Foam::string wmakeCmd(
"wmake -s libso " + this->codePath());
497 Info<<
"Invoking " << wmakeCmd <<
endl;
525 return upToDate(context.
sha1());
fileName findEtcFile(const fileName &, bool mandatory=false)
Search for a file using findEtcFiles.
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
bool createMakeFiles() const
Copy/create Make/files prior to compilation.
static const char *const topDirName
Top-level directory name for copy/compiling.
bool writeCommentSHA1(Ostream &) const
Write SHA1 value as C-comment.
Ostream & write(Ostream &, const bool prefixed=false) const
Write (40-byte) text representation, optionally with '_' prefix.
A class for handling words, derived from string.
A class for handling file names.
bool exists(const fileName &, const bool checkGzip=true)
Does the name exist (as DIRECTORY or FILE) in the file system?
fileName libPath() const
Library path for specified code name.
static void copyAndFilter(ISstream &, OSstream &, const HashTable< string > &mapping)
Copy lines while expanding variables.
bool copyOrCreateFiles(const bool verbose=false) const
Copy/create files prior to compilation.
#define forAll(list, i)
Loop across all elements in list.
bool wmakeLibso() const
Compile a libso.
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Encapsulation of dynamic code dictionaries.
int infoSwitch(const char *name, const int defaultValue=0)
Lookup info switch or add default value.
bool createMakeOptions() const
Copy/create Make/options prior to compilation.
bool writeDigest(const SHA1Digest &) const
Write digest to Make/SHA1Digest.
fileName path() const
Return directory path name (part before last /)
Ostream & endl(Ostream &os)
Add newline and flush stream.
const string & include() const
Return the code-includes.
A class for handling character strings derived from std::string.
void setFilterContext(const dynamicCodeContext &)
Define filter variables for code, codeInclude, SHA1sum.
static bool resolveTemplates(const UList< fileName > &templateNames, DynamicList< fileName > &resolvedFiles, DynamicList< fileName > &badFiles)
Resolve code-templates via the codeTemplateEnvName.
void addCopyFile(const fileName &name)
Add a file template name, which will be found and filtered.
word name() const
Return file name (part beyond last /)
static int allowSystemOperations
Flag if system operations are allowed.
bool isAdministrator()
Is user administrator.
static const word codeTemplateEnvName
Name of the code template environment variable.
dynamicCode(const dynamicCode &)
Disallow default bitwise copy construct.
void addCompileFile(const fileName &name)
Add a file template name, which will be found and filtered.
string getEnv(const word &)
Return environment variable of given name.
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
ISstream & getLine(string &)
Raw, low-level getline into a string function.
virtual const fileName & name() const
Return the name of the stream.
const string & code() const
Return the code.
static void checkSecurity(const char *title, const dictionary &)
Check security for creating dynamic code.
fileName codeRelPath() const
Path for specified code name relative to $FOAM_CASE.
fileName libRelPath() const
Library path for specified code name relative to $FOAM_CASE.
virtual const fileName & name() const
Return the name of the stream.
const SHA1Digest & sha1() const
Return SHA1 digest calculated from include, options, code.
A list of keyword definitions, which are a keyword followed by any number of values (e....
bool isFile(const fileName &, const bool checkGzip=true)
Does the name exist as a FILE in the file system?
bool isDir(const fileName &)
Does the name exist as a DIRECTORY in the file system?
void setFilterVariable(const word &key, const std::string &value)
Define a filter variable.
DynamicList< T, SizeInc, SizeMult, SizeDiv > & append(const T &)
Append an element at the end of the list.
errorManipArg< error, int > exit(error &err, const int errNo=1)
void reset(const dynamicCodeContext &)
Clear files and reset variables to specified context.
An STL-conforming hash table.
static word libraryBaseName(const fileName &libPath)
Return the library basename without leading 'lib' or trailing '.so'.
const string & localCode() const
Return the local (file-scope) code.
bool upToDate(const dynamicCodeContext &context) const
Verify if the copied code is up-to-date, based on Make/SHA1Digest.
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
void setMakeOptions(const std::string &content)
Define contents for Make/options.
void clear()
Clear files and variables.
virtual Ostream & writeQuoted(const std::string &, const bool quoted=true)
Write std::string surrounded by quotes.
static const fileName codeTemplateDirName
Name of the code template sub-directory.
virtual Ostream & writeQuoted(const std::string &, const bool quoted=true)=0
Write std::string surrounded by quotes.
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
word codeName_
Name for code.
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
A 2-tuple for storing two objects of different types.
bool good() const
Return true if next operation might succeed.
bool mkDir(const fileName &, mode_t=0777)
Make a directory and return an error if it could not be created.
string & inplaceExpand(string &, const HashTable< string, word, string::hash > &mapping, const char sigil='$')
Inplace expand occurences of variables according to the mapping.
int system(const std::string &command)
Execute the specified command.
word name(const complex &)
Return a string representation of a complex.
void addCreateFile(const fileName &name, const string &contents)
Add a file to create with its contents. Will not be filtered.
std::string str(const bool prefixed=false) const
Return (40-byte) text representation, optionally with '_' prefix.
string expand(const string &, const HashTable< string, word, string::hash > &mapping, const char sigil='$')
Expand occurences of variables according to the mapping.
word codeDirName_
Name for code subdirectory.
static const char *const libTargetRoot
Root of the LIB target for Make/files.