Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/tools/compiler.mjs
4128 views
1
#!/usr/bin/env node
2
/**
3
* @license
4
* Copyright 2010 The Emscripten Authors
5
* SPDX-License-Identifier: MIT
6
*/
7
8
// JavaScript compiler, main entry point
9
10
import assert from 'node:assert';
11
import {parseArgs} from 'node:util';
12
import {
13
Benchmarker,
14
applySettings,
15
loadDefaultSettings,
16
printErr,
17
readFile,
18
} from '../src/utility.mjs';
19
20
loadDefaultSettings();
21
22
const options = {
23
help: {type: 'boolean', short: 'h'},
24
'symbols-only': {type: 'boolean'},
25
output: {type: 'string', short: 'o'},
26
};
27
const {values, positionals} = parseArgs({options, allowPositionals: true});
28
29
if (values.help) {
30
console.log(`\
31
Main entry point for JS compiler
32
33
If no -o file is specified then the generated code is written to stdout.
34
35
Usage: compiler.mjs <settings.json> [-o out.js] [--symbols-only]`);
36
process.exit(0);
37
}
38
39
// Load settings from JSON passed on the command line
40
let settingsFile = positionals[0];
41
assert(settingsFile, 'settings file not specified');
42
if (settingsFile == '-') {
43
// Read settings json from stdin (FD 0)
44
settingsFile = 0;
45
}
46
const userSettings = JSON.parse(readFile(settingsFile));
47
applySettings(userSettings);
48
49
export const symbolsOnly = values['symbols-only'];
50
51
// TODO(sbc): Remove EMCC_BUILD_DIR at some point. It used to be required
52
// back when ran the JS compiler with overridden CWD.
53
process.env['EMCC_BUILD_DIR'] = process.cwd();
54
55
// In case compiler.mjs is run directly (as in gen_sig_info)
56
// ALL_INCOMING_MODULE_JS_API might not be populated yet.
57
if (!ALL_INCOMING_MODULE_JS_API.length) {
58
ALL_INCOMING_MODULE_JS_API = INCOMING_MODULE_JS_API;
59
}
60
61
EXPORTED_FUNCTIONS = new Set(EXPORTED_FUNCTIONS);
62
WASM_EXPORTS = new Set(WASM_EXPORTS);
63
SIDE_MODULE_EXPORTS = new Set(SIDE_MODULE_EXPORTS);
64
INCOMING_MODULE_JS_API = new Set(INCOMING_MODULE_JS_API);
65
ALL_INCOMING_MODULE_JS_API = new Set(ALL_INCOMING_MODULE_JS_API);
66
EXPORTED_RUNTIME_METHODS = new Set(EXPORTED_RUNTIME_METHODS);
67
WEAK_IMPORTS = new Set(WEAK_IMPORTS);
68
if (symbolsOnly) {
69
INCLUDE_FULL_LIBRARY = 1;
70
}
71
72
// Side modules are pure wasm and have no JS
73
assert(
74
!SIDE_MODULE || (ASYNCIFY && symbolsOnly),
75
'JS compiler should only run on side modules if asyncify is used.',
76
);
77
78
// Load compiler code
79
80
// We can't use static import statements here because several of these
81
// file depend on having the settings defined in the global scope (which
82
// we do dynamically above.
83
await import('../src/modules.mjs');
84
await import('../src/parseTools.mjs');
85
if (!STRICT) {
86
await import('../src/parseTools_legacy.mjs');
87
}
88
const jsifier = await import('../src/jsifier.mjs');
89
90
// ===============================
91
// Main
92
// ===============================
93
94
const B = new Benchmarker();
95
96
try {
97
await jsifier.runJSify(values.output, symbolsOnly);
98
99
B.print('glue');
100
} catch (err) {
101
if (err.toString().includes('Aborting compilation due to previous errors')) {
102
// Compiler failed on user error, don't print the stacktrace in this case.
103
printErr(err);
104
} else {
105
// Compiler failed on internal compiler error!
106
printErr('Internal compiler error JS compiler');
107
printErr('Please create a bug report at https://github.com/emscripten-core/emscripten/issues/');
108
printErr(
109
'with a log of the build and the input files used to run. Exception message: "' +
110
(err.stack || err),
111
);
112
}
113
114
// Work around a node.js bug where stdout buffer is not flushed at process exit:
115
// Instead of process.exit() directly, wait for stdout flush event.
116
// See https://github.com/joyent/node/issues/1669 and https://github.com/emscripten-core/emscripten/issues/2582
117
// Workaround is based on https://github.com/RReverser/acorn/commit/50ab143cecc9ed71a2d66f78b4aec3bb2e9844f6
118
process.stdout.once('drain', () => process.exit(1));
119
// Make sure to print something to force the drain event to occur, in case the
120
// stdout buffer was empty.
121
console.log(' ');
122
// Work around another node bug where sometimes 'drain' is never fired - make
123
// another effort to emit the exit status, after a significant delay (if node
124
// hasn't fired drain by then, give up)
125
setTimeout(() => process.exit(1), 500);
126
}
127
128