OpenASIP 2.2
Loading...
Searching...
No Matches
CompileTools.cc
Go to the documentation of this file.
1/*
2 Copyright (c) 2002-2015 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 CompileTools.cc
26 *
27 * Implementation of CompileTools class.
28 *
29 * Created on: 13.3.2015
30 * @author: Henry Linjamäki (henry.linjamaki-no.spam-tut.fi)
31 * @note rating: red
32 */
33
34#include "CompileTools.hh"
35
36#include <vector>
37#include <fstream>
38
39#include "Machine.hh"
40#include "Program.hh"
41#include "ADFSerializer.hh"
42#include "Application.hh"
43#include "FileSystem.hh"
44#include "Exception.hh"
45
48
51
54 const TTAMachine::Machine& target,
55 const std::string& code,
56 const std::string& cflags) {
57
58 std::stringstream codeStream(code);
59 return compileAsC(target, codeStream, cflags);
60}
61
62/**
63 * Interprets code as C code and tries to convert it to Program.
64 *
65 * @return Pointer to new program if compilation is successful, otherwise NULL.
66 */
69 const TTAMachine::Machine& target,
70 std::stringstream& code,
71 const std::string& cflags) {
72 using namespace TTAProgram;
73
74 Program* resultProgram = NULL;
75
76 std::string tempDir = FileSystem::createTempDirectory(
78 if (tempDir.empty()) {
79 return NULL;
80 }
81 std::string tempAdfFile =
82 tempDir + FileSystem::DIRECTORY_SEPARATOR + "temp.adf";
83 std::string sourceCFile =
84 tempDir + FileSystem::DIRECTORY_SEPARATOR + "temp.c";
85 std::string resultFile =
86 tempDir + FileSystem::DIRECTORY_SEPARATOR + "temp.tpef";
87
88 ADFSerializer serializer;
89 serializer.setDestinationFile(tempAdfFile);
90 try {
91 serializer.writeMachine(target);
92 } catch (const SerializerException& exception) {
94 return NULL;
95 }
96
97 std::ofstream cStream(sourceCFile);
98 if (cStream.fail()) {
100 return NULL;
101 }
102 cStream << code.rdbuf();
103 cStream.close();
104
105 std::string command = Environment::tceCompiler() + " "
106 "-a " + tempAdfFile + " " +
107 "-o " + resultFile + " " +
108 cflags + " " +
109 sourceCFile;
110 std::vector<std::string> output;
111
112 if (Application::runShellCommandAndGetOutput(command, output) == 0 &&
113 FileSystem::fileExists(resultFile)) {
114 try {
115 resultProgram = Program::loadFromTPEF(resultFile, target);
116 } catch (Exception& e) {
118 return NULL;
119 }
120 }
121
123 return resultProgram;
124}
125
126/**
127 * Interprets string stream as LLVM (.ll) assembly code and tries to convert
128 * it to Program.
129 *
130 * @return Pointer to new program if compilation is successful, otherwise NULL.
131 */
134 const TTAMachine::Machine& target,
135 const std::stringstream& code,
136 const std::string compileOptions) {
137 using namespace TTAProgram;
138
139 Program* resultProgram = NULL;
140
141 std::string tempDir = FileSystem::createTempDirectory(
143 if (tempDir.empty()) {
144 return NULL;
145 }
146 std::string tempAdfFile =
147 tempDir + FileSystem::DIRECTORY_SEPARATOR + "temp.adf";
148 std::string sourceLLFile =
149 tempDir + FileSystem::DIRECTORY_SEPARATOR + "temp.ll";
150 std::string resultFile =
151 tempDir + FileSystem::DIRECTORY_SEPARATOR + "temp.tpef";
152
153 ADFSerializer serializer;
154 serializer.setDestinationFile(tempAdfFile);
155 try {
156 serializer.writeMachine(target);
157 } catch (const SerializerException& exception) {
159 return NULL;
160 }
161
162 std::ofstream llStream(sourceLLFile);
163 if (llStream.fail()) {
165 return NULL;
166 }
167 llStream << code.rdbuf();
168 llStream.close();
169
170 std::string compileLLFilecmd = Environment::tceCompiler() + " "
171 + "-a " + tempAdfFile + " "
172 + "-o " + resultFile + " " + " "
173 + compileOptions + " "
174 + sourceLLFile;
175
176 if (Application::runShellCommandSilently(compileLLFilecmd) == 0) {
177 try {
178 resultProgram = Program::loadFromTPEF(resultFile, target);
179 } catch (Exception& e) {
181 return NULL;
182 }
183 }
184
186 return resultProgram;
187}
188
void writeMachine(const TTAMachine::Machine &machine)
static int runShellCommandAndGetOutput(const std::string &command, std::vector< std::string > &outputLines, std::size_t maxOutputLines=DEFAULT_MAX_OUTPUT_LINES, bool includeStdErr=false)
static int runShellCommandSilently(const std::string &command)
virtual ~CompileTools()
static TTAProgram::Program * compileAsLLVM(const TTAMachine::Machine &target, const std::stringstream &code, const std::string compileOptions)
static TTAProgram::Program * compileAsC(const TTAMachine::Machine &target, std::stringstream &code, const std::string &cflags="")
static std::string tceCompiler()
static bool removeFileOrDirectory(const std::string &path)
static const std::string DIRECTORY_SEPARATOR
static std::string createTempDirectory(const std::string &path="/tmp", const std::string &tempDirPrefix="tmp_tce_")
static std::string currentWorkingDir()
static bool fileExists(const std::string fileName)
void setDestinationFile(const std::string &fileName)