OpenASIP  2.0
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"
40 #include "OperationSerializer.hh"
41 #include "ObjectState.hh"
42 #include "Application.hh"
43 
44 using std::vector;
45 using std::string;
46 using std::cerr;
47 using std::endl;
48 
49 const string SCHEMA_FILE_NAME = "Operation_Schema.xsd";
50 
52 
53 /**
54  * Constructor.
55  */
57 }
58 
59 /**
60  * Destructor.
61  */
63 }
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  */
87 string
88 OperationBuilder::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  */
114 string
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  */
140 bool
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 + " ";
151  TCEString LDFLAGS = Environment::environmentVariable("LDFLAGS")
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 
172  if (Application::isInstalled()) {
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  CXXFLAGS += " -L" + Application::installationDir() + "/lib -lopenasip ";
180  }
181 
182  // Add user defined CXXFLAGS + CPPFLAGS to the end because they
183  // might point to paths with incompatible TCE headers.
184  string COMPILE_FLAGS = \
185  INCLUDES + " " +
186  CONFIGURE_CPPFLAGS + " " + CONFIGURE_LDFLAGS + " " +
187  CXXFLAGS + " " + CPPFLAGS + " ";
188 
189  string module = path + FileSystem::DIRECTORY_SEPARATOR +
190  baseName + ".opb";
191 
192  string command = (CXXCOMPILER == "") ? (string(CXX)) : (CXXCOMPILER);
193  command += " " + COMPILE_FLAGS + " " + behaviorFile + " " +
194  string(SHARED_CXX_FLAGS) + " " + LDFLAGS + " -o " + module + " 2>&1";
195 
197  Application::logStream() << command << std::endl;
198  }
199 
200  if (Application::runShellCommandAndGetOutput(command, output) != 0) {
201  return false;
202  }
203  }
204  return true;
205 }
206 
207 /**
208  * Installs the data file containing operation property declarations.
209  *
210  * Creates the destination path if it does not exist. Overwrites the
211  * existing file, if it is already installed in given destination.
212  *
213  * Installation can fail for several reasons: no rights to create
214  * destination directory, no rights to overwrite a previously installed
215  * version of the data file, no rights to read the source data file.
216  *
217  * @param path Source path of data file.
218  * @param xmlFile Name of the data file.
219  * @param destination Destination path where the data file is to be
220  * installed.
221  * @return True, if installation is completed successfully, false otherwise.
222  */
223 bool
225  const std::string& sourcePath,
226  const std::string& xmlFile,
227  const std::string& destinationPath) {
228 
229  const string source =
230  sourcePath + FileSystem::DIRECTORY_SEPARATOR + xmlFile;
231 
232  // an unreadable or nonexisting file should have been caught earlier on
233  // (for example, during validation)
234  assert(FileSystem::fileExists(source) &&
236 
237  if (!FileSystem::fileExists(destinationPath)) {
238  if (!FileSystem::createDirectory(destinationPath)) {
239  return false;
240  }
241  }
242 
243  const string dest =
244  destinationPath + FileSystem::DIRECTORY_SEPARATOR + xmlFile;
245 
246  if (FileSystem::fileExists(dest)) {
248  return false;
249  }
250  }
251 
252  FileSystem::copy(source, dest);
253  return true;
254 }
255 
256 
257 /**
258  * Verifies the correctness of XML file.
259  *
260  * @param file Absolute path of XML file.
261  */
262 bool
263 OperationBuilder::verifyXML(const std::string file) {
264  OperationSerializer serializer;
265  serializer.setSourceFile(file);
266 
267  try {
268  ObjectState* root = serializer.readState();
269  delete root;
270  } catch (const SerializerException& s) {
271  cerr << "Invalid XML file: " << s.errorMessage() << endl;
272  return false;
273  }
274  return true;
275 }
276 
277 /**
278  * Makes an include string for compiler (-I<path1> -I<path2> etc) from the
279  * paths in the vector
280  *
281  * @param Vector containing the include paths
282  */
283 string
284 OperationBuilder::makeIncludeString(const vector<string>& paths) {
285  string includes = "";
286  for (size_t i = 0; i < paths.size(); i++) {
287  includes += "-I" + paths.at(i) + " ";
288  }
289  return includes;
290 }
FileSystem.hh
FileSystem::removeFileOrDirectory
static bool removeFileOrDirectory(const std::string &path)
Definition: FileSystem.cc:493
FileSystem::createDirectory
static bool createDirectory(const std::string &path)
Definition: FileSystem.cc:400
Application::runShellCommandAndGetOutput
static int runShellCommandAndGetOutput(const std::string &command, std::vector< std::string > &outputLines, std::size_t maxOutputLines=DEFAULT_MAX_OUTPUT_LINES, bool includeStdErr=false)
Definition: Application.cc:338
Application::VERBOSE_LEVEL_DEFAULT
static const int VERBOSE_LEVEL_DEFAULT
Default verbose level - do not print anything unnecessary.
Definition: Application.hh:222
OperationBuilder::xmlFilePath
std::string xmlFilePath(const std::string &xmlFile)
Definition: OperationBuilder.cc:88
ObjectState
Definition: ObjectState.hh:59
Application::verboseLevel
static int verboseLevel()
Definition: Application.hh:176
Application::logStream
static std::ostream & logStream()
Definition: Application.cc:155
OperationBuilder::~OperationBuilder
virtual ~OperationBuilder()
Definition: OperationBuilder.cc:62
assert
#define assert(condition)
Definition: Application.hh:86
OperationBuilder::behaviorFile
std::string behaviorFile(const std::string &baseName, std::string &path)
Definition: OperationBuilder.cc:115
Environment::opsetIncludeDir
static std::vector< std::string > opsetIncludeDir()
Definition: Environment.cc:384
OperationBuilder
Definition: OperationBuilder.hh:42
Environment::includeDirPaths
static std::vector< std::string > includeDirPaths()
Definition: Environment.cc:348
OperationSerializer::readState
virtual ObjectState * readState()
Definition: OperationSerializer.cc:118
Application.hh
ObjectState.hh
OperationSerializer.hh
FileSystem::copy
static void copy(const std::string &source, const std::string &target)
Definition: FileSystem.cc:524
OperationSerializer
Definition: OperationSerializer.hh:49
Environment.hh
OperationBuilder::makeIncludeString
std::string makeIncludeString(const std::vector< std::string > &paths)
Definition: OperationBuilder.cc:284
SerializerException
Definition: Exception.hh:675
OperationSerializer::setSourceFile
void setSourceFile(const std::string &filename)
Definition: OperationSerializer.cc:536
Environment::osalPaths
static std::vector< std::string > osalPaths()
Definition: Environment.cc:519
Exception::errorMessage
std::string errorMessage() const
Definition: Exception.cc:123
FileSystem::DIRECTORY_SEPARATOR
static const std::string DIRECTORY_SEPARATOR
Definition: FileSystem.hh:189
Application::isInstalled
static bool isInstalled()
Definition: Application.cc:522
OperationBuilder::buildObject
bool buildObject(const std::string &baseName, const std::string &behaviorFile, const std::string &path, std::vector< std::string > &output)
Definition: OperationBuilder.cc:141
SCHEMA_FILE_NAME
const string SCHEMA_FILE_NAME
Definition: OperationBuilder.cc:49
OperationBuilder::verifyXML
bool verifyXML(const std::string file)
Definition: OperationBuilder.cc:263
FileSystem::fileExists
static bool fileExists(const std::string fileName)
Application::installationDir
static std::string installationDir()
Definition: Application.cc:537
TCEString
Definition: TCEString.hh:53
FileSystem::currentWorkingDir
static std::string currentWorkingDir()
Definition: FileSystem.cc:142
Environment::developerMode
static bool developerMode()
Definition: Environment.cc:1166
Environment::environmentVariable
static std::string environmentVariable(const std::string &variable)
Definition: Environment.cc:406
DS
#define DS
Definition: LLVMBackend.cc:124
FileSystem::fileIsReadable
static bool fileIsReadable(const std::string fileName)
OperationBuilder.hh
OperationBuilder::installDataFile
bool installDataFile(const std::string &path, const std::string &xmlFile, const std::string &destination)
Definition: OperationBuilder.cc:224
OperationBuilder::instance_
static OperationBuilder * instance_
Static unique instance.
Definition: OperationBuilder.hh:70
OperationBuilder::instance
static OperationBuilder & instance()
Definition: OperationBuilder.cc:71
OperationBuilder::OperationBuilder
OperationBuilder()
Definition: OperationBuilder.cc:56