ENH: use HashTable for mapping codeStream entries

- write intermediate code to _SHA1 directories
- add Test-codeStream
This commit is contained in:
Mark Olesen 2011-02-22 12:34:20 +01:00
parent ba873744f3
commit 4aa279d7bc
9 changed files with 336 additions and 118 deletions

View File

@ -0,0 +1,3 @@
Test-codeStream.C
EXE = $(FOAM_USER_APPBIN)/Test-codeStream

View File

@ -0,0 +1 @@
EXE_INC =

View File

@ -0,0 +1,75 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2011 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Application
Test-codeStream
Description
\*---------------------------------------------------------------------------*/
#include "argList.H"
#include "IOstreams.H"
#include "IOobject.H"
#include "IFstream.H"
#include "dictionary.H"
using namespace Foam;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
int main(int argc, char *argv[])
{
argList::noParallel();
argList::validArgs.insert("dict .. dictN");
argList args(argc, argv, false, true);
Info<< nl
<< "FOAM_CASE=" << getEnv("FOAM_CASE") << nl
<< "FOAM_CASENAME=" << getEnv("FOAM_CASENAME") << nl
<< endl;
if (args.size() <= 1)
{
Info<<"specify dictionaries to test\n";
}
else
{
IOobject::writeDivider(Info);
for (label argI=1; argI < args.size(); ++argI)
{
const string& dictFile = args[argI];
IFstream is(dictFile);
dictionary dict(is);
Info<< dict << endl;
}
}
return 0;
}
// ************************************************************************* //

View File

@ -0,0 +1,43 @@
/*--------------------------------*- C++ -*----------------------------------*\
| ========= | |
| \\ / F ield | OpenFOAM: The Open Source CFD Toolbox |
| \\ / O peration | Version: Any |
| \\ / A nd | Web: www.OpenFOAM.com |
| \\/ M anipulation | |
\*---------------------------------------------------------------------------*/
FoamFile
{
version 2.0;
format ascii;
class dictionary;
object testDict;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// #include "codeStreamDefaults"
writeInterval #codeStream
{
codeInclude
#{
#include "fvCFD.H"
#};
codeOptions
#{
-I$(LIB_SRC)/finiteVolume/lnInclude
#};
code
#{
scalar start = 0;
scalar end = 100;
label nDumps = 5;
label interval = end - start;
Info<<"on-the-fly: " << ((interval)/nDumps) << endl;
os << ((interval)/nDumps);
#};
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -21,9 +21,14 @@ License
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>. along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Description
Template for use with codeStream.
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
//{{{ begin codeInclude
${codeInclude} ${codeInclude}
//}}} end codeInclude
using namespace Foam; using namespace Foam;
@ -39,7 +44,9 @@ void ${typeName}
Ostream& os Ostream& os
) )
{ {
//{{{ begin code
${code}; ${code};
//}}} end code
} }
} }

View File

@ -57,6 +57,16 @@ namespace functionEntries
} }
const Foam::word Foam::functionEntries::codeStream::codeTemplateName
= "codeStreamTemplate.C";
const Foam::word Foam::functionEntries::codeStream::codeTemplateEnvName
= "FOAM_CODESTREAM_TEMPLATES";
const Foam::fileName Foam::functionEntries::codeStream::codeTemplateDirName
= "codeTemplates/codeStream";
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
bool Foam::functionEntries::codeStream::execute bool Foam::functionEntries::codeStream::execute
@ -80,57 +90,52 @@ bool Foam::functionEntries::codeStream::execute
} }
// Read three sections of code. Remove any leading empty lines // Read three sections of code.
// (necessary for compilation options, just visually pleasing for includes // Remove any leading whitespace - necessary for compilation options,
// and body). // convenience for includes and body.
dictionary codeDict(is); dictionary codeDict(is);
string codeInclude = ""; // "codeInclude" is optional
string codeInclude;
if (codeDict.found("codeInclude")) if (codeDict.found("codeInclude"))
{ {
codeInclude = stringOps::trimLeft(codeDict["codeInclude"]); codeInclude = stringOps::trimLeft(codeDict["codeInclude"]);
} }
string code = stringOps::trimLeft(codeDict["code"]);
string codeOptions = ""; // "codeOptions" is optional
string codeOptions;
if (codeDict.found("codeOptions")) if (codeDict.found("codeOptions"))
{ {
codeOptions = stringOps::trimLeft(codeDict["codeOptions"]); codeOptions = stringOps::trimLeft(codeDict["codeOptions"]);
} }
// "code" is mandatory
string code = stringOps::trimLeft(codeDict["code"]);
// Create name from the contents // Create name from the contents
SHA1Digest sha; SHA1Digest sha;
{ {
OSHA1stream os; OSHA1stream os;
os << codeInclude << code << codeOptions; os << codeInclude << codeOptions << code;
sha = os.digest(); sha = os.digest();
} }
fileName name;
{
OStringStream str;
str << sha;
name = "codeStream" + str.str();
}
// write code into _SHA1 subdirectory
fileName dir; fileName dir;
if (isA<IOdictionary>(parentDict)) if (isA<IOdictionary>(parentDict))
{ {
const IOdictionary& d = static_cast<const IOdictionary&>(parentDict); const IOdictionary& d = static_cast<const IOdictionary&>(parentDict);
dir = d.db().time().constantPath()/"codeStream"/name; dir = d.db().time().constantPath()/"codeStream"/"_" + sha.str();
} }
else else
{ {
dir = "codeStream"/name; dir = fileName("codeStream")/"_" + sha.str();
} }
fileName name = "codeStream_" + sha.str();
fileName libPath fileName libPath
( (
Foam::getEnv("FOAM_USER_LIBBIN") Foam::getEnv("FOAM_USER_LIBBIN")/"lib" + name + ".so"
/ "lib"
+ name
+ ".so"
); );
void* lib = dlLibraryTable::findLibrary(libPath); void* lib = dlLibraryTable::findLibrary(libPath);
@ -143,39 +148,64 @@ bool Foam::functionEntries::codeStream::execute
{ {
Info<< "Creating new library in " << libPath << endl; Info<< "Creating new library in " << libPath << endl;
fileName templates fileName srcFile;
// try to get template from FOAM_CODESTREAM_TEMPLATES
fileName templateDir
( (
Foam::getEnv("FOAM_CODESTREAM_TEMPLATE_DIR") Foam::getEnv(codeTemplateEnvName)
); );
if (!templates.size())
if (!templateDir.empty())
{
srcFile = templateDir/codeTemplateName;
if (!isFile(srcFile, false))
{
srcFile.clear();
}
}
// not found - fallback to ~OpenFOAM expansion
if (srcFile.empty())
{
srcFile = findEtcFile
(
codeTemplateDirName/codeTemplateName
);
}
if (srcFile.empty())
{ {
FatalIOErrorIn FatalIOErrorIn
( (
"functionEntries::codeStream::execute(..)", "functionEntries::codeStream::execute(..)",
parentDict parentDict
) << "Please set environment variable" ) << "Could not find the code template: "
" FOAM_CODESTREAM_TEMPLATE_DIR" << codeTemplateName << nl
<< " to point to the location of codeStreamTemplate.C" << "Under the $FOAM_CODESTREAM_TEMPLATES directory"
<< " via via the ~OpenFOAM/" / codeTemplateDirName
<< " expansion"
<< exit(FatalIOError); << exit(FatalIOError);
} }
List<fileAndVars> copyFiles(1);
copyFiles[0].first() = templates/"codeStreamTemplate.C";
stringPairList bodyVars(2);
bodyVars[0] = Pair<string>("codeInclude", codeInclude);
bodyVars[1] = Pair<string>("code", code);
copyFiles[0].second() = bodyVars;
List<fileAndContent> filesContents(2); List<codeStreamTools::fileAndVars> copyFiles(1);
copyFiles[0].file() = srcFile;
copyFiles[0].set("codeInclude", codeInclude);
copyFiles[0].set("code", code);
List<codeStreamTools::fileAndContent> filesContents(2);
// Write Make/files // Write Make/files
filesContents[0].first() = "Make/files"; filesContents[0].first() = "Make/files";
filesContents[0].second() = filesContents[0].second() =
"codeStreamTemplate.C \n\ codeTemplateName + "\n"
LIB = $(FOAM_USER_LIBBIN)/lib" + name; "LIB = $(FOAM_USER_LIBBIN)/lib" + name;
// Write Make/options // Write Make/options
filesContents[1].first() = "Make/options"; filesContents[1].first() = "Make/options";
filesContents[1].second() = filesContents[1].second() =
"EXE_INC = -g\\\n" + codeOptions + "\n\nLIB_LIBS = "; "EXE_INC = -g \\\n" + codeOptions + "\n\nLIB_LIBS =";
codeStreamTools writer(name, copyFiles, filesContents); codeStreamTools writer(name, copyFiles, filesContents);
if (!writer.copyFilesContents(dir)) if (!writer.copyFilesContents(dir))
@ -199,7 +229,8 @@ bool Foam::functionEntries::codeStream::execute
( (
"functionEntries::codeStream::execute(..)", "functionEntries::codeStream::execute(..)",
parentDict parentDict
) << "Failed " << wmakeCmd << exit(FatalIOError); ) << "Failed " << wmakeCmd
<< exit(FatalIOError);
} }
} }
@ -209,7 +240,8 @@ bool Foam::functionEntries::codeStream::execute
( (
"functionEntries::codeStream::execute(..)", "functionEntries::codeStream::execute(..)",
parentDict parentDict
) << "Failed loading library " << libPath << exit(FatalIOError); ) << "Failed loading library " << libPath
<< exit(FatalIOError);
} }
lib = dlLibraryTable::findLibrary(libPath); lib = dlLibraryTable::findLibrary(libPath);

View File

@ -29,11 +29,12 @@ Description
generate the entry itself. So generate the entry itself. So
- codeStream reads three entries: 'code', 'codeInclude' (optional), - codeStream reads three entries: 'code', 'codeInclude' (optional),
'codeOptions' (optional) 'codeOptions' (optional)
and uses those to generate library sources inside constant/codeStream/ and uses those to generate library sources inside \f constant/codeStream/
- these get compiled using 'wmake libso' - these get compiled using 'wmake libso'
- the resulting library is loaded in executed with as arguments - the resulting library is loaded in executed with as arguments
const dictionary& dict, \code
Ostream& os (const dictionary& dict, Ostream& os)
\endcode
where the dictionary is the current dictionary. where the dictionary is the current dictionary.
- the code has to write into Ostream which is then used to construct - the code has to write into Ostream which is then used to construct
the actual dictionary entry. the actual dictionary entry.
@ -41,6 +42,7 @@ Description
E.g. to set the internal field of a field: E.g. to set the internal field of a field:
\verbatim
internalField #codeStream internalField #codeStream
{ {
code code
@ -56,19 +58,23 @@ Description
#{ #{
#include "fvCFD.H" #include "fvCFD.H"
#}; #};
//- Optional:
codeOptions codeOptions
#{ #{
-I$(LIB_SRC)/finiteVolume/lnInclude -I$(LIB_SRC)/finiteVolume/lnInclude
#}; #};
}; };
\endverbatim
Note the #{ #} syntax which is just a way of inputting strings with embedded Note the \c \#{ ... \c \#} syntax is a 'verbatim' input mode that allows
newlines. inputting strings with embedded newlines.
Limitations: Limitations:
- '~' symbol not allowed inside the code sections. - '~' symbol not allowed inside the code sections.
- probably some other limitations (uses string::expand which expands $, ~) - probably some other limitations (uses string::expand which expands
\c \$ and \c ~ sequences)
SourceFiles SourceFiles
codeStream.C codeStream.C
@ -106,6 +112,20 @@ class codeStream
public: public:
// Static data members
//- Name of the code template to be used
const static word codeTemplateName;
//- Name of the code template environment variable
// Used to located the codeTemplateName
const static word codeTemplateEnvName;
//- Name of the code template sub-directory
// Used when locating the codeTemplateName via Foam::findEtcFile
const static fileName codeTemplateDirName;
//- Runtime type information //- Runtime type information
ClassName("codeStream"); ClassName("codeStream");

View File

@ -25,6 +25,7 @@ License
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
#include "codeStreamTools.H" #include "codeStreamTools.H"
#include "stringOps.H"
#include "IFstream.H" #include "IFstream.H"
#include "OFstream.H" #include "OFstream.H"
#include "OSspecific.H" #include "OSspecific.H"
@ -43,38 +44,46 @@ int Foam::codeStreamTools::allowSystemOperations
void Foam::codeStreamTools::copyAndExpand void Foam::codeStreamTools::copyAndExpand
( (
ISstream& sourceStr, ISstream& is,
OSstream& destStr OSstream& os,
const HashTable<string>& mapping
) const ) const
{ {
if (!sourceStr.good()) if (!is.good())
{ {
FatalErrorIn FatalErrorIn
( (
"codeStreamTools::copyAndExpand()" "codeStreamTools::copyAndExpand()"
" const" " const"
) << "Failed opening for reading " << sourceStr.name() ) << "Failed opening for reading " << is.name()
<< exit(FatalError); << exit(FatalError);
} }
if (!destStr.good()) if (!os.good())
{ {
FatalErrorIn FatalErrorIn
( (
"codeStreamTools::copyAndExpand()" "codeStreamTools::copyAndExpand()"
" const" " const"
) << "Failed writing " << destStr.name() << exit(FatalError); ) << "Failed writing " << os.name()
<< exit(FatalError);
} }
// Copy file whilst rewriting environment vars // Copy file while rewriting $VARS and ${VARS}
string line; string line;
do do
{ {
sourceStr.getLine(line); is.getLine(line);
line.expand(true, true); // replace any envvars inside substitutions
destStr<< line.c_str() << nl; // normal expansion according to mapping
stringOps::inplaceExpand(line, mapping);
// expand according to env variables
stringOps::inplaceExpandEnv(line, true, true);
os << line.c_str() << nl;
} }
while (sourceStr.good()); while (is.good());
} }
@ -109,11 +118,11 @@ Foam::codeStreamTools::codeStreamTools
{} {}
Foam::codeStreamTools::codeStreamTools(const codeStreamTools& otf) Foam::codeStreamTools::codeStreamTools(const codeStreamTools& tools)
: :
name_(otf.name_), name_(tools.name_),
copyFiles_(otf.copyFiles_), copyFiles_(tools.copyFiles_),
filesContents_(otf.filesContents_) filesContents_(tools.filesContents_)
{} {}
@ -127,21 +136,17 @@ bool Foam::codeStreamTools::copyFilesContents(const fileName& dir) const
( (
"codeStreamTools::copyFilesContents(const fileName&) const" "codeStreamTools::copyFilesContents(const fileName&) const"
) << "Loading a shared library using case-supplied code is not" ) << "Loading a shared library using case-supplied code is not"
<< " enabled by default" << endl << " enabled by default" << nl
<< "because of security issues. If you trust the code you can" << "because of security issues. If you trust the code you can"
<< " enable this" << endl << " enable this" << nl
<< "facility be adding to the InfoSwitches setting in the system" << "facility be adding to the InfoSwitches setting in the system"
<< " controlDict" << endl << " controlDict" << nl
<< endl << nl
<< " allowSystemOperations 1" << endl << " allowSystemOperations 1" << nl << nl
<< endl << "The system controlDict is either" << nl << nl
<< "The system controlDict is either" << endl << " ~/.OpenFOAM/$WM_PROJECT_VERSION/controlDict" << nl << nl
<< endl << "or" << nl << nl
<< " ~/.OpenFOAM/$WM_PROJECT_VERSION/controlDict" << endl << " $WM_PROJECT_DIR/etc/controlDict" << nl
<< endl
<< "or" << endl
<< endl
<< " $WM_PROJECT_DIR/etc/controlDict" << endl
<< endl << endl
<< exit(FatalError); << exit(FatalError);
} }
@ -149,64 +154,64 @@ bool Foam::codeStreamTools::copyFilesContents(const fileName& dir) const
// Create dir // Create dir
mkDir(dir); mkDir(dir);
//Info<< "Setting envvar typeName=" << name_ << endl; // Info<< "set mapping typeName=" << name_ << endl;
setEnv("typeName", name_, true);
// Copy any template files // Copy any template files
forAll(copyFiles_, i) forAll(copyFiles_, i)
{ {
const List<Pair<string> >& rules = copyFiles_[i].second(); const fileName sourceFile(fileName(copyFiles_[i].file()).expand());
forAll(rules, j) const fileName destFile(dir/sourceFile.name());
{
//Info<< "Setting envvar " << rules[j].first() << endl;
setEnv(rules[j].first(), rules[j].second(), true);
}
const fileName sourceFile = fileName(copyFiles_[i].first()).expand(); IFstream is(sourceFile);
const fileName destFile = dir/sourceFile.name(); //Info<< "Reading from " << is.name() << endl;
if (!is.good())
IFstream sourceStr(sourceFile);
//Info<< "Reading from " << sourceStr.name() << endl;
if (!sourceStr.good())
{ {
FatalErrorIn FatalErrorIn
( (
"codeStreamTools::copyFilesContents()" "codeStreamTools::copyFilesContents(const fileName&)"
" const" " const"
) << "Failed opening " << sourceFile << exit(FatalError); ) << "Failed opening " << sourceFile << exit(FatalError);
} }
OFstream destStr(destFile); OFstream os(destFile);
//Info<< "Writing to " << destFile.name() << endl; //Info<< "Writing to " << destFile.name() << endl;
if (!destStr.good()) if (!os.good())
{ {
FatalErrorIn FatalErrorIn
( (
"codeStreamTools::copyFilesContents()" "codeStreamTools::copyFilesContents(const fileName&)"
" const" " const"
) << "Failed writing " << destFile << exit(FatalError); ) << "Failed writing " << destFile << exit(FatalError);
} }
copyAndExpand(sourceStr, destStr); // variables mapping
HashTable<string> mapping(copyFiles_[i]);
mapping.set("typeName", name_);
copyAndExpand(is, os, mapping);
} }
// Files that are always written: // Files that are always written:
forAll(filesContents_, i) forAll(filesContents_, i)
{ {
fileName f = fileName(dir/filesContents_[i].first()).expand(); const fileName dstFile
(
fileName(dir/filesContents_[i].first()).expand()
);
mkDir(f.path()); mkDir(dstFile.path());
OFstream str(f); OFstream os(dstFile);
//Info<< "Writing to " << filesContents_[i].first() << endl; //Info<< "Writing to " << filesContents_[i].first() << endl;
if (!str.good()) if (!os.good())
{ {
FatalErrorIn FatalErrorIn
( (
"codeStreamTools::copyFilesContents()" "codeStreamTools::copyFilesContents()"
" const" " const"
) << "Failed writing " << f << exit(FatalError); ) << "Failed writing " << dstFile << exit(FatalError);
} }
str << filesContents_[i].second().c_str() << endl; os << filesContents_[i].second().c_str() << endl;
} }
return true; return true;
} }
@ -217,16 +222,16 @@ bool Foam::codeStreamTools::writeDigest
const SHA1Digest& sha1 const SHA1Digest& sha1
) )
{ {
OFstream str(dir/"SHA1Digest"); OFstream os(dir/"SHA1Digest");
str << sha1; os << sha1;
return str.good(); return os.good();
} }
Foam::SHA1Digest Foam::codeStreamTools::readDigest(const fileName& dir) Foam::SHA1Digest Foam::codeStreamTools::readDigest(const fileName& dir)
{ {
IFstream str(dir/"SHA1Digest"); IFstream is(dir/"SHA1Digest");
return SHA1Digest(str); return SHA1Digest(is);
} }

View File

@ -39,20 +39,16 @@ SourceFiles
#include "Tuple2.H" #include "Tuple2.H"
#include "Pair.H" #include "Pair.H"
#include "SHA1Digest.H" #include "SHA1Digest.H"
#include "HashTable.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam namespace Foam
{ {
// Forward declaration of classes
typedef List<Pair<string> > stringPairList;
typedef Tuple2<fileName, List<Pair<string> > > fileAndVars;
typedef Tuple2<fileName, string> fileAndContent;
class OSstream;
class ISstream; class ISstream;
class OSstream;
/*---------------------------------------------------------------------------*\ /*---------------------------------------------------------------------------*\
Class codeStreamTools Declaration Class codeStreamTools Declaration
@ -60,20 +56,56 @@ class ISstream;
class codeStreamTools class codeStreamTools
{ {
public:
typedef Tuple2<fileName, string> fileAndContent;
//- Helper class for managing file and variables
class fileAndVars
:
public HashTable<string>
{
// Private data
fileName file_;
public:
//- Construct null
fileAndVars()
{}
//- Return the file name
const fileName& file() const
{
return file_;
}
//- Return the file name
fileName& file()
{
return file_;
}
};
private:
// Private data // Private data
//- Name for underlying set //- Name for underlying set
word name_; word name_;
//- Files to copy //- Files to copy
List<fileAndVars> copyFiles_; List<codeStreamTools::fileAndVars> copyFiles_;
//- Direct contents for files //- Direct contents for files
List<fileAndContent> filesContents_; List<fileAndContent> filesContents_;
protected: protected:
void copyAndExpand(ISstream&, OSstream&) const; void copyAndExpand
(
ISstream&,
OSstream&,
const HashTable<string>& mapping
) const;
public: public:
@ -96,12 +128,17 @@ public:
); );
//- Construct copy //- Construct copy
codeStreamTools(const codeStreamTools& otf); codeStreamTools(const codeStreamTools&);
// Member functions // Member functions
const List<Tuple2<fileName, List<Pair<string> > > >& copyFiles() const const word& name() const
{
return name_;
}
const List<fileAndVars>& copyFiles() const
{ {
return copyFiles_; return copyFiles_;
} }
@ -111,11 +148,6 @@ public:
return filesContents_; return filesContents_;
} }
const word& name() const
{
return name_;
}
bool copyFilesContents(const fileName& dir) const; bool copyFilesContents(const fileName& dir) const;
static void* findLibrary(const fileName& libPath); static void* findLibrary(const fileName& libPath);