Skip to content

Commit 1abe1c5

Browse files
committed
- Issue #14330: For cross builds, don't use host python, use host search paths
for host compiler.
1 parent b457b9b commit 1abe1c5

File tree

6 files changed

+200
-27
lines changed

6 files changed

+200
-27
lines changed

Lib/distutils/util.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ def get_platform ():
5353
return 'win-ia64'
5454
return sys.platform
5555

56+
# Set for cross builds explicitly
57+
if "_PYTHON_HOST_PLATFORM" in os.environ:
58+
return os.environ["_PYTHON_HOST_PLATFORM"]
59+
5660
if os.name != "posix" or not hasattr(os, 'uname'):
5761
# XXX what about the architecture? NT is Intel or Alpha,
5862
# Mac OS is M68k or PPC, etc.

Lib/sysconfig.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ def _safe_realpath(path):
138138
if os.name == "nt" and "\\pcbuild\\amd64" in _PROJECT_BASE[-14:].lower():
139139
_PROJECT_BASE = _safe_realpath(os.path.join(_PROJECT_BASE, pardir, pardir))
140140

141+
# set for cross builds
142+
if "_PROJECT_BASE" in os.environ:
143+
_PROJECT_BASE = _safe_realpath(os.environ["_PROJECT_BASE"])
144+
141145
def _is_python_source_dir(d):
142146
for fn in ("Setup.dist", "Setup.local"):
143147
if os.path.isfile(os.path.join(d, "Modules", fn)):
@@ -673,6 +677,10 @@ def get_platform():
673677
# Mac OS is M68k or PPC, etc.
674678
return sys.platform
675679

680+
# Set for cross builds explicitly
681+
if "_PYTHON_HOST_PLATFORM" in os.environ:
682+
return os.environ["_PYTHON_HOST_PLATFORM"]
683+
676684
# Try to distinguish various flavours of Unix
677685
osname, host, release, version, machine = os.uname()
678686

Makefile.pre.in

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,10 @@ LIBOBJS= @LIBOBJS@
194194
PYTHON= python$(EXE)
195195
BUILDPYTHON= python$(BUILDEXE)
196196

197+
PYTHON_FOR_BUILD=@PYTHON_FOR_BUILD@
198+
_PYTHON_HOST_PLATFORM=@_PYTHON_HOST_PLATFORM@
199+
HOST_GNU_TYPE= @host@
200+
197201
# The task to run while instrument when building the profile-opt target
198202
PROFILE_TASK= $(srcdir)/Tools/pybench/pybench.py -n 2 --with-gc --with-syscheck
199203
#PROFILE_TASK= $(srcdir)/Lib/test/regrtest.py
@@ -446,6 +450,7 @@ build_all_generate_profile:
446450
$(MAKE) all CFLAGS="$(CFLAGS) -fprofile-generate" LIBS="$(LIBS) -lgcov"
447451

448452
run_profile_task:
453+
: # FIXME: can't run for a cross build
449454
$(RUNSHARED) ./$(BUILDPYTHON) $(PROFILE_TASK)
450455

451456
build_all_use_profile:
@@ -462,18 +467,17 @@ $(BUILDPYTHON): Modules/python.o $(LIBRARY) $(LDLIBRARY) $(PY3LIBRARY)
462467
$(LINKCC) $(PY_LDFLAGS) $(LINKFORSHARED) -o $@ Modules/python.o $(BLDLIBRARY) $(LIBS) $(MODLIBS) $(SYSLIBS) $(LDLAST)
463468

464469
platform: $(BUILDPYTHON) $(SYSCONFIGDATA)
465-
$(RUNSHARED) ./$(BUILDPYTHON) -E -c 'import sys ; from sysconfig import get_platform ; print(get_platform()+"-"+sys.version[0:3])' >platform
470+
$(RUNSHARED) $(PYTHON_FOR_BUILD) -c 'import sys ; from sysconfig import get_platform ; print(get_platform()+"-"+sys.version[0:3])' >platform
466471

467472
# Generate the sysconfig build-time data
468473
$(SYSCONFIGDATA): $(BUILDPYTHON)
469-
$(RUNSHARED) ./$(BUILDPYTHON) -SE -m sysconfig --generate-posix-vars
474+
$(RUNSHARED) $(PYTHON_FOR_BUILD) -S -m sysconfig --generate-posix-vars
470475

471476
# Build the shared modules
472477
sharedmods: $(BUILDPYTHON) $(SYSCONFIGDATA)
473-
@case $$MAKEFLAGS in \
474-
*s*) $(RUNSHARED) CC='$(CC)' LDSHARED='$(BLDSHARED)' OPT='$(OPT)' ./$(BUILDPYTHON) -E $(srcdir)/setup.py -q build;; \
475-
*) $(RUNSHARED) CC='$(CC)' LDSHARED='$(BLDSHARED)' OPT='$(OPT)' ./$(BUILDPYTHON) -E $(srcdir)/setup.py build;; \
476-
esac
478+
case $$MAKEFLAGS in *s*) quiet=-q; esac; \
479+
$(RUNSHARED) CC='$(CC)' LDSHARED='$(BLDSHARED)' OPT='$(OPT)' \
480+
$(PYTHON_FOR_BUILD) $(srcdir)/setup.py $$quiet build
477481

478482
# Build static library
479483
# avoid long command lines, same as LIBRARY_OBJS
@@ -1073,25 +1077,25 @@ libinstall: build_all $(srcdir)/Lib/$(PLATDIR) $(srcdir)/Modules/xxmodule.c
10731077
$(DESTDIR)$(LIBDEST)/distutils/tests ; \
10741078
fi
10751079
-PYTHONPATH=$(DESTDIR)$(LIBDEST) $(RUNSHARED) \
1076-
./$(BUILDPYTHON) -Wi $(DESTDIR)$(LIBDEST)/compileall.py \
1080+
$(PYTHON_FOR_BUILD) -Wi $(DESTDIR)$(LIBDEST)/compileall.py \
10771081
-d $(LIBDEST) -f \
10781082
-x 'bad_coding|badsyntax|site-packages|lib2to3/tests/data' \
10791083
$(DESTDIR)$(LIBDEST)
10801084
-PYTHONPATH=$(DESTDIR)$(LIBDEST) $(RUNSHARED) \
1081-
./$(BUILDPYTHON) -Wi -O $(DESTDIR)$(LIBDEST)/compileall.py \
1085+
$(PYTHON_FOR_BUILD) -Wi -O $(DESTDIR)$(LIBDEST)/compileall.py \
10821086
-d $(LIBDEST) -f \
10831087
-x 'bad_coding|badsyntax|site-packages|lib2to3/tests/data' \
10841088
$(DESTDIR)$(LIBDEST)
10851089
-PYTHONPATH=$(DESTDIR)$(LIBDEST) $(RUNSHARED) \
1086-
./$(BUILDPYTHON) -Wi $(DESTDIR)$(LIBDEST)/compileall.py \
1090+
$(PYTHON_FOR_BUILD) -Wi $(DESTDIR)$(LIBDEST)/compileall.py \
10871091
-d $(LIBDEST)/site-packages -f \
10881092
-x badsyntax $(DESTDIR)$(LIBDEST)/site-packages
10891093
-PYTHONPATH=$(DESTDIR)$(LIBDEST) $(RUNSHARED) \
1090-
./$(BUILDPYTHON) -Wi -O $(DESTDIR)$(LIBDEST)/compileall.py \
1094+
$(PYTHON_FOR_BUILD) -Wi -O $(DESTDIR)$(LIBDEST)/compileall.py \
10911095
-d $(LIBDEST)/site-packages -f \
10921096
-x badsyntax $(DESTDIR)$(LIBDEST)/site-packages
10931097
-PYTHONPATH=$(DESTDIR)$(LIBDEST) $(RUNSHARED) \
1094-
./$(BUILDPYTHON) -Wi -c "import lib2to3.pygram, lib2to3.patcomp;lib2to3.patcomp.PatternCompiler()"
1098+
$(PYTHON_FOR_BUILD) -Wi -c "import lib2to3.pygram, lib2to3.patcomp;lib2to3.patcomp.PatternCompiler()"
10951099

10961100
# Create the PLATDIR source directory, if one wasn't distributed..
10971101
$(srcdir)/Lib/$(PLATDIR):
@@ -1185,7 +1189,7 @@ libainstall: all python-config
11851189
# Install the dynamically loadable modules
11861190
# This goes into $(exec_prefix)
11871191
sharedinstall: sharedmods
1188-
$(RUNSHARED) ./$(BUILDPYTHON) -E $(srcdir)/setup.py install \
1192+
$(RUNSHARED) $(PYTHON_FOR_BUILD) $(srcdir)/setup.py install \
11891193
--prefix=$(prefix) \
11901194
--install-scripts=$(BINDIR) \
11911195
--install-platlib=$(DESTSHARED) \
@@ -1257,7 +1261,7 @@ frameworkinstallextras:
12571261
# This installs a few of the useful scripts in Tools/scripts
12581262
scriptsinstall:
12591263
SRCDIR=$(srcdir) $(RUNSHARED) \
1260-
./$(BUILDPYTHON) $(srcdir)/Tools/scripts/setup.py install \
1264+
$(PYTHON_FOR_BUILD) $(srcdir)/Tools/scripts/setup.py install \
12611265
--prefix=$(prefix) \
12621266
--install-scripts=$(BINDIR) \
12631267
--root=$(DESTDIR)/

configure

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,7 @@ CC
682682
EXPORT_MACOSX_DEPLOYMENT_TARGET
683683
CONFIGURE_MACOSX_DEPLOYMENT_TARGET
684684
SGI_ABI
685+
_PYTHON_HOST_PLATFORM
685686
MACHDEP
686687
FRAMEWORKINSTALLAPPSPREFIX
687688
FRAMEWORKUNIXTOOLSPREFIX
@@ -700,6 +701,7 @@ UNIVERSALSDK
700701
CONFIG_ARGS
701702
SOVERSION
702703
VERSION
704+
PYTHON_FOR_BUILD
703705
host_os
704706
host_vendor
705707
host_cpu
@@ -2880,6 +2882,29 @@ case $host_os in *\ *) host_os=`echo "$host_os" | sed 's/ /-/g'`;; esac
28802882

28812883

28822884

2885+
if test "$cross_compiling" = yes; then
2886+
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for python interpreter for cross build" >&5
2887+
$as_echo_n "checking for python interpreter for cross build... " >&6; }
2888+
if test -z "$PYTHON_FOR_BUILD"; then
2889+
for interp in python$PACKAGE_VERSION python3 python; do
2890+
which $interp >/dev/null 2>&1 || continue
2891+
if $interp -c 'import sys;sys.exit(not sys.version_info[:2] >= (3,3))'; then
2892+
break
2893+
fi
2894+
interp=
2895+
done
2896+
if test x$interp = x; then
2897+
as_fn_error $? "python$PACKAGE_VERSION interpreter not found" "$LINENO" 5
2898+
fi
2899+
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $interp" >&5
2900+
$as_echo "$interp" >&6; }
2901+
PYTHON_FOR_BUILD="_PROJECT_BASE=$srcdir"' _PYTHON_HOST_PLATFORM=$(_PYTHON_HOST_PLATFORM) PYTHONPATH=$(srcdir)/Lib:$(srcdir)/Lib/plat-$(MACHDEP) '$interp
2902+
fi
2903+
else
2904+
PYTHON_FOR_BUILD='./$(BUILDPYTHON) -E'
2905+
fi
2906+
2907+
28832908

28842909
if test "$prefix" != "/"; then
28852910
prefix=`echo "$prefix" | sed -e 's/\/$//g'`
@@ -3218,6 +3243,29 @@ then
32183243
esac
32193244
fi
32203245

3246+
3247+
if test "$cross_compiling" = yes; then
3248+
case "$host" in
3249+
*-*-linux*)
3250+
case "$host_cpu" in
3251+
arm*)
3252+
_host_cpu=arm
3253+
;;
3254+
*)
3255+
_host_cpu=$host_cpu
3256+
esac
3257+
;;
3258+
*-*-cygwin*)
3259+
_host_cpu=
3260+
;;
3261+
*)
3262+
# for now, limit cross builds to known configurations
3263+
MACHDEP="unknown"
3264+
as_fn_error $? "cross build not supported for $host" "$LINENO" 5
3265+
esac
3266+
_PYTHON_HOST_PLATFORM="$MACHDEP${_host_cpu:+-$_host_cpu}"
3267+
fi
3268+
32213269
# Some systems cannot stand _XOPEN_SOURCE being defined at all; they
32223270
# disable features if it is defined, without any means to access these
32233271
# features as extensions. For these systems, we skip the definition of
@@ -5540,6 +5588,10 @@ else # shared is disabled
55405588
esac
55415589
fi
55425590

5591+
if test "$cross_compiling" = yes; then
5592+
RUNSHARED=
5593+
fi
5594+
55435595
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $LDLIBRARY" >&5
55445596
$as_echo "$LDLIBRARY" >&6; }
55455597

configure.ac

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,27 @@ AC_CONFIG_HEADER(pyconfig.h)
3535

3636
AC_CANONICAL_HOST
3737

38+
if test "$cross_compiling" = yes; then
39+
AC_MSG_CHECKING([for python interpreter for cross build])
40+
if test -z "$PYTHON_FOR_BUILD"; then
41+
for interp in python$PACKAGE_VERSION python3 python; do
42+
which $interp >/dev/null 2>&1 || continue
43+
if $interp -c 'import sys;sys.exit(not sys.version_info@<:@:2@:>@ >= (3,3))'; then
44+
break
45+
fi
46+
interp=
47+
done
48+
if test x$interp = x; then
49+
AC_MSG_ERROR([python$PACKAGE_VERSION interpreter not found])
50+
fi
51+
AC_MSG_RESULT($interp)
52+
PYTHON_FOR_BUILD="_PROJECT_BASE=$srcdir"' _PYTHON_HOST_PLATFORM=$(_PYTHON_HOST_PLATFORM) PYTHONPATH=$(srcdir)/Lib:$(srcdir)/Lib/plat-$(MACHDEP) '$interp
53+
fi
54+
else
55+
PYTHON_FOR_BUILD='./$(BUILDPYTHON) -E'
56+
fi
57+
AC_SUBST(PYTHON_FOR_BUILD)
58+
3859
dnl Ensure that if prefix is specified, it does not end in a slash. If
3960
dnl it does, we get path names containing '//' which is both ugly and
4061
dnl can cause trouble.
@@ -352,6 +373,29 @@ then
352373
'') MACHDEP="unknown";;
353374
esac
354375
fi
376+
377+
AC_SUBST(_PYTHON_HOST_PLATFORM)
378+
if test "$cross_compiling" = yes; then
379+
case "$host" in
380+
*-*-linux*)
381+
case "$host_cpu" in
382+
arm*)
383+
_host_cpu=arm
384+
;;
385+
*)
386+
_host_cpu=$host_cpu
387+
esac
388+
;;
389+
*-*-cygwin*)
390+
_host_cpu=
391+
;;
392+
*)
393+
# for now, limit cross builds to known configurations
394+
MACHDEP="unknown"
395+
AC_MSG_ERROR([cross build not supported for $host])
396+
esac
397+
_PYTHON_HOST_PLATFORM="$MACHDEP${_host_cpu:+-$_host_cpu}"
398+
fi
355399

356400
# Some systems cannot stand _XOPEN_SOURCE being defined at all; they
357401
# disable features if it is defined, without any means to access these
@@ -913,6 +957,10 @@ else # shared is disabled
913957
esac
914958
fi
915959

960+
if test "$cross_compiling" = yes; then
961+
RUNSHARED=
962+
fi
963+
916964
AC_MSG_RESULT($LDLIBRARY)
917965

918966
AC_PROG_RANLIB

0 commit comments

Comments
 (0)