-
Notifications
You must be signed in to change notification settings - Fork 55
/
sdc.d
116 lines (97 loc) · 2.71 KB
/
sdc.d
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
module driver.sdc;
import source.context;
int main(string[] args) {
import util.main;
return runMain!run(args);
}
int run(Context context, string[] args) {
import sdc.config;
Config conf;
import config.build;
conf.buildGlobalConfig("sdconfig", context);
string[] includePaths, linkerPaths;
bool dontLink, debugBuild, generateMain;
string outputFile;
bool outputLLVM, outputAsm;
import std.getopt;
auto help_info = getopt(
// sdfmt off
args, std.getopt.config.caseSensitive,
"I", "Include path", &includePaths,
"L", "Library path", &linkerPaths,
"O", "Optimization level", &conf.optLevel,
"c", "Stop before linking", &dontLink,
"o", "Output file", &outputFile,
"g", "Generate source-level debug information", &debugBuild,
"S", "Stop before assembling and output assembly file", &outputAsm,
"emit-llvm", "Output LLVM bitcode (-c) or LLVM assembly (-S)", &outputLLVM,
"main", "Generate the main function", &generateMain,
// sdfmt on
);
if (help_info.helpWanted || args.length == 1) {
import std.stdio;
writeln("The Snazzy D Compiler");
writeln("Usage: sdc [options] file.d");
writeln("Options:");
foreach (option; help_info.options) {
writefln(
" %-16s %s",
// bug : optShort is empty if there is no long version
option.optShort.length
? option.optShort
: (option.optLong.length == 3)
? option.optLong[1 .. $]
: option.optLong,
option.help
);
}
return 0;
}
conf.includePaths = includePaths ~ conf.includePaths;
conf.linkerPaths = linkerPaths ~ conf.linkerPaths;
auto files = args[1 .. $];
if (outputAsm) {
dontLink = true;
}
auto executable = "a.out";
auto defaultExtension = ".o";
if (outputAsm) {
defaultExtension = outputLLVM ? ".ll" : ".s";
} else if (dontLink) {
defaultExtension = outputLLVM ? ".bc" : ".o";
}
auto objFile = files[0][0 .. $ - 2] ~ defaultExtension;
if (outputFile.length) {
if (dontLink || outputAsm) {
objFile = outputFile;
} else {
executable = outputFile;
}
}
// If we are generating an executable, we want a main function.
generateMain = generateMain || !dontLink;
// Cannot call the variable "sdc" or DMD complains about name clash
// with the sdc package from the import.
import sdc.sdc;
auto c = new SDC(context, files[0], conf, files, debugBuild);
if (generateMain) {
c.buildMain();
}
if (outputAsm) {
if (outputLLVM) {
c.outputLLVMAsm(objFile);
} else {
c.outputAsm(objFile);
}
} else if (dontLink) {
if (outputLLVM) {
c.outputLLVMBitcode(objFile);
} else {
c.outputObj(objFile);
}
} else {
c.outputObj(objFile);
c.linkExecutable(objFile, executable);
}
return 0;
}