1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
7 -------------------------------------------------------------------------------
9 This file is part of OpenFOAM.
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.
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
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/>.
28 Tools for handling dynamic code compilation
33 \*---------------------------------------------------------------------------*/
39 #include "HashTable.H"
40 #include "DynamicList.H"
42 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
47 // Forward declaration of classes
48 class dynamicCodeContext;
53 /*---------------------------------------------------------------------------*\
54 Class dynamicCode Declaration
55 \*---------------------------------------------------------------------------*/
60 typedef Tuple2<fileName, string> fileAndContent;
65 //- Root for dynamic code compilation
68 //- Subdirectory name for loading libraries
69 const fileName libSubDir_;
74 //- Name for code subdirectory
77 //- Files to copy and filter
78 DynamicList<fileName> compileFiles_;
80 //- Files to copy and filter
81 DynamicList<fileName> copyFiles_;
83 //- Direct contents for files
84 DynamicList<fileAndContent> createFiles_;
86 //- Variables to use during filtering
87 HashTable<string> filterVars_;
89 //- Contents for Make/options
90 std::string makeOptions_;
93 // Private Member Functions
95 //- Disallow default bitwise copy construct
96 dynamicCode(const dynamicCode&);
98 //- Disallow default bitwise assignment
99 void operator=(const dynamicCode&);
104 // Static data members
106 //- Root of the LIB target for Make/files
107 static const char* const libTargetRoot;
109 //- Top-level directory name for copy/compiling
110 static const char* const topDirName;
113 // Protected Member Functions
115 //- Copy lines while expanding variables
116 static void copyAndFilter
120 const HashTable<string>& mapping
123 //- Resolve code-templates via the codeTemplateEnvName
124 // alternatively in the codeTemplateDirName via Foam::findEtcFile
125 static bool resolveTemplates
127 const UList<fileName>& templateNames,
128 DynamicList<fileName>& resolvedFiles,
129 DynamicList<fileName>& badFiles
132 //- Write SHA1 value as C-comment
133 bool writeCommentSHA1(Ostream&) const;
135 //- Copy/create Make/files prior to compilation
136 bool createMakeFiles() const;
138 //- Copy/create Make/options prior to compilation
139 bool createMakeOptions() const;
142 //- Write digest to Make/SHA1Digest
143 bool writeDigest(const SHA1Digest&) const;
145 //- Write digest to Make/SHA1Digest
146 bool writeDigest(const std::string&) const;
151 // Static data members
153 //- Name of the code template environment variable
154 // Used to located the codeTemplateName
155 static const word codeTemplateEnvName;
157 //- Name of the code template sub-directory
158 // Used when locating the codeTemplateName via Foam::findEtcFile
159 static const fileName codeTemplateDirName;
161 //- Flag if system operations are allowed
162 static int allowSystemOperations;
165 // Static Member functions
167 //- Check security for creating dynamic code
168 static void checkSecurity(const char* title, const dictionary&);
170 //- Return the library basename without leading 'lib' or trailing '.so'
171 static word libraryBaseName(const fileName& libPath);
176 //- Construct for a specified code name and code directory name
177 // Defaults to using the code name for the code directory name
180 const word& codeName,
181 const word& codeDirName = ""
187 //- Return the code-name
188 const word& codeName() const
193 //- Return the code-dirname
194 const word& codeDirName() const
199 //- Root for dynamic code compilation
200 // Expanded from \$FOAM_CASE/dynamicCode
201 const fileName& codeRoot() const
206 //- Subdirectory name for loading libraries
207 // Expanded from platforms/\$WM_OPTIONS/lib
208 fileName libSubDir() const
213 //- Path for specified code name
214 // Corresponds to codeRoot()/codeDirName()
215 fileName codePath() const
217 return codeRoot_/codeDirName_;
220 //- Library path for specified code name
221 // Corresponds to codeRoot()/libSubDir()/lib\<codeName\>.so
222 fileName libPath() const
224 return codeRoot_/libSubDir_/"lib" + codeName_ + ".so";
227 //- Path for specified code name relative to \$FOAM_CASE
228 // Corresponds to topDirName/codeDirName()
229 fileName codeRelPath() const;
232 //- Library path for specified code name relative to \$FOAM_CASE
234 // dynamicCode/codeDirName()/libSubDir()/lib\<codeName\>.so
235 fileName libRelPath() const;
238 //- Path for SHA1Digest
239 // Corresponds to codePath()/Make/SHA1Digest
240 fileName digestFile() const
242 return codeRoot_/codeDirName_/"Make/SHA1Digest";
246 //- Clear files and variables
249 //- Clear files and reset variables to specified context
250 void reset(const dynamicCodeContext&);
253 //- Add a file template name, which will be found and filtered
254 void addCompileFile(const fileName& name);
256 //- Add a file template name, which will be found and filtered
257 void addCopyFile(const fileName& name);
259 //- Add a file to create with its contents. Will not be filtered
260 void addCreateFile(const fileName& name, const string& contents);
262 //- Define filter variables for code, codeInclude, SHA1sum
263 void setFilterContext(const dynamicCodeContext&);
265 //- Define a filter variable
266 void setFilterVariable(const word& key, const std::string& value);
268 //- Define contents for Make/options
269 void setMakeOptions(const std::string& content);
272 //- Verify if the copied code is up-to-date, based on Make/SHA1Digest
273 bool upToDate(const dynamicCodeContext& context) const;
275 //- Verify if the copied code is up-to-date, based on Make/SHA1Digest
276 bool upToDate(const SHA1Digest& sha1) const;
278 //- Copy/create files prior to compilation
279 bool copyOrCreateFiles(const bool verbose = false) const;
282 bool wmakeLibso() const;
287 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
289 } // End namespace Foam
291 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
295 // ************************************************************************* //