/
Compiler_PlatformPosix.cpp
251 lines (218 loc) · 7.79 KB
/
Compiler_PlatformPosix.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
//
// Copyright (c) 2010-2011 Matthew Jack and Doug Binks
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would be
// appreciated but is not required.
// 2. Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
// 3. This notice may not be removed or altered from any source distribution.
//
// Notes:
// - We use a single intermediate directory for compiled .obj files, which means
// we don't support compiling multiple files with the same name. Could fix this
// with either mangling names to include paths, or recreating folder structure
//
//
#include "Compiler.h"
#include <string>
#include <vector>
#include <iostream>
#include "assert.h"
#include <sys/wait.h>
#include "ICompilerLogger.h"
using namespace std;
const char c_CompletionToken[] = "_COMPLETION_TOKEN_" ;
class PlatformCompilerImplData
{
public:
PlatformCompilerImplData()
: m_bCompileIsComplete( false )
, m_pLogger( 0 )
, m_ChildForCompilationPID( 0 )
{
m_PipeStdOut[0] = 0;
m_PipeStdOut[1] = 1;
m_PipeStdErr[0] = 0;
m_PipeStdErr[1] = 1;
}
volatile bool m_bCompileIsComplete;
ICompilerLogger* m_pLogger;
pid_t m_ChildForCompilationPID;
int m_PipeStdOut[2];
int m_PipeStdErr[2];
};
Compiler::Compiler()
: m_pImplData( 0 )
{
}
Compiler::~Compiler()
{
}
std::string Compiler::GetObjectFileExtension() const
{
return ".o";
}
bool Compiler::GetIsComplete() const
{
if( !m_pImplData->m_bCompileIsComplete && m_pImplData->m_ChildForCompilationPID )
{
// check for whether process is closed
int procStatus;
pid_t ret = waitpid( m_pImplData->m_ChildForCompilationPID, &procStatus, WNOHANG);
if( ret && ( WIFEXITED(procStatus) || WIFSIGNALED(procStatus) ) )
{
m_pImplData->m_bCompileIsComplete = true;
m_pImplData->m_ChildForCompilationPID = 0;
// get output and log
if( m_pImplData->m_pLogger )
{
const size_t buffSize = 256 * 80; //should allow for a few lines...
char buffer[buffSize];
ssize_t numread = 0;
while( ( numread = read( m_pImplData->m_PipeStdOut[0], buffer, buffSize-1 ) ) > 0 )
{
buffer[numread] = 0;
m_pImplData->m_pLogger->LogInfo( buffer );
}
while( ( numread = read( m_pImplData->m_PipeStdErr[0], buffer, buffSize-1 ) )> 0 )
{
buffer[numread] = 0;
m_pImplData->m_pLogger->LogError( buffer ); //TODO: seperate warnings from errors.
}
}
// close the pipes as this process no longer needs them.
close( m_pImplData->m_PipeStdOut[0] );
m_pImplData->m_PipeStdOut[0] = 0;
close( m_pImplData->m_PipeStdErr[0] );
m_pImplData->m_PipeStdErr[0] = 0;
}
}
return m_pImplData->m_bCompileIsComplete;
}
void Compiler::Initialise( ICompilerLogger * pLogger )
{
m_pImplData = new PlatformCompilerImplData;
m_pImplData->m_pLogger = pLogger;
}
void Compiler::RunCompile( const std::vector<FileSystemUtils::Path>& filesToCompile,
const std::vector<FileSystemUtils::Path>& includeDirList,
const std::vector<FileSystemUtils::Path>& libraryDirList,
const std::vector<FileSystemUtils::Path>& linkLibraryList,
RCppOptimizationLevel optimizationLevel_,
const char* pCompileOptions,
const char* pLinkOptions,
const FileSystemUtils::Path& outputFile,
const FileSystemUtils::Path& intermediatePath )
{
//NOTE: Currently doesn't check if a prior compile is ongoing or not, which could lead to memory leaks
m_pImplData->m_bCompileIsComplete = false;
//create pipes
if ( pipe( m_pImplData->m_PipeStdOut ) != 0 )
{
if( m_pImplData->m_pLogger )
{
m_pImplData->m_pLogger->LogError( "Error in Compiler::RunCompile, cannot create pipe - perhaps insufficient memory?\n");
}
return;
}
//create pipes
if ( pipe( m_pImplData->m_PipeStdErr ) != 0 )
{
if( m_pImplData->m_pLogger )
{
m_pImplData->m_pLogger->LogError( "Error in Compiler::RunCompile, cannot create pipe - perhaps insufficient memory?\n");
}
return;
}
pid_t retPID;
switch( retPID = fork() )
{
case -1: // error, no fork
if( m_pImplData->m_pLogger )
{
m_pImplData->m_pLogger->LogError( "Error in Compiler::RunCompile, cannot fork() process - perhaps insufficient memory?\n");
}
return;
case 0: // child process - carries on below.
break;
default: // current process - returns to allow application to run whilst compiling
close( m_pImplData->m_PipeStdOut[1] );
m_pImplData->m_PipeStdOut[1] = 0;
close( m_pImplData->m_PipeStdErr[1] );
m_pImplData->m_PipeStdErr[1] = 0;
m_pImplData->m_ChildForCompilationPID = retPID;
return;
}
//duplicate the pipe to stdout, so output goes to pipe
dup2( m_pImplData->m_PipeStdErr[1], STDERR_FILENO );
dup2( m_pImplData->m_PipeStdOut[1], STDOUT_FILENO );
close( m_pImplData->m_PipeStdOut[0] );
m_pImplData->m_PipeStdOut[0] = 0;
close( m_pImplData->m_PipeStdErr[0] );
m_pImplData->m_PipeStdErr[0] = 0;
#ifdef __APPLE__
std::string compileString = "clang++ -g -fvisibility=hidden -Xlinker -dylib ";
#else
std::string compileString = "g++ -g -fPIC -fvisibility=hidden -shared ";
#endif //__APPLE__
#ifndef __LP64__
compileString += "-m32 ";
#endif
optimizationLevel_ = GetActualOptimizationLevel( optimizationLevel_ );
switch( optimizationLevel_ )
{
case RCCPPOPTIMIZATIONLEVEL_DEFAULT:
assert(false);
case RCCPPOPTIMIZATIONLEVEL_DEBUG:
compileString += "-O0 ";
break;
case RCCPPOPTIMIZATIONLEVEL_PERF:
compileString += "-Os ";
break;
case RCCPPOPTIMIZATIONLEVEL_NOT_SET:;
}
// include directories
for( size_t i = 0; i < includeDirList.size(); ++i )
{
compileString += "-I\"" + includeDirList[i].m_string + "\" ";
}
// library and framework directories
for( size_t i = 0; i < libraryDirList.size(); ++i )
{
compileString += "-L\"" + libraryDirList[i].m_string + "\" ";
compileString += "-F\"" + libraryDirList[i].m_string + "\" ";
}
// output file
compileString += "-o " + outputFile.m_string + " ";
if( pCompileOptions )
{
compileString += pCompileOptions;
compileString += " ";
}
if( pLinkOptions && strlen(pLinkOptions) )
{
compileString += "-Wl,";
compileString += pLinkOptions;
compileString += " ";
}
// files to compile
for( size_t i = 0; i < filesToCompile.size(); ++i )
{
compileString += "\"" + filesToCompile[i].m_string + "\" ";
}
// libraries to link
for( size_t i = 0; i < linkLibraryList.size(); ++i )
{
compileString += " " + linkLibraryList[i].m_string + " ";
}
std::cout << compileString << std::endl << std::endl;
execl("/bin/sh", "sh", "-c", compileString.c_str(), (const char*)NULL);
}