Skip to content

Commit b2173bd

Browse files
committed
unix: add support for i686-unknown-linux-gnu cross-builds
There were a handful of patches needed to get everything to cross build. This took a few hours of work. But the end result seems pretty reasonable.
1 parent 5e392d7 commit b2173bd

23 files changed

+312
-40
lines changed

cpython-unix/build-cpython.sh

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,28 @@ tar -xf pip-${PIP_VERSION}.tar.gz
2828
if [ "${BUILD_TRIPLE}" != "${TARGET_TRIPLE}" ]; then
2929
pushd "Python-${PYTHON_VERSION}"
3030

31+
# When cross-compiling, we need to build a host Python that has working zlib
32+
# and ctypes extensions, otherwise various things fail. (`make install` fails
33+
# without zlib and setuptools / pip used by target install fail due to missing
34+
# ctypes.)
35+
#
36+
# On Apple, the dependencies are present in the Apple SDK and missing extensions
37+
# are built properly by setup.py. However, on other platforms, we need to teach
38+
# the host build system where to find things.
39+
#
40+
# Adding /usr paths on Linux is a bit funky. This is a side-effect or our
41+
# custom Clang purposefully omitting default system search paths to help
42+
# prevent unwanted dependencies from sneaking in.
43+
case "${TARGET_TRIPLE}" in
44+
i686-unknown-linux-gnu)
45+
EXTRA_HOST_CFLAGS="${EXTRA_HOST_CFLAGS} -I/usr/include/x86_64-linux-gnu"
46+
EXTRA_HOST_CPPFLAGS="${EXTRA_HOST_CPPFLAGS} -I/usr/include/x86_64-linux-gnu"
47+
EXTRA_HOST_LDFLAGS="${EXTRA_HOST_LDFLAGS} -L/usr/lib/x86_64-linux-gnu"
48+
;;
49+
*)
50+
;;
51+
esac
52+
3153
CFLAGS="${EXTRA_HOST_CFLAGS}" CPPFLAGS="${EXTRA_HOST_CFLAGS}" LDFLAGS="${EXTRA_HOST_LDFLAGS}" ./configure --prefix "${TOOLS_PATH}/pyhost"
3254

3355
# When building on macOS 10.15 (and possibly earlier) using the 11.0
@@ -490,16 +512,34 @@ if [ "${PYBUILD_PLATFORM}" = "macos" ]; then
490512
echo "unsupported target triple: ${TARGET_TRIPLE}"
491513
exit 1
492514
fi
493-
494-
# getaddrinfo buggy test fails for some reason.
495-
CONFIGURE_FLAGS="${CONFIGURE_FLAGS} ac_cv_buggy_getaddrinfo=no"
496-
497-
# We also need to nerf the /dev/* check
498-
CONFIGURE_FLAGS="${CONFIGURE_FLAGS} ac_cv_file__dev_ptc=no"
499-
CONFIGURE_FLAGS="${CONFIGURE_FLAGS} ac_cv_file__dev_ptmx=no"
500515
fi
501516
fi
502517

518+
if [ "${BUILD_TRIPLE}" != "${TARGET_TRIPLE}" ]; then
519+
# configure doesn't like a handful of scenarios when cross-compiling.
520+
#
521+
# getaddrinfo buggy test fails for some reason. So we short-circuit it.
522+
#
523+
# The /dev/* check also fails for some reason.
524+
case "${TARGET_TRIPLE}" in
525+
*-apple-*)
526+
CONFIGURE_FLAGS="${CONFIGURE_FLAGS} ac_cv_buggy_getaddrinfo=no"
527+
CONFIGURE_FLAGS="${CONFIGURE_FLAGS} ac_cv_file__dev_ptc=no"
528+
CONFIGURE_FLAGS="${CONFIGURE_FLAGS} ac_cv_file__dev_ptmx=no"
529+
;;
530+
i686-unknown-linux-gnu)
531+
CONFIGURE_FLAGS="${CONFIGURE_FLAGS} ac_cv_buggy_getaddrinfo=no"
532+
CONFIGURE_FLAGS="${CONFIGURE_FLAGS} ac_cv_file__dev_ptc=no"
533+
CONFIGURE_FLAGS="${CONFIGURE_FLAGS} ac_cv_file__dev_ptmx=no"
534+
;;
535+
x86_64-unknown-linux-musl)
536+
;;
537+
*)
538+
echo "unhandled cross-compiling triple; may run into issues"
539+
;;
540+
esac
541+
fi
542+
503543
CFLAGS=$CFLAGS CPPFLAGS=$CFLAGS LDFLAGS=$LDFLAGS \
504544
./configure ${CONFIGURE_FLAGS}
505545

cpython-unix/build-inputproto.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export PKG_CONFIG_PATH=/tools/deps/share/pkgconfig
1515
tar -xf inputproto-${INPUTPROTO_VERSION}.tar.gz
1616
pushd inputproto-${INPUTPROTO_VERSION}
1717

18-
CFLAGS="${EXTRA_TARGET_CFLAGS} -fPIC" ./configure \
18+
CFLAGS="${EXTRA_TARGET_CFLAGS} -fPIC" CPPFLAGS="${EXTRA_TARGET_CFLAGS} -fPIC" LDFLAGS="${EXTRA_TARGET_LDFLAGS}" ./configure \
1919
--build=${BUILD_TRIPLE} \
2020
--host=${TARGET_TRIPLE} \
2121
--prefix=/tools/deps

cpython-unix/build-kbproto.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export PKG_CONFIG_PATH=/tools/deps/share/pkgconfig
1515
tar -xf kbproto-${KBPROTO_VERSION}.tar.gz
1616
pushd kbproto-${KBPROTO_VERSION}
1717

18-
CFLAGS="${EXTRA_TARGET_CFLAGS} -fPIC" ./configure \
18+
CFLAGS="${EXTRA_TARGET_CFLAGS} -fPIC" CPPFLAGS="${EXTRA_TARGET_CFLAGS} -fPIC" LDFLAGS="${EXTRA_TARGET_LDFLAGS}" ./configure \
1919
--build=${BUILD_TRIPLE} \
2020
--host=${TARGET_TRIPLE} \
2121
--prefix=/tools/deps

cpython-unix/build-libX11.sh

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,33 @@ if [ "${CC}" = "musl-clang" ]; then
4141
EXTRA_FLAGS="--disable-shared"
4242
fi
4343

44-
CFLAGS="${EXTRA_TARGET_CFLAGS} -fPIC -I/tools/deps/include" ./configure \
44+
# configure doesn't support cross-compiling in malloc(0) returns null test.
45+
# So we have to force a value.
46+
if [ "${BUILD_TRIPLE}" != "${TARGET_TRIPLE}" ]; then
47+
case "${TARGET_TRIPLE}" in
48+
i686-unknown-linux-gnu)
49+
EXTRA_FLAGS="${EXTRA_FLAGS} --enable-malloc0returnsnull"
50+
;;
51+
52+
*)
53+
echo "cross-compiling but malloc(0) override not set; failures possible"
54+
;;
55+
esac
56+
fi
57+
58+
# CC_FOR_BUILD is here because configure doesn't look for `clang` when
59+
# cross-compiling. So we force it.
60+
CFLAGS="${EXTRA_TARGET_CFLAGS} -fPIC -I/tools/deps/include" \
61+
CPPFLAGS="${EXTRA_TARGET_CFLAGS} -fPIC -I/tools/deps/include" \
62+
LDFLAGS="${EXTRA_TARGET_LDFLAGS}" \
63+
CC_FOR_BUILD=clang \
64+
CFLAGS_FOR_BUILD="-I/tools/deps/include" \
65+
CPPFLAGS_FOR_BUILD="-I/tools/deps/include" \
66+
./configure \
4567
--build=${BUILD_TRIPLE} \
4668
--host=${TARGET_TRIPLE} \
4769
--prefix=/tools/deps \
70+
--disable-silent-rules \
4871
${EXTRA_FLAGS}
4972

5073
make -j `nproc`

cpython-unix/build-libXau.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ if [ "${CC}" = "musl-clang" ]; then
1717
EXTRA_FLAGS="--disable-shared"
1818
fi
1919

20-
CFLAGS="${EXTRA_TARGET_CFLAGS} -fPIC" ./configure \
20+
CFLAGS="${EXTRA_TARGET_CFLAGS} -fPIC" CPPFLAGS="${EXTRA_TARGET_CFLAGS} -fPIC" LDFLAGS="${EXTRA_TARGET_LDFLAGS}" ./configure \
2121
--build=${BUILD_TRIPLE} \
2222
--host=${TARGET_TRIPLE} \
2323
--prefix=/tools/deps \

cpython-unix/build-libxcb.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ if [ "${CC}" = "musl-clang" ]; then
1717
EXTRA_FLAGS="--disable-shared"
1818
fi
1919

20-
CFLAGS="${EXTRA_TARGET_CFLAGS} -fPIC" ./configure \
20+
CFLAGS="${EXTRA_TARGET_CFLAGS} -fPIC" CPPFLAGS="${EXTRA_TARGET_CFLAGS} -fPIC" LDFLAGS="${EXTRA_TARGET_LDFLAGS}" ./configure \
2121
--build=${BUILD_TRIPLE} \
2222
--host=${TARGET_TRIPLE} \
2323
--prefix=/tools/deps \

cpython-unix/build-ncurses.sh

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,26 @@ export PATH=${TOOLS_PATH}/${TOOLCHAIN}/bin:${TOOLS_PATH}/host/bin:$PATH
1111

1212
tar -xf ncurses-${NCURSES_VERSION}.tar.gz
1313

14+
# When cross-compiling, ncurses uses the host `tic` to build the terminfo
15+
# database. But our build environment's `tic` is too old to process this
16+
# ncurses version. Our workaround is to build ncurses for the host when
17+
# cross-compiling then make its `tic` available to the target ncurses
18+
# build.
19+
if [ "${BUILD_TRIPLE}" != "${TARGET_TRIPLE}" ]; then
20+
echo "building host ncurses to provide modern tic for cross-compile"
21+
22+
pushd ncurses-${NCURSES_VERSION}
23+
./configure --prefix=${TOOLS_PATH}/host --without-cxx --without-tests --without-manpages --enable-widec
24+
make -j ${NUM_CPUS}
25+
make -j ${NUM_CPUS} install
26+
27+
popd
28+
29+
# Nuke and re-pave the source directory.
30+
rm -rf ncurses-${NCURSES_VERSION}
31+
tar -xf ncurses-${NCURSES_VERSION}.tar.gz
32+
fi
33+
1434
pushd ncurses-${NCURSES_VERSION}
1535

1636
CONFIGURE_FLAGS="
@@ -22,6 +42,11 @@ CONFIGURE_FLAGS="
2242
--without-manpages
2343
--enable-widec"
2444

45+
# ncurses wants --with-build-cc when cross-compiling.
46+
if [ "${BUILD_TRIPLE}" != "${TARGET_TRIPLE}" ]; then
47+
CONFIGURE_FLAGS="${CONFIGURE_FLAGS} --with-build-cc=${TOOLS_PATH}/${TOOLCHAIN}/bin/clang"
48+
fi
49+
2550
if [ "${PYBUILD_PLATFORM}" = "macos" ]; then
2651
CONFIGURE_FLAGS="${CONFIGURE_FLAGS}
2752
--datadir=/usr/share

cpython-unix/build-x11-util-macros.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export PATH=/tools/${TOOLCHAIN}/bin:/tools/host/bin:$PATH
1111

1212
tar -xzf util-macros-${X11_UTIL_MACROS_VERSION}.tar.gz
1313
pushd util-macros-${X11_UTIL_MACROS_VERSION}
14-
CFLAGS="${EXTRA_TARGET_CFLAGS}" ./configure \
14+
CFLAGS="${EXTRA_TARGET_CFLAGS}" CPPFLAGS="${EXTRA_TARGET_CFLAGS}" LDFLAGS="${EXTRA_TARGET_LDFLAGS}" ./configure \
1515
--build=${BUILD_TRIPLE} \
1616
--host=${TARGET_TRIPLE} \
1717
--prefix=/tools/deps

cpython-unix/build-xcb-proto.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export PKG_CONFIG_PATH=/tools/deps/share/pkgconfig
1515
tar -xf xcb-proto-${XCB_PROTO_VERSION}.tar.gz
1616
pushd xcb-proto-${XCB_PROTO_VERSION}
1717

18-
CFLAGS="${EXTRA_TARGET_CFLAGS} -fPIC" ./configure \
18+
CFLAGS="${EXTRA_TARGET_CFLAGS} -fPIC" CPPFLAGS="${EXTRA_TARGET_CFLAGS} -fPIC" LDFLAGS="${EXTRA_TARGET_LDFLAGS}" ./configure \
1919
--build=${BUILD_TRIPLE} \
2020
--host=${TARGET_TRIPLE} \
2121
--prefix=/tools/deps

cpython-unix/build-xextproto.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export PKG_CONFIG_PATH=/tools/deps/share/pkgconfig
1515
tar -xf xextproto-${XEXTPROTO_VERSION}.tar.gz
1616
pushd xextproto-${XEXTPROTO_VERSION}
1717

18-
CFLAGS="${EXTRA_TARGET_CFLAGS} -fPIC" ./configure \
18+
CFLAGS="${EXTRA_TARGET_CFLAGS} -fPIC" CPPFLAGS="${EXTRA_TARGET_CFLAGS} -fPIC" LDFLAGS="${EXTRA_TARGET_LDFLAGS}" ./configure \
1919
--build=${BUILD_TRIPLE} \
2020
--host=${TARGET_TRIPLE} \
2121
--prefix=/tools/deps

0 commit comments

Comments
 (0)