dynamicCode.H
Go to the documentation of this file.
1/*---------------------------------------------------------------------------*\
2 ========= |
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4 \\ / O peration |
5 \\ / A nd | www.openfoam.com
6 \\/ M anipulation |
7-------------------------------------------------------------------------------
8 Copyright (C) 2011 OpenFOAM Foundation
9 Copyright (C) 2019-2020 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
12 This file is part of OpenFOAM.
13
14 OpenFOAM is free software: you can redistribute it and/or modify it
15 under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26
27Class
28 Foam::dynamicCode
29
30Description
31 Tools for handling dynamic code compilation
32
33SourceFiles
34 dynamicCode.C
35
36\*---------------------------------------------------------------------------*/
37
38#ifndef dynamicCode_H
39#define dynamicCode_H
40
41#include "Tuple2.H"
42#include "HashTable.H"
43#include "DynamicList.H"
44
45// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
46
47namespace Foam
48{
49
50// Forward Declarations
51class dynamicCodeContext;
52class ISstream;
53class OSstream;
54class SHA1Digest;
55
56/*---------------------------------------------------------------------------*\
57 Class dynamicCode Declaration
58\*---------------------------------------------------------------------------*/
60class dynamicCode
61{
62public:
65
66private:
67
68 // Private Data
69
70 //- Root for dynamic code compilation
71 fileName codeRoot_;
72
73 //- Subdirectory name for loading libraries
74 const fileName libSubDir_;
75
76 //- Name for code
77 word codeName_;
78
79 //- Name for code subdirectory
80 word codeDirName_;
81
82 //- Files to copy and filter
83 DynamicList<fileName> compileFiles_;
84
85 //- Files to copy and filter
86 DynamicList<fileName> copyFiles_;
87
88 //- Direct contents for files
89 DynamicList<fileAndContent> createFiles_;
90
91 //- Variables to use during filtering
92 HashTable<string> filterVars_;
93
94 //- Contents for Make/options
95 std::string makeOptions_;
96
97
98 // Private Member Functions
99
100 //- No copy construct
101 dynamicCode(const dynamicCode&) = delete;
102
103 //- No copy assignment
104 void operator=(const dynamicCode&) = delete;
105
106
107protected:
108
109 // Static Data Members
110
111 //- Directory for library targets for Make/files
112 static const char* const targetLibDir;
113
114 //- Top-level directory name for copy/compiling
115 static const char* const topDirName;
116
117
118 // Protected Member Functions
119
120 //- Copy lines while expanding variables
121 static void copyAndFilter
122 (
123 ISstream&,
124 OSstream&,
125 const HashTable<string>& mapping
126 );
127
128 //- Resolve code-templates via the codeTemplateEnvName
129 // alternatively in the codeTemplateDirName via Foam::findEtcFile
130 static bool resolveTemplates
131 (
132 const UList<fileName>& templateNames,
133 DynamicList<fileName>& resolvedFiles,
134 DynamicList<fileName>& badFiles
135 );
136
137 //- Write SHA1 value as C-comment
138 bool writeCommentSHA1(Ostream&) const;
139
140 //- Copy/create Make/files prior to compilation
141 bool createMakeFiles() const;
142
143 //- Copy/create Make/options prior to compilation
144 bool createMakeOptions() const;
145
146
147 //- Write digest to Make/SHA1Digest
148 bool writeDigest(const SHA1Digest&) const;
149
150 //- Write digest to Make/SHA1Digest
151 bool writeDigest(const std::string&) const;
152
153
154public:
155
156 // Static Data Members
157
158 //- Name of the code template environment variable
159 // Used to located the codeTemplateName
160 static const word codeTemplateEnvName;
161
162 //- Name of the code template sub-directory
163 // Used when locating the codeTemplateName via Foam::findEtcFile
164 static const fileName codeTemplateDirName;
165
166 //- Flag if system operations are allowed
167 static int allowSystemOperations;
168
169
170 // Static Member functions
171
172 //- Check security for creating dynamic code
173 static void checkSecurity(const char* title, const dictionary&);
174
175
176 // Constructors
177
178 //- Construct for a specified code name and code directory name
179 // Defaults to using the code name for the code directory name
180 explicit dynamicCode
181 (
182 const word& codeName,
183 const word& codeDirName = ""
184 );
185
186
187 // Member functions
188
189 //- Return the code-name
190 const word& codeName() const
191 {
192 return codeName_;
193 }
194
195 //- Return the code-dirname
196 const word& codeDirName() const
197 {
198 return codeDirName_;
199 }
200
201 //- Root for dynamic code compilation
202 // Expanded from <case>/dynamicCode
203 const fileName& codeRoot() const
204 {
205 return codeRoot_;
206 }
207
208 //- Subdirectory name for loading libraries
209 // Expanded from platforms/\$WM_OPTIONS/lib
210 fileName libSubDir() const
211 {
212 return libSubDir_;
213 }
214
215 //- Path for specified code name
216 // Corresponds to codeRoot()/codeDirName()
217 fileName codePath() const
218 {
219 return codeRoot_/codeDirName_;
220 }
221
222 //- Path for specified code name relative to <case>
223 // Corresponds to topDirName/codeDirName()
224 fileName codeRelPath() const;
225
226 //- Library path for specified code name
227 // Corresponds to codeRoot()/libSubDir()/lib<codeName>.so
228 fileName libPath() const;
229
230 //- Library path for specified code name relative to <case>
231 // Corresponds to
232 // dynamicCode/codeDirName()/libSubDir()/lib<codeName>.so
233 fileName libRelPath() const;
234
235
236 //- Path for SHA1Digest
237 // Corresponds to codePath()/Make/SHA1Digest
238 fileName digestFile() const
239 {
240 return codeRoot_/codeDirName_/"Make/SHA1Digest";
241 }
242
243
244 //- Clear files and variables
245 void clear();
246
247 //- Clear files and reset variables to specified context
248 void reset(const dynamicCodeContext&);
249
250
251 //- Add a file template name, which will be found and filtered
252 void addCompileFile(const fileName& name);
253
254 //- Add a file template name, which will be found and filtered
255 void addCopyFile(const fileName& name);
256
257 //- Add a file to create with its contents. Will not be filtered
258 void addCreateFile(const fileName& name, const string& contents);
259
260 //- Define filter variables for code, codeInclude, SHA1sum
262
263 //- Define a filter variable
264 void setFilterVariable(const word& key, const std::string& value);
265
266 //- Define contents for Make/options
267 void setMakeOptions(const std::string& content);
268
269
270 //- Verify if the copied code is up-to-date, based on Make/SHA1Digest
271 bool upToDate(const dynamicCodeContext& context) const;
272
273 //- Verify if the copied code is up-to-date, based on Make/SHA1Digest
274 bool upToDate(const SHA1Digest& sha1) const;
275
276 //- Copy/create files prior to compilation
277 bool copyOrCreateFiles(const bool verbose = false) const;
278
279 //- Compile a libso
280 bool wmakeLibso() const;
281
282
283 // Convenience
284
285 //- Define a filter variables TemplateType and FieldType
286 template<class Type>
287 void setFieldTemplates()
288 {
289 std::string val(pTraits<Type>::typeName);
290
291 // Template type
292 setFilterVariable("TemplateType", val);
293
294 // Field type - eg, ScalarField, VectorField, ...
295 val[0] = toupper(val[0]);
296 val += "Field";
297 setFilterVariable("FieldType", val);
298 }
299};
300
301
302// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
303
304} // End namespace Foam
305
306// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
307
308#endif
309
310// ************************************************************************* //
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition: DynamicList.H:72
A HashTable similar to std::unordered_map.
Definition: HashTable.H:123
Generic input stream using a standard (STL) stream.
Definition: ISstream.H:58
Generic output stream using a standard (STL) stream.
Definition: OSstream.H:57
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
The SHA1 message digest.
Definition: SHA1Digest.H:61
A 2-tuple for storing two objects of dissimilar types. The container is similar in purpose to std::pa...
Definition: Tuple2.H:58
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: UList.H:94
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:126
Encapsulation of dynamic code dictionaries.
Tools for handling dynamic code compilation.
Definition: dynamicCode.H:60
static int allowSystemOperations
Flag if system operations are allowed.
Definition: dynamicCode.H:166
static void copyAndFilter(ISstream &, OSstream &, const HashTable< string > &mapping)
Copy lines while expanding variables.
Definition: dynamicCode.C:102
fileName codePath() const
Path for specified code name.
Definition: dynamicCode.H:216
bool copyOrCreateFiles(const bool verbose=false) const
Copy/create files prior to compilation.
Definition: dynamicCode.C:403
void addCopyFile(const fileName &name)
Add a file template name, which will be found and filtered.
Definition: dynamicCode.C:359
fileName libSubDir() const
Subdirectory name for loading libraries.
Definition: dynamicCode.H:209
void reset(const dynamicCodeContext &)
Clear files and reset variables to specified context.
Definition: dynamicCode.C:344
static const char *const targetLibDir
Directory for library targets for Make/files.
Definition: dynamicCode.H:111
fileName libRelPath() const
Library path for specified code name relative to <case>
Definition: dynamicCode.C:321
void addCreateFile(const fileName &name, const string &contents)
Add a file to create with its contents. Will not be filtered.
Definition: dynamicCode.C:366
static bool resolveTemplates(const UList< fileName > &templateNames, DynamicList< fileName > &resolvedFiles, DynamicList< fileName > &badFiles)
Resolve code-templates via the codeTemplateEnvName.
Definition: dynamicCode.C:139
void setFieldTemplates()
Define a filter variables TemplateType and FieldType.
Definition: dynamicCode.H:286
static const word codeTemplateEnvName
Name of the code template environment variable.
Definition: dynamicCode.H:159
static const char *const topDirName
Top-level directory name for copy/compiling.
Definition: dynamicCode.H:114
void setFilterVariable(const word &key, const std::string &value)
Define a filter variable.
Definition: dynamicCode.C:388
const fileName & codeRoot() const
Root for dynamic code compilation.
Definition: dynamicCode.H:202
const word & codeDirName() const
Return the code-dirname.
Definition: dynamicCode.H:195
void setFilterContext(const dynamicCodeContext &)
Define filter variables for code, codeInclude, SHA1sum.
Definition: dynamicCode.C:376
static void checkSecurity(const char *title, const dictionary &)
Check security for creating dynamic code.
Definition: dynamicCode.C:65
bool createMakeFiles() const
Copy/create Make/files prior to compilation.
Definition: dynamicCode.C:197
fileName digestFile() const
Path for SHA1Digest.
Definition: dynamicCode.H:237
bool writeDigest(const SHA1Digest &) const
Write digest to Make/SHA1Digest.
Definition: dynamicCode.C:264
fileName codeRelPath() const
Path for specified code name relative to <case>
Definition: dynamicCode.C:309
void addCompileFile(const fileName &name)
Add a file template name, which will be found and filtered.
Definition: dynamicCode.C:353
Tuple2< fileName, string > fileAndContent
Definition: dynamicCode.H:63
void clear()
Clear files and variables.
Definition: dynamicCode.C:327
bool upToDate(const dynamicCodeContext &context) const
Verify if the copied code is up-to-date, based on Make/SHA1Digest.
Definition: dynamicCode.C:530
fileName libPath() const
Library path for specified code name.
Definition: dynamicCode.C:315
const word & codeName() const
Return the code-name.
Definition: dynamicCode.H:189
static const fileName codeTemplateDirName
Name of the code template sub-directory.
Definition: dynamicCode.H:163
bool createMakeOptions() const
Copy/create Make/options prior to compilation.
Definition: dynamicCode.C:235
bool wmakeLibso() const
Compile a libso.
Definition: dynamicCode.C:495
bool writeCommentSHA1(Ostream &) const
Write SHA1 value as C-comment.
Definition: dynamicCode.C:182
void setMakeOptions(const std::string &content)
Define contents for Make/options.
Definition: dynamicCode.C:397
A class for handling file names.
Definition: fileName.H:76
A traits class, which is primarily used for primitives.
Definition: pTraits.H:59
A class for handling words, derived from Foam::string.
Definition: word.H:68
Namespace for OpenFOAM.
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59