All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
dynamicCode.H
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) 2011-2021 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 Class
25  Foam::dynamicCode
26 
27 Description
28  Tools for handling dynamic code compilation
29 
30 SourceFiles
31  dynamicCode.C
32 
33 \*---------------------------------------------------------------------------*/
34 
35 #ifndef dynamicCode_H
36 #define dynamicCode_H
37 
38 #include "Tuple2.H"
39 #include "HashTable.H"
40 #include "DynamicList.H"
41 
42 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
43 
44 namespace Foam
45 {
46 
47 // Forward declaration of classes
48 class dynamicCodeContext;
49 class ISstream;
50 class OSstream;
51 class SHA1Digest;
52 
53 /*---------------------------------------------------------------------------*\
54  Class dynamicCode Declaration
55 \*---------------------------------------------------------------------------*/
56 
57 class dynamicCode
58 {
59 
60 public:
61 
63 
64 
65 private:
66 
67  // Private Data
68 
69  //- Root for dynamic code compilation
70  fileName codeRoot_;
71 
72  //- Subdirectory name for loading libraries
73  const fileName libSubDir_;
74 
75  //- Name for code
76  word codeName_;
77 
78  //- Name for code subdirectory
79  word codeDirName_;
80 
81  //- Files to copy and filter
82  DynamicList<fileName> compileFiles_;
83 
84  //- Files to copy and filter
85  DynamicList<fileName> copyFiles_;
86 
87  //- Direct contents for files
88  DynamicList<fileAndContent> createFiles_;
89 
90  //- Variables to use during filtering
91  HashTable<string> filterVars_;
92 
93  //- Contents for Make/options
94  std::string makeOptions_;
95 
96 
97 protected:
98 
99  // Static Data Members
100 
101  //- Root of the LIB target for Make/files
102  static const char* const libTargetRoot;
103 
104  //- Top-level directory name for copy/compiling
105  static const char* const topDirName;
106 
107 
108  // Protected Member Functions
109 
110  //- Copy lines while expanding variables
111  static void copyAndFilter
112  (
113  ISstream&,
114  OSstream&,
115  const HashTable<string>& mapping
116  );
117 
118  //- Resolve code-templates via the codeTemplateEnvName
119  // alternatively in the codeTemplateDirName via Foam::findEtcFile
120  static bool resolveTemplates
121  (
122  const UList<fileName>& templateNames,
123  DynamicList<fileName>& resolvedFiles,
124  DynamicList<fileName>& badFiles
125  );
126 
127  //- Write SHA1 value as C-comment
128  bool writeCommentSHA1(Ostream&) const;
129 
130  //- Copy/create Make/files prior to compilation
131  bool createMakeFiles() const;
132 
133  //- Copy/create Make/options prior to compilation
134  bool createMakeOptions() const;
135 
136 
137  //- Write digest to Make/SHA1Digest
138  bool writeDigest(const SHA1Digest&) const;
139 
140  //- Write digest to Make/SHA1Digest
141  bool writeDigest(const std::string&) const;
142 
143 
144 public:
145 
146  // Static Data Members
147 
148  //- Name of the code template environment variable
149  // Used to located the codeTemplateName
150  static const word codeTemplateEnvName;
151 
152  //- Name of the code template sub-directory
153  // Used when locating the codeTemplateName via Foam::findEtcFile
154  static const fileName codeTemplateDirName;
155 
156  //- Flag if system operations are allowed
157  static int allowSystemOperations;
158 
159 
160  // Static Member functions
161 
162  //- Check security for creating dynamic code
163  static void checkSecurity(const char* title, const dictionary&);
164 
165  //- Return the library basename without leading 'lib' or trailing '.so'
166  static word libraryBaseName(const fileName& libPath);
167 
168 
169  // Constructors
170 
171  //- Construct for a specified code name and code directory name
172  // Defaults to using the code name for the code directory name
174  (
175  const word& codeName,
176  const word& codeDirName = ""
177  );
178 
179  //- Disallow default bitwise copy construction
180  dynamicCode(const dynamicCode&) = delete;
181 
182 
183  // Member Functions
184 
185  //- Return the code-name
186  const word& codeName() const
187  {
188  return codeName_;
189  }
190 
191  //- Return the code-dirname
192  const word& codeDirName() const
193  {
194  return codeDirName_;
195  }
196 
197  //- Root for dynamic code compilation
198  // Expanded from \$FOAM_CASE/dynamicCode
199  const fileName& codeRoot() const
200  {
201  return codeRoot_;
202  }
203 
204  //- Subdirectory name for loading libraries
205  // Expanded from platforms/\$WM_OPTIONS/lib
206  fileName libSubDir() const
207  {
208  return libSubDir_;
209  }
210 
211  //- Path for specified code name
212  // Corresponds to codeRoot()/codeDirName()
213  fileName codePath() const
214  {
215  return codeRoot_/codeDirName_;
216  }
217 
218  //- Library path for specified code name
219  // Corresponds to codeRoot()/libSubDir()/lib<codeName>.so
220  fileName libPath() const
221  {
222  return codeRoot_/libSubDir_/"lib" + codeName_ + ".so";
223  }
224 
225  //- Path for specified code name relative to \$FOAM_CASE
226  // Corresponds to topDirName/codeDirName()
227  fileName codeRelPath() const;
228 
229  //- Library path for specified code name relative to \$FOAM_CASE
230  // Corresponds to
231  // dynamicCode/codeDirName()/libSubDir()/lib<codeName>.so
232  fileName libRelPath() const;
233 
234  //- Path for SHA1Digest
235  // Corresponds to codePath()/Make/SHA1Digest
236  fileName digestFile() const
237  {
238  return codeRoot_/codeDirName_/"Make/SHA1Digest";
239  }
240 
241  //- Resolve code-template via the codeTemplateEnvName
242  // alternatively in the codeTemplateDirName via Foam::findEtcFile
243  static fileName resolveTemplate(const fileName& templateName);
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 a filter variable
262  void setFilterVariable(const word& key, const std::string& value);
263 
264  //- Define contents for Make/options
265  void setMakeOptions(const std::string& content);
266 
267 
268  //- Verify if the copied code is up-to-date, based on Make/SHA1Digest
269  bool upToDate(const dynamicCodeContext& context) const;
270 
271  //- Verify if the copied code is up-to-date, based on Make/SHA1Digest
272  bool upToDate(const SHA1Digest& sha1) const;
273 
274  //- Copy/create files prior to compilation
275  bool copyOrCreateFiles(const bool verbose = false) const;
276 
277  //- Compile a libso
278  bool wmakeLibso() const;
279 
280 
281  // Member Operators
282 
283  //- Disallow default bitwise assignment
284  void operator=(const dynamicCode&) = delete;
285 };
286 
287 
288 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
289 
290 } // End namespace Foam
291 
292 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
293 
294 #endif
295 
296 // ************************************************************************* //
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition: DynamicList.H:78
An STL-conforming hash table.
Definition: HashTable.H:127
Generic input stream.
Definition: ISstream.H:55
Generic output stream.
Definition: OSstream.H:54
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
The SHA1 message digest.
Definition: SHA1Digest.H:63
A 2-tuple for storing two objects of different types.
Definition: Tuple2.H:66
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:74
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:162
Encapsulation of dynamic code dictionaries.
Tools for handling dynamic code compilation.
Definition: dynamicCode.H:57
const word & codeName() const
Return the code-name.
Definition: dynamicCode.H:185
const word & codeDirName() const
Return the code-dirname.
Definition: dynamicCode.H:191
static int allowSystemOperations
Flag if system operations are allowed.
Definition: dynamicCode.H:156
static void copyAndFilter(ISstream &, OSstream &, const HashTable< string > &mapping)
Copy lines while expanding variables.
Definition: dynamicCode.C:105
dynamicCode(const word &codeName, const word &codeDirName="")
Construct for a specified code name and code directory name.
Definition: dynamicCode.C:323
fileName codePath() const
Path for specified code name.
Definition: dynamicCode.H:212
bool copyOrCreateFiles(const bool verbose=false) const
Copy/create files prior to compilation.
Definition: dynamicCode.C:423
fileName libPath() const
Library path for specified code name.
Definition: dynamicCode.H:219
void addCopyFile(const fileName &name)
Add a file template name, which will be found and filtered.
Definition: dynamicCode.C:391
const fileName & codeRoot() const
Root for dynamic code compilation.
Definition: dynamicCode.H:198
fileName libSubDir() const
Subdirectory name for loading libraries.
Definition: dynamicCode.H:205
void reset(const dynamicCodeContext &)
Clear files and reset variables to specified context.
Definition: dynamicCode.C:370
fileName libRelPath() const
Library path for specified code name relative to $FOAM_CASE.
Definition: dynamicCode.C:347
void addCreateFile(const fileName &name, const string &contents)
Add a file to create with its contents. Will not be filtered.
Definition: dynamicCode.C:398
static bool resolveTemplates(const UList< fileName > &templateNames, DynamicList< fileName > &resolvedFiles, DynamicList< fileName > &badFiles)
Resolve code-templates via the codeTemplateEnvName.
Definition: dynamicCode.C:171
static const word codeTemplateEnvName
Name of the code template environment variable.
Definition: dynamicCode.H:149
static const char *const topDirName
Top-level directory name for copy/compiling.
Definition: dynamicCode.H:104
void setFilterVariable(const word &key, const std::string &value)
Define a filter variable.
Definition: dynamicCode.C:408
static const char *const libTargetRoot
Root of the LIB target for Make/files.
Definition: dynamicCode.H:101
static word libraryBaseName(const fileName &libPath)
Return the library basename without leading 'lib' or trailing '.so'.
Definition: dynamicCode.C:93
static void checkSecurity(const char *title, const dictionary &)
Check security for creating dynamic code.
Definition: dynamicCode.C:58
void operator=(const dynamicCode &)=delete
Disallow default bitwise assignment.
bool createMakeFiles() const
Copy/create Make/files prior to compilation.
Definition: dynamicCode.C:230
fileName digestFile() const
Path for SHA1Digest.
Definition: dynamicCode.H:235
bool writeDigest(const SHA1Digest &) const
Write digest to Make/SHA1Digest.
Definition: dynamicCode.C:296
fileName codeRelPath() const
Path for specified code name relative to $FOAM_CASE.
Definition: dynamicCode.C:341
void addCompileFile(const fileName &name)
Add a file template name, which will be found and filtered.
Definition: dynamicCode.C:385
Tuple2< fileName, string > fileAndContent
Definition: dynamicCode.H:61
void clear()
Clear files and variables.
Definition: dynamicCode.C:353
static fileName resolveTemplate(const fileName &templateName)
Resolve code-template via the codeTemplateEnvName.
Definition: dynamicCode.C:143
bool upToDate(const dynamicCodeContext &context) const
Verify if the copied code is up-to-date, based on Make/SHA1Digest.
Definition: dynamicCode.C:549
static const fileName codeTemplateDirName
Name of the code template sub-directory.
Definition: dynamicCode.H:153
bool createMakeOptions() const
Copy/create Make/options prior to compilation.
Definition: dynamicCode.C:267
bool wmakeLibso() const
Compile a libso.
Definition: dynamicCode.C:520
bool writeCommentSHA1(Ostream &) const
Write SHA1 value as C-comment.
Definition: dynamicCode.C:216
void setMakeOptions(const std::string &content)
Define contents for Make/options.
Definition: dynamicCode.C:417
A class for handling file names.
Definition: fileName.H:82
A class for handling words, derived from string.
Definition: word.H:62
Namespace for OpenFOAM.
word name(const bool)
Return a word representation of a bool.
Definition: boolIO.C:39