Skip to content

Commit d356d7c

Browse files
authored
Merge pull request #163 from beeware/dynamic-loading-3.9
[3.9] Use dynamically generated binary modules, rather than static linking
2 parents cae2f1c + b1a45a0 commit d356d7c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+2362
-6884
lines changed

.gitignore

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
*.swo
22
*.swp
3-
src/*
3+
.envrc
4+
.vscode/
45
archive/*
56
build/*
6-
downloads/*
77
diff/*
88
dist/*
9-
.envrc
10-
.vscode/
9+
downloads/*
10+
install/*
1111
local/*
12+
merge/*
13+
src/*
14+
support/*
15+
wheels/*
1216
*.dist-info
1317
__pycache__
1418
tests/testbed/macOS

Makefile

Lines changed: 735 additions & 464 deletions
Large diffs are not rendered by default.

README.rst

Lines changed: 87 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Python Apple Support
22
====================
33

4-
**This repository branch builds a packaged version of Python 3.9.12**.
4+
**This repository branch builds a packaged version of Python 3.9.14**.
55
Other Python versions are available by cloning other branches of the main
66
repository.
77

@@ -10,9 +10,11 @@ into a macOS, iOS, tvOS or watchOS project.
1010

1111
It works by downloading, patching, and building a fat binary of Python and
1212
selected pre-requisites, and packaging them as static libraries that can be
13-
incorporated into an XCode project.
13+
incorporated into an XCode project. The binary modules in the Python standard
14+
library are statically compiled, but are distribted as ``.so`` objects that
15+
can be dynamically loaded at runtime.
1416

15-
It exposed *almost* all the modules in the Python standard library except for:
17+
It exposes *almost* all the modules in the Python standard library except for:
1618
* dbm.gnu
1719
* tkinter
1820
* readline
@@ -23,7 +25,11 @@ It exposed *almost* all the modules in the Python standard library except for:
2325
The following standard library modules are available on macOS, but not the other
2426
Apple platforms:
2527
* curses
28+
* grp
29+
* multiprocessing
2630
* posixshmem
31+
* posixsubprocess
32+
* syslog
2733

2834
The binaries support x86_64 and arm64 for macOS; arm64 for iOS and appleTV
2935
devices; and arm64_32 for watchOS. It also supports device simulators on both
@@ -35,7 +41,7 @@ x86_64 and M1 hardware. This should enable the code to run on:
3541
* Mac Mini (including M1 Apple Silicon Mac minis)
3642
* Mac Studio (all models)
3743
* Mac Pro (all models)
38-
* iOS 13.0 or later, on:
44+
* iOS 12.0 or later, on:
3945
* iPhone (6s or later)
4046
* iPad (5th gen or later)
4147
* iPad Air (all models)
@@ -48,6 +54,11 @@ x86_64 and M1 hardware. This should enable the code to run on:
4854
Quickstart
4955
----------
5056

57+
The easist way to use these packages is by creating a project with `Briefcase
58+
<https://github.com/beeware/briefcase>`__. Briefcase will download pre-compiled
59+
versions of these support packages, and add them to an XCode project (or
60+
pre-build stub application, in the case of macOS).
61+
5162
Pre-built versions of the frameworks can be downloaded `for macOS`_, `for
5263
iOS`_, `for tvOS`_, and `for watchOS`_, and added to your project.
5364

@@ -66,10 +77,76 @@ This should:
6677
2. Patch them as required for compatibility with the selected OS
6778
3. Build the packages as XCode-compatible XCFrameworks.
6879

69-
The build products will be in the `build` directory; the compiled frameworks
70-
will be in the `dist` directory.
80+
The resulting support packages will be packaged as a ``.tar.gz`` file
81+
in the ``dist`` folder.
82+
83+
Each support package contains:
84+
85+
* ``VERSIONS``, a text file describing the specific versions of code used to
86+
build the support package;
87+
* ``Python.xcframework``, a multi-architecture build of libPython3.9.a
88+
* ``python-stdlib``, the code and binary modules comprising the Python standard
89+
library. On iOS, tvOS and watchOS, there are 2 copies of every binary module -
90+
one for physical devices, and one for the simulator. The simulator binaries
91+
are "fat", containing code for both x86_64 and arm64.
92+
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+
104+
To add a support package to your own Xcode project:
105+
106+
1. Drag ``Python.xcframework`` and ``python-stdlib`` into your Xcode project
107+
tree.
108+
2. Ensure that these two objects are added to any targets that need to use
109+
them;
110+
3. Add a custom build phase to purge any binary modules for the platform you are
111+
*not* targetting; and
112+
4. Add a custom build phase to sign any of the binary modules in your app.
113+
5. Add CPython API code to your app to create an instance of the Python
114+
interpreter.
115+
116+
For examples of the scripts needed for steps 3 and 4, and the code needed for
117+
step 5, compare with a project generated with Briefcase.
118+
119+
On macOS, you must also either:
120+
1. Enable the "Disable Library Validation" entitlement (found on the "Signing
121+
& Capabilities" tab in XCode); or
122+
2. Sign your app with a Development or Distribution certificate. This will
123+
require a paid Apple Developer subscription.
124+
125+
It is not possible to use an ad-hoc signing certificate with the "Disable
126+
Library Validation" entitlement disabled.
127+
128+
On iOS/tvOS/watchOS, you can use the default developer certificate for deploying
129+
to a device simulator. However, to deploy to a physical device (including your
130+
own), you will require a Development or Distribution certificate, which requires
131+
a paid Apple Developer subscription.
132+
133+
Building binary wheels
134+
----------------------
135+
136+
When building binary wheels, you may need to use the libraries built by this
137+
project as inputs (e.g., the `cffi` module uses `libffi`). To support this, this
138+
project is able to package these dependencies as "wheels" that can be added to
139+
the `server/pypi/dist` directory of the [binary dependency builder
140+
project](https://github.com/freakboy3742/chaquopy).
141+
142+
To build these wheels, run:
143+
144+
* `make wheels` to make all wheels for all mobile platforms
145+
* `make wheels-iOS` to build all the iOS wheels
146+
* `make wheels-tvOS` to build all the tvOS wheels
147+
* `make wheels-watchOS` to build all the watchOS wheels
71148

72-
.. _for macOS: https://briefcase-support.org/python?platform=macOS&version=3.9
73-
.. _for iOS: https://briefcase-support.org/python?platform=iOS&version=3.9
74-
.. _for tvOS: https://briefcase-support.org/python?platform=tvOS&version=3.9
75-
.. _for watchOS: https://briefcase-support.org/python?platform=watchOS&version=3.9
149+
.. _for macOS: https://briefcase-support.s3.amazonaws.com/python/3.9/macOS/Python-3.9-macOS-support.b8.tar.gz
150+
.. _for iOS: https://briefcase-support.s3.amazonaws.com/python/3.9/iOS/Python-3.9-iOS-support.b8.tar.gz
151+
.. _for tvOS: https://briefcase-support.s3.amazonaws.com/python/3.9/tvOS/Python-3.9-tvOS-support.b8.tar.gz
152+
.. _for watchOS: https://briefcase-support.s3.amazonaws.com/python/3.9/watchOS/Python-3.9-watchOS-support.b8.tar.gz

0 commit comments

Comments
 (0)