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-2023 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
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 
27 Class
28  Foam::dynamicCode
29 
30 Description
31  Tools for handling dynamic code compilation
32 
33 SourceFiles
34  dynamicCode.C
35 
36 \*---------------------------------------------------------------------------*/
37 
38 #ifndef Foam_dynamicCode_H
39 #define Foam_dynamicCode_H
40 
41 #include "fileName.H"
42 #include "DynamicList.H"
43 #include "HashTable.H"
44 #include "Tuple2.H"
45 
46 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
47 
48 namespace Foam
49 {
50 
51 // Forward Declarations
52 class dynamicCodeContext;
53 class ISstream;
54 class OSstream;
55 class SHA1Digest;
56 
57 /*---------------------------------------------------------------------------*\
58  Class dynamicCode Declaration
59 \*---------------------------------------------------------------------------*/
60 
61 class dynamicCode
62 {
63  // Private Data
64 
65  //- Root for dynamic code compilation
66  fileName codeRoot_;
67 
68  //- Subdirectory name for loading libraries
69  const fileName libSubDir_;
70 
71  //- Name for code
72  word codeName_;
73 
74  //- Name for code subdirectory
75  word codeDirName_;
76 
77  //- Files to copy and filter
78  DynamicList<fileName> compileFiles_;
79 
80  //- Files to copy and filter
81  DynamicList<fileName> copyFiles_;
82 
83  //- Direct contents for files
85 
86  //- Variables to use during filtering
87  HashTable<string> filterVars_;
88 
89  //- Contents for Make/options
90  std::string makeOptions_;
91 
92 
93 protected:
94 
95  // Static Data Members
96 
97  //- Directory for library targets for Make/files
98  static const char* const targetLibDir;
99 
100  //- Top-level directory name for copy/compiling
101  static const char* const topDirName;
102 
103 
104  // Protected Member Functions
105 
106  //- Copy lines while expanding variables
107  static void copyAndFilter
108  (
109  ISstream&,
110  OSstream&,
111  const HashTable<string>& mapping
112  );
114  //- Resolve code-templates via the codeTemplateEnvName
115  // alternatively in the codeTemplateDirName via Foam::findEtcFile
116  static bool resolveTemplates
117  (
118  const UList<fileName>& templateNames,
119  DynamicList<fileName>& resolvedFiles,
120  DynamicList<fileName>& badFiles
121  );
122 
123  //- Write SHA1 value as C-comment
124  bool writeCommentSHA1(Ostream&) const;
125 
126  //- Copy/create Make/files prior to compilation
127  bool createMakeFiles() const;
128 
129  //- Copy/create Make/options prior to compilation
130  bool createMakeOptions() const;
131 
132 
133  //- Write digest to Make/SHA1Digest
134  bool writeDigest(const SHA1Digest&) const;
135 
136  //- Write digest to Make/SHA1Digest
137  bool writeDigest(const std::string&) const;
138 
139 
140 public:
141 
142  // Static Data Members
143 
144  //- Name of the code template environment variable
145  // Used to located the codeTemplateName
146  static const word codeTemplateEnvName;
147 
148  //- Name of the code template sub-directory
149  // Used when locating the codeTemplateName via Foam::findEtcFile
150  static const fileName codeTemplateDirName;
151 
152  //- Flag if system operations are allowed
153  static int allowSystemOperations;
154 
155 
156  // Static Member Functions
157 
158  //- Check security for creating dynamic code
159  static void checkSecurity(const char* title, const dictionary&);
160 
161 
162  // Generated Methods
163 
164  //- No copy construct
165  dynamicCode(const dynamicCode&) = delete;
166 
167  //- No copy assignment
168  void operator=(const dynamicCode&) = delete;
169 
170 
171  // Constructors
172 
173  //- Construct for a specified code name and code directory name
174  // Defaults to using the code name for the code directory name
175  explicit dynamicCode
176  (
177  const word& codeName,
178  const word& codeDirName = ""
179  );
180 
182  // Member Functions
183 
184  //- Return the code-name
185  const word& codeName() const noexcept { return codeName_; }
186 
187  //- Return the code-dirname
188  const word& codeDirName() const noexcept { return codeDirName_; }
189 
190  //- Root for dynamic code compilation
191  // Expanded from <case>/dynamicCode
192  const fileName& codeRoot() const noexcept { return codeRoot_; }
194  //- Subdirectory name for loading libraries
195  // Expanded from platforms/\$WM_OPTIONS/lib
196  const fileName& libSubDir() const noexcept { return libSubDir_; }
197 
198  //- Path for specified code name
199  // Corresponds to codeRoot()/codeDirName()
200  fileName codePath() const
201  {
202  return codeRoot_/codeDirName_;
203  }
204 
205  //- Path for specified code name relative to <case>
206  // Corresponds to topDirName/codeDirName()
207  fileName codeRelPath() const;
208 
209  //- Library path for specified code name
210  // Corresponds to codeRoot()/libSubDir()/lib<codeName>.so
211  fileName libPath() const;
212 
213  //- Library path for specified code name relative to <case>
214  // Corresponds to
215  // dynamicCode/codeDirName()/libSubDir()/lib<codeName>.so
216  fileName libRelPath() const;
217 
218 
219  //- Path for SHA1Digest
220  // Corresponds to codePath()/Make/SHA1Digest
221  fileName digestFile() const
222  {
223  return codeRoot_/codeDirName_/"Make/SHA1Digest";
224  }
225 
226 
227  //- Clear files and variables
228  void clear();
229 
230  //- Clear files and reset variables to specified context
231  void reset(const dynamicCodeContext&);
232 
233 
234  //- Add a file template name, which will be found and filtered
235  void addCompileFile(const fileName& name);
237  //- Add a file template name, which will be found and filtered
238  void addCopyFile(const fileName& name);
239 
240  //- Add a file to create with its contents. Will not be filtered
241  void addCreateFile(const fileName& name, const std::string& contents);
242 
243  //- Define filter variables for code, codeInclude, SHA1sum
245 
246  //- Define a filter variable
247  void setFilterVariable(const word& key, const std::string& value);
249  //- Define contents for Make/options
250  void setMakeOptions(const std::string& content);
251 
252 
253  //- Verify if the copied code is up-to-date, based on Make/SHA1Digest
254  bool upToDate(const dynamicCodeContext& context) const;
256  //- Verify if the copied code is up-to-date, based on Make/SHA1Digest
257  bool upToDate(const SHA1Digest& sha1) const;
258 
259  //- Copy/create files prior to compilation
260  bool copyOrCreateFiles(const bool verbose = false) const;
261 
262  //- Compile a libso
263  bool wmakeLibso() const;
264 
265 
266  // Synchronisation
267 
268  //- Wait for libPath() file to appear on sub-ranks.
269  // Returns immediately if non-parallel or
270  // IOobject::fileModificationSkew is not set
271  static void waitForFile
272  (
273  const fileName& file,
274  const dictionary& contextDict
275  );
276 
277 
278  // Convenience
279 
280  //- Define a filter variables TemplateType and FieldType
281  template<class Type>
282  void setFieldTemplates()
283  {
284  std::string val(pTraits<Type>::typeName);
285 
286  // Template type
287  setFilterVariable("TemplateType", val);
288 
289  // Field type - eg, ScalarField, VectorField, ...
290  val[0] = toupper(val[0]);
291  val += "Field";
292  setFilterVariable("FieldType", val);
293  }
294 };
296 
297 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
298 
299 } // End namespace Foam
300 
301 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
302 
303 #endif
304 
305 // ************************************************************************* //
Generic output stream using a standard (STL) stream.
Definition: OSstream.H:50
bool writeDigest(const SHA1Digest &) const
Write digest to Make/SHA1Digest.
Definition: dynamicCode.C:257
A class for handling file names.
Definition: fileName.H:72
void addCreateFile(const fileName &name, const std::string &contents)
Add a file to create with its contents. Will not be filtered.
Definition: dynamicCode.C:359
void reset(const dynamicCodeContext &)
Clear files and reset variables to specified context.
Definition: dynamicCode.C:337
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
void setMakeOptions(const std::string &content)
Define contents for Make/options.
Definition: dynamicCode.C:390
static void waitForFile(const fileName &file, const dictionary &contextDict)
Wait for libPath() file to appear on sub-ranks.
Definition: dynamicCode.C:532
static bool resolveTemplates(const UList< fileName > &templateNames, DynamicList< fileName > &resolvedFiles, DynamicList< fileName > &badFiles)
Resolve code-templates via the codeTemplateEnvName.
Definition: dynamicCode.C:133
A traits class, which is primarily used for primitives and vector-space.
Definition: pTraits.H:75
The SHA1 message digest.
Definition: SHA1Digest.H:56
void setFilterContext(const dynamicCodeContext &)
Define filter variables for code, codeInclude, SHA1sum.
Definition: dynamicCode.C:369
static const word codeTemplateEnvName
Name of the code template environment variable.
Definition: dynamicCode.H:181
static const char *const topDirName
Top-level directory name for copy/compiling.
Definition: dynamicCode.H:118
static const char *const targetLibDir
Directory for library targets for Make/files.
Definition: dynamicCode.H:113
void addCompileFile(const fileName &name)
Add a file template name, which will be found and filtered.
Definition: dynamicCode.C:346
bool writeCommentSHA1(Ostream &) const
Write SHA1 value as C-comment.
Definition: dynamicCode.C:176
bool createMakeOptions() const
Copy/create Make/options prior to compilation.
Definition: dynamicCode.C:228
fileName codeRelPath() const
Path for specified code name relative to <case>
Definition: dynamicCode.C:302
fileName digestFile() const
Path for SHA1Digest.
Definition: dynamicCode.H:295
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects...
Definition: DynamicList.H:51
fileName libRelPath() const
Library path for specified code name relative to <case>
Definition: dynamicCode.C:314
A class for handling words, derived from Foam::string.
Definition: word.H:63
const fileName & codeRoot() const noexcept
Root for dynamic code compilation.
Definition: dynamicCode.H:248
static const fileName codeTemplateDirName
Name of the code template sub-directory.
Definition: dynamicCode.H:188
static int allowSystemOperations
Flag if system operations are allowed.
Definition: dynamicCode.H:193
static void copyAndFilter(ISstream &, OSstream &, const HashTable< string > &mapping)
Copy lines while expanding variables.
Definition: dynamicCode.C:96
bool wmakeLibso() const
Compile a libso.
Definition: dynamicCode.C:488
A HashTable similar to std::unordered_map.
Definition: HashTable.H:108
void operator=(const dynamicCode &)=delete
No copy assignment.
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:105
fileName codePath() const
Path for specified code name.
Definition: dynamicCode.H:262
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
const direction noexcept
Definition: Scalar.H:258
bool upToDate(const dynamicCodeContext &context) const
Verify if the copied code is up-to-date, based on Make/SHA1Digest.
Definition: dynamicCode.C:523
bool copyOrCreateFiles(const bool verbose=false) const
Copy/create files prior to compilation.
Definition: dynamicCode.C:396
const word & codeName() const noexcept
Return the code-name.
Definition: dynamicCode.H:236
Tools for handling dynamic code compilation.
Definition: dynamicCode.H:56
Encapsulation of dynamic code dictionaries.
Generic input stream using a standard (STL) stream.
Definition: ISstream.H:51
void setFilterVariable(const word &key, const std::string &value)
Define a filter variable.
Definition: dynamicCode.C:381
auto key(const Type &t) -> typename std::enable_if< std::is_enum< Type >::value, typename std::underlying_type< Type >::type >::type
Definition: foamGltfBase.H:103
void addCopyFile(const fileName &name)
Add a file template name, which will be found and filtered.
Definition: dynamicCode.C:352
fileName libPath() const
Library path for specified code name.
Definition: dynamicCode.C:308
void clear()
Clear files and variables.
Definition: dynamicCode.C:320
dynamicCode(const dynamicCode &)=delete
No copy construct.
void setFieldTemplates()
Define a filter variables TemplateType and FieldType.
Definition: dynamicCode.H:385
const word & codeDirName() const noexcept
Return the code-dirname.
Definition: dynamicCode.H:241
static void checkSecurity(const char *title, const dictionary &)
Check security for creating dynamic code.
Definition: dynamicCode.C:59
bool createMakeFiles() const
Copy/create Make/files prior to compilation.
Definition: dynamicCode.C:190
const fileName & libSubDir() const noexcept
Subdirectory name for loading libraries.
Definition: dynamicCode.H:255
Namespace for OpenFOAM.