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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
|
// **********************************************************************
//
// Copyright (c) 2003-2009 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************
#include <IceUtil/DisableWarnings.h>
#include <Slice/Preprocessor.h>
#include <Slice/Util.h>
#include <IceUtil/StringUtil.h>
#include <IceUtil/FileUtil.h>
#include <IceUtil/UUID.h>
#include <IceUtil/Unicode.h>
#include <algorithm>
#include <fstream>
#include <sys/types.h>
#include <sys/stat.h>
#include <cstring>
#ifndef _WIN32
# include <sys/wait.h>
#endif
using namespace std;
using namespace Slice;
//
// mcpp defines
//
namespace Slice
{
enum Outdest
{
Out=0, Err=1, Dbg=2, Num_Outdest=3
};
};
extern "C" int mcpp_lib_main(int argc, char** argv);
extern "C" void mcpp_use_mem_buffers(int tf);
extern "C" char* mcpp_get_mem_buffer(Outdest od);
Slice::Preprocessor::Preprocessor(const string& path, const string& fileName, const vector<string>& args) :
_path(path),
_fileName(fileName),
_args(args),
_cppHandle(0)
{
}
Slice::Preprocessor::~Preprocessor()
{
close();
}
string
Slice::Preprocessor::getBaseName()
{
string base(_fileName);
string suffix;
string::size_type pos = base.rfind('.');
if(pos != string::npos)
{
base.erase(pos);
}
return base;
}
string
Slice::Preprocessor::addQuotes(const string& arg)
{
//
// Add quotes around the given argument to ensure that arguments
// with spaces will be preserved as a single argument. We also
// escape the "\" character to ensure that we don't end up with a
// \" at the end of the string.
//
return "\"" + IceUtilInternal::escapeString(arg, "\\") + "\"";
}
string
Slice::Preprocessor::normalizeIncludePath(const string& path)
{
string result = path;
replace(result.begin(), result.end(), '\\', '/');
string::size_type pos;
while((pos = result.find("//")) != string::npos)
{
result.replace(pos, 2, "/");
}
if(result == "/" || (result.size() == 3 && isalpha(static_cast<unsigned char>(result[0])) && result[1] == ':' &&
result[2] == '/'))
{
return result;
}
if(result.size() > 1 && result[result.size() - 1] == '/')
{
result.erase(result.size() - 1);
}
return result;
}
FILE*
Slice::Preprocessor::preprocess(bool keepComments)
{
if(!checkInputFile())
{
return 0;
}
//
// Build arguments list.
//
vector<string> args = _args;
if(keepComments)
{
args.push_back("-C");
}
args.push_back("-e");
args.push_back("utf8");
args.push_back(_fileName);
const char** argv = new const char*[args.size() + 1];
argv[0] = "mcpp";
for(unsigned int i = 0; i < args.size(); ++i)
{
argv[i + 1] = args[i].c_str();
}
//
// Call mcpp using memory buffer.
//
mcpp_use_mem_buffers(1);
int status = mcpp_lib_main(static_cast<int>(args.size()) + 1, const_cast<char**>(argv));
delete[] argv;
//
// Print errors to stderr.
//
char* err = mcpp_get_mem_buffer(Err);
if(err)
{
::fputs(err, stderr);
}
if(status == 0)
{
//
// Write output to temporary file.
//
char* buf = mcpp_get_mem_buffer(Out);
//
// First try to open temporay file in tmp directory.
//
#ifdef _WIN32
wchar_t* name = _wtempnam(NULL, L".preprocess");
if(name)
{
_cppFile = wstring(name);
free(name);
_cppHandle = ::_wfopen(_cppFile.c_str(), L"w+");
}
#else
_cppHandle = tmpfile();
#endif
//
// If that fails try to open file in current directory.
//
if(_cppHandle == 0)
{
#ifdef _WIN32
_cppFile = L".preprocess." + IceUtil::stringToWstring(IceUtil::generateUUID());
_cppHandle = ::_wfopen(_cppFile.c_str(), L"w+");
#else
_cppFile = ".preprocess." + IceUtil::generateUUID();
_cppHandle = ::fopen(_cppFile.c_str(), "w+");
#endif
}
if(_cppHandle != 0)
{
if(buf)
{
::fwrite(buf, strlen(buf), 1, _cppHandle);
}
::rewind(_cppHandle);
}
else
{
cerr << "Could not open temporary file: ";
#ifdef _WIN32
cerr << IceUtil::wstringToString(_cppFile);
#else
cerr << _cppFile;
#endif
cerr << endl;
}
}
//
// Calling this again causes the memory buffers to be freed.
//
mcpp_use_mem_buffers(1);
return _cppHandle;
}
void
Slice::Preprocessor::printMakefileDependencies(Language lang, const vector<string>& includePaths)
{
if(!checkInputFile())
{
return;
}
//
// Build arguments list.
//
vector<string> args = _args;
args.push_back("-M");
args.push_back("-e");
args.push_back("utf8");
args.push_back(_fileName);
const char** argv = new const char*[args.size() + 1];
for(unsigned int i = 0; i < args.size(); ++i)
{
argv[i + 1] = args[i].c_str();
}
//
// Call mcpp using memory buffer.
//
mcpp_use_mem_buffers(1);
int status = mcpp_lib_main(static_cast<int>(args.size() + 1), const_cast<char**>(argv));
delete[] argv;
//
// Print errors to stderr.
//
char* err = mcpp_get_mem_buffer(Err);
if(err)
{
::fputs(err, stderr);
}
if(status != 0)
{
//
// Calling this again causes the memory buffers to be freed.
//
mcpp_use_mem_buffers(1);
return;
}
//
// Get mcpp output.
//
string unprocessed;
char* buf = mcpp_get_mem_buffer(Out);
if(buf)
{
unprocessed = string(buf);
}
//
// Calling this again causes the memory buffers to be freed.
//
mcpp_use_mem_buffers(1);
//
// We now need to massage the result to get the desired output.
// First make it a single line.
//
string::size_type pos;
while((pos = unprocessed.find("\\\n")) != string::npos)
{
unprocessed.replace(pos, 2, "");
}
//
// Get the main output file name.
//
#ifdef _WIN32
string suffix = ".obj:";
#else
string suffix = ".o:";
#endif
pos = unprocessed.find(suffix) + suffix.size();
string result = unprocessed.substr(0, pos);
vector<string> fullIncludePaths;
vector<string>::const_iterator p;
for(p = includePaths.begin(); p != includePaths.end(); ++p)
{
fullIncludePaths.push_back(fullPath(*p));
}
string absoluteFileName = fullPath(_fileName);
//
// Process each dependency.
//
string::size_type end;
while((end = unprocessed.find(".ice", pos)) != string::npos)
{
end += 4;
string file = IceUtilInternal::trim(unprocessed.substr(pos, end - pos));
if(IceUtilInternal::isAbsolutePath(file))
{
if(file == absoluteFileName)
{
file = _fileName;
}
else
{
//
// Transform back full paths generated by mcpp to paths relative to the specified
// include paths.
//
string newFile = file;
for(vector<string>::const_iterator p = fullIncludePaths.begin(); p != fullIncludePaths.end(); ++p)
{
if(file.compare(0, p->length(), *p) == 0)
{
string s = includePaths[p - fullIncludePaths.begin()] + file.substr(p->length());
if(IceUtilInternal::isAbsolutePath(newFile) || s.size() < newFile.size())
{
newFile = s;
}
}
}
file = newFile;
}
}
//
// Escape spaces in the file name.
//
string::size_type space = 0;
while((space = file.find(" ", space)) != string::npos)
{
file.replace(space, 1, "\\ ");
space += 2;
}
//
// Add to result
//
result += " \\\n " + file;
pos = end;
}
result += "\n";
/*
* icecpp emits dependencies in any of the following formats, depending on the
* length of the filenames:
*
* x.o[bj]: /path/x.ice /path/y.ice
*
* x.o[bj]: /path/x.ice \
* /path/y.ice
*
* x.o[bj]: /path/x.ice /path/y.ice \
* /path/z.ice
*
* x.o[bj]: \
* /path/x.ice
*
* x.o[bj]: \
* /path/x.ice \
* /path/y.ice
*
* Spaces embedded within filenames are escaped with a backslash. Note that
* Windows filenames may contain colons.
*
*/
switch(lang)
{
case CPlusPlus:
{
//
// Change .o[bj] suffix to .cpp suffix.
//
string::size_type pos;
while((pos = result.find(suffix)) != string::npos)
{
result.replace(pos, suffix.size() - 1, ".cpp");
}
break;
}
case Java:
{
//
// We want to shift the files left one position, so that
// "x.cpp: x.ice y.ice" becomes "x.ice: y.ice".
//
//
// Remove the first file.
//
string::size_type start = result.find(suffix);
assert(start != string::npos);
start = result.find_first_not_of(" \t\r\n\\", start + suffix.size()); // Skip to beginning of next file.
assert(start != string::npos);
result.erase(0, start);
//
// Find end of next file.
//
string::size_type pos = 0;
while((pos = result.find_first_of(" :\t\r\n\\", pos + 1)) != string::npos)
{
if(result[pos] == ':')
{
result.insert(pos, 1, '\\'); // Escape colons.
++pos;
}
else if(result[pos] == '\\') // Ignore escaped characters.
{
++pos;
}
else
{
break;
}
}
if(pos == string::npos)
{
result.append(":");
}
else
{
result.insert(pos, 1, ':');
}
break;
}
case CSharp:
{
//
// Change .o[bj] suffix to .cs suffix.
//
string::size_type pos;
while((pos = result.find(suffix)) != string::npos)
{
result.replace(pos, suffix.size() - 1, ".cs");
}
break;
}
default:
{
abort();
break;
}
}
//
// Output result
//
fputs(result.c_str(), stdout);
}
bool
Slice::Preprocessor::close()
{
if(_cppHandle != 0)
{
int status = fclose(_cppHandle);
_cppHandle = 0;
if(_cppFile.size() != 0)
{
#ifdef _WIN32
_wunlink(_cppFile.c_str());
#else
unlink(_cppFile.c_str());
#endif
}
if(status != 0)
{
return false;
}
}
return true;
}
bool
Slice::Preprocessor::checkInputFile()
{
string base(_fileName);
string suffix;
string::size_type pos = base.rfind('.');
if(pos != string::npos)
{
suffix = IceUtilInternal::toLower(base.substr(pos));
}
if(suffix != ".ice")
{
cerr << _path << ": input files must end with `.ice'" << endl;
return false;
}
ifstream test(_fileName.c_str());
if(!test)
{
cerr << _path << ": can't open `" << _fileName << "' for reading" << endl;
return false;
}
test.close();
return true;
}
|