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