Skip to content

Commit

Permalink
linux: support building Python and dependencies with musl
Browse files Browse the repository at this point in the history
I believe this works!

We had to use --disable-shared in various places because we can't
build shared libraries with musl. In theory this should be possible.
I suspect the musl-clang wrapper is passing conflicting
compiler/linker flags or something.

C++ compilation doesn't work with musl. So we disabled C++
compilation in some components. This would be a reasonable change
otherwise as we should never use the C++ bits.

Some hacks were required here and there. For example, OpenSSL
insists on looking for kernel headers, which we don't have. So
we had to disable some OpenSSL functionality to get things to
build. Disabling secure memory is sounds a bit scary. I left a
TODO to track investigating that.
  • Loading branch information
indygreg committed May 26, 2019
1 parent f36b5e6 commit 0d3ae2a
Show file tree
Hide file tree
Showing 18 changed files with 169 additions and 75 deletions.
6 changes: 6 additions & 0 deletions build-linux.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

def bootstrap():
parser = argparse.ArgumentParser()
parser.add_argument('--musl', action='store_true')
parser.add_argument('--optimized', action='store_true')

args = parser.parse_args()
Expand All @@ -40,6 +41,8 @@ def bootstrap():
os.environ['PATH'] = '%s:%s' % (str(VENV / 'bin'), os.environ['PATH'])
os.environ['PYTHONPATH'] = str(ROOT)

if args.musl:
os.environ['PYBUILD_MUSL'] = '1'
if args.optimized:
os.environ['PYBUILD_OPTIMIZED'] = '1'

Expand All @@ -58,6 +61,9 @@ def run():
basename = 'cpython-linux64'
extra = ''

if 'PYBUILD_MUSL' in os.environ:
basename += '-musl'
extra = '-musl'
if 'PYBUILD_OPTIMIZED' in os.environ:
basename += '-pgo'
extra = '-pgo'
Expand Down
6 changes: 5 additions & 1 deletion cpython-linux/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ COMMON_DEPENDS := \

PLATFORM := linux64

ifdef PYBUILD_MUSL
PLATFORM := $(PLATFORM)-musl
endif

BASE_TOOLCHAIN_DEPENDS := \
$(OUTDIR)/binutils-linux64.tar \
$(OUTDIR)/gcc-linux64.tar \
Expand All @@ -22,7 +26,7 @@ TOOLCHAIN_DEPENDS := \
$(OUTDIR)/musl-linux64.tar \
$(NULL)

default: $(OUTDIR)/cpython-linux64$(if $(PYBUILD_OPTIMIZED),-pgo,).tar
default: $(OUTDIR)/cpython-$(PLATFORM)$(if $(PYBUILD_OPTIMIZED),-pgo,).tar

$(OUTDIR)/image-%.tar: $(HERE)/%.Dockerfile $(COMMON_DEPENDS)
$(BUILD) image-$*
Expand Down
5 changes: 2 additions & 3 deletions cpython-linux/build-bdb.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ set -ex
cd /build

export PATH=/tools/${TOOLCHAIN}/bin:/tools/host/bin:$PATH
export CC=clang
export CXX=clang++

tar -xf db-${BDB_VERSION}.tar.gz

Expand All @@ -19,7 +17,8 @@ CLFAGS="${EXTRA_TARGET_CFLAGS} -fPIC" CPPFLAGS="${EXTRA_TARGET_CFLAGS} -fPIC" ..
--build=x86_64-unknown-linux-gnu \
--target=${TARGET} \
--prefix=/tools/deps \
--enable-dbm
--enable-dbm \
--disable-shared

make -j `nproc`
make -j `nproc` install DESTDIR=/build/out
2 changes: 1 addition & 1 deletion cpython-linux/build-bzip2.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pushd bzip2-${BZIP2_VERSION}

make -j `nproc` install \
AR=/tools/host/bin/${TOOLCHAIN_PREFIX}ar \
CC=clang \
CC="${CC}" \
CFLAGS="${EXTRA_TARGET_CFLAGS} -fPIC" \
LDFLAGS="${EXTRA_TARGET_LDFLAGS}" \
PREFIX=/build/out/tools/deps
10 changes: 8 additions & 2 deletions cpython-linux/build-cpython.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ set -ex
cd /build

export PATH=/tools/clang-linux64/bin:/tools/host/bin:/tools/deps/bin:$PATH
export CC=clang
export CXX=clang++

# configure somehow has problems locating llvm-profdata even though it is in
# PATH. The macro it is using allows us to specify its path via an
Expand All @@ -26,6 +24,8 @@ tar -xf Python-${PYTHON_VERSION}.tar.xz
cat Setup.local
mv Setup.local Python-${PYTHON_VERSION}/Modules/Setup.local

cat Makefile.extra

pushd Python-${PYTHON_VERSION}

cp Modules/readline.c Modules/readline-libedit.c
Expand All @@ -44,6 +44,12 @@ CFLAGS="-fPIC -I/tools/deps/include -I/tools/deps/include/ncurses"
CPPFLAGS=$CFLAGS
LDFLAGS="-L/tools/deps/lib"

if [ "${CC}" = "musl-clang" ]; then
CFLAGS="${CFLAGS} -static"
CPPFLAGS="${CPPFLAGS} -static"
LDFLAGS="${LDFLAGS} -static"
fi

CONFIGURE_FLAGS="--prefix=/install --with-openssl=/tools/deps --without-ensurepip"

# TODO support --with-lto
Expand Down
3 changes: 1 addition & 2 deletions cpython-linux/build-gdbm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ set -ex
cd /build

export PATH=/tools/${TOOLCHAIN}/bin:/tools/host/bin:$PATH
export CC=clang
export CXX=clang++

tar -xf gdbm-${GDBM_VERSION}.tar.gz

Expand All @@ -21,6 +19,7 @@ CLFAGS="${EXTRA_TARGET_CFLAGS} -fPIC" CPPFLAGS="${EXTRA_TARGET_CFLAGS} -fPIC" ./
--build=x86_64-unknown-linux-gnu \
--target=${TARGET} \
--prefix=/tools/deps \
--disable-shared \
--enable-libgdbm-compat

make -j `nproc`
Expand Down
8 changes: 6 additions & 2 deletions cpython-linux/build-libedit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,25 @@ set -ex
cd /build

export PATH=/tools/${TOOLCHAIN}/bin:/tools/host/bin:$PATH
export CC=clang
export CXX=clang++

tar -xf libedit-${LIBEDIT_VERSION}.tar.gz

pushd libedit-${LIBEDIT_VERSION}

cflags="${EXTRA_TARGET_CFLAGS} -fPIC -I/tools/deps/include -I/tools/deps/include/ncurses"

# musl doesn't define __STDC_ISO_10646__, so work around that.
if [ "${CC}" = "musl-clang" ]; then
cflags="${cflags} -D__STDC_ISO_10646__=201103L"
fi

# Install to /tools/deps/libedit so it doesn't conflict with readline's files.
CLFAGS="${cflags}" CPPFLAGS="${cflags}" LDFLAGS="-L/tools/deps/lib" \
./configure \
--build=x86_64-unknown-linux-gnu \
--host=${TARGET} \
--prefix=/tools/deps/libedit \
--disable-shared

make -j `nproc`
make -j `nproc` install DESTDIR=/build/out
Expand Down
5 changes: 2 additions & 3 deletions cpython-linux/build-libffi.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ set -ex
cd /build

export PATH=/tools/${TOOLCHAIN}/bin:/tools/host/bin:$PATH
export CC=clang
export CXX=clang++

tar -xf libffi-${LIBFFI_VERSION}.tar.gz

Expand All @@ -18,7 +16,8 @@ pushd libffi-${LIBFFI_VERSION}
CFLAGS="${EXTRA_TARGET_CFLAGS} -fPIC" CPPFLAGS="${EXTRA_TARGET_CFLAGS} -fPIC" ./configure \
--build=x86_64-unknown-linux-gnu \
--host=${TARGET} \
--prefix=/tools/deps
--prefix=/tools/deps \
--disable-shared

make -j `nproc`
make -j `nproc` install DESTDIR=/build/out
5 changes: 2 additions & 3 deletions cpython-linux/build-ncurses.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ set -ex
cd /build

export PATH=/tools/${TOOLCHAIN}/bin:/tools/host/bin:$PATH
export CC=clang
export CXX=clang++

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

Expand All @@ -18,6 +16,7 @@ pushd ncurses-${NCURSES_VERSION}
CFLAGS="${EXTRA_TARGET_CFLAGS} -fPIC" ./configure \
--build=x86_64-unknown-linux-gnu \
--host=${TARGET} \
--prefix=/tools/deps
--prefix=/tools/deps \
--without-cxx
make -j `nproc`
make -j `nproc` install DESTDIR=/build/out
11 changes: 8 additions & 3 deletions cpython-linux/build-openssl.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,19 @@ set -ex
cd /build

export PATH=/tools/${TOOLCHAIN}/bin:/tools/host/bin:$PATH
export CC=clang
export CXX=clang++

tar -xf openssl-${OPENSSL_VERSION}.tar.gz

pushd openssl-${OPENSSL_VERSION}

/usr/bin/perl ./Configure --prefix=/tools/deps linux-x86_64
# musl is missing support for various primitives.
# TODO disable secure memory is a bit scary. We should look into a proper
# workaround.
if [ "${CC}" = "musl-clang" ]; then
EXTRA_FLAGS="no-async -DOPENSSL_NO_ASYNC -D__STDC_NO_ATOMICS__=1 no-engine -DOPENSSL_NO_SECURE_MEMORY "
fi

/usr/bin/perl ./Configure --prefix=/tools/deps linux-x86_64 no-shared ${EXTRA_FLAGS}

make -j `nproc`
make -j `nproc` install DESTDIR=/build/out
3 changes: 1 addition & 2 deletions cpython-linux/build-readline.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ set -ex
cd /build

export PATH=/tools/${TOOLCHAIN}/bin:/tools/host/bin:$PATH
export CC=clang
export CXX=clang++

tar -xf readline-${READLINE_VERSION}.tar.gz

Expand All @@ -20,6 +18,7 @@ CLFAGS="${EXTRA_TARGET_CFLAGS} -fPIC" CPPFLAGS="${EXTRA_TARGET_CFLAGS} -fPIC" LD
--build=x86_64-unknown-linux-gnu \
--host=${TARGET} \
--prefix=/tools/deps \
--disable-shared \
--with-curses

make -j `nproc`
Expand Down
6 changes: 3 additions & 3 deletions cpython-linux/build-sqlite.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ set -ex
cd /build

export PATH=/tools/${TOOLCHAIN}/bin:/tools/host/bin:$PATH
export CC=clang
export CXX=clang++

tar -xf sqlite-autoconf-3280000.tar.gz
pushd sqlite-autoconf-3280000

CFLAGS="-fPIC" CPPFLAGS="-fPIC" ./configure --prefix /tools/deps
CFLAGS="-fPIC" CPPFLAGS="-fPIC" ./configure \
--prefix /tools/deps \
--disable-shared

make -j `nproc`
make -j `nproc` install DESTDIR=/build/out
2 changes: 0 additions & 2 deletions cpython-linux/build-tcltk.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ set -ex
cd /build

export PATH=/tools/${TOOLCHAIN}/bin:/tools/host/bin:$PATH
export CC=clang
export CXX=clang++

tar -xf tcl8.6.9-src.tar.gz

Expand Down
6 changes: 3 additions & 3 deletions cpython-linux/build-uuid.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ set -ex
cd /build

export PATH=/tools/${TOOLCHAIN}/bin:/tools/host/bin:$PATH
export CC=clang
export CXX=clang++

tar -xf libuuid-${UUID_VERSION}.tar.gz
pushd libuuid-${UUID_VERSION}

CFLAGS="-fPIC" CPPFLAGS="-fPIC" ./configure --prefix=/tools/deps
CFLAGS="-fPIC" CPPFLAGS="-fPIC" ./configure \
--prefix=/tools/deps \
--disable-shared

make -j `nproc`
make -j `nproc` install DESTDIR=/build/out
3 changes: 1 addition & 2 deletions cpython-linux/build-xz.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@ set -ex
cd /build

export PATH=/tools/${TOOLCHAIN}/bin:/tools/host/bin:$PATH
export CC=clang
export CXX=clang++

tar -xf xz-${XZ_VERSION}.tar.gz

pushd xz-${XZ_VERSION}

CFLAGS="-fPIC" CPPFLAGS="-fPIC" CCASFLAGS="-fPIC" ./configure \
--prefix=/tools/deps \
--disable-shared \
--disable-xz \
--disable-xzdec \
--disable-lzmadec \
Expand Down
2 changes: 0 additions & 2 deletions cpython-linux/build-zlib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ set -ex
cd /build

export PATH=/tools/${TOOLCHAIN}/bin:/tools/host/bin:$PATH
export CC=clang
export CXX=clang++

tar -xf zlib-${ZLIB_VERSION}.tar.gz

Expand Down
Loading

0 comments on commit 0d3ae2a

Please sign in to comment.