ENH: refactor codeStreamTools, use for codedFixedValueFvPatchScalarField

This commit is contained in:
Mark Olesen 2011-02-22 15:01:20 +01:00
parent 4aa279d7bc
commit f0f2bfa632
9 changed files with 210 additions and 133 deletions

View File

@ -49,7 +49,7 @@ Example: Look up dictionary entries and do some calculation
code, codeInclude, codeOptions sections (these are just strings) and
calculates the SHA1 checksum of the contents.
- it copies a template file
($FOAM_CODESTREAM_TEMPLATE_DIR/codeStreamTemplate.C), substituting all
($FOAM_CODESTREAM_TEMPLATES/codeStreamTemplate.C), substituting all
occurences of code, codeInclude, codeOptions.
- it writes library source files to constant/codeStream/<sha1> and compiles it
using 'wmake libso'.

View File

@ -102,7 +102,7 @@ Foam::string Foam::getEnv(const word& envName)
bool Foam::setEnv
(
const word& envName,
const string& value,
const std::string& value,
const bool overwrite
)
{
@ -1066,7 +1066,7 @@ bool Foam::ping(const word& hostname, const label timeOut)
}
int Foam::system(const string& command)
int Foam::system(const std::string& command)
{
return ::system(command.c_str());
}
@ -1084,14 +1084,14 @@ bool Foam::dlClose(void* handle)
}
void* Foam::dlSym(void* handle, const string& symbol)
void* Foam::dlSym(void* handle, const std::string& symbol)
{
void* fun = dlsym(handle, symbol.c_str());
char *error;
if ((error = dlerror()) != NULL)
{
WarningIn("dlSym(void*, const string&)")
WarningIn("dlSym(void*, const std::string&)")
<< "Cannot lookup symbol " << symbol << " : " << error
<< endl;
}

View File

@ -57,15 +57,9 @@ namespace functionEntries
}
const Foam::word Foam::functionEntries::codeStream::codeTemplateName
const Foam::word Foam::functionEntries::codeStream::codeTemplateC
= "codeStreamTemplate.C";
const Foam::word Foam::functionEntries::codeStream::codeTemplateEnvName
= "FOAM_CODESTREAM_TEMPLATES";
const Foam::fileName Foam::functionEntries::codeStream::codeTemplateDirName
= "codeTemplates/codeStream";
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
@ -148,49 +142,30 @@ bool Foam::functionEntries::codeStream::execute
{
Info<< "Creating new library in " << libPath << endl;
fileName srcFile;
// try to get template from FOAM_CODESTREAM_TEMPLATES
fileName templateDir
const fileName fileCsrc
(
Foam::getEnv(codeTemplateEnvName)
codeStreamTools::findTemplate
(
codeTemplateC
)
);
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())
// not found!
if (fileCsrc.empty())
{
FatalIOErrorIn
(
"functionEntries::codeStream::execute(..)",
parentDict
) << "Could not find the code template: "
<< codeTemplateName << nl
<< "Under the $FOAM_CODESTREAM_TEMPLATES directory"
<< " via via the ~OpenFOAM/" / codeTemplateDirName
<< " expansion"
<< codeTemplateC << nl
<< codeStreamTools::searchedLocations()
<< exit(FatalIOError);
}
List<codeStreamTools::fileAndVars> copyFiles(1);
copyFiles[0].file() = srcFile;
copyFiles[0].file() = fileCsrc;
copyFiles[0].set("codeInclude", codeInclude);
copyFiles[0].set("code", code);
@ -199,7 +174,7 @@ bool Foam::functionEntries::codeStream::execute
// Write Make/files
filesContents[0].first() = "Make/files";
filesContents[0].second() =
codeTemplateName + "\n"
codeTemplateC + "\n\n"
"LIB = $(FOAM_USER_LIBBIN)/lib" + name;
// Write Make/options
@ -214,7 +189,7 @@ bool Foam::functionEntries::codeStream::execute
(
"functionEntries::codeStream::execute(..)",
parentDict
) << "Failed writing " << endl
) << "Failed writing " <<nl
<< copyFiles << endl
<< filesContents
<< exit(FatalIOError);

View File

@ -114,16 +114,8 @@ 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;
//- Name of the C code template to be used
const static word codeTemplateC;
//- Runtime type information

View File

@ -40,6 +40,53 @@ int Foam::codeStreamTools::allowSystemOperations
);
const Foam::word Foam::codeStreamTools::codeTemplateEnvName
= "FOAM_CODESTREAM_TEMPLATES";
const Foam::fileName Foam::codeStreamTools::codeTemplateDirName
= "codeTemplates/codeStream";
// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
Foam::fileName Foam::codeStreamTools::findTemplate(const word& templateFile)
{
// try to get template from FOAM_CODESTREAM_TEMPLATES
fileName templateDir(Foam::getEnv(codeTemplateEnvName));
fileName file;
if (!templateDir.empty() && isDir(templateDir))
{
file = templateDir/templateFile;
if (!isFile(file, false))
{
file.clear();
}
}
// not found - fallback to ~OpenFOAM expansion
if (file.empty())
{
file = findEtcFile(codeTemplateDirName/templateFile);
}
return file;
}
Foam::string Foam::codeStreamTools::searchedLocations()
{
return
(
"Under the $"
+ codeTemplateDirName
+ " directory or via via the ~OpenFOAM/"
+ codeTemplateDirName
+ " expansion"
);
}
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
void Foam::codeStreamTools::copyAndExpand

View File

@ -109,7 +109,18 @@ protected:
public:
static int allowSystemOperations;
// Static data members
//- 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;
static int allowSystemOperations;
// Constructors
@ -150,6 +161,15 @@ public:
bool copyFilesContents(const fileName& dir) const;
//- Find a code-template via the codeTemplateEnvName
// alternatively in the codeTemplateDirName via Foam::findEtcFile
static fileName findTemplate(const word& templateName);
//- List searched locations in a format suitable for display an error
static string searchedLocations();
static void* findLibrary(const fileName& libPath);
static bool writeDigest(const fileName& dir, const SHA1Digest& sha1);

View File

@ -65,7 +65,7 @@ bool env(const word&);
string getEnv(const word&);
//- Set an environment variable
bool setEnv(const word& name, const string& value, const bool overwrite);
bool setEnv(const word& name, const std::string& value, const bool overwrite);
//- Return the system's host name, as per hostname(1)
// Optionally with the full name (as per the '-f' option)
@ -182,7 +182,7 @@ bool ping(const word&, const label port, const label timeOut);
bool ping(const word&, const label timeOut=10);
//- Execute the specified command
int system(const string& command);
int system(const std::string& command);
//- open a shared library. Return handle to library
void* dlOpen(const fileName& lib);
@ -191,7 +191,7 @@ void* dlOpen(const fileName& lib);
bool dlClose(void*);
//- Lookup a symbol in a dlopened library using handle
void* dlSym(void* handle, const string& symbol);
void* dlSym(void* handle, const std::string& symbol);
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -34,6 +34,16 @@ License
#include "OFstream.H"
#include "codeStreamTools.H"
#include "codeProperties.H"
#include "stringOps.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
const Foam::word Foam::codedFixedValueFvPatchScalarField::codeTemplateC
= "fixedValueFvPatchScalarFieldTemplate.C";
const Foam::word Foam::codedFixedValueFvPatchScalarField::codeTemplateH
= "fixedValueFvPatchScalarFieldTemplate.H";
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
@ -68,84 +78,101 @@ Foam::codedFixedValueFvPatchScalarField::dict() const
void Foam::codedFixedValueFvPatchScalarField::writeLibrary
(
const fileName dir,
const fileName libPath,
const fileName& dir,
const fileName& libPath,
const dictionary& dict
)
{
// Write files for new library
if (!Pstream::master())
{
return;
}
// "codeInclude" is optional
string codeInclude;
if (dict.found("codeInclude"))
{
codeInclude = stringOps::trimLeft(dict["codeInclude"]);
}
// "codeOptions" is optional
string codeOptions;
if (dict.found("codeOptions"))
{
codeOptions = stringOps::trimLeft(dict["codeOptions"]);
}
// "code" is mandatory
string code = stringOps::trimLeft(dict["code"]);
Info<< "Creating new library in " << libPath << endl;
// Write files for new library
if (Pstream::master())
const fileName fileCsrc
(
codeStreamTools::findTemplate
(
codeTemplateC
)
);
const fileName fileHsrc
(
codeStreamTools::findTemplate
(
codeTemplateH
)
);
if (fileCsrc.empty() || fileHsrc.empty())
{
fileName templates(Foam::getEnv("FOAM_CODESTREAM_TEMPLATE_DIR"));
if (!templates.size())
{
FatalIOErrorIn
(
"codedFixedValueFvPatchScalarField::writeLibrary(..)",
dict
) << "Please set environment variable"
<< " FOAM_CODESTREAM_TEMPLATE_DIR"
<< " to point to the location of "
<< "fixedValueFvPatchScalarFieldTemplate.C"
<< exit(FatalIOError);
}
// Extract sections of code
string codeInclude = "";
if (dict.found("codeInclude"))
{
codeInclude = codeStreamTools::stripLeading(dict["codeInclude"]);
}
string code = codeStreamTools::stripLeading(dict["code"]);
string codeOptions = "";
if (dict.found("codeOptions"))
{
codeOptions = codeStreamTools::stripLeading(dict["codeOptions"]);
}
List<fileAndVars> copyFiles(2);
copyFiles[0].first() =
templates/"fixedValueFvPatchScalarFieldTemplate.C";
copyFiles[0].second().setSize(2);
copyFiles[0].second()[0] = Pair<string>("codeInclude", codeInclude);
copyFiles[0].second()[1] = Pair<string>("code", code);
copyFiles[1].first() =
templates/"fixedValueFvPatchScalarFieldTemplate.H";
FatalIOErrorIn
(
"codedFixedValueFvPatchScalarField::writeLibrary(..)",
dict
) << "Could not find one or both code templates: "
<< codeTemplateC << ", " << codeTemplateH << nl
<< codeStreamTools::searchedLocations()
<< exit(FatalIOError);
}
List<fileAndContent> filesContents(2);
// Write Make/files
filesContents[0].first() = "Make/files";
filesContents[0].second() =
"fixedValueFvPatchScalarFieldTemplate.C \n\n"
"LIB = $(FOAM_USER_LIBBIN)/lib" + redirectType_;
// Write Make/options
filesContents[1].first() = "Make/options";
filesContents[1].second() =
"EXE_INC = -g\\\n -I$(LIB_SRC)/finiteVolume/lnInclude\\\n"
+ codeOptions
+ "\n\nLIB_LIBS = ";
List<codeStreamTools::fileAndVars> copyFiles(2);
copyFiles[0].file() = fileCsrc;
copyFiles[0].set("codeInclude", codeInclude);
copyFiles[0].set("code", code);
codeStreamTools writer(redirectType_, copyFiles, filesContents);
if (!writer.copyFilesContents(dir))
{
FatalIOErrorIn
(
"codedFixedValueFvPatchScalarField::writeLibrary(..)",
dict
) << "Failed writing " << endl
<< copyFiles << endl
<< filesContents
<< exit(FatalIOError);
}
copyFiles[1].file() = fileHsrc;
List<codeStreamTools::fileAndContent> filesContents(2);
// Write Make/files
filesContents[0].first() = "Make/files";
filesContents[0].second() =
codeTemplateC + "\n\n"
"LIB = $(FOAM_USER_LIBBIN)/lib" + redirectType_;
// Write Make/options
filesContents[1].first() = "Make/options";
filesContents[1].second() =
"EXE_INC = -g \\\n-I$(LIB_SRC)/finiteVolume/lnInclude\\\n"
+ codeOptions
+ "\n\nLIB_LIBS = ";
codeStreamTools writer(redirectType_, copyFiles, filesContents);
if (!writer.copyFilesContents(dir))
{
FatalIOErrorIn
(
"codedFixedValueFvPatchScalarField::writeLibrary(..)",
dict
) << "Failed writing " << nl
<< copyFiles << nl
<< filesContents
<< exit(FatalIOError);
}
}
@ -221,7 +248,7 @@ void Foam::codedFixedValueFvPatchScalarField::updateLibrary()
{
if (Pstream::master())
{
Foam::string wmakeCmd("wmake libso " + dir);
const Foam::string wmakeCmd("wmake libso " + dir);
Info<< "Invoking " << wmakeCmd << endl;
if (Foam::system(wmakeCmd))
{
@ -229,7 +256,8 @@ void Foam::codedFixedValueFvPatchScalarField::updateLibrary()
(
"codedFixedValueFvPatchScalarField::updateLibrary()",
dict_
) << "Failed " << wmakeCmd << exit(FatalIOError);
) << "Failed " << wmakeCmd
<< exit(FatalIOError);
}
}
@ -242,7 +270,8 @@ void Foam::codedFixedValueFvPatchScalarField::updateLibrary()
(
"codedFixedValueFvPatchScalarField::updateLibrary()",
dict_
) << "Failed loading library " << libPath << exit(FatalIOError);
) << "Failed loading library " << libPath
<< exit(FatalIOError);
}
}
}

View File

@ -32,7 +32,7 @@ Description
See also codeStream.
Example:
\verbatim
movingWall
{
type codedFixedValue<scalar>;
@ -53,13 +53,14 @@ Description
//#{
// -I$(LIB_SRC)/finiteVolume/lnInclude
//#};
}
\endverbatim
A special form is if the 'code' section is not supplied. In this case
the code gets read from a (runTimeModifiable!) dictionary system/codeDict
which would have an entry
\verbatim
rampedFixedValue
{
code
@ -67,6 +68,10 @@ Description
operator==(min(10, 0.1*this->db().time().value()));
#};
}
\endverbatim
SeeAlso
Foam::codeStreamTools for constant paths used
SourceFiles
codedFixedValueFvPatchScalarField.C
@ -86,7 +91,7 @@ namespace Foam
class codeProperties;
/*---------------------------------------------------------------------------*\
Class codedFixedValueFvPatchScalarField Declaration
Class codedFixedValueFvPatchScalarField Declaration
\*---------------------------------------------------------------------------*/
class codedFixedValueFvPatchScalarField
@ -108,8 +113,8 @@ class codedFixedValueFvPatchScalarField
void writeLibrary
(
const fileName dir,
const fileName libPath,
const fileName& dir,
const fileName& libPath,
const dictionary& dict
);
@ -117,6 +122,15 @@ class codedFixedValueFvPatchScalarField
public:
// Static data members
//- Name of the C code template to be used
const static word codeTemplateC;
//- Name of the H code template to be used
const static word codeTemplateH;
//- Runtime type information
TypeName("codedFixedValue<scalar>");