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()
152 forAll(templateNames, fileI)
154 const fileName& templateName = templateNames[fileI];
157 if (!templateDir.empty() &&
isDir(templateDir))
159 file = templateDir/templateName;
174 badFiles.
append(templateName);
179 resolvedFiles.
append(file);
189 const bool hasSHA1 = filterVars_.found(
"SHA1sum");
193 os <<
"/* dynamicCode:\n * SHA1 = ";
194 os.
writeQuoted(filterVars_[
"SHA1sum"],
false) <<
"\n */\n";
204 if (compileFiles_.empty())
219 <<
"Failed writing " << dstFile
226 forAll(compileFiles_, fileI)
228 os.writeQuoted(compileFiles_[fileI],
false) <<
nl;
241 if (compileFiles_.empty() || makeOptions_.empty())
256 <<
"Failed writing " << dstFile
261 os.writeQuoted(makeOptions_,
false) <<
nl;
286 os.writeQuoted(sha1,
false) <<
nl;
297 libSubDir_(stringOps::
expand(
"platforms/$WM_OPTIONS/lib")),
299 codeDirName_(codeDirName)
301 if (codeDirName_.empty())
303 codeDirName_ = codeName_;
320 return codeRelPath()/libSubDir_/
"lib" + codeName_ +
".so";
326 compileFiles_.clear();
328 createFiles_.clear();
330 filterVars_.set(
"typeName", codeName_);
331 filterVars_.set(
"SHA1sum",
SHA1Digest().str());
352 compileFiles_.append(name);
358 copyFiles_.append(name);
365 const string& contents
377 filterVars_.set(
"localCode", context.
localCode());
378 filterVars_.set(
"code", context.
code());
379 filterVars_.set(
"codeInclude", context.
include());
380 filterVars_.set(
"SHA1sum", context.
sha1().
str());
387 const std::string& value
390 filterVars_.set(key, value);
396 makeOptions_ = content;
407 const label nFiles = compileFiles_.size() + copyFiles_.size();
416 if (!badFiles.
empty())
419 <<
"Could not find the code template(s): " 422 <<
" directory or via via the ~OpenFOAM/" 436 forAll(resolvedFiles, fileI)
438 const fileName& srcFile = resolvedFiles[fileI];
446 <<
"Failed opening " << srcFile
455 <<
"Failed writing " << dstFile
465 forAll(createFiles_, fileI)
478 <<
"Failed writing " << dstFile
481 os.writeQuoted(createFiles_[fileI].second(),
false) <<
nl;
498 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.
const string & localCode() const
Return the local (file-scope) code.
const string & include() const
Return the code-includes.
#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 occurences of variables according to the mapping.
bool writeDigest(const SHA1Digest &) const
Write digest to Make/SHA1Digest.
const word & codeDirName() const
Return the code-dirname.
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
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.
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 surrounded by quotes.
Ostream & endl(Ostream &os)
Add newline and flush stream.
void setFilterContext(const dynamicCodeContext &)
Define filter variables for code, codeInclude, SHA1sum.
static const word codeTemplateEnvName
Name of the code template environment variable.
static const char *const topDirName
Top-level directory name for copy/compiling.
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.
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.
bool isFile(const fileName &, const bool checkGzip=true, const bool followLink=true)
Does the name exist as a FILE in the file system?
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.
bool isAdministrator()
Is user administrator.
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 occurences 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.
const string & code() const
Return the code.
bool exists(const fileName &, const bool checkGzip=true, const bool followLink=true)
Does the name exist (as DIRECTORY or FILE) in the file system?
int system(const std::string &command)
Execute the specified command.
ISstream & getLine(string &)
Raw, low-level getline into a string function.
Tuple2< fileName, string > fileAndContent