diff --git a/Makefile.pre.in b/Makefile.pre.in index 10f299819cd40f..bc093faa95791a 100644 --- a/Makefile.pre.in +++ b/Makefile.pre.in @@ -3373,7 +3373,7 @@ Python/thread.o: @THREADHEADERS@ $(srcdir)/Python/condvar.h ########################################################################## # Module dependencies and platform-specific files -cpython-sys: Modules/cpython-sys/Cargo.toml Modules/cpython-sys/build.rs Modules/cpython-sys/wrapper.h Modules/cpython-sys/parser.h +cpython-sys: $(LIBRARY) Modules/cpython-sys/Cargo.toml Modules/cpython-sys/build.rs Modules/cpython-sys/wrapper.h Modules/cpython-sys/parser.h 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 # force rebuild when header file or module build flavor (static/shared) is changed diff --git a/Modules/Setup b/Modules/Setup index 86b43541f37ecc..8056ca2722b092 100644 --- a/Modules/Setup +++ b/Modules/Setup @@ -182,7 +182,7 @@ PYTHONPATH=$(COREPYTHONPATH) #_codecs_tw cjkcodecs/_codecs_tw.c #_multibytecodec cjkcodecs/multibytecodec.c #unicodedata unicodedata.c -#_base64 _base64/Cargo.toml _base64/src/lib.rs lib_base64.a +#_base64 _base64/Cargo.toml _base64/src/lib.rs # Modules with some UNIX dependencies diff --git a/Modules/Setup.stdlib.in b/Modules/Setup.stdlib.in index 635be3cbe8dbc0..6b56dd858ce585 100644 --- a/Modules/Setup.stdlib.in +++ b/Modules/Setup.stdlib.in @@ -120,7 +120,7 @@ ############################################################################ # Rust modules # -@MODULE__BASE64_TRUE@_base64 _base64/Cargo.toml _base64/src/lib.rs lib_base64.a +@MODULE__BASE64_TRUE@_base64 _base64/Cargo.toml _base64/src/lib.rs ############################################################################ # Modules with some UNIX dependencies diff --git a/Modules/_base64/Cargo.toml b/Modules/_base64/Cargo.toml index 0810b787ab2773..3fe4185f074eb7 100644 --- a/Modules/_base64/Cargo.toml +++ b/Modules/_base64/Cargo.toml @@ -8,4 +8,4 @@ cpython-sys ={ path = "../cpython-sys" } [lib] name = "_base64" -crate-type = ["staticlib"] +crate-type = ["cdylib"] diff --git a/Modules/_base64/src/lib.rs b/Modules/_base64/src/lib.rs index 7e13438ca21c6f..64e4d718789372 100644 --- a/Modules/_base64/src/lib.rs +++ b/Modules/_base64/src/lib.rs @@ -115,7 +115,6 @@ impl Drop for BorrowedBuffer { /// # Safety /// `module` must be a valid pointer of PyObject representing the module. /// `args` must be a valid pointer to an array of valid PyObject pointers with length `nargs`. -#[unsafe(no_mangle)] pub unsafe extern "C" fn standard_b64encode( _module: *mut PyObject, args: *mut *mut PyObject, @@ -193,13 +192,11 @@ fn standard_b64encode_impl(source: &PyObject) -> Result<*mut PyObject, ()> { Ok(result) } -#[unsafe(no_mangle)] pub extern "C" fn _base64_clear(_obj: *mut PyObject) -> c_int { //TODO 0 } -#[unsafe(no_mangle)] pub extern "C" fn _base64_free(_o: *mut c_void) { //TODO } diff --git a/Modules/cpython-sys/build.rs b/Modules/cpython-sys/build.rs index 248b141bfd6d99..6e9fb8ab48c640 100644 --- a/Modules/cpython-sys/build.rs +++ b/Modules/cpython-sys/build.rs @@ -2,6 +2,7 @@ use std::env; use std::path::{Path, PathBuf}; fn main() { + println!("cargo::rerun-if-env-changed=LIBRARY"); let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()); let srcdir = manifest_dir .parent() @@ -12,6 +13,11 @@ fn main() { if gil_disabled(srcdir, builddir.as_deref()) { println!("cargo:rustc-cfg=py_gil_disabled"); } + if let Ok(libpython) = env::var("LIBRARY") + && libpython.len() != 0 + { + println!("cargo::rustc-link-lib=static={}", libpython); + } generate_c_api_bindings(srcdir, builddir.as_deref(), out_path.as_path()); // TODO(emmatyping): generate bindings to the internal parser API // The parser includes things slightly differently, so we should generate diff --git a/Modules/makesetup b/Modules/makesetup index bd33c8fb2804c7..f4523e811b5dc5 100755 --- a/Modules/makesetup +++ b/Modules/makesetup @@ -232,7 +232,6 @@ sed -e 's/[ ]*#.*//' -e '/^[ ]*$/d' | yes) continue;; esac objs='' - custom_ldflags='' if test "x$rust" = "x"; then for src in $srcs do @@ -272,6 +271,23 @@ sed -e 's/[ ]*#.*//' -e '/^[ ]*$/d' | esac echo "$rule" >>$rulesf done + + case $doconfig in + yes) OBJS="$OBJS $objs";; + esac + for mod in $mods + do + file="$srcdir/$mod\$(EXT_SUFFIX)" + case $doconfig in + no) + SHAREDMODS="$SHAREDMODS $file" + BUILT_SHARED="$BUILT_SHARED $mod" + ;; + esac + rule="$file: $objs \$(MODULE_${mods_upper}_LDEPS)" + rule="$rule; \$(BLDSHARED) $objs $libs \$(LIBPYTHON) -o $file" + echo "$rule" >>$rulesf + done else prefixed_srcs= for src in $srcs @@ -279,44 +295,25 @@ sed -e 's/[ ]*#.*//' -e '/^[ ]*$/d' | prefixed_srcs="$prefixed_srcs $srcdir/$src" done objs= - # there's actually only one obj, so just set it to the lib - for lib in $libs - do - objs="target/\$(if \$(CARGO_TARGET),\$(CARGO_TARGET)/\$(CARGO_TARGET_DIR),\$(CARGO_TARGET_DIR))/$lib" - done libs= - # depends on the headers through cpython-sys - rule="$objs: cpython-sys \$(srcdir)/Cargo.toml \$(srcdir)/Cargo.lock \$(srcdir)/$srcdir/$manifest $prefixed_srcs \$(PYTHON_HEADERS)" - 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" - echo "$rule" >>$rulesf + for mod in $mods do - case $UNAME_SYSTEM in - Darwin*) - custom_ldflags="$custom_ldflags -Wl,-u,_PyInit_$mod" - ;; - *) - custom_ldflags="$custom_ldflags -Wl,--defsym=PyInit_$mod=PyInit_$mod" + rust_shared="target/\$(CARGO_TARGET)/\$(CARGO_TARGET_DIR)/lib$mod\$(SHLIB_SUFFIX)" + file="$srcdir/$mod\$(EXT_SUFFIX)" + case $doconfig in + no) + SHAREDMODS="$SHAREDMODS $file" + BUILT_SHARED="$BUILT_SHARED $mod" ;; esac + # depends on the headers through cpython-sys + rule="$rust_shared: cpython-sys \$(srcdir)/Cargo.toml \$(srcdir)/Cargo.lock \$(srcdir)/$srcdir/$manifest $prefixed_srcs \$(PYTHON_HEADERS) \$(MODULE_${mods_upper}_LDEPS) \$(LIBRARY)" + 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" + echo "$rule" >>$rulesf + echo "$file: $rust_shared; mv $rust_shared $file" >>$rulesf done fi - case $doconfig in - yes) OBJS="$OBJS $objs";; - esac - for mod in $mods - do - file="$srcdir/$mod\$(EXT_SUFFIX)" - case $doconfig in - no) - SHAREDMODS="$SHAREDMODS $file" - BUILT_SHARED="$BUILT_SHARED $mod" - ;; - esac - rule="$file: $objs \$(MODULE_${mods_upper}_LDEPS)" - rule="$rule; \$(BLDSHARED) $custom_ldflags $objs $libs \$(LIBPYTHON) -o $file" - echo "$rule" >>$rulesf - done done case $SHAREDMODS in diff --git a/configure b/configure index 5b09de36f9a0b8..24943a854aea5a 100755 --- a/configure +++ b/configure @@ -36187,3 +36187,4 @@ if test "$ac_cv_header_stdatomic_h" != "yes"; then { printf "%s\n" "$as_me:${as_lineno-$LINENO}: Your compiler or platform does have a working C11 stdatomic.h. A future version of Python may require stdatomic.h." >&5 printf "%s\n" "$as_me: Your compiler or platform does have a working C11 stdatomic.h. A future version of Python may require stdatomic.h." >&6;} fi +