/
make.js
462 lines (383 loc) · 15.9 KB
/
make.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
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
457
458
459
460
461
462
/* vim: set expandtab ts=4 sw=4: */
/*
* You may redistribute this program and/or modify it under the terms of
* the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
var Fs = require('fs');
var nThen = require('nthen');
var Codestyle = require('./Codestyle');
var Cp = require('./Cp');
var Spawn = require('child_process').spawn;
var Os = require('os');
var FindPython2 = require('./FindPython2');
var CanCompile = require('./CanCompile');
var Builder = require('./builder');
var TestRunner = require('./TestRunner');
// ['linux','darwin','sunos','win32','freebsd','openbsd','netbsd']
var SYSTEM = process.env['SYSTEM'] || process.platform;
var GCC = process.env['CC'];
var CFLAGS = process.env['CFLAGS'];
var LDFLAGS = process.env['LDFLAGS'];
var NO_MARCH_FLAG = ['ppc', 'ppc64'];
if (GCC) {
// Already specified.
} else if (SYSTEM === 'openbsd') {
GCC = 'egcc';
} else if (SYSTEM === 'freebsd') {
GCC = 'clang';
} else {
GCC = 'gcc';
}
Builder.configure({
systemName: SYSTEM,
crossCompiling: process.env['CROSS'] !== undefined,
gcc: GCC,
tempDir: process.env['CJDNS_BUILD_TMPDIR'] || '/tmp',
optimizeLevel: '-O3',
logLevel: process.env['Log_LEVEL'] || 'DEBUG'
}, function (builder, waitFor) {
builder.config.cflags.push(
'-std=c99',
'-Wall',
'-Wextra',
'-Werror',
'-Wno-pointer-sign',
'-pedantic',
'-D', builder.config.systemName + '=1',
'-D', 'CJD_PACKAGE_VERSION="' + builder.config.version + '"',
'-Wno-unused-parameter',
// '-fomit-frame-pointer',
'-D', 'Log_' + builder.config.logLevel,
'-g',
// f4 = 16 peers max, fixed width 4 bit
// f8 = 241 peers max, fixed width 8 bit
// v3x5x8 = 256 peers max, variable width, 3, 5 or 8 bits plus 1 or 2 bits of prefix
// v4x8 = 256 peers max, variable width, 4, or 8 bits plus 1 bit prefix
'-D', 'NumberCompress_TYPE=v3x5x8',
// enable for safety (don't worry about speed, profiling shows they add ~nothing)
'-D', 'Identity_CHECK=1',
'-D', 'Allocator_USE_CANARIES=1',
'-D', 'PARANOIA=1'
);
if (process.env['SUBNODE']) { builder.config.cflags.push('-DSUBNODE=1'); }
if (process.env['GCOV']) {
builder.config.cflags.push('-fprofile-arcs', '-ftest-coverage');
builder.config.ldflags.push('-fprofile-arcs', '-ftest-coverage');
}
var android = /android/i.test(builder.config.gcc);
if (process.env['TESTING']) {
builder.config.cflags.push('-D', 'TESTING=1');
}
if (!builder.config.crossCompiling) {
if (NO_MARCH_FLAG.indexOf(process.arch) < -1) {
builder.config.cflags.push('-march=native');
}
}
if (builder.config.systemName === 'win32') {
builder.config.cflags.push('-Wno-format');
} else if (builder.config.systemName === 'linux') {
builder.config.ldflags.push('-Wl,-z,relro,-z,now,-z,noexecstack');
builder.config.cflags.push('-DHAS_ETH_INTERFACE=1');
} else if (builder.config.systemName === 'darwin') {
builder.config.cflags.push('-DHAS_ETH_INTERFACE=1');
}
if (process.env['NO_PIE'] === undefined && builder.config.systemName !== 'freebsd'
&& builder.config.systemName !== 'win32')
{
builder.config.cflags.push('-fPIE');
// just using `-pie` on OS X >= 10.10 results in this warning:
// clang: warning: argument unused during compilation: '-pie'
if (builder.config.systemName !== "darwin")
{
builder.config.ldflags.push('-pie');
} else {
builder.config.ldflags.push('-Wl,-pie');
}
}
if (builder.config.compilerType.isClang) {
// blows up when preprocessing before js preprocessor
builder.config.cflags.push(
'-Wno-invalid-pp-token',
'-Wno-dollar-in-identifier-extension',
'-Wno-newline-eof',
'-Wno-unused-value',
// lots of places where depending on preprocessor conditions, a statement might be
// a case of if (1 == 1)
'-Wno-tautological-compare',
'-Wno-error'
);
builder.config.cflags.slice(builder.config.cflags.indexOf('-Werror'), 1);
}
// Install any user-defined CFLAGS. Necessary if you are messing about with building cnacl
// with NEON on the BBB, or want to set -Os (OpenWrt)
// Allow -O0 so while debugging all variables are present.
if (CFLAGS) {
var cflags = CFLAGS.split(' ');
cflags.forEach(function(flag) {
if (/^\-O[^02s]$/.test(flag)) {
console.log("Skipping " + flag + ", assuming " +
builder.config.optimizeLevel + " instead.");
} else if (/^\-O[02s]$/.test(flag)) {
builder.config.optimizeLevel = flag;
} else {
[].push.apply(builder.config.cflags, cflags);
}
});
}
// We also need to pass various architecture/floating point flags to GCC when invoked as
// a linker.
if (LDFLAGS) {
[].push.apply(builder.config.ldflags, LDFLAGS.split(' '));
}
if (android) {
builder.config.cflags.push('-Dandroid=1');
}
CanCompile.check(builder,
'int main() { return 0; }',
[ builder.config.cflags, '-flto', '-x', 'c' ],
function (err, can) {
if (can) {
console.log("Compiler supports link time optimization");
builder.config.ldflags.push(
'-flto',
builder.config.optimizeLevel
);
} else {
console.log("Link time optimization not supported [" + err + "]");
}
builder.config.cflags.push(builder.config.optimizeLevel);
});
var uclibc = process.env['UCLIBC'] == '1';
var libssp;
switch (process.env['SSP_SUPPORT']) {
case 'y':
case '1': libssp = true; break;
case 'n':
case '' :
case '0': libssp = false; break;
case undefined: break;
default: throw new Error();
}
if (libssp === false) {
console.log("Stack Smashing Protection (security feature) is disabled");
} else if (builder.config.systemName == 'win32') {
builder.config.libs.push('-lssp');
} else if ((!uclibc && builder.config.systemName !== 'sunos') || libssp === true) {
builder.config.cflags.push(
// Broken GCC patch makes -fstack-protector-all not work
// workaround is to give -fno-stack-protector first.
// see: https://bugs.launchpad.net/ubuntu/+source/gcc-4.5/+bug/691722
'-fno-stack-protector',
'-fstack-protector-all',
'-Wstack-protector'
);
// Static libssp provides __stack_chk_fail_local, which x86 needs in
// order to avoid expensively looking up the location of __stack_chk_fail.
var x86 = process.env['TARGET_ARCH'] == 'i386';
if (uclibc) {
if (x86) {
builder.config.libs.push('-Wl,-Bstatic', '-lssp', '-Wl,-Bdynamic');
} else {
builder.config.libs.push('-lssp');
}
}
} else {
console.log("Stack Smashing Protection (security feature) is disabled");
}
if (process.env['Pipe_PREFIX'] !== undefined) {
builder.config.cflags.push(
'-D', 'Pipe_PREFIX="' + process.env['Pipe_PREFIX'] + '"'
);
}
var dependencyDir = builder.config.buildDir + '/dependencies';
var libuvLib = dependencyDir + '/libuv/out/Release/libuv.a';
if (['win32', 'netbsd'].indexOf(builder.config.systemName) >= 0) {//this might be needed for other BSDs
libuvLib = dependencyDir + '/libuv/out/Release/obj.target/libuv.a';
}
// Build dependencies
nThen(function (waitFor) {
Fs.exists(dependencyDir, waitFor(function (exists) {
if (exists) { return; }
console.log("Copy dependencies");
Cp('./node_build/dependencies', dependencyDir, waitFor());
}));
}).nThen(function (waitFor) {
builder.config.libs.push(dependencyDir + '/cnacl/jsbuild/libnacl.a');
builder.config.includeDirs.push(dependencyDir + '/cnacl/jsbuild/include/');
// needed for Sign.c which pulls in crypto_int32.h
builder.config.includeDirs.push(dependencyDir + '/cnacl/jsbuild/include_internal/');
Fs.exists(dependencyDir + '/cnacl/jsbuild/libnacl.a', waitFor(function (exists) {
if (exists) { return; }
console.log("Build NaCl");
var cwd = process.cwd();
process.chdir(dependencyDir + '/cnacl/');
var NaCl = require(process.cwd() + '/node_build/make.js');
NaCl.build(function (args, callback) {
if (builder.config.systemName !== 'win32') {
args.unshift('-fPIC');
}
args.unshift(builder.config.optimizeLevel, '-fomit-frame-pointer');
if (CFLAGS) {
[].push.apply(args, CFLAGS.split(' '));
}
if (!builder.config.crossCompiling) {
if (NO_MARCH_FLAG.indexOf(process.arch) < -1) {
builder.config.cflags.push('-march=native');
}
}
builder.cc(args, callback);
},
builder.config,
waitFor(function () {
process.chdir(cwd);
}));
}));
}).nThen(function (waitFor) {
builder.config.libs.push(libuvLib);
if (!android) {
builder.config.libs.push('-lpthread');
}
if (builder.config.systemName === 'win32') {
builder.config.libs.push(
'-lws2_32',
'-lpsapi', // GetProcessMemoryInfo()
'-liphlpapi' // GetAdapterAddresses()
);
} else if (builder.config.systemName === 'linux' && !android) {
builder.config.libs.push('-lrt'); // clock_gettime()
} else if (builder.config.systemName === 'darwin') {
builder.config.libs.push('-framework', 'CoreServices');
} else if (['freebsd', 'openbsd', 'netbsd'].indexOf(builder.config.systemName) >= 0) {
builder.config.cflags.push('-Wno-overlength-strings');
builder.config.libs.push('-lkvm');
} else if (builder.config.systemName === 'sunos') {
builder.config.libs.push(
'-lsocket',
'-lsendfile',
'-lkstat',
'-lnsl'
);
}
builder.config.includeDirs.push(dependencyDir + '/libuv/include/');
var libuvBuilt;
var python;
nThen(function (waitFor) {
Fs.exists(libuvLib, waitFor(function (exists) {
if (exists) { libuvBuilt = true; }
}));
}).nThen(function (waitFor) {
if (libuvBuilt) { return; }
FindPython2.find(builder.tmpFile(), waitFor(function (err, pythonExec) {
if (err) { throw err; }
python = pythonExec;
}));
}).nThen(function (waitFor) {
if (libuvBuilt) { return; }
console.log("Build Libuv");
var cwd = process.cwd();
process.chdir(dependencyDir + '/libuv/');
var args = ['./gyp_uv.py'];
var env = process.env;
env.CC = builder.config.gcc;
if (env.TARGET_ARCH) {
args.push('-Dtarget_arch=' + env.TARGET_ARCH);
}
//args.push('--root-target=libuv');
if (android) {
args.push('-DOS=android');
}
if (builder.config.systemName === 'win32') {
args.push('-DOS=win');
}
if (env.GYP_ADDITIONAL_ARGS) {
args.push.apply(args, env.GYP_ADDITIONAL_ARGS.split(' '));
}
if (['freebsd', 'openbsd', 'netbsd'].indexOf(builder.config.systemName) !== -1) {
// This platform lacks a functioning sem_open implementation, therefore...
args.push('--no-parallel');
args.push('-DOS=' + builder.config.systemName);
}
var gyp = Spawn(python, args, {env:env, stdio:'inherit'});
gyp.on('error', function () {
console.error("couldn't launch gyp [" + python + "]");
});
gyp.on('close', waitFor(function () {
var args = [
'-j', builder.processors,
'-C', 'out',
'BUILDTYPE=Release',
'CC=' + builder.config.gcc,
'CXX=' + builder.config.gcc,
'V=1'
];
var cflags = [builder.config.optimizeLevel, '-DNO_EMFILE_TRICK=1'];
if (!(/darwin|win32/i.test(builder.config.systemName))) {
cflags.push('-fPIC');
}
args.push('CFLAGS=' + cflags.join(' '));
var makeCommand = ['freebsd', 'openbsd', 'netbsd'].indexOf(builder.config.systemName) >= 0 ? 'gmake' : 'make';
var make = Spawn(makeCommand, args, {stdio: 'inherit'});
make.on('error', function (err) {
if (err.code === 'ENOENT') {
console.error('\033[1;31mError: ' + makeCommand + ' is required!\033[0m');
} else {
console.error(
'\033[1;31mFail run ' + process.cwd() + ': ' + makeCommand + ' '
+ args.join(' ') + '\033[0m'
);
console.error('Message:', err);
}
waitFor.abort();
});
make.on('close', waitFor(function () {
process.chdir(cwd);
}));
}));
}).nThen(waitFor());
}).nThen(waitFor());
}).build(function (builder, waitFor) {
builder.buildExecutable('client/cjdroute2.c', 'cjdroute');
builder.buildExecutable('contrib/c/publictoip6.c');
builder.buildExecutable('contrib/c/privatetopublic.c');
builder.buildExecutable('contrib/c/sybilsim.c');
builder.buildExecutable('contrib/c/makekeys.c');
builder.buildExecutable('contrib/c/mkpasswd.c');
builder.buildExecutable('crypto/random/randombytes.c');
builder.lintFiles(function (fileName, file, callback) {
if (/dependencies/.test(fileName)) {
callback('', false);
return;
}
Codestyle.lint(fileName, file, callback);
});
var testcjdroute = builder.buildTest('test/testcjdroute.c');
if (builder.config.crossCompiling) {
console.log("Cross compiling. Building, but not running tests.");
return;
}
var testRunner = TestRunner.local(['all']);
if (process.env['REMOTE_TEST']) {
testRunner = TestRunner.remote(process.env['REMOTE_TEST'], ['all']);
}
if (!process.env['NO_TEST']) {
builder.runTest(testcjdroute, testRunner);
}
}).success(function (builder, waitFor) {
console.log('\033[1;32mBuild completed successfully, type ./cjdroute to begin setup.\033[0m');
}).failure(function (builder, waitFor) {
console.log('\033[1;31mFailed to build cjdns.\033[0m');
process.exit(1);
}).complete(function (builder, waitFor) {
if (builder.failure) {
process.exit(1);
}
});