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 | Copyright (C) 2011 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 public:
61 
62 private:
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
84  DynamicList<fileAndContent> createFiles_;
85 
86  //- Variables to use during filtering
87  HashTable<string> filterVars_;
88 
89  //- Contents for Make/options
90  std::string makeOptions_;
91 
92 
93  // Private Member Functions
94 
95  //- Disallow default bitwise copy construct
96  dynamicCode(const dynamicCode&);
97 
98  //- Disallow default bitwise assignment
99  void operator=(const dynamicCode&);
100 
101 
102 protected:
103 
104  // Static data members
105 
106  //- Root of the LIB target for Make/files
107  static const char* const libTargetRoot;
108 
109  //- Top-level directory name for copy/compiling
110  static const char* const topDirName;
111 
112 
113  // Protected Member Functions
114 
115  //- Copy lines while expanding variables
116  static void copyAndFilter
117  (
118  ISstream&,
119  OSstream&,
120  const HashTable<string>& mapping
121  );
122 
123  //- Resolve code-templates via the codeTemplateEnvName
124  // alternatively in the codeTemplateDirName via Foam::findEtcFile
125  static bool resolveTemplates
126  (
127  const UList<fileName>& templateNames,
128  DynamicList<fileName>& resolvedFiles,
129  DynamicList<fileName>& badFiles
130  );
131 
132  //- Write SHA1 value as C-comment
133  bool writeCommentSHA1(Ostream&) const;
134 
135  //- Copy/create Make/files prior to compilation
136  bool createMakeFiles() const;
137 
138  //- Copy/create Make/options prior to compilation
139  bool createMakeOptions() const;
140 
141 
142  //- Write digest to Make/SHA1Digest
143  bool writeDigest(const SHA1Digest&) const;
144 
145  //- Write digest to Make/SHA1Digest
146  bool writeDigest(const std::string&) const;
147 
148 
149 public:
150 
151  // Static data members
152 
153  //- Name of the code template environment variable
154  // Used to located the codeTemplateName
155  static const word codeTemplateEnvName;
156 
157  //- Name of the code template sub-directory
158  // Used when locating the codeTemplateName via Foam::findEtcFile
159  static const fileName codeTemplateDirName;
160 
161  //- Flag if system operations are allowed
162  static int allowSystemOperations;
163 
164 
165  // Static Member functions
166 
167  //- Check security for creating dynamic code
168  static void checkSecurity(const char* title, const dictionary&);
169 
170  //- Return the library basename without leading 'lib' or trailing '.so'
171  static word libraryBaseName(const fileName& libPath);
172 
173 
174  // Constructors
175 
176  //- Construct for a specified code name and code directory name
177  // Defaults to using the code name for the code directory name
179  (
180  const word& codeName,
181  const word& codeDirName = ""
182  );
183 
184 
185  // Member functions
186 
187  //- Return the code-name
188  const word& codeName() const
189  {
190  return codeName_;
191  }
192 
193  //- Return the code-dirname
194  const word& codeDirName() const
195  {
196  return codeDirName_;
197  }
198 
199  //- Root for dynamic code compilation
200  // Expanded from \$FOAM_CASE/dynamicCode
201  const fileName& codeRoot() const
202  {
203  return codeRoot_;
204  }
205 
206  //- Subdirectory name for loading libraries
207  // Expanded from platforms/\$WM_OPTIONS/lib
208  fileName libSubDir() const
209  {
210  return libSubDir_;
211  }
212 
213  //- Path for specified code name
214  // Corresponds to codeRoot()/codeDirName()
215  fileName codePath() const
216  {
217  return codeRoot_/codeDirName_;
218  }
219 
220  //- Library path for specified code name
221  // Corresponds to codeRoot()/libSubDir()/lib<codeName>.so
222  fileName libPath() const
223  {
224  return codeRoot_/libSubDir_/"lib" + codeName_ + ".so";
225  }
226 
227  //- Path for specified code name relative to \$FOAM_CASE
228  // Corresponds to topDirName/codeDirName()
229  fileName codeRelPath() const;
230 
231 
232  //- Library path for specified code name relative to \$FOAM_CASE
233  // Corresponds to
234  // dynamicCode/codeDirName()/libSubDir()/lib<codeName>.so
235  fileName libRelPath() const;
236 
237 
238  //- Path for SHA1Digest
239  // Corresponds to codePath()/Make/SHA1Digest
240  fileName digestFile() const
241  {
242  return codeRoot_/codeDirName_/"Make/SHA1Digest";
243  }
244 
245 
246  //- Clear files and variables
247  void clear();
248 
249  //- Clear files and reset variables to specified context
250  void reset(const dynamicCodeContext&);
251 
252 
253  //- Add a file template name, which will be found and filtered
254  void addCompileFile(const fileName& name);
255 
256  //- Add a file template name, which will be found and filtered
257  void addCopyFile(const fileName& name);
258 
259  //- Add a file to create with its contents. Will not be filtered
260  void addCreateFile(const fileName& name, const string& contents);
261 
262  //- Define filter variables for code, codeInclude, SHA1sum
264 
265  //- Define a filter variable
266  void setFilterVariable(const word& key, const std::string& value);
267 
268  //- Define contents for Make/options
269  void setMakeOptions(const std::string& content);
270 
271 
272  //- Verify if the copied code is up-to-date, based on Make/SHA1Digest
273  bool upToDate(const dynamicCodeContext& context) const;
274 
275  //- Verify if the copied code is up-to-date, based on Make/SHA1Digest
276  bool upToDate(const SHA1Digest& sha1) const;
277 
278  //- Copy/create files prior to compilation
279  bool copyOrCreateFiles(const bool verbose = false) const;
280 
281  //- Compile a libso
282  bool wmakeLibso() const;
283 
284 };
285 
286 
287 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
288 
289 } // End namespace Foam
290 
291 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
292 
293 #endif
294 
295 // ************************************************************************* //
bool upToDate(const dynamicCodeContext &context) const
Verify if the copied code is up-to-date, based on Make/SHA1Digest.
Definition: dynamicCode.C:524
Generic output stream.
Definition: OSstream.H:51
const word & codeName() const
Return the code-name.
Definition: dynamicCode.H:187
A class for handling file names.
Definition: fileName.H:69
bool copyOrCreateFiles(const bool verbose=false) const
Copy/create files prior to compilation.
Definition: dynamicCode.C:400
fileName codePath() const
Path for specified code name.
Definition: dynamicCode.H:214
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:137
void setMakeOptions(const std::string &content)
Define contents for Make/options.
Definition: dynamicCode.C:394
A 2-tuple for storing two objects of different types.
Definition: Tuple2.H:47
fileName libPath() const
Library path for specified code name.
Definition: dynamicCode.H:221
bool writeCommentSHA1(Ostream &) const
Write SHA1 value as C-comment.
Definition: dynamicCode.C:187
static const char *const libTargetRoot
Root of the LIB target for Make/files.
Definition: dynamicCode.H:106
static bool resolveTemplates(const UList< fileName > &templateNames, DynamicList< fileName > &resolvedFiles, DynamicList< fileName > &badFiles)
Resolve code-templates via the codeTemplateEnvName.
Definition: dynamicCode.C:142
The SHA1 message digest.
Definition: SHA1Digest.H:62
void setFilterContext(const dynamicCodeContext &)
Define filter variables for code, codeInclude, SHA1sum.
Definition: dynamicCode.C:373
static const word codeTemplateEnvName
Name of the code template environment variable.
Definition: dynamicCode.H:154
static const char *const topDirName
Top-level directory name for copy/compiling.
Definition: dynamicCode.H:109
void addCompileFile(const fileName &name)
Add a file template name, which will be found and filtered.
Definition: dynamicCode.C:350
const fileName & codeRoot() const
Root for dynamic code compilation.
Definition: dynamicCode.H:200
bool createMakeOptions() const
Copy/create Make/options prior to compilation.
Definition: dynamicCode.C:238
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects...
Definition: DynamicList.H:56
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:158
bool writeDigest(const SHA1Digest &) const
Write digest to Make/SHA1Digest.
Definition: dynamicCode.C:267
static int allowSystemOperations
Flag if system operations are allowed.
Definition: dynamicCode.H:161
static void copyAndFilter(ISstream &, OSstream &, const HashTable< string > &mapping)
Copy lines while expanding variables.
Definition: dynamicCode.C:105
fileName digestFile() const
Path for SHA1Digest.
Definition: dynamicCode.H:239
void addCreateFile(const fileName &name, const string &contents)
Add a file to create with its contents. Will not be filtered.
Definition: dynamicCode.C:363
const word & codeDirName() const
Return the code-dirname.
Definition: dynamicCode.H:193
An STL-conforming hash table.
Definition: HashTable.H:61
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
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
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
fileName codeRelPath() const
Path for specified code name relative to $FOAM_CASE.
Definition: dynamicCode.C:312
bool createMakeFiles() const
Copy/create Make/files prior to compilation.
Definition: dynamicCode.C:201
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:385
bool wmakeLibso() const
Compile a libso.
Definition: dynamicCode.C:495
void addCopyFile(const fileName &name)
Add a file template name, which will be found and filtered.
Definition: dynamicCode.C:356
fileName libSubDir() const
Subdirectory name for loading libraries.
Definition: dynamicCode.H:207
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
fileName libRelPath() const
Library path for specified code name relative to $FOAM_CASE.
Definition: dynamicCode.C:318
Namespace for OpenFOAM.
Tuple2< fileName, string > fileAndContent
Definition: dynamicCode.H:59