8000 gh-127503: Emscripten make Python.sh function as proper Python CLI (#… · python/cpython@87faf0a · GitHub
[go: up one dir, main page]

Skip to content

Commit 87faf0a

Browse files
authored
gh-127503: Emscripten make Python.sh function as proper Python CLI (#127506)
Modifies the python.sh script to work on macOS, and adapt to recent emscripten changes.
1 parent 43634fc commit 87faf0a

File tree

4 files changed

+51
-17
lines changed

4 files changed

+51
-17
lines changed

Tools/wasm/emscripten/__main__.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,26 @@ def configure_emscripten_python(context, working_dir):
218218
f"""\
219219
#!/bin/sh
220220
221+
# Macs come with FreeBSD coreutils which doesn't have the -s option
222+
# so feature detect and work around it.
223+
if which grealpath > /dev/null; then
224+
# It has brew installed gnu core utils, use that
225+
REALPATH="grealpath -s"
226+
elif which realpath > /dev/null && realpath --version 2&>1 | grep GNU > /dev/null; then
227+
# realpath points to GNU realpath so use it.
228+
REALPATH="realpath -s"
229+
else
230+
# Shim for macs without GNU coreutils
231+
abs_path () {{
232+
echo "$(cd "$(dirname "$1")" || exit; pwd)/$(basename "$1")"
233+
}}
234+
REALPATH=abs_path
235+
fi
236+
221237
# We compute our own path, not following symlinks and pass it in so that
222238
# node_entry.mjs can set sys.executable correctly.
223-
exec {host_runner} {node_entry} "$(realpath -s $0)" "$@"
239+
# Intentionally allow word splitting on NODEFLAGS.
240+
exec {host_runner} $NODEFLAGS {node_entry} --this-program="$($REALPATH "$0")" "$@"
224241
"""
225242
)
226243
)
@@ -233,7 +250,7 @@ def configure_emscripten_python(context, working_dir):
233250
def make_emscripten_python(context, working_dir):
234251
"""Run `make` for the emscripten/host build."""
235252
call(
236-
["make", "--jobs", str(cpu_count()), "commoninstall"],
253+
["make", "--jobs", str(cpu_count()), "all"],
237254
env=updated_env(),
238255
quiet=context.quiet,
239256
)

Tools/wasm/emscripten/node_entry.mjs

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,47 @@
11
import EmscriptenModule from "./python.mjs";
2-
import { dirname } from 'node:path';
3-
import { fileURLToPath } from 'node:url';
2+
import fs from "node:fs";
43

54
if (process?.versions?.node) {
65
const nodeVersion = Number(process.versions.node.split(".", 1)[0]);
76
if (nodeVersion < 18) {
8-
process.stderr.write(
9-
`Node version must be >= 18, got version ${process.version}\n`,
10-
);
11-
process.exit(1);
7+
process.stderr.write(
8+
`Node version must be >= 18, got version ${process.version}\n`,
9+
);
10+
process.exit(1);
1211
}
1312
}
1413

14+
function rootDirsToMount(Module) {
15+
return fs
16+
.readdirSync("/")
17+
.filter((dir) => !["dev", "lib", "proc"].includes(dir))
18+
.map((dir) => "/" + dir);
19+
}
20+
21+
function mountDirectories(Module) {
22+
for (const dir of rootDirsToMount(Module)) {
23+
Module.FS.mkdirTree(dir);
24+
Module.FS.mount(Module.FS.filesystems.NODEFS, { root: dir }, dir);
25+
}
26+
}
27+
28+
const thisProgram = "--this-program=";
29+
const thisProgramIndex = process.argv.findIndex((x) =>
30+
x.startsWith(thisProgram),
31+
);
32+
1533
const settings = {
1634
preRun(Module) {
17-
const __dirname = dirname(fileURLToPath(import.meta.url));
18-
Module.FS.mkdirTree("/lib/");
19-
Module.FS.mount(Module.FS.filesystems.NODEFS, { root: __dirname + "/lib/" }, "/lib/");
35+
mountDirectories(Module);
36+
Module.FS.chdir(process.cwd());
37+
Object.assign(Module.ENV, process.env);
2038
},
21-
// The first three arguments are: "node", path to this file, path to
22-
// python.sh. After that come the arguments the user passed to python.sh.
23-
arguments: process.argv.slice(3),
2439
// Ensure that sys.executable, sys._base_executable, etc point to python.sh
2540
// not to this file. To properly handle symlinks, python.sh needs to compute
2641
// its own path.
27-
thisProgram: process.argv[2],
42+
thisProgram: process.argv[thisProgramIndex],
43+
// After python.sh come the arguments thatthe user passed to python.sh.
44+
arguments: process.argv.slice(thisProgramIndex + 1),
2845
};
2946

3047
await EmscriptenModule(settings);

configure

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

configure.ac

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2332,7 +2332,7 @@ AS_CASE([$ac_sys_system],
23322332
23332333
dnl Include file system support
23342334
AS_VAR_APPEND([LDFLAGS_NODIST], [" -sFORCE_FILESYSTEM -lidbfs.js -lnodefs.js -lproxyfs.js -lworkerfs.js"])
2335-
AS_VAR_APPEND([LDFLAGS_NODIST], [" -sEXPORTED_RUNTIME_METHODS=FS,callMain"])
2335+
AS_VAR_APPEND([LDFLAGS_NODIST], [" -sEXPORTED_RUNTIME_METHODS=FS,callMain,ENV"])
23362336
AS_VAR_APPEND([LDFLAGS_NODIST], [" -sEXPORTED_FUNCTIONS=_main,_Py_Version"])
23372337
AS_VAR_APPEND([LDFLAGS_NODIST], [" -sSTACK_SIZE=5MB"])
23382338

0 commit comments

Comments
 (0)
0