Go to the documentation of this file.
41 #define EXT_SO ".dylib"
58 =
"FOAM_CODE_TEMPLATES";
61 =
"codeTemplates/dynamicCode";
64 "LIB = $(PWD)/../platforms/$(WM_OPTIONS)/lib/lib";
80 <<
"This code should not be executed by someone"
81 <<
" with administrator rights for security reasons." <<
nl
82 <<
"It generates a shared library which is loaded using dlopen"
87 if (!allowSystemOperations)
90 <<
"Loading shared libraries using case-supplied code may have"
91 <<
" been disabled" <<
nl
92 <<
"by default for security reasons." <<
nl
93 <<
"If you trust the code, you may enable this by adding"
95 <<
" allowSystemOperations 1" <<
nl <<
nl
96 <<
"to the InfoSwitches setting in the system controlDict." <<
nl
97 <<
"The system controlDict is any of" <<
nl <<
nl
99 <<
" ~/.OpenFOAM/controlDict" <<
nl
100 <<
" $WM_PROJECT_DIR/etc/controlDict" <<
nl <<
endl
126 <<
"Failed opening for reading " << is.
name()
133 <<
"Failed writing " << os.
name()
164 for (
const fileName& templateName : templateNames)
167 if (!templateDir.empty() &&
isDir(templateDir))
169 file = templateDir/templateName;
179 file =
findEtcFile(codeTemplateDirName/templateName);
184 badFiles.
append(templateName);
189 resolvedFiles.
append(file);
199 const auto fnd = filterVars_.cfind(
"SHA1sum");
206 os <<
"/* dynamicCode:\n * SHA1 = ";
215 if (compileFiles_.empty())
220 const fileName dstFile(this->codePath()/
"Make/files");
230 <<
"Failed writing " << dstFile
234 writeCommentSHA1(os);
237 for (
const fileName& file : compileFiles_)
239 os.writeQuoted(file,
false) <<
nl;
243 << libTargetRoot << codeName_.c_str() <<
nl;
252 if (compileFiles_.empty() || makeOptions_.empty())
257 const fileName dstFile(this->codePath()/
"Make/options");
267 <<
"Failed writing " << dstFile
271 writeCommentSHA1(os);
272 os.writeQuoted(makeOptions_,
false) <<
nl;
297 os.writeQuoted(sha1,
false) <<
nl;
305 Foam::dynamicCode::dynamicCode(
const word& codeName,
const word& codeDirName)
307 codeRoot_(
argList::envGlobalPath()/topDirName),
308 libSubDir_(stringOps::
expand(
"platforms/${WM_OPTIONS}/lib")),
310 codeDirName_(codeDirName)
312 if (codeDirName_.empty())
314 codeDirName_ = codeName_;
325 return topDirName/codeDirName_;
331 return codeRoot_/libSubDir_/
"lib" + codeName_ +
EXT_SO;
337 return codeRelPath()/libSubDir_/
"lib" + codeName_ +
EXT_SO;
343 compileFiles_.clear();
345 createFiles_.clear();
347 filterVars_.set(
"typeName", codeName_);
348 filterVars_.set(
"SHA1sum",
SHA1Digest().str());
363 setFilterContext(context);
369 compileFiles_.append(
name);
375 copyFiles_.append(
name);
382 const string& contents
394 filterVars_.set(
"localCode", context.
localCode());
395 filterVars_.set(
"code", context.
code());
396 filterVars_.set(
"codeInclude", context.
include());
397 filterVars_.set(
"SHA1sum", context.
sha1().
str());
404 const std::string& value
407 filterVars_.set(key, value);
413 makeOptions_ = content;
422 <<
"Creating new library in " << this->libRelPath() <<
endl;
425 const label nFiles = compileFiles_.size() + copyFiles_.size();
431 resolveTemplates(compileFiles_, resolvedFiles, badFiles);
432 resolveTemplates(copyFiles_, resolvedFiles, badFiles);
434 if (!badFiles.empty())
437 <<
"Could not find code template(s): "
439 <<
"Under the $" << codeTemplateEnvName
440 <<
" directory or via the <etc>/"
441 << codeTemplateDirName <<
" expansion"
448 const fileName outputDir = this->codePath();
454 for (
const fileName& srcFile : resolvedFiles)
463 <<
"Failed opening " << srcFile
472 <<
"Failed writing " << dstFile
477 copyAndFilter(is, os, filterVars_);
492 <<
"Failed writing " << dstFile
495 os.writeQuoted(content.second(),
false) <<
nl;
503 writeDigest(filterVars_[
"SHA1sum"]);
511 stringList cmd({
"wmake",
"-s",
"libso", this->codePath()});
520 Info<<
"Invoking wmake libso " << this->codePath().c_str() <<
endl;
525 Serr<<
"Invoking wmake libso " << this->codePath().c_str() <<
endl;
552 return upToDate(context.
sha1());
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
bool createMakeFiles() const
Copy/create Make/files prior to compilation.
bool exists(const fileName &name, const bool checkGzip=true, const bool followLink=true)
Does the name exist (as DIRECTORY or FILE) in the file system?
static const char *const topDirName
Top-level directory name for copy/compiling.
bool writeCommentSHA1(Ostream &) const
Write SHA1 value as C-comment.
static std::string nameLessExt(const std::string &str)
Return basename, without extension.
A class for handling words, derived from Foam::string.
ISstream & getLine(std::string &str, char delim='\n')
Raw, low-level getline (until delimiter) into a string.
A class for handling file names.
static void copyAndFilter(ISstream &, OSstream &, const HashTable< string > &mapping)
Copy lines while expanding variables.
std::string str(const bool prefixed=false) const
The digest (40-byte) text representation, optionally with '_' prefix.
bool copyOrCreateFiles(const bool verbose=false) const
Copy/create files prior to compilation.
static std::string path(const std::string &str)
Return directory path name (part before last /)
bool wmakeLibso() const
Compile a libso.
Input from file stream, using an ISstream.
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Encapsulation of dynamic code dictionaries.
static std::string name(const std::string &str)
Return basename (part beyond last /), including its extension.
int system(const std::string &command, const bool bg=false)
Execute the specified command via the shell.
virtual Ostream & writeQuoted(const std::string &str, const bool quoted=true)=0
Write std::string surrounded by quotes.
bool createMakeOptions() const
Copy/create Make/options prior to compilation.
virtual Ostream & writeQuoted(const std::string &str, const bool quoted=true)
Write std::string surrounded by quotes.
bool writeDigest(const SHA1Digest &) const
Write digest to Make/SHA1Digest.
Generic input stream using a standard (STL) stream.
Extract command arguments and options from the supplied argc and argv parameters.
bool isFile(const fileName &name, const bool checkGzip=true, const bool followLink=true)
Does the name exist as a FILE in the file system?
Ostream & endl(Ostream &os)
Add newline and flush stream.
const string & include() const
Return the code-includes.
OSstream Serr
An Ostream wrapper for std::cerr.
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.
string getEnv(const std::string &envName)
Get environment value for given envName.
static int allowSystemOperations
Flag if system operations are allowed.
bool isAdministrator()
Is the current user the administrator (root)
string expand(const std::string &s, const HashTable< string, word, string::hash > &mapping, const char sigil='$')
int infoSwitch(const char *name, const int deflt=0)
Lookup info switch or add default value.
fileName libPath() const
Library path for specified code name.
static const word codeTemplateEnvName
Name of the code template environment variable.
void addCompileFile(const fileName &name)
Add a file template name, which will be found and filtered.
Ostream & write(Ostream &os, const bool prefixed=false) const
Write (40-byte) text representation, optionally with '_' prefix.
fileName findEtcFile(const fileName &name, const bool mandatory=false, unsigned short location=0777)
Search for a single FILE within the etc directories.
messageStream Info
Information stream (uses stdout - output is on the master only)
word name(const complex &c)
Return string representation of complex.
virtual const fileName & name() const
Return the name of the stream.
DynamicList< T, SizeMin > & append(const T &val)
Append an element to the end of this list.
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 <case>
void inplaceExpand(std::string &s, const HashTable< string, word, string::hash > &mapping, const char sigil='$')
Generic output stream using a standard (STL) stream.
fileName libRelPath() const
Library path for specified code name relative to <case>
virtual const fileName & name() const
Return the name of the stream.
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
void setFilterVariable(const word &key, const std::string &value)
Define a filter variable.
int infoDetailLevel
Global for selective suppression of Info output.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Output to file stream, using an OSstream.
void reset(const dynamicCodeContext &)
Clear files and reset variables to specified context.
A HashTable similar to std::unordered_map.
static word libraryBaseName(const fileName &libPath)
Return the library basename without leading 'lib' or trailing '.so'.
Functions to search 'etc' directories for configuration files etc.
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.
Input/output from file streams.
void clear()
Clear files and variables.
static const fileName codeTemplateDirName
Name of the code template sub-directory.
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
bool removeStart(const std::string &text)
Remove the given text from the start of the string.
#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 dissimilar types. The container is similar in purpose to std::pa...
bool good() const
Return true if next operation might succeed.
const SHA1 & sha1() const
Return SHA1 calculated from options, libs, include, code.
bool mkDir(const fileName &pathName, mode_t mode=0777)
Make a directory and return an error if it could not be created.
void addCreateFile(const fileName &name, const string &contents)
Add a file to create with its contents. Will not be filtered.
bool isDir(const fileName &name, const bool followLink=true)
Does the name exist as a DIRECTORY in the file system?
static const char *const libTargetRoot
Root of the LIB target for Make/files.