Skip to content

Commit 80fe693

Browse files
committed
pythongh-156109: Allow static, non-framework iOS builds
A shared Python on iOS has to be packaged as a framework for App Store Connect to accept it. A static libpython is linked into the app binary and loads nothing at runtime, so the requirement does not apply to it, but configure refused that configuration outright. Refuse it only where it cannot work: a shared build with no framework. The LINKFORSHARED and MODULE_DEPS_SHARED framework references are gated on enable_framework, as the Darwin arm above already does, since a build without a framework has nothing to link against. Passing no framework option at all still errors, so a non-framework build stays an explicit opt-in. A non-framework build cannot load extension modules at runtime either, so make that configuration self-consistent, mirroring what WASI already does for the same reason: * force MODULE_BUILDTYPE=static for iOS builds without a framework, and error out if MODULE_BUILDTYPE=shared was requested explicitly; * mark the test modules that must be built as shared libraries (see Modules/Setup.stdlib.in) as unavailable, so that the build no longer requires --disable-test-modules. Framework builds are unaffected. Document that this configuration supports neither loadable extension modules nor binary wheels. configure was edited by hand; it needs to be regenerated with the pinned autoconf container before merging.
1 parent e8158d1 commit 80fe693

5 files changed

Lines changed: 115 additions & 15 deletions

File tree

‎Doc/using/configure.rst‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1338,6 +1338,22 @@ See :source:`Platforms/Apple/iOS/README.md`.
13381338
Create a Python.framework. Unlike macOS, the *INSTALLDIR* argument
13391339
specifying the installation path is mandatory.
13401340

1341+
.. option:: --disable-framework
1342+
1343+
Build a static ``libpython`` for embedding directly in an app binary,
1344+
instead of a ``Python.framework``. This option requires
1345+
``--disable-shared``, and must be requested explicitly: an iOS build that
1346+
specifies neither ``--enable-framework`` nor ``--disable-framework`` is an
1347+
error.
1348+
1349+
A non-framework build cannot load extension modules at runtime, so every
1350+
extension module must be compiled into ``libpython``
1351+
(``MODULE_BUILDTYPE=static`` is enforced), binary wheels are not supported,
1352+
and the standard library test modules that must be built as shared
1353+
libraries are marked as unavailable.
1354+
1355+
.. versionadded:: 3.16
1356+
13411357
.. option:: --with-framework-name=FRAMEWORK
13421358

13431359
Specify the name for the framework (default: ``Python``).
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
iOS builds may now be configured with ``--disable-framework`` when combined with
2+
``--disable-shared``, producing a static ``libpython`` for embedding in an app
3+
binary. In this configuration all extension modules are linked into
4+
``libpython``, and the test modules that must be built as shared libraries are
5+
marked as unavailable. A shared iOS build must still be a framework build.

‎Platforms/Apple/iOS/README.md‎

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,23 @@ Python build for a single framework, the following options are available.
9090
installed. If `DIR` is not specified, the framework will be installed into
9191
a subdirectory of the `iOS/Frameworks` folder.
9292

93-
This argument *must* be provided when configuring iOS builds. iOS does not
94-
support non-framework builds.
93+
Either this argument or `--disable-framework` *must* be provided when
94+
configuring iOS builds; there is no default.
95+
96+
* `--disable-framework`
97+
98+
Build a static `libpython` for embedding directly in an app binary, instead
99+
of a `Python.framework`. This option requires `--disable-shared`, and must be
100+
requested explicitly.
101+
102+
A non-framework build cannot load extension modules at runtime, as there is
103+
no framework for those modules to link against. Every extension module must
104+
therefore be compiled into `libpython`; `MODULE_BUILDTYPE=static` is enforced
105+
for this configuration, binary wheels are not supported, and the standard
106+
library test modules that must be built as shared libraries are marked as
107+
unavailable. This configuration is only useful if you are embedding Python in
108+
an app that links `libpython` at compile time, and are able to compile any
109+
third-party extension modules into that app as well.
95110

96111
* `--with-framework-name=NAME`
97112

@@ -113,9 +128,10 @@ framework to contain non-library content, so the iOS build will produce a
113128
The `lib` folder will be needed at runtime to support the Python library.
114129

115130
If you want to use Python in a real iOS project, you need to produce multiple
116-
`Python.framework` builds, one for each ABI and architecture. iOS builds of
117-
Python *must* be constructed as framework builds. To support this, you must
118-
provide the `--enable-framework` flag when configuring the build. The build
131+
`Python.framework` builds, one for each ABI and architecture. Unless you are
132+
statically linking Python into your app (see `--disable-framework` above), iOS
133+
builds of Python *must* be constructed as framework builds. To support this,
134+
you must provide the `--enable-framework` flag when configuring the build. The build
119135
also requires the use of cross-compilation. The minimal commands for building
120136
Python for the ARM64 iOS simulator will look something like:
121137
```

‎configure‎

Lines changed: 38 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎configure.ac‎

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,6 @@ AC_ARG_ENABLE([framework],
579579
case $enableval in
580580
no)
581581
case $ac_sys_system in
582-
iOS) AC_MSG_ERROR([iOS builds must use --enable-framework]) ;;
583582
*)
584583
PYTHONFRAMEWORK=
585584
PYTHONFRAMEWORKDIR=no-framework
@@ -689,7 +688,7 @@ AC_ARG_ENABLE([framework],
689688
esac
690689
],[
691690
case $ac_sys_system in
692-
iOS) AC_MSG_ERROR([iOS builds must use --enable-framework]) ;;
691+
iOS) AC_MSG_ERROR([iOS builds must use --enable-framework, or --disable-framework with --disable-shared to build a static libpython]) ;;
693692
*)
694693
PYTHONFRAMEWORK=
695694
PYTHONFRAMEWORKDIR=no-framework
@@ -1689,6 +1688,10 @@ else # shared is disabled
16891688
fi
16901689
AC_MSG_RESULT([$LDLIBRARY])
16911690

1691+
if test "$ac_sys_system" = "iOS" && test "$PY_ENABLE_SHARED" = 1 && test -z "$PYTHONFRAMEWORK"; then
1692+
AC_MSG_ERROR([iOS builds must use --enable-framework, or --disable-framework with --disable-shared to build a static libpython])
1693+
fi
1694+
16921695
# HOSTRUNNER - Program to run CPython for the host platform
16931696
AC_MSG_CHECKING([HOSTRUNNER])
16941697
if test -z "$HOSTRUNNER"
@@ -3830,7 +3833,11 @@ then
38303833
fi
38313834
LINKFORSHARED="$LINKFORSHARED"
38323835
elif test $ac_sys_system = "iOS"; then
3833-
LINKFORSHARED="-Wl,-stack_size,$stack_size $LINKFORSHARED "'$(PYTHONFRAMEWORKDIR)/$(PYTHONFRAMEWORK)'
3836+
LINKFORSHARED="-Wl,-stack_size,$stack_size $LINKFORSHARED"
3837+
3838+
if test "$enable_framework"; then
3839+
LINKFORSHARED="$LINKFORSHARED "'$(PYTHONFRAMEWORKDIR)/$(PYTHONFRAMEWORK)'
3840+
fi
38343841
fi
38353842
;;
38363843
OpenUNIX*|UnixWare*) LINKFORSHARED="-Wl,-Bexport";;
@@ -6755,8 +6762,9 @@ if test "$PY_ENABLE_SHARED" = "1" && ( test -n "$ANDROID_API_LEVEL" || test "$MA
67556762
LIBPYTHON="-lpython${VERSION}${ABIFLAGS}"
67566763
fi
67576764

6758-
# On iOS the shared libraries must be linked with the Python framework
6759-
if test "$ac_sys_system" = "iOS"; then
6765+
# On iOS the shared libraries must be linked with the Python framework, when one
6766+
# is being built
6767+
if test "$ac_sys_system" = "iOS" && test "$enable_framework"; then
67606768
MODULE_DEPS_SHARED="$MODULE_DEPS_SHARED \$(PYTHONFRAMEWORKDIR)/\$(PYTHONFRAMEWORK)"
67616769
fi
67626770

@@ -8300,6 +8308,21 @@ AS_CASE([$ac_sys_system],
83008308
[spwd],
83018309
[syslog],
83028310
)
8311+
dnl A non-framework build is statically linked into the app binary, so it
8312+
dnl cannot load extension modules at runtime. Test modules that must be
8313+
dnl compiled as shared libraries are not supported
8314+
dnl (see Modules/Setup.stdlib.in).
8315+
AS_IF([test -z "$PYTHONFRAMEWORK"], [
8316+
PY_STDLIB_MOD_SET_NA(
8317+
[_ctypes_test],
8318+
[_testimportmultiple],
8319+
[_testmultiphase],
8320+
[_testsinglephase],
8321+
[xxlimited],
8322+
[xxlimited_35],
8323+
[xxlimited_3_13],
8324+
)
8325+
])
83038326
],
83048327
[CYGWIN*], [PY_STDLIB_MOD_SET_NA([_scproxy])],
83058328
[QNX*], [PY_STDLIB_MOD_SET_NA([_scproxy])],
@@ -8378,6 +8401,13 @@ AS_CASE([$ac_sys_system],
83788401
dnl AC_MSG_NOTICE([m4_set_list([_PY_STDLIB_MOD_SET_NA])])
83798402

83808403
dnl Default value for Modules/Setup.stdlib build type
8404+
dnl A non-framework iOS build has no framework for extension modules to link
8405+
dnl against, so every extension module must be linked into libpython.
8406+
AS_IF([test "$ac_sys_system" = "iOS" && test -z "$PYTHONFRAMEWORK"], [
8407+
AS_VAR_IF([MODULE_BUILDTYPE], [shared],
8408+
[AC_MSG_ERROR([non-framework iOS builds require MODULE_BUILDTYPE=static])])
8409+
MODULE_BUILDTYPE=static
8410+
])
83818411
AS_CASE([$host_cpu],
83828412
[wasm32|wasm64], [MODULE_BUILDTYPE=static],
83838413
[MODULE_BUILDTYPE=${MODULE_BUILDTYPE:-shared}]

0 commit comments

Comments
 (0)