OpenASIP  2.0
CompiledSimCompiler.cc
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2009 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 CompiledSimCompiler.cc
26  *
27  * Definition of CompiledSimCompiler class.
28  *
29  * @author Viljami Korhonen 2007 (viljami.korhonen-no.spam-tut.fi)
30  * @note rating: red
31  */
32 
33 #include <boost/format.hpp>
34 #include <ctime>
35 #include <cstdlib>
36 
37 #include "CompiledSimCompiler.hh"
38 #include "Conversion.hh"
39 #include "tce_config.h"
40 #include "Application.hh"
41 #include "Environment.hh"
42 #include "FileSystem.hh"
43 
44 using std::string;
45 using std::endl;
46 using std::vector;
47 using std::time_t;
48 
49 
50 // Initialize statics
51 
52 // the most important one is "fno-working-directory" which is used because
53 // c5Bcache doesn't like changing directory paths. Rest is just minor tweaks.
54 const char* CompiledSimCompiler::COMPILED_SIM_CPP_FLAGS = " -fpic -std=c++11 "
55 #ifndef __clang__
56 // not supported (yet) in Clang 3.4
57  " -fno-working-directory "
58  "-fno-enforce-eh-specs "
59 #endif
60  "-fno-rtti -DNDEBUG ";
61 // "-fno-rtti "
62 // "-fno-threadsafe-statics "
63 // "-fvisibility=hidden "
64 // "-fvisibility-inlines-hidden ";
65 
66 // this was not recognized on Ubuntu 6.06's gcc:
67 // "-fno-stack-protector "
68 
69 // Flags when compiling shared libraries
70 #ifdef __APPLE__
71 const char* CompiledSimCompiler::COMPILED_SIM_SO_FLAGS = SHARED_CXX_FLAGS;
72 #else
73 const char* CompiledSimCompiler::COMPILED_SIM_SO_FLAGS = " -shared -fpic ";
74 #endif
75 
76 /**
77  * The constructor
78  */
80 
81  // Get number of threads
82  threadCount_ = 3;
83  std::string USER_THREAD_COUNT =
84  Environment::environmentVariable("TTASIM_COMPILER_THREADS");
85  if (USER_THREAD_COUNT != "") {
86  threadCount_ = Conversion::toInt(string(USER_THREAD_COUNT));
87  }
88 
89  // Get compiler
90  compiler_ = "g++";
91  std::string USER_COMPILER =
92  Environment::environmentVariable("TTASIM_COMPILER");
93  if (USER_COMPILER != "") {
94  compiler_ = string(USER_COMPILER);
95  }
96 
97  // Get global compile flags
98  globalCompileFlags_ = " -O0 ";
99  std::string fl =
100  Environment::environmentVariable("TTASIM_COMPILER_FLAGS");
101  if (fl != "")
102  globalCompileFlags_ = std::string(fl);
103 }
104 
105 /**
106  * The destructor
107  */
109 }
110 
111 /**
112  * Compiles a directory with given flags using a pre-generated makefile
113  *
114  * In case environment variable TTASIM_COMPILER is set, it is used
115  * to compile the simulation code, otherwise 'gcc' is used. The
116  * count of compiler threads is read from TTASIM_COMPILER_THREADS,
117  * and defaults to 3.
118  *
119  * @param dirName a source directory containing the .cpp files and the Makefile
120  * @param flags additional compile flags given by the user. for instance, "-O3"
121  * @param verbose Print information of the compilation progress.
122  * @return Return value given by system(). 0 on success. !=0 on failure
123  */
124 int
126  const string& dirName,
127  const string& flags,
128  bool verbose) const {
129 
130  string command =
131  "make -sC " + dirName + " CC=\"" + compiler_ + "\" opt_flags=\"" +
132  globalCompileFlags_ + " " + flags + " \" -j" +
134 
135  if (verbose) {
137  << "Compiling the simulation engine with command "
138  << command << endl;
139  }
140 
141  time_t startTime = std::time(NULL);
142  int retval = system(command.c_str());
143  time_t endTime = std::time(NULL);
144 
145  time_t elapsed = endTime - startTime;
146 
147  if (verbose) {
149  << "Compiling the simulation engine with opt. switches '"
150  << globalCompileFlags_ << " " << flags
151  << "' took " << elapsed / 60 << "m " << (elapsed % 60) << "s "
152  << endl;
153  }
154 
155  return retval;
156 }
157 
158 /**
159  * Compiles a single C++ file using the set flags
160  *
161  * @param path Path to the file
162  * @param flags custom flags to be used for compiling
163  * @param outputExtension extension to append to the filename. default is ".o"
164  * @param verbose Print information of the compilation progress.
165  * @return Return value given by system() call. 0 on success. !=0 on failure
166  */
167 int
169  const std::string& path,
170  const string& flags,
171  const string& outputExtension,
172  bool verbose) const {
173 
174  std::string directory = FileSystem::directoryOfPath(path);
175  std::string fileNameBody = FileSystem::fileNameBody(path);
176  std::string fileName = FileSystem::fileOfPath(path);
177  std::string DS = FileSystem::DIRECTORY_SEPARATOR;
178 
179  // Get includes
180  vector<string> includePaths = Environment::includeDirPaths();
181  string includes;
182  for (vector<string>::const_iterator it = includePaths.begin();
183  it != includePaths.end(); ++it) {
184  includes += "-I" + *it + " ";
185  }
186 
187  if (verbose) {
188  Application::logStream() << "Compiling simulation file "
189  << path << endl;
190  }
191 
192  string command = compiler_ + " " + includes + COMPILED_SIM_CPP_FLAGS
193  + globalCompileFlags_ + " " + flags + " "
194  + path + " -o " + directory + DS + fileNameBody + outputExtension;
195 
196  return system(command.c_str());
197 }
198 
199 /**
200  * Compiles a single C++ file to a shared library (.so)
201  *
202  * Used for generating .so files in dynamic compiled simulation
203  *
204  * @param path Path to the file
205  * @param flags custom flags to be used for compiling
206  * @param verbose Print information of the compilation progress.
207  * @return Return value given by system() call. 0 on success. !=0 on failure
208  */
209 int
211  const std::string& path,
212  const string& flags,
213  bool verbose) const {
214 
215  return compileFile(path, COMPILED_SIM_SO_FLAGS + flags, ".so", verbose);
216 }
217 
CompiledSimCompiler::compileDirectory
int compileDirectory(const std::string &dirName, const std::string &flags="", bool verbose=false) const
Definition: CompiledSimCompiler.cc:125
CompiledSimCompiler::COMPILED_SIM_SO_FLAGS
static const char * COMPILED_SIM_SO_FLAGS
flags used when compiling .so files
Definition: CompiledSimCompiler.hh:66
FileSystem.hh
CompiledSimCompiler::CompiledSimCompiler
CompiledSimCompiler()
Definition: CompiledSimCompiler.cc:79
CompiledSimCompiler::COMPILED_SIM_CPP_FLAGS
static const char * COMPILED_SIM_CPP_FLAGS
cpp flags used for compiled simulation
Definition: CompiledSimCompiler.hh:63
Application::logStream
static std::ostream & logStream()
Definition: Application.cc:155
CompiledSimCompiler::~CompiledSimCompiler
virtual ~CompiledSimCompiler()
Definition: CompiledSimCompiler.cc:108
FileSystem::fileOfPath
static std::string fileOfPath(const std::string pathName)
Definition: FileSystem.cc:101
Conversion::toString
static std::string toString(const T &source)
CompiledSimCompiler::threadCount_
int threadCount_
Number of threads to use while compiling through a Makefile.
Definition: CompiledSimCompiler.hh:75
CompiledSimCompiler::compiler_
std::string compiler_
The compiler to use.
Definition: CompiledSimCompiler.hh:77
Environment::includeDirPaths
static std::vector< std::string > includeDirPaths()
Definition: Environment.cc:348
Conversion.hh
Application.hh
FileSystem::directoryOfPath
static std::string directoryOfPath(const std::string fileName)
Definition: FileSystem.cc:79
Environment.hh
CompiledSimCompiler.hh
FileSystem::DIRECTORY_SEPARATOR
static const std::string DIRECTORY_SEPARATOR
Definition: FileSystem.hh:189
CompiledSimCompiler::compileFile
int compileFile(const std::string &path, const std::string &flags="", const std::string &outputExtension=".o", bool verbose=false) const
Definition: CompiledSimCompiler.cc:168
CompiledSimCompiler::compileToSO
int compileToSO(const std::string &path, const std::string &flags="", bool verbose=false) const
Definition: CompiledSimCompiler.cc:210
CompiledSimCompiler::globalCompileFlags_
std::string globalCompileFlags_
Global compile flags (from env variable)
Definition: CompiledSimCompiler.hh:79
Conversion::toInt
static int toInt(const T &source)
Environment::environmentVariable
static std::string environmentVariable(const std::string &variable)
Definition: Environment.cc:406
DS
#define DS
Definition: LLVMBackend.cc:124
FileSystem::fileNameBody
static std::string fileNameBody(const std::string &fileName)
Definition: FileSystem.cc:291