44 =
"FOAM_CODE_TEMPLATES";
47 =
"codeTemplates/dynamicCode";
50 "LIB = $(PWD)/../platforms/$(WM_OPTIONS)/lib/lib";
66 <<
"This code should not be executed by someone with administrator"
67 <<
" rights due to security reasons." <<
nl
68 <<
"(it writes a shared library which then gets loaded "
76 <<
"Loading a shared library using case-supplied code is not"
77 <<
" enabled by default" <<
nl
78 <<
"because of security issues. If you trust the code you can"
79 <<
" enable this" <<
nl
80 <<
"facility be adding to the InfoSwitches setting in the system"
81 <<
" controlDict:" <<
nl <<
nl
82 <<
" allowSystemOperations 1" <<
nl <<
nl
83 <<
"The system controlDict is either" <<
nl <<
nl
84 <<
" ~/.OpenFOAM/$WM_PROJECT_VERSION/controlDict" <<
nl <<
nl
86 <<
" $WM_PROJECT_DIR/etc/controlDict" <<
nl
114 <<
"Failed opening for reading " << is.
name()
121 <<
"Failed writing " << os.
name()
151 if (!templateDir.empty() &&
isDir(templateDir))
153 file = templateDir/templateName;
181 forAll(templateNames, fileI)
183 const fileName& templateName = templateNames[fileI];
186 if (!templateDir.empty() &&
isDir(templateDir))
188 file = templateDir/templateName;
203 badFiles.
append(templateName);
218 const bool hasSHA1 = filterVars_.found(
"SHA1sum");
222 os <<
"# dynamicCode:\n# SHA1 = ";
223 os.
writeQuoted(filterVars_[
"SHA1sum"],
false) <<
"\n\n";
233 if (compileFiles_.empty())
238 const fileName dstFile(this->codePath()/
"Make/files");
248 <<
"Failed writing " << dstFile
252 writeCommentSHA1(os);
255 forAll(compileFiles_, fileI)
261 << libTargetRoot << codeName_.c_str() <<
nl;
270 if (compileFiles_.empty() || makeOptions_.empty())
275 const fileName dstFile(this->codePath()/
"Make/options");
285 <<
"Failed writing " << dstFile
289 writeCommentSHA1(os);
325 codeRoot_(stringOps::
expandEnvVar(
"$FOAM_CASE")/topDirName),
326 libSubDir_(stringOps::
expandEnvVar(
"platforms/$WM_OPTIONS/lib")),
328 codeDirName_(codeDirName)
330 if (codeDirName_.empty())
332 codeDirName_ = codeName_;
343 return topDirName/codeDirName_;
349 return codeRelPath()/libSubDir_/
"lib" + codeName_ +
".so";
355 compileFiles_.clear();
357 createFiles_.clear();
359 filterVars_.set(
"typeName", codeName_);
360 filterVars_.set(
"SHA1sum",
SHA1Digest().str());
378 setFilterVariable(iter.key(), iter());
381 setFilterVariable(
"SHA1sum", context.
sha1().
str());
387 compileFiles_.append(
name);
393 copyFiles_.append(
name);
400 const string& contents
410 const std::string& value
413 filterVars_.set(key, value);
419 makeOptions_ = content;
427 Info<<
"Creating new library in " << this->libRelPath() <<
endl;
430 const label nFiles = compileFiles_.size() + copyFiles_.size();
436 resolveTemplates(compileFiles_, resolvedFiles, badFiles);
437 resolveTemplates(copyFiles_, resolvedFiles, badFiles);
439 if (!badFiles.
empty())
442 <<
"Could not find the code template(s): "
444 <<
"Under the $" << codeTemplateEnvName
445 <<
" directory or via via the ~OpenFOAM/"
446 << codeTemplateDirName <<
" expansion"
453 const fileName outputDir = this->codePath();
459 forAll(resolvedFiles, fileI)
461 const fileName& srcFile = resolvedFiles[fileI];
464 Info << srcFile <<
" " << dstFile <<
endl;
471 <<
"Failed opening " << srcFile
480 <<
"Failed writing " << dstFile
485 copyAndFilter(is, os, filterVars_);
490 forAll(createFiles_, fileI)
503 <<
"Failed writing " << dstFile
514 writeDigest(filterVars_[
"SHA1sum"]);
522 const Foam::string wmakeCmd(
"wmake -s libso " + this->codePath());
523 Info<<
"Invoking " << wmakeCmd <<
endl;
551 return upToDate(context.
sha1());
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.
DynamicList< T, SizeInc, SizeMult, SizeDiv > & append(const T &)
Append an element at the end of the list.
An STL-conforming hash table.
bool good() const
Return true if next operation might succeed.
virtual const fileName & name() const
Return the name of the stream.
ISstream & getLine(string &, const bool continuation=true)
Read line into a string.
virtual Ostream & writeQuoted(const std::string &, const bool quoted=true)
Write std::string with optional double quotes.
virtual const fileName & name() const
Return the name of the stream.
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
virtual Ostream & writeQuoted(const std::string &, const bool quoted=true)=0
Write std::string surrounded by quotes.
std::string str(const bool prefixed=false) const
Return (40-byte) text representation, optionally with '_' prefix.
Ostream & write(Ostream &, const bool prefixed=false) const
Write (40-byte) text representation, optionally with '_' prefix.
A 2-tuple for storing two objects of different types.
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
bool empty() const
Return true if the UList is empty (ie, size() is zero)
A list of keyword definitions, which are a keyword followed by any number of values (e....
Encapsulation of dynamic code dictionaries.
const HashTable< string > & code() const
Return the code table.
const SHA1Digest & sha1() const
Return SHA1 digest calculated from include, options, code.
static int allowSystemOperations
Flag if system operations are allowed.
static void copyAndFilter(ISstream &, OSstream &, const HashTable< string > &mapping)
Copy lines while expanding variables.
dynamicCode(const word &codeName, const word &codeDirName="")
Construct for a specified code name and code directory name.
bool copyOrCreateFiles(const bool verbose=false) const
Copy/create files prior to compilation.
void addCopyFile(const fileName &name)
Add a file template name, which will be found and filtered.
void reset(const dynamicCodeContext &)
Clear files and reset variables to specified context.
fileName libRelPath() const
Library path for specified code name relative to $FOAM_CASE.
void addCreateFile(const fileName &name, const string &contents)
Add a file to create with its contents. Will not be filtered.
static bool resolveTemplates(const UList< fileName > &templateNames, DynamicList< fileName > &resolvedFiles, DynamicList< fileName > &badFiles)
Resolve code-templates via the codeTemplateEnvName.
static const word codeTemplateEnvName
Name of the code template environment variable.
static const char *const topDirName
Top-level directory name for copy/compiling.
void setFilterVariable(const word &key, const std::string &value)
Define a filter variable.
static const char *const libTargetRoot
Root of the LIB target for Make/files.
static word libraryBaseName(const fileName &libPath)
Return the library basename without leading 'lib' or trailing '.so'.
static void checkSecurity(const char *title, const dictionary &)
Check security for creating dynamic code.
bool createMakeFiles() const
Copy/create Make/files prior to compilation.
bool writeDigest(const SHA1Digest &) const
Write digest to Make/SHA1Digest.
fileName codeRelPath() const
Path for specified code name relative to $FOAM_CASE.
void addCompileFile(const fileName &name)
Add a file template name, which will be found and filtered.
void clear()
Clear files and variables.
static fileName resolveTemplate(const fileName &templateName)
Resolve code-template via the codeTemplateEnvName.
bool upToDate(const dynamicCodeContext &context) const
Verify if the copied code is up-to-date, based on Make/SHA1Digest.
static const fileName codeTemplateDirName
Name of the code template sub-directory.
bool createMakeOptions() const
Copy/create Make/options prior to compilation.
bool wmakeLibso() const
Compile a libso.
bool writeCommentSHA1(Ostream &) const
Write SHA1 value as C-comment.
void setMakeOptions(const std::string &content)
Define contents for Make/options.
A class for handling file names.
word name() const
Return file name (part beyond last /)
fileName path() const
Return directory path name (part before last /)
A class for handling character strings derived from std::string.
A class for handling words, derived from string.
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Functions to search 'etc' directories for configuration files etc.
int infoSwitch(const char *name, const int defaultValue=0)
Lookup info switch or add default value.
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.
bool isFile(const fileName &, const bool checkVariants=true, const bool followLink=true)
Does the name exist as a file in the file system?
errorManipArg< error, int > exit(error &err, const int errNo=1)
int system(const std::string &command)
Execute the specified command.
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.
word name(const bool)
Return a word representation of a bool.
bool exists(const fileName &, const bool checkVariants=true, const bool followLink=true)
Does the name exist (as directory or file) in the file system?
labelList second(const UList< labelPair > &p)
fileName findEtcFile(const fileName &, bool mandatory=false)
Search for a file using findEtcFiles.
labelList first(const UList< labelPair > &p)
bool isDir(const fileName &, const bool followLink=true)
Does the name exist as a directory in the file system?
string getEnv(const word &)
Return environment variable of given name.