This repository has been archived by the owner on Jan 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 47
/
warpdrive.d
189 lines (171 loc) · 4.79 KB
/
warpdrive.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
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
/**
* C preprocessor driver
* Copyright 2014 Facebook, Inc.
* License: $(LINK2 http://boost.org/LICENSE_1_0.txt, Boost License 1.0).
* Authors: Andrei Alexandrescu
*/
// Forwards to warp by emulating the predefined options defined by gcc
// 4.7 or gcc 4.8. Use -version=gcc47 or -version=gcc48 when building.
//
// The predefined preprocessor flags have been
// obtained by running:
//
// g++ -dM -E - </dev/null|sed -e "s/#define /\"-D'/" -e "s/ /=/" -e "s/\$/' \"/"
//
// with the appropriate version of gcc.
import std.algorithm, std.datetime, std.path, std.process, std.stdio,
std.string;
// Uncomment this line to debug this script
//debug = warpdrive;
// Import the appropriate built-in #define menagerie
version (gcc4_7_1) {
version = gnu;
import defines_gcc4_7_1, defines_gxx4_7_1;
}
version (gcc4_8_1) {
version = gnu;
import defines_gcc4_8_1, defines_gxx4_8_1;
}
version (clang3_2) {
version = clang;
import defines_clang3_2, defines_clangxx3_2;
}
version (clang3_4) {
version = clang;
import defines_clang3_4, defines_clangxx3_4;
}
version (clangdev) {
version = clang;
import defines_clangdev, defines_clangxxdev;
}
// Compiler-dependent extra defines
version (gnu) {
immutable extras = [
"-D_GNU_SOURCE=1",
"-D__USE_GNU=1",
];
}
version (clang) {
immutable extras = [
"-D__has_attribute(x)=0",
"-D__has_builtin(x)=0",
"-D__has_extension(x)=0",
"-D__has_feature(x)=0",
"-D__has_include(x)=0",
"-D__has_include_next(x)=0",
"-D__has_warning(x)=0",
];
}
// Defines for all builds
immutable defaultOptions = [
"-D__I86__=6",
"-D__FUNC__=__func__",
"-D__FUNCTION__=__func__",
"-D__PRETTY_FUNCTION__=__func__",
];
// Path to warp binary. If relative, it's assumed to start in the same
// dir as warpdrive. If absolute, then, well, it's absolute.
immutable warp = "warp";
int main(string[] args) {
debug (warpdrive) stderr.writefln("Exec@%s: %s", Clock.currTime(), warp);
debug (warpdrive) stderr.writeln(args);
string[] options;
// The warp binary is assumed to be in the same directory as
// warpdrive UNLESS it is actually an absolute path.
options ~= warp.startsWith('/') ? warp : buildPath(args[0].dirName, warp);
options ~= defaultOptions;
options ~= extras;
string toCompile;
bool dashOhPassed = false;
for (size_t i = 1; i < args.length; ++i) {
auto arg = args[i];
if (arg.startsWith("-")) {
if (arg == "--param" || arg == "-iprefix") {
// There are options set with --param name=value, see e.g.
// http://gcc.gnu.org/onlinedocs/gcc-3.4.5/gcc/Optimize-Options.html
// Just skip those. Also skip -iprefix /some/path/. Note that
// the versions without space after the flag name are taken
// care of in the test coming after this.
++i;
continue;
}
if (arg == "-o") {
dashOhPassed = true;
options ~= "-o";
options ~= args[++i];
continue;
}
// __SANITIZE_ADDRESS__ for ASAN
if (arg == "-fsanitize=address") {
options ~= "-D__SANITIZE_ADDRESS__=1";
continue;
}
// __OPTIMIZE__
if (arg.startsWith("-O") && arg != "-O0") {
options ~= "-D__OPTIMIZE__=1";
options ~= "-D__USE_EXTERN_INLINES=1";
continue;
}
if (!arg.startsWith("-isystem", "-I", "-d", "-MF", "-MQ", "-D",
"-MD", "-MMD")) {
continue;
}
// Sometimes there may be a space between -I and the path, or
// between "-D" and the macro defined. Merge them.
if (arg == "-I" || arg == "-D") {
assert(i + 1 < args.length);
options ~= arg;
options ~= args[++i];
continue;
}
} else {
// This must be the file to compile
assert(!toCompile, toCompile);
toCompile = arg;
continue;
}
if (arg == "-MF" || arg == "-MD" || arg == "-MMD") {
// This is a weird one: optional parameter after -MMD
if (i + 1 < args.length && args[i + 1].startsWith('-')) {
// Optional argument
continue;
}
options ~= "--dep=" ~ args[++i];
continue;
}
if (arg == "-MQ") {
// just skip the object file
++i;
continue;
}
if (arg == "-isystem") {
arg = "--isystem=" ~ args[++i];
}
options ~= arg;
}
// If no -o default to output to stdout
if (!dashOhPassed) {
options ~= "--stdout";
}
if (toCompile.endsWith(".c")) {
options ~= defines;
} else {
options ~= xxdefines;
}
// Add the file to compile at the very end for easy spotting by humans
options ~= toCompile;
debug (warpdrive) {
string cmd;
foreach (opt; options) {
cmd ~= " " ~ escapeShellCommand(opt);
}
stderr.writeln(cmd);
}
return execvp(options[0], options);
}
/*
* Local Variables:
* mode: d
* c-basic-offset: 4
* End:
*/