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-2019 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 
230  //- Library path for specified code name relative to \$FOAM_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 a filter variable
261  void setFilterVariable(const word& key, const std::string& value);
262 
263  //- Define contents for Make/options
264  void setMakeOptions(const std::string& content);
265 
266 
267  //- Verify if the copied code is up-to-date, based on Make/SHA1Digest
268  bool upToDate(const dynamicCodeContext& context) const;
269 
270  //- Verify if the copied code is up-to-date, based on Make/SHA1Digest
271  bool upToDate(const SHA1Digest& sha1) const;
272 
273  //- Copy/create files prior to compilation
274  bool copyOrCreateFiles(const bool verbose = false) const;
275 
276  //- Compile a libso
277  bool wmakeLibso() const;
278 
279 
280  // Member Operators
281 
282  //- Disallow default bitwise assignment
283  void operator=(const dynamicCode&) = delete;
284 };
285 
286 
287 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
288 
289 } // End namespace Foam
290 
291 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
292 
293 #endif
294 
295 // ************************************************************************* //
fileName libSubDir() const
Subdirectory name for loading libraries.
Definition: dynamicCode.H:205
Generic output stream.
Definition: OSstream.H:51
bool writeDigest(const SHA1Digest &) const
Write digest to Make/SHA1Digest.
Definition: dynamicCode.C:267
const word & codeDirName() const
Return the code-dirname.
Definition: dynamicCode.H:191
A class for handling file names.
Definition: fileName.H:79
void reset(const dynamicCodeContext &)
Clear files and reset variables to specified context.
Definition: dynamicCode.C:341
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:158
void setMakeOptions(const std::string &content)
Define contents for Make/options.
Definition: dynamicCode.C:388
const word & codeName() const
Return the code-name.
Definition: dynamicCode.H:185
A 2-tuple for storing two objects of different types.
Definition: HashTable.H:65
static const char *const libTargetRoot
Root of the LIB target for Make/files.
Definition: dynamicCode.H:101
static bool resolveTemplates(const UList< fileName > &templateNames, DynamicList< fileName > &resolvedFiles, DynamicList< fileName > &badFiles)
Resolve code-templates via the codeTemplateEnvName.
Definition: dynamicCode.C:142
const fileName & codeRoot() const
Root for dynamic code compilation.
Definition: dynamicCode.H:198
The SHA1 message digest.
Definition: SHA1Digest.H:62
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 addCompileFile(const fileName &name)
Add a file template name, which will be found and filtered.
Definition: dynamicCode.C:356
bool writeCommentSHA1(Ostream &) const
Write SHA1 value as C-comment.
Definition: dynamicCode.C:187
bool createMakeOptions() const
Copy/create Make/options prior to compilation.
Definition: dynamicCode.C:238
fileName codeRelPath() const
Path for specified code name relative to $FOAM_CASE.
Definition: dynamicCode.C:312
fileName digestFile() const
Path for SHA1Digest.
Definition: dynamicCode.H:237
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects...
Definition: DynamicList.H:56
fileName libRelPath() const
Library path for specified code name relative to $FOAM_CASE.
Definition: dynamicCode.C:318
A class for handling words, derived from string.
Definition: word.H:59
static const fileName codeTemplateDirName
Name of the code template sub-directory.
Definition: dynamicCode.H:153
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:294
void addCreateFile(const fileName &name, const string &contents)
Add a file to create with its contents. Will not be filtered.
Definition: dynamicCode.C:369
bool wmakeLibso() const
Compile a libso.
Definition: dynamicCode.C:489
An STL-conforming hash table.
Definition: HashTable.H:61
void operator=(const dynamicCode &)=delete
Disallow default bitwise 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:60
fileName libPath() const
Library path for specified code name.
Definition: dynamicCode.H:219
fileName codePath() const
Path for specified code name.
Definition: dynamicCode.H:212
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:54
bool upToDate(const dynamicCodeContext &context) const
Verify if the copied code is up-to-date, based on Make/SHA1Digest.
Definition: dynamicCode.C:518
bool copyOrCreateFiles(const bool verbose=false) const
Copy/create files prior to compilation.
Definition: dynamicCode.C:394
static word libraryBaseName(const fileName &libPath)
Return the library basename without leading &#39;lib&#39; or trailing &#39;.so&#39;.
Definition: dynamicCode.C:93
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
Tools for handling dynamic code compilation.
Definition: dynamicCode.H:56
Encapsulation of dynamic code dictionaries.
Generic input stream.
Definition: ISstream.H:51
void setFilterVariable(const word &key, const std::string &value)
Define a filter variable.
Definition: dynamicCode.C:379
void addCopyFile(const fileName &name)
Add a file template name, which will be found and filtered.
Definition: dynamicCode.C:362
void clear()
Clear files and variables.
Definition: dynamicCode.C:324
static void checkSecurity(const char *title, const dictionary &)
Check security for creating dynamic code.
Definition: dynamicCode.C:58
bool createMakeFiles() const
Copy/create Make/files prior to compilation.
Definition: dynamicCode.C:201
Namespace for OpenFOAM.
Tuple2< fileName, string > fileAndContent
Definition: dynamicCode.H:61