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 -------------------------------------------------------------------------------
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:
63 
65 
66 private:
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 
107 protected:
108 
109  // Static Data Members
110 
111  //- Root of the LIB target for Make/files
112  static const char* const libTargetRoot;
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 
154 public:
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 // ************************************************************************* //
Foam::dynamicCode::codeRoot
const fileName & codeRoot() const
Root for dynamic code compilation.
Definition: dynamicCode.H:202
Foam::dynamicCode::createMakeFiles
bool createMakeFiles() const
Copy/create Make/files prior to compilation.
Definition: dynamicCode.C:196
Foam::dynamicCode::topDirName
static const char *const topDirName
Top-level directory name for copy/compiling.
Definition: dynamicCode.H:114
Foam::dynamicCode::writeCommentSHA1
bool writeCommentSHA1(Ostream &) const
Write SHA1 value as C-comment.
Definition: dynamicCode.C:181
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:101
Foam::dynamicCode::copyOrCreateFiles
bool copyOrCreateFiles(const bool verbose=false) const
Copy/create files prior to compilation.
Definition: dynamicCode.C:401
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:493
Foam::DynamicList
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition: DynamicList.H:55
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:233
Foam::dynamicCode::writeDigest
bool writeDigest(const SHA1Digest &) const
Write digest to Make/SHA1Digest.
Definition: dynamicCode.C:262
Foam::dynamicCode::codePath
fileName codePath() const
Path for specified code name.
Definition: dynamicCode.H:216
Foam::dynamicCode::libSubDir
fileName libSubDir() const
Subdirectory name for loading libraries.
Definition: dynamicCode.H:209
Foam::ISstream
Generic input stream using a standard (STL) stream.
Definition: ISstream.H:55
Foam::dynamicCode::setFilterContext
void setFilterContext(const dynamicCodeContext &)
Define filter variables for code, codeInclude, SHA1sum.
Definition: dynamicCode.C:374
Foam::dynamicCode::digestFile
fileName digestFile() const
Path for SHA1Digest.
Definition: dynamicCode.H:237
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:138
Foam::dynamicCode::addCopyFile
void addCopyFile(const fileName &name)
Add a file template name, which will be found and filtered.
Definition: dynamicCode.C:357
Foam::dynamicCode::allowSystemOperations
static int allowSystemOperations
Flag if system operations are allowed.
Definition: dynamicCode.H:166
Foam::dynamicCode::fileAndContent
Tuple2< fileName, string > fileAndContent
Definition: dynamicCode.H:63
Foam::dynamicCode::libPath
fileName libPath() const
Library path for specified code name.
Definition: dynamicCode.C:313
Foam::dynamicCode::codeTemplateEnvName
static const word codeTemplateEnvName
Name of the code template environment variable.
Definition: dynamicCode.H:159
Foam::dynamicCode::addCompileFile
void addCompileFile(const fileName &name)
Add a file template name, which will be found and filtered.
Definition: dynamicCode.C:351
Foam::dynamicCode::codeName
const word & codeName() const
Return the code-name.
Definition: dynamicCode.H:189
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:195
Foam::dynamicCode::checkSecurity
static void checkSecurity(const char *title, const dictionary &)
Check security for creating dynamic code.
Definition: dynamicCode.C:64
Foam::dynamicCode::codeRelPath
fileName codeRelPath() const
Path for specified code name relative to <case>
Definition: dynamicCode.C:307
Foam::OSstream
Generic output stream using a standard (STL) stream.
Definition: OSstream.H:54
Foam::dynamicCode::libRelPath
fileName libRelPath() const
Library path for specified code name relative to <case>
Definition: dynamicCode.C:319
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:386
Foam::dynamicCode::setFieldTemplates
void setFieldTemplates()
Define a filter variables TemplateType and FieldType.
Definition: dynamicCode.H:286
Foam::dynamicCode::reset
void reset(const dynamicCodeContext &)
Clear files and reset variables to specified context.
Definition: dynamicCode.C:342
Foam::HashTable
A HashTable similar to std::unordered_map.
Definition: HashTable.H:105
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:528
Foam::dynamicCode::setMakeOptions
void setMakeOptions(const std::string &content)
Define contents for Make/options.
Definition: dynamicCode.C:395
Foam::dynamicCode::clear
void clear()
Clear files and variables.
Definition: dynamicCode.C:325
Foam::pTraits
Traits class for primitives.
Definition: pTraits.H:54
Foam::dynamicCode::codeTemplateDirName
static const fileName codeTemplateDirName
Name of the code template sub-directory.
Definition: dynamicCode.H:163
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:364
Foam::dynamicCode::libTargetRoot
static const char *const libTargetRoot
Root of the LIB target for Make/files.
Definition: dynamicCode.H:111