-
Notifications
You must be signed in to change notification settings - Fork 0
/
configure.js
executable file
Β·323 lines (284 loc) Β· 8.39 KB
/
configure.js
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
/* @flow */
const BUILD_DIR = "gen";
const path = require('path');
const fs = require('fs');
const updfile = require('./tools/lib/updfile');
const options = {
compilerBinary: 'clang++',
};
const argv = process.argv;
for (let i = 2; i < argv.length; ++i) {
const arg = argv[i];
switch (arg) {
case '--compiler':
if (++i === argv.length) throw new Error('`--compiler` needs value');
options.compilerBinary = argv[i];
break;
default:
throw new Error('unrecognized argument: `' + arg + '`');
}
}
const OPTIMIZATION_FLAGS = ['-Os'];
if (options.compilerBinary === 'clang++') {
OPTIMIZATION_FLAGS.push('-fno-rtti');
}
const manifest = new updfile.ManifestBuilder();
const COMMON_NATIVE_COMPILE_FLAGS = ["-Wall", '-Wextra', "-MMD"];
if (options.compilerBinary === 'clang++') {
COMMON_NATIVE_COMPILE_FLAGS.push('-Wshadow-all', "-Werror", '-pedantic-errors');
} else {
COMMON_NATIVE_COMPILE_FLAGS.push('-fpermissive');
}
const COMMON_CPLUSPLUS_COMPILE_FLAGS =
COMMON_NATIVE_COMPILE_FLAGS.concat(["-std=c++14"]);
if (options.compilerBinary === 'clang++') {
COMMON_CPLUSPLUS_COMPILE_FLAGS.push("-stdlib=libc++");
}
const compilerPath = resolveBinary(options.compilerBinary);
const compile_optimized_cpp_cli = manifest.cli_template(
compilerPath,
updfile.makeCli(
["-c", "-o", "$output_file"].concat(
COMMON_CPLUSPLUS_COMPILE_FLAGS,
OPTIMIZATION_FLAGS,
["-MF", "$depfile", "$input_files"],
)
)
);
const compile_debug_cpp_cli = manifest.cli_template(compilerPath, [
{literals: ["-c", "-o"], variables: ["output_file"]},
{
literals: COMMON_CPLUSPLUS_COMPILE_FLAGS.concat(['-g', "-MF"]),
variables: ["depfile"]
},
{literals: [], variables: ["input_files"]},
]);
const compile_optimized_c_cli = manifest.cli_template(compilerPath, [
{literals: ["-c", "-o"], variables: ["output_file"]},
{
literals: COMMON_NATIVE_COMPILE_FLAGS.concat(["-x", "c"], OPTIMIZATION_FLAGS, ["-MF"]),
variables: ["depfile"]
},
{literals: [], variables: ["input_files"]},
]);
const compile_debug_c_cli = manifest.cli_template(compilerPath, [
{literals: ["-c", "-o"], variables: ["output_file"]},
{
literals: COMMON_NATIVE_COMPILE_FLAGS.concat(["-x", "c", "-g", "-MF"]),
variables: ["depfile"]
},
{literals: [], variables: ["input_files"]},
]);
const nodePath = resolveBinary('node');
const compiled_tools = manifest.rule(
manifest.cli_template(
nodePath,
updfile.makeCli([
'node_modules/.bin/babel',
"--source-maps", "inline", "--plugins", "transform-flow-strip-types",
"-o", "$output_file", "$input_files"
]),
),
[manifest.source('(tools/**/*.js)')],
`${BUILD_DIR}/($1)`
);
const cppt_sources = manifest.source("(src/**/*.cppt)");
// const cppt_sources = manifest.source("(src/update_log/read_fd_forward.cppt)");
const testingHeaderPath = path.resolve(__dirname, 'tools/lib/testing.h');
const test_cpp_files = manifest.rule(
manifest.cli_template(nodePath, [
{
literals: [`${BUILD_DIR}/tools/compile_test.js`],
variables: ["input_files", "output_file", "depfile"],
},
{
literals: [testingHeaderPath],
}
]),
[cppt_sources],
`${BUILD_DIR}/($1).cpp`,
[compiled_tools]
);
const cli_parser_cpp_file = manifest.rule(
manifest.cli_template(nodePath, [
{
literals: [`${BUILD_DIR}/tools/gen_cli_parser.js`],
variables: ["output_file"]
},
{
literals: [`${BUILD_DIR}/src/cli/parse_options.h`],
variables: ["depfile", "input_files"],
},
]),
[manifest.source("(src/cli/parse_options).json")],
`${BUILD_DIR}/($1).cpp`,
[compiled_tools]
);
const struct_header_files = manifest.rule(
manifest.cli_template(nodePath, [
{
literals: [`${BUILD_DIR}/tools/gen_cpp_struct.js`],
variables: ["output_file", "depfile", "input_files"],
},
]),
[manifest.source("(src/**/*).struct.json")],
`${BUILD_DIR}/($1).h`,
[compiled_tools],
);
const mock_cpp_files = [manifest.source("(src/**/*.mock.cpp)")];
const impl_cpp_files = [manifest.source("(src/**/*.impl.cpp)")];
const cpp_files = [manifest.source("(src/**/*).cpp"), cli_parser_cpp_file];
function compile_cpp(files, type: 'optimized' | 'debug') {
return manifest.rule(
type === 'optimized' ? compile_optimized_cpp_cli : compile_debug_cpp_cli,
files,
`${BUILD_DIR}/${type}/$1.o`,
[cli_parser_cpp_file, struct_header_files],
);
}
const compiled_optimized_cpp_files = compile_cpp(cpp_files, 'optimized');
const compiled_debug_cpp_files = compile_cpp(cpp_files, 'debug');
const compiled_optimized_impl_files = compile_cpp(impl_cpp_files, 'optimized');
const compiled_debug_impl_files = compile_cpp(impl_cpp_files, 'debug');
const compiled_debug_mock_files = compile_cpp(mock_cpp_files, 'debug');
const c_files = manifest.source("(src/**/*).c");
const compiled_optimized_c_files = manifest.rule(
compile_optimized_c_cli,
[c_files],
`${BUILD_DIR}/optimized/$1.o`
);
const compiled_debug_c_files = manifest.rule(
compile_debug_c_cli,
[c_files],
`${BUILD_DIR}/debug/$1.o`
);
const test_manifest_files = manifest.rule(
manifest.cli_template(nodePath, [
{
literals: [`${BUILD_DIR}/tools/gen_test_manifest.js`],
variables: ["output_file", "depfile", "input_files"],
},
]),
[cppt_sources],
`${BUILD_DIR}/($1).json`,
[compiled_tools]
);
const test_index_cpp_file = manifest.rule(
manifest.cli_template(nodePath, [
{
literals: [`${BUILD_DIR}/tools/index_tests.js`],
variables: ["output_file", "depfile"],
},
{
literals: [testingHeaderPath],
variables: ["input_files"],
},
]),
[test_manifest_files],
`${BUILD_DIR}/(tests).cpp`,
[compiled_tools]
);
const package_cpp_file = manifest.rule(
manifest.cli_template(nodePath, [
{
literals: [`${BUILD_DIR}/tools/gen_package_info.js`],
variables: ["output_file", "depfile", "input_files"],
}
]),
[manifest.source("package.json")],
`${BUILD_DIR}/(package).cpp`,
[compiled_tools]
);
const compiled_optimized_main_files = manifest.rule(
compile_optimized_cpp_cli,
[package_cpp_file],
`${BUILD_DIR}/optimized/$1.o`,
[cli_parser_cpp_file]
);
const compiled_debug_main_files = manifest.rule(
compile_debug_cpp_cli,
[package_cpp_file],
`${BUILD_DIR}/debug/$1.o`,
[cli_parser_cpp_file]
);
const compiled_test_files = manifest.rule(
compile_debug_cpp_cli,
[manifest.source("(tools/lib/testing).cpp"), test_cpp_files, test_index_cpp_file],
`${BUILD_DIR}/debug/$1.o`,
[cli_parser_cpp_file, struct_header_files]
);
const commonLinkFlags = ["-Wall", "-std=c++14"];
if (options.compilerBinary === 'clang++') {
commonLinkFlags.push("-stdlib=libc++");
} else {
commonLinkFlags.push('-static-libstdc++', '-static-libgcc');
}
const link_optimized_cpp_cli = manifest.cli_template(compilerPath, [
{literals: ["-o"], variables: ["output_file"]},
{
literals: commonLinkFlags.concat(OPTIMIZATION_FLAGS),
variables: ["input_files"]
},
{literals: ["-lpthread"]},
], {PATH: process.env.PATH || ''});
const link_debug_cpp_cli = manifest.cli_template(compilerPath, [
{literals: ["-o"], variables: ["output_file"]},
{
literals: commonLinkFlags.concat(["-g"]),
variables: ["input_files"]
},
{literals: ["-lpthread"]},
], {PATH: process.env.PATH || ''});
manifest.rule(
manifest.cli_template(resolveBinary('strip'), [
{literals: ['-o'], variables: ['output_file', 'input_files']},
]),
[
manifest.rule(
link_optimized_cpp_cli,
[
compiled_optimized_cpp_files,
compiled_optimized_impl_files,
compiled_optimized_c_files,
compiled_optimized_main_files,
],
`${BUILD_DIR}/pre_strip_upd`
),
],
`dist/upd`
);
manifest.rule(
link_debug_cpp_cli,
[
compiled_debug_cpp_files,
compiled_debug_impl_files,
compiled_debug_c_files,
compiled_debug_main_files,
],
"dist/upd_debug"
);
manifest.rule(
link_debug_cpp_cli,
[
compiled_debug_cpp_files,
compiled_debug_mock_files,
compiled_debug_c_files,
compiled_test_files,
],
"dist/unit_tests"
);
manifest.export(__dirname);
function resolveBinary(name) {
const {PATH} = process.env;
if (PATH == null) {
throw new Error('PATH environment variable is missing');
}
const dirPaths = PATH.split(path.delimiter);
for (const dirPath of dirPaths) {
const filePath = path.resolve(dirPath, name);
if (fs.existsSync(filePath)) {
return filePath;
}
}
throw new Error(`could not resolve binary ${name}`);
}