Skip to content

Commit 9250f70

Browse files
committed
Add a templated cross-platform sitecustomize.py to non-macOS support packages.
1 parent 4ecd795 commit 9250f70

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

Makefile

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -872,8 +872,7 @@ $$(PYTHON_STDLIB-$(os)): \
872872
$$(foreach sdk,$$(SDKS-$(os)),cp $$(PYTHON_FATSTDLIB-$$(sdk))/lib-dynload/* $$(PYTHON_STDLIB-$(os))/lib-dynload; )
873873

874874
dist/Python-$(PYTHON_VER)-$(os)-support.$(BUILD_NUMBER).tar.gz: $$(PYTHON_XCFRAMEWORK-$(os)) $$(PYTHON_STDLIB-$(os))
875-
@echo ">>> Create final distribution artefact for $(os)"
876-
mkdir -p dist
875+
@echo ">>> Create VERSIONS file for $(os)"
877876
echo "Python version: $(PYTHON_VERSION) " > support/$(os)/VERSIONS
878877
echo "Build: $(BUILD_NUMBER)" >> support/$(os)/VERSIONS
879878
echo "Min $(os) version: $$(VERSION_MIN-$(os))" >> support/$(os)/VERSIONS
@@ -887,6 +886,17 @@ endif
887886
echo "OpenSSL: $(OPENSSL_VERSION)" >> support/$(os)/VERSIONS
888887
echo "XZ: $(XZ_VERSION)" >> support/$(os)/VERSIONS
889888

889+
ifneq ($(os),macOS)
890+
@echo ">>> Create cross-platform site sitecustomize.py for $(os)"
891+
mkdir -p support/$(os)/platform-site
892+
cat $(PROJECT_DIR)/patch/Python/sitecustomize.py \
893+
| sed -e "s/{{os}}/$(os)/g" \
894+
| sed -e "s/{{tag}}/$$(shell echo $(os) | tr '[:upper:]' '[:lower:]')_$$(shell echo $$(VERSION_MIN-$(os)) | sed "s/\./_/g")/g" \
895+
> support/$(os)/platform-site/sitecustomize.py
896+
endif
897+
898+
@echo ">>> Create final distribution artefact for $(os)"
899+
mkdir -p dist
890900
# Build a "full" tarball with all content for test purposes
891901
tar zcvf dist/Python-$(PYTHON_VER)-$(os)-support.test-$(BUILD_NUMBER).tar.gz -X patch/Python/test.exclude -C support/$(os) `ls -A support/$(os)`
892902
# Build a distributable tarball

README.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,17 @@ Each support package contains:
9090
one for physical devices, and one for the simulator. The simulator binaries
9191
are "fat", containing code for both x86_64 and arm64.
9292

93+
Non-macOS platforms also contain a ``platform-site`` folder. This contains a
94+
site customization script that can be used to make your local Python install
95+
look like it is an on-device install. This is needed because when you run
96+
``pip`` you'll be on a macOS machine; if ``pip`` tries to install a binary
97+
package, it will install a macOS binary wheel (which won't work on
98+
iOS/tvOS/watchOS). However, if you add the ``platform-site`` folder to your
99+
``PYTHONPATH`` when invoking pip, the site customization will make your Python
100+
install return ``platform`` and ``sysconfig`` responses consistent with
101+
on-device behavior, which will cause ``pip`` to install platform-appropriate
102+
packages.
103+
93104
To add a support package to your own Xcode project:
94105

95106
1. Drag ``Python.xcframework`` and ``python-stdlib`` into your Xcode project

patch/Python/sitecustomize.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# A site customization that can be used to trick pip into installing
2+
# packages cross-platform. If the folder containing this file is on
3+
# your PYTHONPATH when you invoke pip, pip will behave as if it were
4+
# running on {{os}}.
5+
import os
6+
import platform
7+
import sys
8+
import sysconfig
9+
10+
# Make platform.system() return "{{os}}"
11+
def custom_system():
12+
return "{{os}}"
13+
14+
platform.system = custom_system
15+
16+
# Make sysconfig.get_platform() return "{{tag}}"
17+
def custom_get_platform():
18+
return "{{tag}}"
19+
20+
sysconfig.get_platform = custom_get_platform
21+
22+
# Call the next sitecustomize script if there is one
23+
# (https://nedbatchelder.com/blog/201001/running_code_at_python_startup.html).
24+
del sys.modules["sitecustomize"]
25+
this_dir = os.path.dirname(__file__)
26+
path_index = sys.path.index(this_dir)
27+
del sys.path[path_index]
28+
try:
29+
import sitecustomize # noqa: F401
30+
finally:
31+
sys.path.insert(path_index, this_dir)

0 commit comments

Comments
 (0)