OpenASIP 2.2
Loading...
Searching...
No Matches
OperationBuilder.cc
Go to the documentation of this file.
1/*
2 Copyright (c) 2002-2013 Tampere University.
3
4 This file is part of TTA-Based Codesign Environment (TCE).
5
6 Permission is hereby granted, free of charge, to any person obtaining a
7 copy of this software and associated documentation files (the "Software"),
8 to deal in the Software without restriction, including without limitation
9 the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 and/or sell copies of the Software, and to permit persons to whom the
11 Software is furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 DEALINGS IN THE SOFTWARE.
23 */
24/**
25 * @file OperationBuilder.cc
26 *
27 * Definition of OperationBuilder class.
28 *
29 * @author Jussi Nykänen 2004 (nykanen-no.spam-cs.tut.fi)
30 * @author Pekka Jääskeläinen 2005-2013 (pjaaskel-no.spam-cs.tut.fi)
31 * @note rating: red
32 */
33
34#include <iostream>
35
36#include "OperationBuilder.hh"
37#include "Environment.hh"
38#include "FileSystem.hh"
39#include "tce_config.h"
41#include "ObjectState.hh"
42#include "Application.hh"
43
44using std::vector;
45using std::string;
46using std::cerr;
47using std::endl;
48
49const string SCHEMA_FILE_NAME = "Operation_Schema.xsd";
50
52
53/**
54 * Constructor.
55 */
58
59/**
60 * Destructor.
61 */
64
65/**
66 * Returns an instance of OperationBuilder.
67 *
68 * @return An instance of OperationBuilder.
69 */
72 if (instance_ == NULL) {
74 }
75 return *instance_;
76}
77
78/**
79 * Finds the path for XML file.
80 *
81 * File is searched only from certain locations. If file is not found,
82 * returns an empty string.
83 *
84 * @param xmlFile The name of the XML file.
85 * @return The path to XML file.
86 */
87string
88OperationBuilder::xmlFilePath(const std::string& xmlFile) {
89
90 vector<string> searchPaths = Environment::osalPaths();
91 searchPaths.push_back(FileSystem::currentWorkingDir());
92
93 string filePath = "";
94 for (unsigned int i = 0; i < searchPaths.size(); i++) {
95 string file = searchPaths[i] + FileSystem::DIRECTORY_SEPARATOR +
96 xmlFile;
97 if (FileSystem::fileExists(file)) {
98 filePath = searchPaths[i];
99 break;
100 }
101 }
102 return filePath;
103}
104
105/**
106 * Searches for the path of behavior file.
107 *
108 * If path for the file is not given, it is searched from the current
109 * working directory. If file is not found, an empty string is returned.
110 *
111 * @param baseName The name of behavior file.
112 * @param path The path where the file is located.
113 */
114string
116 const std::string& baseName,
117 std::string& path) {
118
119 string behFile = baseName + ".cc";
120 if (path == "") {
122 }
123 string pathToFile = path + FileSystem::DIRECTORY_SEPARATOR + behFile;
124 if (FileSystem::fileExists(pathToFile)) {
125 return pathToFile;
126 }
127 return "";
128}
129
130/**
131 * Builds dynamic module and copies it to the same location where
132 * XML property file is located.
133 *
134 * @param baseName The base name for the module.
135 * @param behaviorFile The full path for behavior file.
136 * @param path Path where built module is put.
137 * @param output Output of compilation.
138 * @return True if everything is successful, false otherwise.
139 */
140bool
142 const std::string& baseName,
143 const std::string& behaviorFile,
144 const std::string& path,
145 std::vector<std::string>& output) {
146
147 if (behaviorFile != "") {
148 TCEString CPPFLAGS = Environment::environmentVariable("CPPFLAGS");
149 TCEString CXXFLAGS = Environment::environmentVariable("CXXFLAGS")
150 + " " + CONFIGURE_CPPFLAGS + " ";
152 + " " + CONFIGURE_LDFLAGS + " ";
153 TCEString CXXCOMPILER = Environment::environmentVariable("CXX");
154 vector<string> includes = Environment::includeDirPaths();
155
156 TCEString INCLUDES = makeIncludeString(includes);
157
158 // Ugly fix. When compiling TCE the base opset is compiled in the
159 // source directory. When distributed version is enabled buildopset
160 // needs to know some include paths from the source tree because these
161 // headers aren't resident in places that includeDirPaths() returns.
163 string cwd = FileSystem::currentWorkingDir();
165 string srcOpsetDir = TCE_SRC_ROOT + DS + "opset" + DS + "base";
166 if (cwd == srcOpsetDir) {
167 vector<string> extraIncludes = Environment::opsetIncludeDir();
168 INCLUDES += makeIncludeString(extraIncludes);
169 }
170 }
171
173 /* Add a dependency to the installed libopenasip.so just in case
174 the .opb will be loaded through a libopenasip.so that is loaded
175 through a dlopen() with the RTLD_LOCAL flag. In that case
176 it can happen the loading of libopenasip.so to access some
177 specific functionality might not import all the symbols
178 required by the .opb loaded by libopenasip later. */
179 LDFLAGS += " -L" + Application::installationDir() + "/lib -lopenasip ";
180 } else {
181 LDFLAGS += " -L" + string(TCE_SRC_ROOT) + "/src/.libs -lopenasip ";
182 }
183
184 // Add user defined CXXFLAGS + CPPFLAGS to the end because they
185 // might point to paths with incompatible TCE headers.
186 string COMPILE_FLAGS = \
187 INCLUDES + " " +
188 CONFIGURE_CPPFLAGS + " " + CONFIGURE_LDFLAGS + " " +
189 CXXFLAGS + " " + CPPFLAGS + " ";
190
191 string module = path + FileSystem::DIRECTORY_SEPARATOR +
192 baseName + ".opb";
193
194 string command = (CXXCOMPILER == "") ? (string(CXX)) : (CXXCOMPILER);
195 command += " " + COMPILE_FLAGS + " " + behaviorFile + " " +
196 string(SHARED_CXX_FLAGS) + " " + LDFLAGS + " -o " + module + " 2>&1";
197
199 Application::logStream() << command << std::endl;
200 }
201
202 if (Application::runShellCommandAndGetOutput(command, output) != 0) {
203 return false;
204 }
205 }
206 return true;
207}
208
209/**
210 * Installs the data file containing operation property declarations.
211 *
212 * Creates the destination path if it does not exist. Overwrites the
213 * existing file, if it is already installed in given destination.
214 *
215 * Installation can fail for several reasons: no rights to create
216 * destination directory, no rights to overwrite a previously installed
217 * version of the data file, no rights to read the source data file.
218 *
219 * @param path Source path of data file.
220 * @param xmlFile Name of the data file.
221 * @param destination Destination path where the data file is to be
222 * installed.
223 * @return True, if installation is completed successfully, false otherwise.
224 */
225bool
227 const std::string& sourcePath,
228 const std::string& xmlFile,
229 const std::string& destinationPath) {
230
231 const string source =
232 sourcePath + FileSystem::DIRECTORY_SEPARATOR + xmlFile;
233
234 // an unreadable or nonexisting file should have been caught earlier on
235 // (for example, during validation)
238
239 if (!FileSystem::fileExists(destinationPath)) {
240 if (!FileSystem::createDirectory(destinationPath)) {
241 return false;
242 }
243 }
244
245 const string dest =
246 destinationPath + FileSystem::DIRECTORY_SEPARATOR + xmlFile;
247
248 if (FileSystem::fileExists(dest)) {
250 return false;
251 }
252 }
253
254 FileSystem::copy(source, dest);
255 return true;
256}
257
258
259/**
260 * Verifies the correctness of XML file.
261 *
262 * @param file Absolute path of XML file.
263 */
264bool
265OperationBuilder::verifyXML(const std::string file) {
266 OperationSerializer serializer;
267 serializer.setSourceFile(file);
268
269 try {
270 ObjectState* root = serializer.readState();
271 delete root;
272 } catch (const SerializerException& s) {
273 cerr << "Invalid XML file: " << s.errorMessage() << endl;
274 return false;
275 }
276 return true;
277}
278
279/**
280 * Makes an include string for compiler (-I<path1> -I<path2> etc) from the
281 * paths in the vector
282 *
283 * @param Vector containing the include paths
284 */
285string
286OperationBuilder::makeIncludeString(const vector<string>& paths) {
287 string includes = "";
288 for (size_t i = 0; i < paths.size(); i++) {
289 includes += "-I" + paths.at(i) + " ";
290 }
291 return includes;
292}
#define assert(condition)
#define DS
const string SCHEMA_FILE_NAME
static bool isInstalled()
static int runShellCommandAndGetOutput(const std::string &command, std::vector< std::string > &outputLines, std::size_t maxOutputLines=DEFAULT_MAX_OUTPUT_LINES, bool includeStdErr=false)
static std::string installationDir()
static const int VERBOSE_LEVEL_DEFAULT
Default verbose level - do not print anything unnecessary.
static int verboseLevel()
static std::ostream & logStream()
static std::string environmentVariable(const std::string &variable)
static std::vector< std::string > includeDirPaths()
static bool developerMode()
static std::vector< std::string > osalPaths()
static std::vector< std::string > opsetIncludeDir()
std::string errorMessage() const
Definition Exception.cc:123
static bool fileIsReadable(const std::string fileName)
static bool createDirectory(const std::string &path)
static bool removeFileOrDirectory(const std::string &path)
static const std::string DIRECTORY_SEPARATOR
static void copy(const std::string &source, const std::string &target)
static std::string currentWorkingDir()
static bool fileExists(const std::string fileName)
static OperationBuilder * instance_
Static unique instance.
static OperationBuilder & instance()
std::string xmlFilePath(const std::string &xmlFile)
virtual ~OperationBuilder()
bool installDataFile(const std::string &path, const std::string &xmlFile, const std::string &destination)
std::string makeIncludeString(const std::vector< std::string > &paths)
bool verifyXML(const std::string file)
bool buildObject(const std::string &baseName, const std::string &behaviorFile, const std::string &path, std::vector< std::string > &output)
std::string behaviorFile(const std::string &baseName, std::string &path)
void setSourceFile(const std::string &filename)
virtual ObjectState * readState()