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  const word typeBase(name + "Base");
104  if (context.dict().found(typeBase))
105  {
106  const HashTable<word> typeToBaseMap(context.dict().lookup(typeBase));
107  dynCode.setFilterVariable(typeBase, typeToBaseMap[type]);
108  }
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(name(instantiatedName), optionsDict(templateName)),
151  templateName_(templateName),
152  substitutions_(substitutions)
153 {
154  this->updateLibrary();
155 }
156 
157 
158 // ************************************************************************* //
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:288
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:160
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:318
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:306
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:251
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:260
fileType type(const fileName &, const bool checkVariants=true, const bool followLink=true)
Return the file type: directory or file.
Definition: POSIX.C:488