Skip to content

Commit 992565f

Browse files
tiranErlend Egeberg Aaslandemmatyping
authored
bpo-45881: configure --with-freeze-module --with-build-python (GH-29835)
Co-authored-by: Erlend Egeberg Aasland <[email protected]> Co-authored-by: Ethan Smith <[email protected]>
1 parent b394af1 commit 992565f

File tree

5 files changed

+202
-52
lines changed

5 files changed

+202
-52
lines changed

Doc/using/configure.rst

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,56 @@ See ``Mac/README.rst``.
513513
:option:`--enable-framework` is set (default: ``Python``).
514514

515515

516+
Cross Compiling Options
517+
-----------------------
518+
519+
Cross compiling, also known as cross building, can be used to build Python
520+
for another CPU architecture or platform. Cross compiling requires a Python
521+
interpreter and the :program:`_freeze_module` binary from another build. The
522+
version of the build Python and :program:`_freeze_module` command must be
523+
the same as the cross compiled host Python.
524+
525+
.. cmdoption:: --build=BUILD
526+
527+
configure for building on BUILD, usually guessed by :program:`config.guess`.
528+
529+
.. cmdoption:: --host=HOST
530+
531+
cross-compile to build programs to run on HOST (target platform)
532+
533+
.. cmdoption:: --with-freeze-module=Programs/_freeze_module
534+
535+
path to ``_freeze_module`` binary for cross compiling.
536+
537+
.. versionadded:: 3.11
538+
539+
.. cmdoption:: --with-build-python=python3.xx
540+
541+
path to build ``python`` binary for cross compiling
542+
543+
.. versionadded:: 3.11
544+
545+
.. cmdoption:: CONFIG_SITE=file
546+
547+
An environment variable that points to a file with configure overrides.
548+
549+
Example *config.site* file::
550+
551+
# config.site-aarch64
552+
ac_cv_buggy_getaddrinfo=no
553+
ac_cv_file__dev_ptmx=yes
554+
ac_cv_file__dev_ptc=no
555+
556+
557+
Cross compiling example::
558+
559+
CONFIG_SITE=config.site-aarch64 ../configure \
560+
--build=x86_64-pc-linux-gnu \
561+
--host=aarch64-unknown-linux-gnu \
562+
--with-freeze-module=../x86_64/Programs/_freeze_module \
563+
--with-build-python=../x86_64/python
564+
565+
516566
Python Build System
517567
===================
518568

Makefile.pre.in

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,8 @@ PYTHON_FOR_BUILD=@PYTHON_FOR_BUILD@
282282
_PYTHON_HOST_PLATFORM=@_PYTHON_HOST_PLATFORM@
283283
BUILD_GNU_TYPE= @build@
284284
HOST_GNU_TYPE= @host@
285+
# Allow developers to override freeze_module command for cross building (bpo-45886)
286+
FREEZE_MODULE?=@FREEZE_MODULE@
285287

286288
# Tcl and Tk config info from --with-tcltk-includes and -libs options
287289
TCLTK_INCLUDES= @TCLTK_INCLUDES@
@@ -1011,9 +1013,6 @@ Python/deepfreeze/frozen_only.c: Python/frozen_modules/frozen_only.h $(DEEPFREEZ
10111013
############################################################################
10121014
# frozen modules (including importlib)
10131015

1014-
# Allow developers to override freeze_module command for cross building (bpo-45886)
1015-
FREEZE_MODULE?=Programs/_freeze_module
1016-
10171016
# FROZEN_FILES_* are auto-generated by Tools/scripts/freeze_modules.py.
10181017
FROZEN_FILES_IN = \
10191018
Lib/importlib/_bootstrap.py \
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
The :program:`configure` script now accepts ``--with-build-python`` and
2+
``--with-freeze-module`` options to make cross compiling easier.

configure

Lines changed: 88 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -931,8 +931,9 @@ PKG_CONFIG
931931
CONFIG_ARGS
932932
SOVERSION
933933
VERSION
934-
PYTHON_FOR_BUILD
935934
PYTHON_FOR_REGEN
935+
PYTHON_FOR_BUILD
936+
FREEZE_MODULE
936937
host_os
937938
host_vendor
938939
host_cpu
@@ -988,6 +989,8 @@ SHELL'
988989
ac_subst_files=''
989990
ac_user_opts='
990991
enable_option_checking
992+
with_freeze_module
993+
with_build_python
991994
with_pkg_config
992995
enable_universalsdk
993996
with_universal_archs
@@ -1721,6 +1724,11 @@ Optional Features:
17211724
Optional Packages:
17221725
--with-PACKAGE[=ARG] use PACKAGE [ARG=yes]
17231726
--without-PACKAGE do not use PACKAGE (same as --with-PACKAGE=no)
1727+
--with-freeze-module=Programs/_freeze_module
1728+
path to _freeze_module binary for cross compiling
1729+
--with-build-python=python3.11
1730+
path to build python binary for cross compiling
1731+
(default: python3.11)
17241732
--with-pkg-config=[yes|no|check]
17251733
use pkg-config to detect build options (default is
17261734
check)
@@ -3177,9 +3185,88 @@ case $host_os in *\ *) host_os=`echo "$host_os" | sed 's/ /-/g'`;; esac
31773185

31783186

31793187

3188+
if test "x$cross_compiling" = xmaybe; then :
3189+
as_fn_error $? "Cross compiling required --host=HOST-TUPLE and --build=ARCH" "$LINENO" 5
3190+
3191+
fi
3192+
31803193
# pybuilddir.txt will be created by --generate-posix-vars in the Makefile
31813194
rm -f pybuilddir.txt
31823195

3196+
3197+
# Check whether --with-freeze-module was given.
3198+
if test "${with_freeze_module+set}" = set; then :
3199+
withval=$with_freeze_module;
3200+
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for --with-freeze-module" >&5
3201+
$as_echo_n "checking for --with-freeze-module... " >&6; }
3202+
if test "x$cross_compiling" = xno; then :
3203+
as_fn_error $? "--with-freeze-module only applies to cross compiling" "$LINENO" 5
3204+
fi
3205+
if test "$with_freeze_module" = yes -o "$with_freeze_module" = no; then
3206+
as_fn_error $? "invalid --with-freeze-module option: expected path, not \"$with_freeze_module\"" "$LINENO" 5
3207+
fi
3208+
if ! $(command -v "$with_freeze_module" >/dev/null 2>&1); then
3209+
as_fn_error $? "invalid or missing freeze module binary \"$with_freeze_module\"" "$LINENO" 5
3210+
fi
3211+
FREEZE_MODULE="$with_freeze_module"
3212+
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $FREEZE_MODULE" >&5
3213+
$as_echo "$FREEZE_MODULE" >&6; }
3214+
3215+
else
3216+
3217+
if test "x$cross_compiling" = xyes; then :
3218+
as_fn_error $? "Cross compiling requires --with-freeze-module" "$LINENO" 5
3219+
3220+
fi
3221+
FREEZE_MODULE=Programs/_freeze_module
3222+
3223+
3224+
fi
3225+
3226+
3227+
3228+
3229+
# Check whether --with-build-python was given.
3230+
if test "${with_build_python+set}" = set; then :
3231+
withval=$with_build_python;
3232+
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for --with-build-python" >&5
3233+
$as_echo_n "checking for --with-build-python... " >&6; }
3234+
3235+
if test "x$cross_compiling" = xno; then :
3236+
as_fn_error $? "--with-build-python only applies to cross compiling" "$LINENO" 5
3237+
fi
3238+
if test "x$with_build_python" = xyes; then :
3239+
with_build_python=python$PACKAGE_VERSION
3240+
fi
3241+
if test "x$with_build_python" = xno; then :
3242+
as_fn_error $? "invalid --with-build-python option: expected path, not \"no\"" "$LINENO" 5
3243+
fi
3244+
3245+
if ! $(command -v "$with_build_python" >/dev/null 2>&1); then
3246+
as_fn_error $? "invalid or missing build python binary \"$with_build_python\"" "$LINENO" 5
3247+
fi
3248+
build_python_ver=$($with_build_python -c "import sys; print(f'{sys.version_info.major}.{sys.version_info.minor}')")
3249+
if test "$build_python_ver" != "$PACKAGE_VERSION"; then
3250+
as_fn_error $? "\"$with_build_python\" has incompatible version $build_python_ver (expected: $PACKAGE_VERSION)" "$LINENO" 5
3251+
fi
3252+
ac_cv_prog_PYTHON_FOR_REGEN=$with_build_python
3253+
PYTHON_FOR_BUILD='_PYTHON_PROJECT_BASE=$(abs_builddir) _PYTHON_HOST_PLATFORM=$(_PYTHON_HOST_PLATFORM) PYTHONPATH=$(shell test -f pybuilddir.txt && echo $(abs_builddir)/`cat pybuilddir.txt`:)$(srcdir)/Lib _PYTHON_SYSCONFIGDATA_NAME=_sysconfigdata_$(ABIFLAGS)_$(MACHDEP)_$(MULTIARCH) '$with_build_python
3254+
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $with_build_python" >&5
3255+
$as_echo "$with_build_python" >&6; }
3256+
3257+
else
3258+
3259+
if test "x$cross_compiling" = xyes; then :
3260+
as_fn_error $? "Cross compiling requires --with-build-python" "$LINENO" 5
3261+
3262+
fi
3263+
PYTHON_FOR_BUILD='./$(BUILDPYTHON) -E'
3264+
3265+
3266+
fi
3267+
3268+
3269+
31833270
for ac_prog in python$PACKAGE_VERSION python3.10 python3.9 python3.8 python3.7 python3.6 python3 python
31843271
do
31853272
# Extract the first word of "$ac_prog", so it can be a program name with args.
@@ -3235,31 +3322,6 @@ else
32353322
$as_echo "missing" >&6; }
32363323
fi
32373324

3238-
if test "$cross_compiling" = yes; then
3239-
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for python interpreter for cross build" >&5
3240-
$as_echo_n "checking for python interpreter for cross build... " >&6; }
3241-
if test -z "$PYTHON_FOR_BUILD"; then
3242-
for interp in python$PACKAGE_VERSION python3 python; do
3243-
which $interp >/dev/null 2>&1 || continue
3244-
if $interp -c "import sys;sys.exit(not '.'.join(str(n) for n in sys.version_info[:2]) == '$PACKAGE_VERSION')"; then
3245-
break
3246-
fi
3247-
interp=
3248-
done
3249-
if test x$interp = x; then
3250-
as_fn_error $? "python$PACKAGE_VERSION interpreter not found" "$LINENO" 5
3251-
fi
3252-
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $interp" >&5
3253-
$as_echo "$interp" >&6; }
3254-
PYTHON_FOR_BUILD='_PYTHON_PROJECT_BASE=$(abs_builddir) _PYTHON_HOST_PLATFORM=$(_PYTHON_HOST_PLATFORM) PYTHONPATH=$(shell test -f pybuilddir.txt && echo $(abs_builddir)/`cat pybuilddir.txt`:)$(srcdir)/Lib _PYTHON_SYSCONFIGDATA_NAME=_sysconfigdata_$(ABIFLAGS)_$(MACHDEP)_$(MULTIARCH) '$interp
3255-
fi
3256-
elif test "$cross_compiling" = maybe; then
3257-
as_fn_error $? "Cross compiling required --host=HOST-TUPLE and --build=ARCH" "$LINENO" 5
3258-
else
3259-
PYTHON_FOR_BUILD='./$(BUILDPYTHON) -E'
3260-
fi
3261-
3262-
32633325

32643326
if test "$prefix" != "/"; then
32653327
prefix=`echo "$prefix" | sed -e 's/\/$//g'`

configure.ac

Lines changed: 60 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,69 @@ AC_CANONICAL_HOST
9393
AC_SUBST(build)
9494
AC_SUBST(host)
9595

96+
AS_VAR_IF([cross_compiling], [maybe],
97+
[AC_MSG_ERROR([Cross compiling required --host=HOST-TUPLE and --build=ARCH])]
98+
)
99+
96100
# pybuilddir.txt will be created by --generate-posix-vars in the Makefile
97101
rm -f pybuilddir.txt
98102

103+
dnl cross-compiling needs a freeze_module binary for build platform
104+
AC_ARG_WITH(
105+
[freeze-module],
106+
[AS_HELP_STRING([--with-freeze-module=Programs/_freeze_module],
107+
[path to _freeze_module binary for cross compiling])],
108+
[
109+
AC_MSG_CHECKING([for --with-freeze-module])
110+
AS_VAR_IF([cross_compiling], [no], AC_MSG_ERROR([--with-freeze-module only applies to cross compiling]))
111+
if test "$with_freeze_module" = yes -o "$with_freeze_module" = no; then
112+
AC_MSG_ERROR([invalid --with-freeze-module option: expected path, not "$with_freeze_module"])
113+
fi
114+
if ! $(command -v "$with_freeze_module" >/dev/null 2>&1); then
115+
AC_MSG_ERROR([invalid or missing freeze module binary "$with_freeze_module"])
116+
fi
117+
FREEZE_MODULE="$with_freeze_module"
118+
AC_MSG_RESULT([$FREEZE_MODULE])
119+
], [
120+
AS_VAR_IF([cross_compiling], [yes],
121+
[AC_MSG_ERROR([Cross compiling requires --with-freeze-module])]
122+
)
123+
FREEZE_MODULE=Programs/_freeze_module
124+
]
125+
)
126+
AC_SUBST([FREEZE_MODULE])
127+
128+
AC_ARG_WITH(
129+
[build-python],
130+
[AS_HELP_STRING([--with-build-python=python]PYTHON_VERSION,
131+
[path to build python binary for cross compiling (default: python]PYTHON_VERSION[)])],
132+
[
133+
AC_MSG_CHECKING([for --with-build-python])
134+
135+
AS_VAR_IF([cross_compiling], [no], AC_MSG_ERROR([--with-build-python only applies to cross compiling]))
136+
AS_VAR_IF([with_build_python], [yes], [with_build_python=python$PACKAGE_VERSION])
137+
AS_VAR_IF([with_build_python], [no], [AC_MSG_ERROR([invalid --with-build-python option: expected path, not "no"])])
138+
139+
if ! $(command -v "$with_build_python" >/dev/null 2>&1); then
140+
AC_MSG_ERROR([invalid or missing build python binary "$with_build_python"])
141+
fi
142+
build_python_ver=$($with_build_python -c "import sys; print(f'{sys.version_info.major}.{sys.version_info.minor}')")
143+
if test "$build_python_ver" != "$PACKAGE_VERSION"; then
144+
AC_MSG_ERROR(["$with_build_python" has incompatible version $build_python_ver (expected: $PACKAGE_VERSION)])
145+
fi
146+
dnl use build Python for regeneration, too
147+
ac_cv_prog_PYTHON_FOR_REGEN=$with_build_python
148+
PYTHON_FOR_BUILD='_PYTHON_PROJECT_BASE=$(abs_builddir) _PYTHON_HOST_PLATFORM=$(_PYTHON_HOST_PLATFORM) PYTHONPATH=$(shell test -f pybuilddir.txt && echo $(abs_builddir)/`cat pybuilddir.txt`:)$(srcdir)/Lib _PYTHON_SYSCONFIGDATA_NAME=_sysconfigdata_$(ABIFLAGS)_$(MACHDEP)_$(MULTIARCH) '$with_build_python
149+
AC_MSG_RESULT([$with_build_python])
150+
], [
151+
AS_VAR_IF([cross_compiling], [yes],
152+
[AC_MSG_ERROR([Cross compiling requires --with-build-python])]
153+
)
154+
PYTHON_FOR_BUILD='./$(BUILDPYTHON) -E'
155+
]
156+
)
157+
AC_SUBST([PYTHON_FOR_BUILD])
158+
99159
AC_CHECK_PROGS([PYTHON_FOR_REGEN],
100160
[python$PACKAGE_VERSION python3.10 python3.9 python3.8 python3.7 python3.6 python3 python],
101161
[python3])
@@ -108,29 +168,6 @@ else
108168
AC_MSG_RESULT([missing])
109169
fi
110170

111-
if test "$cross_compiling" = yes; then
112-
AC_MSG_CHECKING([for python interpreter for cross build])
113-
if test -z "$PYTHON_FOR_BUILD"; then
114-
for interp in python$PACKAGE_VERSION python3 python; do
115-
which $interp >/dev/null 2>&1 || continue
116-
if $interp -c "import sys;sys.exit(not '.'.join(str(n) for n in sys.version_info@<:@:2@:>@) == '$PACKAGE_VERSION')"; then
117-
break
118-
fi
119-
interp=
120-
done
121-
if test x$interp = x; then
122-
AC_MSG_ERROR([python$PACKAGE_VERSION interpreter not found])
123-
fi
124-
AC_MSG_RESULT($interp)
125-
PYTHON_FOR_BUILD='_PYTHON_PROJECT_BASE=$(abs_builddir) _PYTHON_HOST_PLATFORM=$(_PYTHON_HOST_PLATFORM) PYTHONPATH=$(shell test -f pybuilddir.txt && echo $(abs_builddir)/`cat pybuilddir.txt`:)$(srcdir)/Lib _PYTHON_SYSCONFIGDATA_NAME=_sysconfigdata_$(ABIFLAGS)_$(MACHDEP)_$(MULTIARCH) '$interp
126-
fi
127-
elif test "$cross_compiling" = maybe; then
128-
AC_MSG_ERROR([Cross compiling required --host=HOST-TUPLE and --build=ARCH])
129-
else
130-
PYTHON_FOR_BUILD='./$(BUILDPYTHON) -E'
131-
fi
132-
AC_SUBST(PYTHON_FOR_BUILD)
133-
134171
dnl Ensure that if prefix is specified, it does not end in a slash. If
135172
dnl it does, we get path names containing '//' which is both ugly and
136173
dnl can cause trouble.

0 commit comments

Comments
 (0)