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-2026 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  Encapsulation of dynamic code dictionaries and functionality
29 
30 SourceFiles
31  dynamicCode.C
32 
33 \*---------------------------------------------------------------------------*/
34 
35 #ifndef dynamicCode_H
36 #define dynamicCode_H
37 
38 #include "dictionary.H"
39 #include "HashTable.H"
40 #include "SHA1Digest.H"
41 
42 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
43 
44 namespace Foam
45 {
46 
47 /*---------------------------------------------------------------------------*\
48  Class dynamicCode Declaration
49 \*---------------------------------------------------------------------------*/
50 
51 class dynamicCode
52 {
53  // Private Data
54 
55  //- Top-level directory name for copy/compiling
56  static const word topDirName;
57 
58  //- Name of the code template sub-directory
59  // Used when locating the codeTemplateName via Foam::findEtcFile
60  static const fileName codeTemplateDirName;
61 
62  //- Root of the LIB target for Make/files
63  static const char* const libTargetRoot;
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  //- Unique name for code with the SHA1 appendedm
75  word codeSha1Name_;
76 
77  //- Name for code subdirectory
78  word codeDirName_;
79 
80  //- Keywords associated with source code
81  const wordList& codeKeys_;
82 
83  //- Name of the dictionary variables in the source code
84  const wordList& codeDictVars_;
85 
86  //- Options file name
87  const word& optionsFileName_;
88 
89  //- Files to copy and filter and compile
90  const wordList& compileFiles_;
91 
92  //- Files to copy and filter
93  const wordList& copyFiles_;
94 
95  //- Code strings
96  // Stored to write for restart
97  List<verbatimString> codeStrings_;
98 
99  //- Variable substitutions
100  HashTable<string> varSubstitutions_;
101 
102  //- Code key substitutions
103  HashTable<string> codeKeySubstitutions_;
104 
105  //- Optional "codeOptions" string
106  // Stored to write for restart
107  verbatimString optionsString_;
108 
109  //- Optional "codeOptions" entry
110  string options_;
111 
112  //- Optional "codeLibs" string
113  // Stored to write for restart
114  verbatimString libsString_;
115 
116  //- Optional "codeLibs" entry
117  string libs_;
118 
119  //- Calculated SHA1Digest
120  SHA1Digest sha1_;
121 
122 
123  // Private Member Functions
124 
125  //- Path for SHA1Digest
126  // Corresponds to codePath()/Make/SHA1Digest
127  fileName digestFile() const
128  {
129  return codeRoot_/codeDirName_/"Make/SHA1Digest";
130  }
131 
132  //- Path for specified code name
133  // Corresponds to codeRoot()/codeDirName()
134  fileName codePath() const
135  {
136  return codeRoot_/codeDirName_;
137  }
138 
139  //- Path for specified code name relative to \$FOAM_CASE
140  // Corresponds to topDirName/codeDirName()
141  fileName codeRelPath() const
142  {
143  return topDirName/codeDirName_;
144  }
145 
146  //- Add a \#line directive to the start of the given source string that
147  // compilation messages are meaningful
148  static void addLineDirective
149  (
150  string&,
151  const label lineNum,
152  const fileName& name
153  );
154 
155  //- Copy lines while expanding variables
156  static void copyAndFilter
157  (
158  ISstream&,
159  OSstream&,
160  const HashTable<string>& mapping
161  );
162 
163  //- Resolve code-templates via Foam::findConfigFile
164  static bool resolveTemplates
165  (
166  const wordList& templateNames,
167  DynamicList<fileName>& resolvedFiles,
168  DynamicList<fileName>& badFiles
169  );
170 
171  //- Copy/create Make/files prior to compilation
172  bool createMakeFiles() const;
173 
174  //- Copy/create Make/options prior to compilation
175  bool createMakeOptions() const;
176 
177  //- Write digest to Make/SHA1Digest
178  bool writeDigest() const;
179 
180 
181 public:
182 
183  // Static Data Members
184 
185  // Declare name of the class and its debug switch
186  ClassName("dynamicCode");
187 
188  //- Flag if system operations are allowed
189  static int allowSystemOperations;
190 
191 
192  // Constructors
193 
194  //- Construct from the context and code dictionaries
195  // and lists of which entries correspond to code
197  (
198  const dictionary& contextDict,
199  const dictionary& codeDict,
200  const word& codeName,
201  const word& codeDirName,
202  const wordList& codeKeys,
203  const wordList& codeDictVars,
204  const word& optionsFileName,
205  const wordList& compileFiles,
206  const wordList& copyFiles
207  );
208 
209  //- Construct from the context dictionary also containing the code
210  // and lists of which entries correspond
212  (
213  const dictionary& contextDict,
214  const word& codeName,
215  const word& codeDirName,
216  const wordList& codeKeys,
217  const wordList& codeDictVars,
218  const word& optionsFileName,
219  const wordList& compileFiles,
220  const wordList& copyFiles
221  );
222 
223 
224  // Member Functions
225 
226  //- Return the code-name
227  const word& codeName() const
228  {
229  return codeName_;
230  }
231 
232  //- Return the unique code name
233  const word& codeSha1Name() const
234  {
235  return codeSha1Name_;
236  }
237 
238  //- Library path for specified code name
239  // Corresponds to codeRoot()/libSubDir()/lib<codeSha1Name>.so
240  fileName libPath() const
241  {
242  return codeRoot_/libSubDir_/"lib" + codeSha1Name_ + ".so";
243  }
244 
245  //- Library path for specified code name relative to \$FOAM_CASE
246  // Corresponds to
247  // dynamicCode/codeDirName()/libSubDir()/lib<codeSha1Name>.so
248  fileName libRelPath() const
249  {
250  return codeRelPath()/libSubDir_/"lib" + codeSha1Name_ + ".so";
251  }
252 
254  {
255  return varSubstitutions_;
256  }
257 
258  //- Return the library basename without leading 'lib' or trailing '.so'
259  static word libraryBaseName(const fileName& libPath);
260 
261  //- Resolve code-template via Foam::findConfigFile
262  static fileName resolveTemplate(const fileName& templateName);
263 
264  //- Verify if the copied code is up-to-date, based on Make/SHA1Digest
265  bool upToDate() const;
266 
267  //- Copy/create files prior to compilation
268  bool copyOrCreateFiles(const bool verbose = false) const;
269 
270  //- Compile a libso
271  bool wmakeLibso() const;
272 
273  void* loadLibrary(const fileName& libPath) const;
274 
275  void createLibrary
276  (
277  const dictionary& dict,
278  const bool masterOnlyRead = false
279  ) const;
280 
281  void read(const dictionary& contextDict, const dictionary& codeDict);
282 
283  void read(const dictionary& contextDict);
284 
285  //- Write the code for restart
286  void write(Ostream& os) const;
287 };
288 
289 
290 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
291 
292 } // End namespace Foam
293 
294 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
295 
296 #endif
297 
298 // ************************************************************************* //
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 list of keywords followed by any number of values (e.g. words and numbers) or sub-dictionaries.
Definition: dictionary.H:162
Encapsulation of dynamic code dictionaries and functionality.
Definition: dynamicCode.H:51
const word & codeName() const
Return the code-name.
Definition: dynamicCode.H:226
dynamicCode(const dictionary &contextDict, const dictionary &codeDict, const word &codeName, const word &codeDirName, const wordList &codeKeys, const wordList &codeDictVars, const word &optionsFileName, const wordList &compileFiles, const wordList &copyFiles)
Construct from the context and code dictionaries.
Definition: dynamicCode.C:284
static int allowSystemOperations
Flag if system operations are allowed.
Definition: dynamicCode.H:188
bool upToDate() const
Verify if the copied code is up-to-date, based on Make/SHA1Digest.
Definition: dynamicCode.C:600
bool copyOrCreateFiles(const bool verbose=false) const
Copy/create files prior to compilation.
Definition: dynamicCode.C:494
fileName libPath() const
Library path for specified code name.
Definition: dynamicCode.H:239
HashTable< string > & varSubstitutions()
Definition: dynamicCode.H:252
fileName libRelPath() const
Library path for specified code name relative to $FOAM_CASE.
Definition: dynamicCode.H:247
void write(Ostream &os) const
Write the code for restart.
Definition: dynamicCode.C:744
void * loadLibrary(const fileName &libPath) const
Definition: dynamicCode.C:613
static word libraryBaseName(const fileName &libPath)
Return the library basename without leading 'lib' or trailing '.so'.
Definition: dynamicCode.C:472
const word & codeSha1Name() const
Return the unique code name.
Definition: dynamicCode.H:232
static fileName resolveTemplate(const fileName &templateName)
Resolve code-template via Foam::findConfigFile.
Definition: dynamicCode.C:481
ClassName("dynamicCode")
bool wmakeLibso() const
Compile a libso.
Definition: dynamicCode.C:585
void createLibrary(const dictionary &dict, const bool masterOnlyRead=false) const
Definition: dynamicCode.C:630
void read(const dictionary &contextDict, const dictionary &codeDict)
Definition: dynamicCode.C:386
A class for handling file names.
Definition: fileName.H:82
A class for handling verbatimStrings, derived from string.
A class for handling words, derived from string.
Definition: word.H:63
Namespace for OpenFOAM.
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
word name(const LagrangianState state)
Return a string representation of a Lagrangian state enumeration.
dictionary dict