43 const Foam::word Foam::dynamicCode::codeTemplateEnvName
44 =
"FOAM_CODE_TEMPLATES";
47 =
"codeTemplates/dynamicCode";
49 const char*
const Foam::dynamicCode::libTargetRoot =
50 "LIB = $(PWD)/../platforms/$(WM_OPTIONS)/lib/lib";
52 const char*
const Foam::dynamicCode::topDirName =
"dynamicCode";
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 " 73 if (!allowSystemOperations)
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;
154 if (!
isFile(file,
false,
false))
181 forAll(templateNames, fileI)
183 const fileName& templateName = templateNames[fileI];
186 if (!templateDir.empty() &&
isDir(templateDir))
188 file = templateDir/templateName;
189 if (!
isFile(file,
false,
false))
203 badFiles.
append(templateName);
208 resolvedFiles.
append(file);
218 const bool hasSHA1 = filterVars_.found(
"SHA1sum");
222 os <<
"# dynamicCode:\n# SHA1 = ";
223 os.
writeQuoted(filterVars_[
"SHA1sum"],
false) <<
"\n\n";
233 if (compileFiles_.empty())
248 <<
"Failed writing " << dstFile
255 forAll(compileFiles_, fileI)
257 os.writeQuoted(compileFiles_[fileI].
name(),
false) <<
nl;
270 if (compileFiles_.empty() || makeOptions_.empty())
285 <<
"Failed writing " << dstFile
290 os.writeQuoted(makeOptions_,
false) <<
nl;
315 os.writeQuoted(sha1,
false) <<
nl;
326 libSubDir_(stringOps::
expand(
"platforms/$WM_OPTIONS/lib")),
328 codeDirName_(codeDirName)
330 if (codeDirName_.empty())
332 codeDirName_ = codeName_;
349 return codeRelPath()/libSubDir_/
"lib" + codeName_ +
".so";
355 compileFiles_.clear();
357 createFiles_.clear();
359 filterVars_.set(
"typeName", codeName_);
360 filterVars_.set(
"SHA1sum",
SHA1Digest().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;
430 const label nFiles = compileFiles_.size() + copyFiles_.size();
439 if (!badFiles.
empty())
442 <<
"Could not find the code template(s): " 445 <<
" directory or via via the ~OpenFOAM/" 459 forAll(resolvedFiles, fileI)
461 const fileName& srcFile = resolvedFiles[fileI];
464 Info << srcFile <<
" " << dstFile <<
endl;
471 <<
"Failed opening " << srcFile
480 <<
"Failed writing " << dstFile
490 forAll(createFiles_, fileI)
503 <<
"Failed writing " << dstFile
506 os.writeQuoted(createFiles_[fileI].
second(),
false) <<
nl;
523 Info<<
"Invoking " << wmakeCmd <<
endl;
string getEnv(const word &)
Return environment variable of given name.
virtual const fileName & name() const
Return the name of the stream.
labelList first(const UList< labelPair > &p)
bool exists(const fileName &, const bool checkVariants=true, const bool followLink=true)
Does the name exist (as directory or file) in the file system?
#define forAll(list, i)
Loop across all elements in list.
bool empty() const
Return true if the UList is empty (ie, size() is zero)
string expand(const string &, const HashTable< string, word, string::hash > &mapping, const char sigil='$')
Expand occurrences of variables according to the mapping.
bool writeDigest(const SHA1Digest &) const
Write digest to Make/SHA1Digest.
const word & codeDirName() const
Return the code-dirname.
A class for handling file names.
const SHA1Digest & sha1() const
Return SHA1 digest calculated from include, options, code.
void reset(const dynamicCodeContext &)
Clear files and reset variables to specified context.
virtual const fileName & name() const
Return the name of the stream.
errorManipArg< error, int > exit(error &err, const int errNo=1)
A list of keyword definitions, which are a keyword followed by any number of values (e...
void setMakeOptions(const std::string &content)
Define contents for Make/options.
const word & codeName() const
Return the code-name.
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
#define forAllConstIter(Container, container, iter)
Iterate across all elements in the container object of type.
bool isFile(const fileName &, const bool checkVariants=true, const bool followLink=true)
Does the name exist as a file in the file system?
static const char *const libTargetRoot
Root of the LIB target for Make/files.
static bool resolveTemplates(const UList< fileName > &templateNames, DynamicList< fileName > &resolvedFiles, DynamicList< fileName > &badFiles)
Resolve code-templates via the codeTemplateEnvName.
virtual Ostream & writeQuoted(const std::string &, const bool quoted=true)
Write std::string with optional double quotes.
Ostream & endl(Ostream &os)
Add newline and flush stream.
static const word codeTemplateEnvName
Name of the code template environment variable.
static const char *const topDirName
Top-level directory name for copy/compiling.
static fileName resolveTemplate(const fileName &templateName)
Resolve code-template via the codeTemplateEnvName.
void addCompileFile(const fileName &name)
Add a file template name, which will be found and filtered.
bool writeCommentSHA1(Ostream &) const
Write SHA1 value as C-comment.
bool good() const
Return true if next operation might succeed.
int infoSwitch(const char *name, const int defaultValue=0)
Lookup info switch or add default value.
virtual Ostream & writeQuoted(const std::string &, const bool quoted=true)=0
Write std::string surrounded by quotes.
bool createMakeOptions() const
Copy/create Make/options prior to compilation.
fileName codeRelPath() const
Path for specified code name relative to $FOAM_CASE.
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
Ostream & write(Ostream &, const bool prefixed=false) const
Write (40-byte) text representation, optionally with '_' prefix.
fileName digestFile() const
Path for SHA1Digest.
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects...
fileName libRelPath() const
Library path for specified code name relative to $FOAM_CASE.
bool isDir(const fileName &, const bool followLink=true)
Does the name exist as a directory in the file system?
A class for handling words, derived from string.
Functions to search 'etc' directories for configuration files etc.
static const fileName codeTemplateDirName
Name of the code template sub-directory.
static int allowSystemOperations
Flag if system operations are allowed.
word name() const
Return file name (part beyond last /)
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.
DynamicList< T, SizeInc, SizeMult, SizeDiv > & append(const T &)
Append an element at the end of the list.
void addCreateFile(const fileName &name, const string &contents)
Add a file to create with its contents. Will not be filtered.
bool wmakeLibso() const
Compile a libso.
An STL-conforming hash table.
fileName findEtcFile(const fileName &, bool mandatory=false)
Search for a file using findEtcFiles.
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
fileName codePath() const
Path for specified code name.
An Ostream is an abstract base class for all output systems (streams, files, token lists...
bool upToDate(const dynamicCodeContext &context) 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.
static word libraryBaseName(const fileName &libPath)
Return the library basename without leading 'lib' or trailing '.so'.
bool mkDir(const fileName &, mode_t=0777)
Make a directory and return an error if it could not be created.
word name(const complex &)
Return a string representation of a complex.
const HashTable< string > & code() const
Return the code table.
labelList second(const UList< labelPair > &p)
bool isAdministrator()
Is user administrator.
ISstream & getLine(string &, const bool continuation=true)
Read line into a string.
Encapsulation of dynamic code dictionaries.
void setFilterVariable(const word &key, const std::string &value)
Define a filter variable.
string & inplaceExpand(string &, const HashTable< string, word, string::hash > &mapping, const char sigil='$')
Inplace expand occurrences of variables according to the mapping.
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
void addCopyFile(const fileName &name)
Add a file template name, which will be found and filtered.
fileName path() const
Return directory path name (part before last /)
std::string str(const bool prefixed=false) const
Return (40-byte) text representation, optionally with '_' prefix.
void clear()
Clear files and variables.
static void checkSecurity(const char *title, const dictionary &)
Check security for creating dynamic code.
A class for handling character strings derived from std::string.
bool createMakeFiles() const
Copy/create Make/files prior to compilation.
int system(const std::string &command)
Execute the specified command.
Tuple2< fileName, string > fileAndContent