-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
build: Remove build host Python from cross compiled environment
When installing a Python program with a shebang the shebang points to the native Python. Because the virtualenv builder symlinks packages to their original path, it means that we retain references to the native Python, bringing it into the runtime closure of a cross compiled application. By rewriting the shebang to the _target host_ Python at install time we can avoid references to the _build host_ Python in the runtime closure.
- Loading branch information
1 parent
4b74b01
commit c1a70b2
Showing
4 changed files
with
74 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#!/usr/bin/env python | ||
import os | ||
import os.path | ||
import sys | ||
from pathlib import Path | ||
from typing import Optional | ||
|
||
|
||
def main(): | ||
# Output directory | ||
out_dir = Path(os.environ["out"]) | ||
bin_dir = out_dir.joinpath("bin") | ||
|
||
# Cross python interpreter | ||
cross_bin = os.path.dirname(sys.executable) | ||
cross_shebang = f"#!{cross_bin}".encode() | ||
|
||
# Target host interpreter | ||
host_bin = os.path.dirname(sys.argv[1]) | ||
host_shebang = f"#!{host_bin}".encode() | ||
|
||
if not bin_dir.exists(): | ||
return | ||
|
||
for bin in bin_dir.iterdir(): | ||
script: Optional[bytes] = None | ||
|
||
with bin.open(mode="rb") as fd: | ||
preamble = fd.read(len(cross_shebang)) | ||
if preamble == cross_shebang: | ||
script = host_shebang + fd.read() | ||
|
||
if script: | ||
print(f"Rewriting shebang for '{bin}'") | ||
|
||
with bin.open(mode="wb") as fd: | ||
fd.write(script) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
pyprojectCrossShebangHook() { | ||
@pythonInterpreter@ @script@ @hostInterpreter@ | ||
} | ||
|
||
if [ -z "${dontUsePyprojectCrossShebangHook-}" ]; then | ||
preFixupPhases+=" pyprojectCrossShebangHook" | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters