-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
112 lines (88 loc) · 2.99 KB
/
index.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
'use strict'
var assert = require('assert')
var path = require('path')
var fs = require('fs')
var cp = require('child_process')
var os = require('os')
module.exports = function requireEmscripten(file, options) {
assert(path.isAbsolute(file),
'requireEmscripten() needs an absolute file path, unlike require()!')
var outFile = file + '.requireemscripten.js'
if (require.cache && (outFile in require.cache)) {
// We've already compiled this, and required it.
// Node has helpfully cached it, so no need to recompile.
return require(outFile)
}
compile(file, options)
return require(outFile)
}
var shellReplace =
module.exports.shellReplace =
function shellReplace(string, variables) {
for (var key in variables) if (variables.hasOwnProperty(key)) {
string = string
.replace('$' + key, variables[key])
}
return string
}
var readConfig =
module.exports.readConfig =
function readConfig(file) {
var emccExecutable = file.match(/\/\*\s*?require-emscripten-emcc-executable[: ]\s*?(.*?)\s*?\*\//)
if (emccExecutable) {
emccExecutable = emccExecutable[1]
}
var toBitcode = file.match(/\/\*\s*?require-emscripten-to-bitcode[: ]\s*?(.*?)\s*?\*\//)
if (toBitcode) {
toBitcode = toBitcode[1]
}
var theComment = file.match(/\/\*\s*?require-emscripten[: ]\s*?(.*?)\s*?\*\//)
return {
emccExecutable: emccExecutable,
toBitcode: toBitcode,
cliArgs: theComment ? theComment[1].trim() : ''
}
}
var emccOrEmccBat = os.type() === 'Windows_NT' ? 'emcc.bat' : 'emcc'
var compile =
module.exports.compile =
function (file, config) {
var outp = file + '.requireemscripten.js'
var bcOutp = file + '.requireemscripten.bc'
var inputFile = fs.readFileSync(file, 'utf-8')
if (!config)
config = module.exports.readConfig(inputFile, { INPUT: file, OUTPUT: bcOutp })
if (config.toBitcode) {
// Input file for emscripten is the .bc output from the user compiler
var toBitcodeCommand = module.exports.shellReplace(
config.toBitcode, { INPUT: file, OUTPUT: bcOutp })
cp.execSync(toBitcodeCommand)
file = bcOutp
}
var command = config.emccExecutable ? config.emccExecutable :
emccOrEmccBat;
var preJs = __dirname + '/pre-js.prejs'
var postJs = __dirname + '/post-js.postjs'
var commandArgs = [
file,
'--pre-js', preJs,
'--post-js', postJs,
'--memory-init-file', '0',
'-s', 'EXPORT_ALL=1',
'-s', 'LINKABLE=1',
];
commandArgs = commandArgs
.concat(config.cliArgs ?
config.cliArgs.split(/\s+/g) :
[])
.concat(['-o', outp])
var result = cp.spawnSync(command, commandArgs)
if (result && result.error)
throw result.error
if (result && result.status)
throw new Error('emcc command failed!\n' + result.stderr + '')
if (config.toBitcode) {
fs.unlinkSync(bcOutp)
}
return outp
}