[r403]: cmajor++ / Cm / Cm.Compiler / Main.cpp  Maximize  Restore  History

Download this file

457 lines (444 with data), 19.0 kB

  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
/*========================================================================
Copyright (c) 2012-2013 Seppo Laakko
http://sourceforge.net/projects/cmajor/
Distributed under the GNU General Public License, version 3 (GPLv3).
(See accompanying LICENSE.txt or http://www.gnu.org/licenses/gpl.html)
========================================================================*/
#include <Cm.Build/Build.hpp>
#include <Cm.Build/InitDone.hpp>
#include <Cm.Core/Error.hpp>
#include <Cm.Core/Assert.hpp>
#include <Cm.Core/ConfigSettings.hpp>
#include <Cm.Core/Platform.hpp>
#include <Cm.Core/Flags.hpp>
#include <Cm.Core/Statistics.hpp>
#include <Cm.Core/Function.hpp>
#include <Cm.Core/DebugInfo.hpp>
#include <Soul.Util/TextUtils.hpp>
#include <Soul.Parsing/Exception.hpp>
#include <iostream>
#include <string>
#include <chrono>
#include <signal.h>
#if !defined(NDEBUG) && defined(WIN32)
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#endif
const char* version = "0.6.0.0";
using namespace Cm::Build;
using namespace Cm::Core;
using namespace Soul::Util;
using namespace Soul::Parsing;
using namespace std::chrono;
inline bool UseDevEnvErrorFormat()
{
return (GetGlobalFlags() & GlobalFlags::devEnvErrorFormat) != GlobalFlags::none;
}
class AccessViolation
{
};
void SegmentationFault(int signal)
{
throw AccessViolation();
}
class Aborted
{
};
void Abort(int signal)
{
throw Aborted();
}
int main(int argc, const char** argv)
{
#if !defined(NDEBUG) && defined(WIN32)
_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG | _CRTDBG_LEAK_CHECK_DF);
//_CrtSetBreakAlloc(9991671);
#endif
#ifndef NDEBUG
const char* compilerMode = "debug mode";
const char* compilerName = "cmcd";
#elif defined(ASSERTIONS)
const char* compilerMode = "assert mode";
const char* compilerName = "cmca";
#else
const char* compilerMode = "release mode";
const char* compilerName = "cmc";
#endif
try
{
signal(SIGSEGV, SegmentationFault);
signal(SIGTERM, Abort);
signal(SIGABRT, Abort);
signal(SIGINT, Abort);
steady_clock::time_point start = steady_clock::now();
InitDone initDone;
std::string platformBits = GetPlatformBits();
std::string platformPathSeparator = std::string(1, GetPlatformPathSeparatorChar());
std::string cmajorVersionStr = std::string("Cmajor ") + compilerMode + " compiler version " + version + " " + platformBits;
Cm::Core::SetCmajorVersionStr(cmajorVersionStr);
if (argc < 2)
{
std::cerr << cmajorVersionStr << std::endl;
std::cerr << "usage: " << compilerName << " [options] {file.cms | file.cmp}" << std::endl;
std::cerr << "build solution file.cms or project file.cmp\n";
std::cerr << "--------------------------------------------------------------------------\n";
std::cerr << "options:\n";
std::cerr << "-R : rebuild solution or project\n";
std::cerr << "-clean : clean solution or project\n";
std::cerr << "-config=<config> : use <config> configuration (default=debug)\n";
std::cerr << "-O=<n> : set optimization level to <n> (0-3)\n";
std::cerr << " (defaults: debug: O=0, release: O=3)\n";
std::cerr << "-c <file.cm> : compile only <file.cm>, do not link\n";
std::cerr << "-g : generate debug information\n";
std::cerr << "-D <symbol> : define conditional compilation symbol <symbol>\n";
std::cerr << "-L <dir1>" + platformPathSeparator + "<dir2>" + platformPathSeparator +
"... : add <dir1>, <dir2>, ... to library directories\n";
std::cerr << "-S : generate native assembly source code to .s file\n";
std::cerr << "-nocttc : no constrained template type checking\n";
std::cerr << "-time : report compilation time\n";
std::cerr << "-quiet : output only errors\n";
std::cerr << "-verbose : generate verbose output\n";
std::cerr << "-stats : print statistics\n";
std::cerr << "--------------------------------------------------------------------------\n";
std::cerr << "compiler debugging options:\n";
std::cerr << "-debug:cc : debug compiling classes\n";
std::cerr << "-debug:cf : debug compiling functions\n";
std::cerr << "-debug:ti : debug template instantiation\n";
std::cerr << "-debug:arch : debug generated archetypes\n";
std::cerr << "-debug:aor : debug ambiguous overload resolution\n";
std::cerr << "-debug:sfp : debug source file parsing (=> <file.parse.xml>)\n";
std::cerr << "-debug:mem : debug memory usage\n";
std::cerr << "--------------------------------------------------------------------------\n";
std::cerr << "options for IDE:\n";
std::cerr << "-definesfromsym : read project defines from .cmp.sym files\n";
std::cerr << "-devenverrorformat : format error messages for IDE\n";
return 1;
}
ConfigSettings::Instance()->ReadWrite("cmajor.cfg");
BuildSettings settings;
settings.AddLibraryDirectories(ConfigSettings::Instance()->LibraryDirectories());
std::vector<std::string> files;
std::vector<std::string> cmFiles;
bool prevWasL = false;
bool prevWasC = false;
bool prevWasD = false;
bool reportTime = false;
bool stats = false;
GlobalFlags flags = GlobalFlags::none;
for (int i = 1; i < argc; ++i)
{
std::string arg = argv[i];
if (!arg.empty())
{
if (arg[0] == '-')
{
if (arg.find('=') != std::string::npos)
{
std::vector<std::string> parts = Split(arg, '=');
if (parts.size() == 2)
{
if (parts[0] == "-config")
{
settings.SetConfigurationName(parts[1]);
}
else if (parts[0] == "-O")
{
const std::string level = parts[1];
settings.SetOptimizationLevel(level);
}
else
{
throw std::runtime_error("unknown option '" + parts[0] + "'");
}
}
else
{
throw std::runtime_error("option syntax error: " + arg);
}
}
else
{
if (arg == "-R")
{
settings.SetRebuild(true);
}
else if (arg == "-clean")
{
settings.SetClean(true);
}
else if (arg == "-S")
{
settings.SetGenerateNativeAssemblySourceCode();
}
else if (arg == "-pic")
{
settings.SetLlcPrintAfterAll();
}
else if (arg == "-nocttc")
{
settings.SetNoConstrainedTemplateTypeChecking();
}
else if (arg == "-time")
{
reportTime = true;
}
else if (arg == "-stats")
{
stats = true;
flags = flags | GlobalFlags::stats;
}
else if (arg == "-quiet")
{
flags = flags | GlobalFlags::quiet;
}
else if (arg == "-verbose")
{
flags = flags | GlobalFlags::verbose;
}
else if (arg == "-g")
{
flags = flags | GlobalFlags::generateDebugInfo;
}
else if (arg == "-debug:cc")
{
flags = flags | GlobalFlags::debugCompilingClasses;
}
else if (arg == "-debug:cf")
{
flags = flags | GlobalFlags::debugCompilingFunctions;
}
else if (arg == "-debug:ti")
{
flags = flags | GlobalFlags::debugTemplateInstantiation;
}
else if (arg == "-debug:arch")
{
flags = flags | GlobalFlags::debugArchetypes;
}
else if (arg == "-debug:aor")
{
flags = flags | GlobalFlags::debugOverloadResolution;
}
else if (arg == "-debug:sfp")
{
flags = flags | GlobalFlags::debugParsing;
}
else if (arg == "-debug:mem")
{
flags = flags | GlobalFlags::debugMem;
}
else if (arg == "-L")
{
prevWasL = true;
}
else if (arg == "-c")
{
prevWasC = true;
}
else if (arg == "-D")
{
prevWasD = true;
}
else if (arg == "-devenverrorformat")
{
flags = flags | GlobalFlags::devEnvErrorFormat;
}
else if (arg == "-definesfromsym")
{
flags = flags | GlobalFlags::definesFromSym;
}
else
{
throw std::runtime_error("unknown option '" + arg + "'");
}
}
}
else if (prevWasL)
{
prevWasL = false;
std::vector<std::string> libraryDirs = Split(arg, GetPlatformPathSeparatorChar());
settings.AddLibraryDirectories(libraryDirs);
}
else if (prevWasC)
{
prevWasC = false;
cmFiles.push_back(arg);
}
else if (prevWasD)
{
prevWasD = false;
settings.DefineSymbol(arg);
}
else
{
files.push_back(arg);
}
}
}
SetGlobalFlags(flags);
if ((flags & GlobalFlags::quiet) == GlobalFlags::none)
{
std::cout << "Cmajor " << compilerMode << " compiler version " << version << " " << platformBits << std::endl;
}
if (cmFiles.empty())
{
for (const std::string& file : files)
{
Build(file, settings, cmFiles);
}
}
else
{
if (files.size() != 1 || path(files.front()).extension() != ".cmp")
{
throw std::runtime_error("one project file (.cmp) must be given when compiling single source files");
}
Build(files.front(), settings, cmFiles);
}
settings.Dispose();
steady_clock::time_point end = steady_clock::now();
auto dur = end - start;
if (reportTime)
{
long long totalSecs = duration_cast<seconds>(dur).count() + 1;
int hours = static_cast<int>(totalSecs / 3600);
int mins = static_cast<int>(totalSecs / 60 % 3600);
int secs = static_cast<int>(totalSecs % 60);
std::cout <<
(hours > 0 ? std::to_string(hours) + " hour" + ((hours != 1) ? "s " : " ") : "") <<
(mins > 0 ? std::to_string(mins) + " minute" + ((mins != 1) ? "s " : " ") : "") <<
secs << " second" << ((secs != 1) ? "s" : "") << std::endl;
}
if (stats)
{
PrintStatistics();
}
return 0;
}
catch (const LinkException& ex)
{
if (UseDevEnvErrorFormat())
{
std::cout << ex.what() << std::endl;
PrintErrorForDevEnv("llvm-link", L0 + LINK_ERROR, ex.what(), Position(), std::vector<ReferencePos>(), GetCurrentProject() ? GetCurrentProject()->Name() : "");
}
else
{
std::cerr << ex.what() << std::endl;
}
}
catch (const AssemblyException& ex)
{
if (UseDevEnvErrorFormat())
{
std::cout << ex.Message() << std::endl;
PrintErrorForDevEnv(ex.ToolName(), A0 + ASSEMBLY_ERROR, ex.Message(), ex.FilePath(), ex.Line(), ex.Column(), GetCurrentProject() ? GetCurrentProject()->Name() : "");
}
else
{
std::cerr << "assembly error in file " << ex.FilePath() << " at line " << ex.Line() << " col " << ex.Column() << ":\n" << ex.ToolName() << ": " << ex.Message() << std::endl;
}
}
catch (const ParsingException& ex)
{
if (UseDevEnvErrorFormat())
{
std::cout << ex.what() << std::endl;
PrintErrorForDevEnv(compilerName, 1, ex.what(), ex.GetPosition(), std::vector<ReferencePos>(), GetCurrentProject() ? GetCurrentProject()->Name() : "");
}
else
{
std::cerr << ex.what() << std::endl;
}
}
catch (const ExpectationFailure& ex)
{
if (UseDevEnvErrorFormat())
{
std::cout << ex.what() << std::endl;
PrintErrorForDevEnv(compilerName, 1, ex.what(), ex.GetPosition(), std::vector<ReferencePos>(), GetCurrentProject() ? GetCurrentProject()->Name() : "");
}
else
{
std::cerr << ex.what() << std::endl;
}
}
catch (Exception& ex)
{
if (UseDevEnvErrorFormat())
{
PrintError(ex.Number(), ex, true);
PrintErrorForDevEnv(compilerName, ex.Number(), ex.Message(), ex.ErrorPos(), ex.ReferencePositions(), GetCurrentProject() ? GetCurrentProject()->Name() : "");
}
else
{
PrintError(ex.Number(), ex);
}
}
catch (const AssertionFailed& ex)
{
if (UseDevEnvErrorFormat())
{
std::cout << ex.Message() + " in file " + ex.File() + " at line " + std::to_string(ex.Line()) + (!ex.Function().empty() ? (" when compiling " + ex.Function()) : "") << std::endl;
PrintErrorForDevEnv(compilerName, I0 + ASSERTION_FAILED, ex.Message() + " in file " + ex.File() + " at line " + std::to_string(ex.Line()) + (!ex.Function().empty() ? (" when compiling " + ex.Function()) : ""),
Position(), std::vector<ReferencePos>(), GetCurrentProject() ? GetCurrentProject()->Name() : "");
}
else
{
std::cerr << ex.Message() << " in file " << ex.File() << " at line " << ex.Line() << (!ex.Function().empty() ? (" when compiling " + ex.Function()) : "") << std::endl;
}
}
catch (const GCCException& ex)
{
if (UseDevEnvErrorFormat())
{
std::cout << "error generating executable " << ex.ExePath() << ": " << ex.what() << std::endl;
PrintErrorForDevEnv("gcc", G0 + GCC_ERROR, "error generating executable " + ex.ExePath() + ": " + ex.what(), Position(), std::vector<ReferencePos>(), GetCurrentProject() ? GetCurrentProject()->Name() : "");
}
else
{
std::cerr << "error generating executable " << ex.ExePath() << ": " << ex.what() << std::endl;
}
}
catch (const std::exception& ex)
{
if (UseDevEnvErrorFormat())
{
std::cout << ex.what() << std::endl;
PrintErrorForDevEnv(compilerName, I0 + STD_EXCEPTION, ex.what(), Position(), std::vector<ReferencePos>(), GetCurrentProject() ? GetCurrentProject()->Name() : "");
}
else
{
std::cerr << ex.what() << std::endl;
}
}
catch (AccessViolation)
{
if (UseDevEnvErrorFormat())
{
std::cout << "internal compiler error (access violation)" << std::endl;
PrintErrorForDevEnv(compilerName, I0 + ACCESS_VIOLATION, "internal compiler error (access violation)", Position(), std::vector<ReferencePos>(), GetCurrentProject() ? GetCurrentProject()->Name() : "");
}
else
{
std::cerr << "internal compiler error (access violation)" << std::endl;
}
}
catch (Aborted)
{
}
catch (...)
{
if (UseDevEnvErrorFormat())
{
std::cout << "internal compiler error (unkown exception occurred)" << std::endl;
PrintErrorForDevEnv(compilerName, I0 + ACCESS_VIOLATION, "internal compiler error (unkown exception occurred)", Position(), std::vector<ReferencePos>(), GetCurrentProject() ? GetCurrentProject()->Name() : "");
}
else
{
std::cerr << "internal compiler error (unkown exception occurred)" << std::endl;
}
}
return 2;
}

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks