compileTemplate.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2021-2023 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "compileTemplate.H"
27 #include "dynamicCodeContext.H"
28 #include "Time.H"
29 #include "IFstream.H"
30 #include "OSspecific.H"
31 
32 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
33 
34 Foam::fileName Foam::compileTemplate::name
35 (
36  const word& instantiatedName
37 ) const
38 {
39  fileName templateFileName(instantiatedName);
40  templateFileName.replaceAll(',', '_');
41  templateFileName.replaceAll('<', '_');
42  templateFileName.replaceAll('>', '_');
43 
44  return templateFileName;
45 }
46 
47 
48 Foam::dictionary Foam::compileTemplate::optionsDict
49 (
50  const word& templateName
51 ) const
52 {
53  IFstream optionsFile(dynamicCode::resolveTemplate(templateName));
54  if (!optionsFile.good())
55  {
57  << "Failed to open dictionary file " << templateName
58  << exit(FatalError);
59  }
60 
61  return dictionary(optionsFile);
62 }
63 
64 
65 void Foam::compileTemplate::setFilterVariable
66 (
67  dynamicCode& dynCode,
68  const dynamicCodeContext& context,
69  const Pair<word>& substitution
70 ) const
71 {
72  const word& name(substitution.first());
73  word type(substitution.second());
74  const word typeRenameMapName(name + "Renamed");
75 
76  if (context.dict().found(name))
77  {
78  const HashSet<word> types(context.dict().lookup(name));
79  if (!types.found(type))
80  {
81  FatalIOErrorInFunction(context.dict())
82  << "Unknown " << name << " type " << type << nl
83  << "Supported " << name << " types: " << types
84  << exit(FatalIOError);
85  }
86  }
87 
88  if (context.dict().found(typeRenameMapName))
89  {
90  const HashTable<word> renameMap
91  (
92  context.dict().lookup(typeRenameMapName)
93  );
94 
95  if (renameMap.found(type))
96  {
97  type = renameMap[type];
98  }
99  }
100 
101  dynCode.setFilterVariable(name, type);
102 }
103 
104 
105 void Foam::compileTemplate::prepare
106 (
107  dynamicCode& dynCode,
108  const dynamicCodeContext& context
109 ) const
110 {
111  dynCode.setFilterVariable("typeName", codeName());
112 
113  forAll(substitutions_, i)
114  {
115  setFilterVariable(dynCode, context, substitutions_[i]);
116  }
117 
118  // Compile filtered C template
119  dynCode.addCompileFile(codeTemplateC(templateName_));
120 
121  // Define Make/options
122  dynCode.setMakeOptions(context.options() + "\n\n" + context.libs());
123 
124  // Make verbose if debugging
125  dynCode.setFilterVariable("verbose", Foam::name(bool(debug)));
126 
127  if (debug)
128  {
129  Info<<"compile " << codeName() << " sha1: " << context.sha1() << endl;
130  }
131 }
132 
133 
134 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
135 
137 (
138  const word& templateName,
139  const word& instantiatedName,
140  const List<Pair<word>>& substitutions
141 )
142 :
143  codedBase(name(instantiatedName), optionsDict(templateName)),
144  templateName_(templateName),
145  substitutions_(substitutions)
146 {
147  this->updateLibrary();
148 }
149 
150 
151 // ************************************************************************* //
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: List.H:91
Base class for function objects and boundary conditions using dynamic code.
Definition: codedBase.H:54
void updateLibrary() const
Update library as required.
Definition: codedBase.C:314
compileTemplate(const word &templateName, const word &instantiatedName, const List< Pair< word >> &substitutions)
Construct from name and dictionary.
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:162
static fileName resolveTemplate(const fileName &templateName)
Resolve code-template via the codeTemplateEnvName.
Definition: dynamicCode.C:143
A class for handling file names.
Definition: fileName.H:82
A class for handling words, derived from string.
Definition: word.H:62
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:346
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:334
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:257
word name(const bool)
Return a word representation of a bool.
Definition: boolIO.C:39
messageStream Info
IOerror FatalIOError
error FatalError
static const char nl
Definition: Ostream.H:266
fileType type(const fileName &, const bool checkVariants=true, const bool followLink=true)
Return the file type: directory or file.
Definition: POSIX.C:488