Skip to content

Commit d0f1540

Browse files
committed
Support docker emscripten 3.0.1
1 parent 573335b commit d0f1540

File tree

1 file changed

+36
-6
lines changed

1 file changed

+36
-6
lines changed

bin/compile.js

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,18 @@ var child_process = require('child_process');
77
var rpython = path.join(__dirname, 'rpython');
88
var rpydir = path.join(rpython, '../..')
99
var platform = os.platform();
10+
var uid = os.userInfo().uid;
11+
var gid = os.userInfo().gid;
1012

1113
process.env.RPY_USE_EMSCRIPTEN = 'true';
1214

13-
function check_exist(command, dont_append_version) {
15+
function check_exist(command, dont_append_version, log_error) {
1416
try {
1517
child_process.execSync(command + (dont_append_version ? '' : ' --version'), {stdio: 'ignore'});
1618
return true;
1719
}
1820
catch (error) {
21+
if (log_error) console.log(error);
1922
return false;
2023
}
2124
}
@@ -69,33 +72,39 @@ function rpythonShrinkToInitial(copy) {
6972

7073
var use_wasm = process.argv.indexOf('--wasm') !== -1;
7174

72-
var emcc = platform === 'win32' ? 'emcc.bat' : 'emcc';
75+
var use_docker = process.argv.indexOf('--docker') !== -1;
76+
77+
var emcc = (platform === 'win32' && !use_docker) ? 'emcc.bat' : 'emcc';
7378
var python = 'pypy';
7479
if (!check_exist('pypy')) {
7580
python = 'python2.7';
7681
if (!check_exist('python2.7')) throw new Error('PyPy (pypy not pypy3) or Python 2.7 (python2.7) must be installed and exist on PATH');
7782
}
7883
if (!check_exist('make -v', true)) throw new Error('make (usually comes from build-essential, or just install the standalone package) must be installed and exist on PATH');
7984
//if (!check_exist('gcc -v', true)) console.error('GCC (gcc) is somewhat needed, but not necessary');
80-
if (!check_exist(emcc + ' -v', true)) throw new Error('emcc (comes with emsdk) must be installed and exist on PATH');
81-
var tempdir = path.join(os.tmpdir(), 'rpython-' + (new Date()).getTime());
85+
if (!use_docker && !check_exist(emcc + ' -v', true)) throw new Error('emcc (comes with emsdk) must be installed and exist on PATH');
86+
if (use_docker && !check_exist('docker -v', true)) throw new Error('Docker must be installed and exist on PATH');
87+
var namedir = 'rpython-' + (new Date()).getTime();
88+
var tempdir = path.join(os.tmpdir(), namedir);
8289
fs.mkdirSync(tempdir);
8390
process.env.RPYTHON_TARGET_FILE = process.argv[2];
8491
process.env.PYPY_USESSION_DIR = platform === 'win32' ? cygpath(tempdir) : tempdir;
8592
process.env.USER = 'current';
8693
child_process.execSync([python, rpython, '--gc=none', '--no-translation-jit', '-s'].concat(process.argv.slice(2)).join(' '), {stdio: 'inherit', env: process.env});
8794
async function handle() {
8895
var file = process.argv[2].split('.py')[0];
96+
var usession = path.join(tempdir, 'usession-unknown-0');
8997
var directory = path.join(tempdir, 'usession-unknown-0', 'testing_1');
9098
var makefile = path.join(directory, 'Makefile');
9199
var make = fs.readFileSync(makefile).toString();
92100
var debug_flag = process.argv.indexOf('--debug') !== -1;
93101
var source_flag = process.argv.indexOf('--source-map') !== -1;
94102
if (process.argv.indexOf('--use-pthread') === -1) make = make.replace(/-pthread/g, '');
95103
if (platform === 'win32') make = make.replace('RPYDIR = ', 'RPYDIR = "' + rpydir + '"#')
104+
if (use_docker) make = make.replace('RPYDIR = ', 'RPYDIR = "/src/rpython/"#')
96105
make = make.replace(/-lutil/g, '');
97106
make = make.replace(/--export-all-symbols/g, '--export-dynamic');
98-
make = make.replace('CC = ', 'CC = ' + emcc + (!use_wasm ? ' -s WASM=0 ' : ' ') + '-fdiagnostics-color=always -fsanitize=undefined -s ALLOW_MEMORY_GROWTH=1 -s \'EXPORTED_FUNCTIONS=["_main", "_malloc", "_onresolve", "_onfunctioncall"]\' -s \'EXPORTED_RUNTIME_METHODS=["ccall", "wasmMemory"]\'' + (debug_flag ? ' -g3' : (source_flag ? ' -g4' : '')) + ' #');
107+
make = make.replace('CC = ', 'CC = ' + emcc + (!use_wasm ? ' -s WASM=0 ' : ' ') + '-fdiagnostics-color=always -s ALLOW_MEMORY_GROWTH=1 -s \'EXPORTED_FUNCTIONS=["_main", "_malloc", "_onresolve", "_onfunctioncall"]\' -s \'EXPORTED_RUNTIME_METHODS=["ccall"]\'' + (debug_flag ? ' -g3' : (source_flag ? ' -g4' : '')) + ' #');
99108
make = make.replace('TARGET = ', 'TARGET = ' + file + '.js #');
100109
make = make.replace('DEFAULT_TARGET = ', 'DEFAULT_TARGET = ' + file + '.js #');
101110
fs.writeFileSync(makefile, make);
@@ -105,12 +114,32 @@ async function handle() {
105114
if (platform === 'darwin') {
106115
process.env.C_INCLUDE_PATH = path.join(__dirname, '../dmidecode');
107116
}
117+
if (use_docker) {
118+
child_process.execSync('docker volume create ' + namedir);
119+
child_process.execSync('docker create --name ' + namedir + ' -v ' + namedir + ':/rpython hello-world');
120+
child_process.execSync(`docker cp ${tempdir} ${namedir}:/rpython/`);
121+
child_process.execSync(`docker cp ${rpydir} ${namedir}:/rpython/`);
122+
child_process.execSync('docker rm ' + namedir);
123+
}
108124
var error = '';
109-
var code = await new Promise((resolve) => child_process.spawn('make', ['-j', cores], {env: process.env, stdio: ['inherit', 'inherit', 'pipe'], cwd: directory}).on('close', resolve).stderr.on('data', (data) => process.stderr.write(error += data.toString())));
125+
var command = 'make';
126+
var args = ['-j', cores];
127+
if (use_docker) {
128+
args = ['run', ...(platform === 'darwin' ? ['-e', 'C_INCLUDE_PATH=/src/rpython/dmidecode'] : []), '--rm', '-v', `${namedir}:/src`, '-w', '/src/' + namedir + '/usession-unknown-0/testing_1','emscripten/emsdk:3.0.1', command].concat(args);
129+
command = 'docker';
130+
}
131+
var code = await new Promise((resolve) => child_process.spawn(command, args, {env: process.env, stdio: ['inherit', 'inherit', 'pipe'], cwd: directory}).on('close', resolve).stderr.on('data', (data) => process.stderr.write(/*error +=*/ data.toString())));
110132
if (code !== 0) {
111133
if (error.includes('wasm2js is not compatible with USE_OFFSET_CONVERTER')) throw new Error('\x1b[31mYour emcc wasm2js support is not patched yet, add --wasm to rpython command or comment out "wasm2js is not compatible with USE_OFFSET_CONVERTER" in emcc.py\x1b[0m').toString();
134+
if (use_docker) child_process.execSync('docker volume rm ' + namedir);
112135
process.exit();
113136
}
137+
if (use_docker) {
138+
child_process.execSync('docker volume create ' + namedir);
139+
child_process.execSync('docker create --name ' + namedir + ' -v ' + namedir + ':/rpython hello-world');
140+
child_process.execSync(`docker cp ${namedir}:/rpython/${namedir}/usession-unknown-0 ${tempdir}`);
141+
child_process.execSync('docker rm ' + namedir);
142+
}
114143
for (var filename of fs.readdirSync(directory)) {
115144
if (filename.startsWith(file + '.')) {
116145
if (use_wasm || filename !== (file + '.js')) fs.copyFileSync(path.join(directory, filename), path.join(process.cwd(), filename));
@@ -138,6 +167,7 @@ async function handle() {
138167
catch (error) {
139168
child_process.execSync('rm -rf ' + tempdir);
140169
}
170+
if (use_docker) child_process.execSync('docker volume rm ' + namedir);
141171
}
142172
catch (error) {
143173
console.warn(error);

0 commit comments

Comments
 (0)