Commit 67d9d7d
committed
Implement run-time Python interpreter searching
There is currently an assumption that the Python interpreter used while
building .pex files (i.e. the value of `DefaultInterpreter`) is also the
Python interpreter that runs them. This can be overridden with the
`shebang` parameter on individual `python_binary` and `python_test`
targets, which sets the shebang in the generated .pex file to a specific
value. The functionality this provides is limited:
- If additional interpreter arguments are specified by the target, the
generated .pex file will depend on a shell at `/bin/sh` in the
run-time environment (which isn't guaranteed to exist, e.g. in
distroless containers), as well as `/usr/bin/env` (also not guaranteed
to exist).
- It is possible to build the .pex file using a Python interpreter
defined as a build target in the repo, but it isn't possible to run
the .pex file using that interpreter because the .pex file's shebang
is (by definition) static, but the interpreter's path relative to the
.pex file's path varies depending on the context (e.g. whether the
.pex file exists inside a build environment vs outside of one). This
was discussed in please-build#247.
Provide more flexibility in how .pex files are run by prepending a
native-code preamble that implements dynamic searching of Python
interpreters. The behaviour of this preamble is controlled at run time
by a special zip member within the .pex file,
`.bootstrap/PLZ_PREAMBLE_CONFIG`, which contains a list of candidate
paths for Python interpreters under which the .pex file should be
executed and a list of arguments to pass to the interpreter when
attempting to execute it. The lists can be customised per
`python_binary` or `python_test` target via the `runtime_interpreters`
and `runtime_interpreter_args` parameters respectively; new plugin
configuration options, `DefaultRuntimeInterpreters` and
`DefaultRuntimeInterpreterArgs`, provide default values. If both are
unspecified, the plugin's current behaviour is maintained (i.e. the same
interpreter is used to both build and run the code). The flexibility
offered by the new .pex preamble is sufficient to be able to remove
`python_binary` and `python_test`'s `shebang` parameter (and the
`DefaultShebang` plugin configuration option) altogether.
The new .pex preamble is compact enough to prepend to all .pex files in
a repo without collectively making them prohibitively large: ~98KB
statically linked with musl on Linux, ~83KB dynamically linked on Darwin
(which doesn't have a stable kernel ABI, and strongly discourages static
linking).
Among other possibilities, this allows for the creation of .pex files
that are truly portable between run-time environments of the same
operating system and architecture - e.g., the preamble can be configured
to attempt to execute an interpreter that only exists within a
production environment (such as a distroless container with its
interpreter at `/python/bin/python3`), falling back on an interpreter
defined as a build target in the repo if the .pex file is still located
within Please's development/testing environment.1 parent 3c3513a commit 67d9d7d
File tree
28 files changed
+1252
-111
lines changed- .github/workflows
- build_defs
- plugins
- test
- interpreter_options_repo
- third_party
- cc
- cjson
- kuba_zip
- log_c
- openbsd
- go
- tools/please_pex
- pex
- preamble
- zip
28 files changed
+1252
-111
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
| 31 | + | |
| 32 | + | |
31 | 33 | | |
32 | 34 | | |
33 | 35 | | |
| |||
36 | 38 | | |
37 | 39 | | |
38 | 40 | | |
39 | | - | |
40 | | - | |
41 | | - | |
42 | | - | |
43 | 41 | | |
44 | 42 | | |
45 | | - | |
46 | | - | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
47 | 46 | | |
48 | 47 | | |
49 | 48 | | |
| |||
55 | 54 | | |
56 | 55 | | |
57 | 56 | | |
58 | | - | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
59 | 64 | | |
60 | 65 | | |
61 | 66 | | |
62 | | - | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
63 | 73 | | |
64 | 74 | | |
65 | 75 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
| 2 | + | |
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
9 | 29 | | |
10 | 30 | | |
11 | 31 | | |
| |||
21 | 41 | | |
22 | 42 | | |
23 | 43 | | |
| 44 | + | |
24 | 45 | | |
25 | 46 | | |
26 | 47 | | |
| |||
30 | 51 | | |
31 | 52 | | |
32 | 53 | | |
33 | | - | |
| 54 | + | |
34 | 55 | | |
35 | | - | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
36 | 66 | | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
37 | 71 | | |
38 | | - | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
39 | 75 | | |
40 | 76 | | |
41 | 77 | | |
42 | 78 | | |
43 | 79 | | |
44 | 80 | | |
45 | 81 | | |
46 | | - | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | 82 | | |
53 | 83 | | |
54 | 84 | | |
| |||
146 | 176 | | |
147 | 177 | | |
148 | 178 | | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
149 | 185 | | |
150 | 186 | | |
151 | 187 | | |
| |||
0 commit comments