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

Download this file

384 lines (374 with data), 15.9 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
/*========================================================================
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 <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;
bool UseDevEnvErrorFormat()
{
return (GetFlags() & Flags::devEnvErrorFormat) != Flags::none;
}
void AccessViolation(int signal)
{
throw "access violation";
}
int main(int argc, const char** argv)
{
#if !defined(NDEBUG) && defined(WIN32)
int dbgFlags = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
dbgFlags |= _CRTDBG_LEAK_CHECK_DF;
_CrtSetDbgFlag(dbgFlags);
//_CrtSetBreakAlloc(2345207);
#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, AccessViolation);
steady_clock::time_point start = steady_clock::now();
InitDone initDone;
std::string platformBits = GetPlatformBits();
std::string platformPathSeparator = std::string(1, GetPlatformPathSeparatorChar());
if (argc < 2)
{
std::cerr << "Cmajor " << compilerMode << " compiler version " << version << " " << platformBits << std::endl;
std::cerr << "usage: " << compilerName << " [options] {file.cms | file.cmp}" << std::endl;
std::cerr << "build solution file.cms or project file.cmp using options:\n";
std::cerr << "-R : rebuild\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>\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 << "-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\n";
std::cerr << "-pic : print intermediate code after each optimization pass\n";
std::cerr << " to file file.opt\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 << "-debug:cc : debug compiling classes\n";
std::cerr << "-debug:cf : debug compiling functions\n";
std::cerr << "-debug:ti : generate debug output for template instantiation\n";
std::cerr << "-debug:arch : generate debug output for generated archetypes\n";
std::cerr << "-debug:nothrow : generate debug output for nothrow calculation\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;
Flags flags = Flags::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")
{
settings.SetOptimizationLevel(parts[1]);
}
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 == "-quiet")
{
flags = flags | Flags::quiet;
}
else if (arg == "-verbose")
{
flags = flags | Flags::verbose;
}
else if (arg == "-debug:cc")
{
flags = flags | Flags::debugCompilingClasses;
}
else if (arg == "-debug:cf")
{
flags = flags | Flags::debugCompilingFunctions;
}
else if (arg == "-debug:ti")
{
flags = flags | Flags::debugTemplateInstantiation;
}
else if (arg == "-debug:arch")
{
flags = flags | Flags::debugArchetypes;
}
else if (arg == "-L")
{
prevWasL = true;
}
else if (arg == "-c")
{
prevWasC = true;
}
else if (arg == "-D")
{
prevWasD = true;
}
else if (arg == "-devenverrorformat")
{
flags = flags | Flags::devEnvErrorFormat;
}
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);
}
}
}
SetFlags(flags);
if ((flags & Flags::quiet) == Flags::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;
}
return 0;
}
catch (const LinkException& ex)
{
if (UseDevEnvErrorFormat())
{
std::cout << ex.what() << std::endl;
PrintErrorForDevEnv("llvm-link", L0 + LINK_ERROR, ex.what(), Position(), Position(), 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(), Position(), 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(), Position(), GetCurrentProject() ? GetCurrentProject()->Name() : "");
}
else
{
std::cerr << ex.what() << std::endl;
}
}
catch (const Exception& ex)
{
if (UseDevEnvErrorFormat())
{
PrintError(ex.Number(), ex, true);
PrintErrorForDevEnv(compilerName, ex.Number(), ex.Message(), ex.ErrorPos(), ex.InnerErrorPos(), 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(), Position(), 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(), Position(), 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(), Position(), GetCurrentProject() ? GetCurrentProject()->Name() : "");
}
else
{
std::cerr << ex.what() << std::endl;
}
}
catch (...)
{
if (UseDevEnvErrorFormat())
{
std::cout << "internal compiler error (access violation)" << std::endl;
PrintErrorForDevEnv(compilerName, I0 + ACCESS_VIOLATION, "internal compiler error (access violation)", Position(), Position(), GetCurrentProject() ? GetCurrentProject()->Name() : "");
}
else
{
std::cerr << "internal compiler error (access violation)" << std::endl;
}
}
return 2;
}

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

Sign up for the SourceForge newsletter:





No, thanks