Skip to content

Commit 95753bb

Browse files
authored
Implement the Unix build system refactor (python#24)
This PR pretty much completely refactors the Rust build system. There are now two main ways Rust code is built: # both - The Rust triple is deduced based on the preprocessor run on Misc/platform_triplet.c - A cpython-build-helper crate is used to pass the proper link arguments for each configuration - The proper Rust toolchain is downloaded for iOS/Android/WASI for CI on those platforms - cpython-sys is updated significantly to ensure bindgen properly generates the bindings for each platform # shared For shared builds, we build a crate into a cdylib and pass through link arguments from the makefile/configure and the linker executable so that the final link will match what is done for C programs. # static For static builds, a cpython-rust-staticlib crate is a built which depends on and re-exports the module initializers for each crate. This ensures there aren't duplicated Rust stdlib/core symbols. Fixes python#23
1 parent e776700 commit 95753bb

File tree

22 files changed

+516
-126
lines changed

22 files changed

+516
-126
lines changed

.github/workflows/build.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,9 @@ jobs:
383383
- uses: actions/checkout@v4
384384
with:
385385
persist-credentials: false
386+
- uses: dtolnay/rust-toolchain@1.91.1
387+
with:
388+
targets: ${{ matrix.arch }}-linux-android
386389
- name: Build and test
387390
run: ./Android/android.py ci --fast-ci ${{ matrix.arch }}-linux-android
388391

@@ -396,7 +399,9 @@ jobs:
396399
- uses: actions/checkout@v4
397400
with:
398401
persist-credentials: false
399-
402+
- uses: dtolnay/rust-toolchain@1.91.1
403+
with:
404+
targets: aarch64-apple-ios-sim
400405
# GitHub recommends explicitly selecting the desired Xcode version:
401406
# https://github.com/actions/runner-images/issues/12541#issuecomment-3083850140
402407
# This became a necessity as a result of

.github/workflows/reusable-wasi.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ jobs:
4848
uses: actions/setup-python@v5
4949
with:
5050
python-version: '3.x'
51+
- uses: dtolnay/rust-toolchain@1.91.1
52+
with:
53+
targets: wasm32-wasip1
5154
- name: "Runner image version"
5255
run: echo "IMAGE_OS_VERSION=${ImageOS}-${ImageVersion}" >> "$GITHUB_ENV"
5356
- name: "Configure build Python"

Cargo.lock

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

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[workspace]
22
resolver = "3"
33
members = [
4-
"Modules/_base64",
4+
"Modules/_base64", "Modules/cpython-build-helper", "Modules/cpython-rust-staticlib",
55
"Modules/cpython-sys"
66
]

Makefile.pre.in

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ CARGO_HOME=@CARGO_HOME@
6262
CARGO_TARGET_DIR=@CARGO_TARGET_DIR@
6363
CARGO_PROFILE=@CARGO_PROFILE@
6464
CARGO_TARGET=@CARGO_TARGET@
65+
CARGO_TARGET_LINKER_ENV=@CARGO_TARGET_LINKER_ENV@
66+
CARGO_DYLIB_SUFFIX=@CARGO_DYLIB_SUFFIX@
67+
LLVM_TARGET=@LLVM_TARGET@
68+
RUST_STATICLIB_DEP=@RUST_STATICLIB_DEP@
6569

6670
GNULD= @GNULD@
6771

@@ -178,7 +182,8 @@ CONFINCLUDEPY= $(CONFINCLUDEDIR)/python$(LDVERSION)
178182
SHLIB_SUFFIX= @SHLIB_SUFFIX@
179183
EXT_SUFFIX= @EXT_SUFFIX@
180184
LDSHARED= @LDSHARED@ $(PY_LDFLAGS)
181-
BLDSHARED= @BLDSHARED@ $(PY_CORE_LDFLAGS)
185+
BLDSHARED_EXE= @BLDSHARED_EXE@
186+
BLDSHARED_ARGS= @BLDSHARED_ARGS@ $(PY_CORE_LDFLAGS)
182187
LDCXXSHARED= @LDCXXSHARED@ $(PY_LDFLAGS)
183188
DESTSHARED= $(BINLIBDEST)/lib-dynload
184189

@@ -988,7 +993,7 @@ clinic-tests: check-clean-src $(srcdir)/Lib/test/clinic.test.c
988993
$(PYTHON_FOR_REGEN) $(srcdir)/Tools/clinic/clinic.py -f $(srcdir)/Lib/test/clinic.test.c
989994

990995
# Build the interpreter
991-
$(BUILDPYTHON): Programs/python.o $(LINK_PYTHON_DEPS)
996+
$(BUILDPYTHON): Programs/python.o $(LINK_PYTHON_DEPS) $(RUST_STATICLIB_DEP)
992997
$(LINKCC) $(PY_CORE_LDFLAGS) $(LINKFORSHARED) -o $@ Programs/python.o $(LINK_PYTHON_OBJS) $(LIBS) $(MODLIBS) $(SYSLIBS)
993998

994999
platform: $(PYTHON_FOR_BUILD_DEPS) pybuilddir.txt
@@ -1021,16 +1026,16 @@ $(LIBRARY): $(LIBRARY_OBJS)
10211026
libpython$(LDVERSION).so: $(LIBRARY_OBJS) $(DTRACE_OBJS)
10221027
# AIX Linker don't support "-h" option
10231028
if test "$(MACHDEP)" != "aix"; then \
1024-
$(BLDSHARED) -Wl,-h$(INSTSONAME) -o $(INSTSONAME) $(LIBRARY_OBJS) $(MODLIBS) $(SHLIBS) $(LIBC) $(LIBM); \
1029+
$(BLDSHARED_EXE) $(BLDSHARED_ARGS) -Wl,-h$(INSTSONAME) -o $(INSTSONAME) $(LIBRARY_OBJS) $(MODLIBS) $(SHLIBS) $(LIBC) $(LIBM); \
10251030
else \
1026-
$(BLDSHARED) -o $@ $(LIBRARY_OBJS) $(MODLIBS) $(SHLIBS) $(LIBC) $(LIBM); \
1031+
$(BLDSHARED_EXE) $(BLDSHARED_ARGS) -o $@ $(LIBRARY_OBJS) $(MODLIBS) $(SHLIBS) $(LIBC) $(LIBM); \
10271032
fi
10281033
if test $(INSTSONAME) != $@; then \
10291034
$(LN) -f $(INSTSONAME) $@; \
10301035
fi
10311036

10321037
libpython3.so: libpython$(LDVERSION).so
1033-
$(BLDSHARED) $(NO_AS_NEEDED) -o $@ -Wl,-h$@ $^
1038+
$(BLDSHARED_EXE) $(BLDSHARED_ARGS) $(NO_AS_NEEDED) -o $@ -Wl,-h$@ $^
10341039

10351040
libpython$(LDVERSION).dylib: $(LIBRARY_OBJS)
10361041
$(CC) -dynamiclib $(PY_CORE_LDFLAGS) -undefined dynamic_lookup -Wl,-install_name,$(prefix)/lib/libpython$(LDVERSION).dylib -Wl,-compatibility_version,$(VERSION) -Wl,-current_version,$(VERSION) -o $@ $(LIBRARY_OBJS) $(DTRACE_OBJS) $(SHLIBS) $(LIBC) $(LIBM); \
@@ -3374,7 +3379,12 @@ Python/thread.o: @THREADHEADERS@ $(srcdir)/Python/condvar.h
33743379
# Module dependencies and platform-specific files
33753380

33763381
cpython-sys: Modules/cpython-sys/Cargo.toml Modules/cpython-sys/build.rs Modules/cpython-sys/wrapper.h Modules/cpython-sys/parser.h
3377-
CARGO_TARGET_DIR=$(abs_builddir)/target PYTHON_BUILD_DIR=$(abs_builddir) \$(CARGO_HOME)/bin/cargo build --lib --locked --package cpython-sys --profile $(CARGO_PROFILE) $(if $(CARGO_TARGET),--target=$(CARGO_TARGET)) --manifest-path $(srcdir)/Cargo.toml
3382+
CARGO_TARGET_DIR=$(abs_builddir)/target PYTHON_BUILD_DIR=$(abs_builddir) PY_CC="$(CC)" PY_CPPFLAGS="$(CPPFLAGS)" PY_CFLAGS="$(CFLAGS)" LLVM_TARGET="$(LLVM_TARGET)" $(CARGO_HOME)/bin/cargo build --lib --locked --package cpython-sys --profile $(CARGO_PROFILE) $(if $(CARGO_TARGET),--target=$(CARGO_TARGET)) --manifest-path $(srcdir)/Cargo.toml
3383+
3384+
RUST_STATICLIB_A= target/$(if $(CARGO_TARGET),$(CARGO_TARGET)/$(CARGO_TARGET_DIR),$(CARGO_TARGET_DIR))/libcpython_rust_staticlib.a
3385+
3386+
cpython-rust-staticlib: cpython-sys
3387+
CARGO_TARGET_DIR=$(abs_builddir)/target PYTHON_BUILD_DIR=$(abs_builddir) $(CARGO_HOME)/bin/cargo build --lib --locked --package cpython-rust-staticlib --profile $(CARGO_PROFILE) $(if $(CARGO_TARGET),--target=$(CARGO_TARGET)) --manifest-path $(srcdir)/Cargo.toml
33783388

33793389
# force rebuild when header file or module build flavor (static/shared) is changed
33803390
MODULE_DEPS_STATIC=Modules/config.c

0 commit comments

Comments
 (0)