Skip to content

Commit d013390

Browse files
committed
Build shared library directly
This commit modifies the build system to directly generate shared objects rather than generating a static library that is linked to libpython into a shared object. The downside is that we should probably figure out a way to handle custom linker flags and customization of `BLDSHARED`.
1 parent e7fe182 commit d013390

File tree

8 files changed

+44
-36
lines changed

8 files changed

+44
-36
lines changed

Makefile.pre.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3373,7 +3373,7 @@ Python/thread.o: @THREADHEADERS@ $(srcdir)/Python/condvar.h
33733373
##########################################################################
33743374
# Module dependencies and platform-specific files
33753375

3376-
cpython-sys: Modules/cpython-sys/Cargo.toml Modules/cpython-sys/build.rs Modules/cpython-sys/wrapper.h Modules/cpython-sys/parser.h
3376+
cpython-sys: $(LIBPYTHON) Modules/cpython-sys/Cargo.toml Modules/cpython-sys/build.rs Modules/cpython-sys/wrapper.h Modules/cpython-sys/parser.h
33773377
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
33783378

33793379
# force rebuild when header file or module build flavor (static/shared) is changed

Modules/Setup

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ PYTHONPATH=$(COREPYTHONPATH)
182182
#_codecs_tw cjkcodecs/_codecs_tw.c
183183
#_multibytecodec cjkcodecs/multibytecodec.c
184184
#unicodedata unicodedata.c
185-
#_base64 _base64/Cargo.toml _base64/src/lib.rs lib_base64.a
185+
#_base64 _base64/Cargo.toml _base64/src/lib.rs
186186

187187
# Modules with some UNIX dependencies
188188

Modules/Setup.stdlib.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@
120120
############################################################################
121121
# Rust modules
122122
#
123-
@MODULE__BASE64_TRUE@_base64 _base64/Cargo.toml _base64/src/lib.rs lib_base64.a
123+
@MODULE__BASE64_TRUE@_base64 _base64/Cargo.toml _base64/src/lib.rs
124124

125125
############################################################################
126126
# Modules with some UNIX dependencies

Modules/_base64/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ cpython-sys ={ path = "../cpython-sys" }
88

99
[lib]
1010
name = "_base64"
11-
crate-type = ["staticlib"]
11+
crate-type = ["cdylib"]

Modules/cpython-sys/build.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::env;
22
use std::path::{Path, PathBuf};
33

44
fn main() {
5+
println!("cargo::rerun-if-env-changed=LIBPYTHON");
56
let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
67
let srcdir = manifest_dir
78
.parent()
@@ -12,6 +13,10 @@ fn main() {
1213
if gil_disabled(&srcdir, builddir.as_deref()) {
1314
println!("cargo:rustc-cfg=py_gil_disabled");
1415
}
16+
if let Ok(libpython) = env::var("LIBPYTHON")
17+
&& libpython.len() != 0 {
18+
println!("cargo::rustc-link-lib=static={}", libpython);
19+
}
1520
generate_c_api_bindings(srcdir, builddir.as_deref(), &out_path.as_path());
1621
// TODO(emmatyping): generate bindings to the internal parser API
1722
// The parser includes things slightly differently, so we should generate

Modules/makesetup

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,6 @@ sed -e 's/[ ]*#.*//' -e '/^[ ]*$/d' |
232232
yes) continue;;
233233
esac
234234
objs=''
235-
custom_ldflags=''
236235
if test "x$rust" = "x"; then
237236
for src in $srcs
238237
do
@@ -272,51 +271,49 @@ sed -e 's/[ ]*#.*//' -e '/^[ ]*$/d' |
272271
esac
273272
echo "$rule" >>$rulesf
274273
done
274+
275+
case $doconfig in
276+
yes) OBJS="$OBJS $objs";;
277+
esac
278+
for mod in $mods
279+
do
280+
file="$srcdir/$mod\$(EXT_SUFFIX)"
281+
case $doconfig in
282+
no)
283+
SHAREDMODS="$SHAREDMODS $file"
284+
BUILT_SHARED="$BUILT_SHARED $mod"
285+
;;
286+
esac
287+
rule="$file: $objs \$(MODULE_${mods_upper}_LDEPS)"
288+
rule="$rule; \$(BLDSHARED) $objs $libs \$(LIBPYTHON) -o $file"
289+
echo "$rule" >>$rulesf
290+
done
275291
else
276292
prefixed_srcs=
277293
for src in $srcs
278294
do
279295
prefixed_srcs="$prefixed_srcs $srcdir/$src"
280296
done
281297
objs=
282-
# there's actually only one obj, so just set it to the lib
283-
for lib in $libs
284-
do
285-
objs="target/\$(CARGO_TARGET_DIR)/$lib"
286-
done
287298
libs=
288-
# depends on the headers through cpython-sys
289-
rule="$objs: cpython-sys \$(srcdir)/Cargo.toml \$(srcdir)/Cargo.lock \$(srcdir)/$srcdir/$manifest $prefixed_srcs \$(PYTHON_HEADERS)"
290-
rule="$rule; CARGO_TARGET_DIR=\$(abs_builddir)/target PYTHON_BUILD_DIR=\$(abs_builddir) \$(CARGO_HOME)/bin/cargo build --lib --locked --package ${mods} --profile \$(CARGO_PROFILE) \$(if \$(CARGO_TARGET),--target=\$(CARGO_TARGET)) --manifest-path \$(srcdir)/Cargo.toml"
291-
echo "$rule" >>$rulesf
299+
292300
for mod in $mods
293301
do
294-
case $UNAME_SYSTEM in
295-
Darwin*)
296-
custom_ldflags="$custom_ldflags -Wl,-u,_PyInit_$mod"
297-
;;
298-
*)
299-
custom_ldflags="$custom_ldflags -Wl,--defsym=PyInit_$mod=PyInit_$mod"
302+
rust_shared="target/\$(CARGO_TARGET)/\$(CARGO_TARGET_DIR)/lib$mod\$(SHLIB_SUFFIX)"
303+
file="$srcdir/$mod\$(EXT_SUFFIX)"
304+
case $doconfig in
305+
no)
306+
SHAREDMODS="$SHAREDMODS $file"
307+
BUILT_SHARED="$BUILT_SHARED $mod"
300308
;;
301309
esac
310+
# depends on the headers through cpython-sys
311+
rule="$rust_shared: cpython-sys \$(srcdir)/Cargo.toml \$(srcdir)/Cargo.lock \$(srcdir)/$srcdir/$manifest $prefixed_srcs \$(PYTHON_HEADERS) \$(MODULE_${mods_upper}_LDEPS) \$(LIBPYTHON)"
312+
rule="$rule; CARGO_TARGET_DIR=\$(abs_builddir)/target PYTHON_BUILD_DIR=\$(abs_builddir) \$(CARGO_HOME)/bin/cargo build --lib --locked --package ${mod} --profile \$(CARGO_PROFILE) \$(if \$(CARGO_TARGET),--target=\$(CARGO_TARGET)) --manifest-path \$(srcdir)/Cargo.toml"
313+
echo "$rule" >>$rulesf
314+
echo "$file: $rust_shared; mv $rust_shared $file" >>$rulesf
302315
done
303316
fi
304-
case $doconfig in
305-
yes) OBJS="$OBJS $objs";;
306-
esac
307-
for mod in $mods
308-
do
309-
file="$srcdir/$mod\$(EXT_SUFFIX)"
310-
case $doconfig in
311-
no)
312-
SHAREDMODS="$SHAREDMODS $file"
313-
BUILT_SHARED="$BUILT_SHARED $mod"
314-
;;
315-
esac
316-
rule="$file: $objs \$(MODULE_${mods_upper}_LDEPS)"
317-
rule="$rule; \$(BLDSHARED) $custom_ldflags $objs $libs \$(LIBPYTHON) -o $file"
318-
echo "$rule" >>$rulesf
319-
done
320317
done
321318

322319
case $SHAREDMODS in

configure

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

configure.ac

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4333,6 +4333,9 @@ else
43334333
aarch64-apple-ios-simulator)
43344334
CARGO_TARGET="aarch64-apple-ios-sim"
43354335
;;
4336+
x86_64-*-linux-gnu)
4337+
CARGO_TARGET="x86_64-unknown-linux-gnu"
4338+
;;
43364339
*)
43374340
CARGO_TARGET="$host"
43384341
;;

0 commit comments

Comments
 (0)