forked from evanw/thinscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
123 lines (102 loc) · 3.56 KB
/
build.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
var child_process = require('child_process');
var fs = require('fs');
eval(fs.readFileSync('./out/common.js', 'utf8'));
// Always build all targets to catch errors in other targets
function compile(compiler, sources) {
var compiled = compileJavaScript(compiler);
var compiledC = compiled(sources, 'C', 'compiled');
if (compiledC.stdout) process.stdout.write(compiledC.stdout);
if (!compiledC.success) process.exit(1);
var compiledJS = compiled(sources, 'JavaScript', 'compiled');
if (compiledJS.stdout) process.stdout.write(compiledJS.stdout);
if (!compiledJS.success) process.exit(1);
var compiledWASM = compiled(sources, 'WebAssembly', 'compiled');
if (compiledWASM.stdout) process.stdout.write(compiledWASM.stdout);
if (!compiledWASM.success) process.exit(1);
return {
c: compiledC.output,
h: compiledC.secondaryOutput,
js: compiledJS.output,
wasm: compiledWASM.output,
};
}
function compileNativeUnix() {
try {
var command = [
'cc',
__dirname + '/lib/thinc.c',
__dirname + '/out/compiled.c',
'-o', __dirname + '/out/thinc',
'-Wall',
'-Wextra',
'-Wno-unused-parameter',
'-Wno-unused-function',
'-std=c99',
'-O3',
];
console.log(command.join(' '));
var child = child_process.spawn(command.shift(), command, {stdio: 'inherit'});
} catch (e) {
console.log('failed to build the native compiler');
}
}
function compileNativeWindows() {
// Find all installed Visual Studio versions
var versions = [];
Object.keys(process.env).forEach(function(key) {
var match = /^VS(\d+)COMNTOOLS$/.exec(key);
if (match) {
versions.push(match[1] | 0);
}
});
// Try the compilers in descending order
versions.sort(function(a, b) {
return b - a;
});
next();
function next() {
if (!versions.length) {
console.log('failed to build the native compiler');
return;
}
var version = versions.shift();
var folder = process.env['VS' + version + 'COMNTOOLS'];
var child = child_process.spawn('cmd.exe', [], {cwd: __dirname, stdio: ['pipe', process.stdout, process.stderr]});
child.stdin.write('"' + folder + '/../../VC/bin/vcvars32.bat"\n');
child.stdin.write('cl.exe /O2 lib/thinc.c out/compiled.c /Fe"out/thinc.exe"\n');
child.stdin.end();
child.on('close', function(code) {
if (code !== 0 || !fs.existsSync(__dirname + '/out/thinc.exe')) {
next();
}
});
}
}
var sourceDir = __dirname + '/src';
var sources = [];
fs.readdirSync(sourceDir).forEach(function(entry) {
if (/\.thin$/.test(entry)) {
sources.push({
name: entry,
contents: fs.readFileSync(sourceDir + '/' + entry, 'utf8').replace(/\r\n/g, '\n'),
});
}
});
var compiled = fs.readFileSync(__dirname + '/out/compiled.js', 'utf8');
console.log('compiling...');
var compiled = compile(compiled, sources);
console.log('compiling again...');
var compiled = compile(compiled.js, sources);
console.log('compiling again...');
var compiled = compile(compiled.js, sources);
fs.writeFileSync(__dirname + '/out/compiled.c', compiled.c);
fs.writeFileSync(__dirname + '/out/compiled.h', compiled.h);
console.log('wrote to "out/compiled.c"');
console.log('wrote to "out/compiled.h"');
fs.writeFileSync(__dirname + '/out/compiled.js', compiled.js);
console.log('wrote to "out/compiled.js"');
fs.writeFileSync(__dirname + '/out/compiled.wasm', Buffer(compiled.wasm));
console.log('wrote to "out/compiled.wasm"');
console.log('building the native compiler...');
if (process.platform === 'win32') compileNativeWindows();
else compileNativeUnix();