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-2024 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 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 
34 const Foam::wordList Foam::compileTemplate::codeKeys(wordList::null());
35 
36 const Foam::wordList Foam::compileTemplate::codeDictVars(wordList::null());
37 
38 
39 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
40 
41 Foam::fileName Foam::compileTemplate::name
42 (
43  const word& instantiatedName
44 ) const
45 {
46  fileName templateFileName(instantiatedName);
47  templateFileName.replaceAll(',', '_');
48  templateFileName.replaceAll('<', '_');
49  templateFileName.replaceAll('>', '_');
50 
51  return templateFileName;
52 }
53 
54 
55 Foam::dictionary Foam::compileTemplate::optionsDict
56 (
57  const word& templateName
58 ) const
59 {
60  IFstream optionsFile(dynamicCode::resolveTemplate(templateName));
61  if (!optionsFile.good())
62  {
64  << "Failed to open dictionary file " << templateName
65  << exit(FatalError);
66  }
67 
68  return dictionary(optionsFile);
69 }
70 
71 
72 void Foam::compileTemplate::setFilterVariable
73 (
74  dynamicCode& dynCode,
75  const dynamicCodeContext& context,
76  const Pair<word>& substitution
77 ) const
78 {
79  const word& name(substitution.first());
80  word type(substitution.second());
81  const word typeRenameMapName(name + "Renamed");
82 
83  if (dict_.found(name))
84  {
85  const HashSet<word> types(dict_.lookup(name));
86  if (!types.found(type))
87  {
89  << "Unknown " << name << " type " << type << nl
90  << "Supported " << name << " types: " << types
91  << exit(FatalIOError);
92  }
93  }
94 
95  if (dict_.found(typeRenameMapName))
96  {
97  const HashTable<word> renameMap
98  (
99  dict_.lookup(typeRenameMapName)
100  );
101 
102  if (renameMap.found(type))
103  {
104  type = renameMap[type];
105  }
106  }
107 
108  dynCode.setFilterVariable(name, type);
109 }
110 
111 
112 void Foam::compileTemplate::prepare
113 (
114  dynamicCode& dynCode,
115  const dynamicCodeContext& context
116 ) const
117 {
118  dynCode.setFilterVariable("typeName", codeName());
119 
120  forAll(substitutions_, i)
121  {
122  setFilterVariable(dynCode, context, substitutions_[i]);
123  }
124 
125  // Compile filtered C template
126  dynCode.addCompileFile(codeTemplateC(templateName_));
127 
128  // Define Make/options
129  dynCode.setMakeOptions(context.options() + "\n\n" + context.libs());
130 
131  // Make verbose if debugging
132  dynCode.setFilterVariable("verbose", Foam::name(bool(debug)));
133 
134  if (debug)
135  {
136  Info<<"compile " << codeName() << " sha1: " << context.sha1() << endl;
137  }
138 }
139 
140 
141 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
142 
144 (
145  const word& templateName,
146  const word& instantiatedName,
147  const List<Pair<word>>& substitutions
148 )
149 :
150  codedBase
151  (
152  name(instantiatedName),
153  optionsDict(templateName),
154  codeKeys,
155  codeDictVars
156  ),
157  templateName_(templateName),
158  substitutions_(substitutions),
159  dict_(optionsDict(templateName))
160 {
161  this->updateLibrary(dict_);
162 }
163 
164 
165 // ************************************************************************* //
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:433
Base class for function objects and boundary conditions using dynamic code.
Definition: codedBase.H:52
bool updateLibrary(const dictionary &dict) const
Update library from given updated dictionary as required.
Definition: codedBase.C:398
compileTemplate(const word &templateName, const word &instantiatedName, const List< Pair< word >> &substitutions)
Construct from name and dictionary.
A list of keywords followed by any number of values (e.g. words and numbers) or sub-dictionaries.
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:258
messageStream Info
IOerror FatalIOError
word name(const LagrangianState state)
Return a string representation of a Lagrangian state enumeration.
error FatalError
static const char nl
Definition: Ostream.H:267
fileType type(const fileName &, const bool checkVariants=true, const bool followLink=true)
Return the file type: directory or file.
Definition: POSIX.C:488