@@ -18,7 +18,7 @@ pub use impl_::{
1818 PythonVersion , Triple ,
1919} ;
2020
21- use target_lexicon:: OperatingSystem ;
21+ use target_lexicon:: { Architecture , OperatingSystem } ;
2222
2323/// Adds all the [`#[cfg]` flags](index.html) to the current compilation.
2424///
@@ -95,23 +95,7 @@ fn _add_extension_module_link_args(
9595/// for more details.
9696pub fn add_libpython_rpath_link_args ( ) {
9797 let target = impl_:: target_triple_from_env ( ) ;
98- _add_libpython_rpath_link_args (
99- get ( ) ,
100- impl_:: is_linking_libpython_for_target ( & target) ,
101- std:: io:: stdout ( ) ,
102- )
103- }
104-
105- fn _add_libpython_rpath_link_args (
106- interpreter_config : & InterpreterConfig ,
107- is_linking_libpython : bool ,
108- mut writer : impl std:: io:: Write ,
109- ) {
110- if is_linking_libpython {
111- if let Some ( lib_dir) = interpreter_config. lib_dir ( ) {
112- writeln ! ( writer, "cargo:rustc-link-arg=-Wl,-rpath,{lib_dir}" ) . unwrap ( ) ;
113- }
114- }
98+ pyo3_build_script_impl:: print_libpython_rpath_link_args ( & target, get ( ) ) ;
11599}
116100
117101/// Adds linker arguments suitable for linking against the Python framework on macOS.
@@ -162,32 +146,6 @@ fn get_inner() -> InterpreterConfig {
162146 interpreter_config. expect ( "failed to parse PyO3 config" )
163147}
164148
165- /// Helper to print a feature cfg with a minimum rust version required.
166- fn print_feature_cfg ( minor_version_required : u32 , cfg : & str ) {
167- let minor_version = rustc_minor_version ( ) . unwrap_or ( 0 ) ;
168-
169- if minor_version >= minor_version_required {
170- println ! ( "cargo:rustc-cfg={cfg}" ) ;
171- }
172-
173- // rustc 1.80.0 stabilized `rustc-check-cfg` feature, don't emit before
174- if minor_version >= 80 {
175- println ! ( "cargo:rustc-check-cfg=cfg({cfg})" ) ;
176- }
177- }
178-
179- /// Use certain features if we detect the compiler being used supports them.
180- ///
181- /// Features may be removed or added as MSRV gets bumped or new features become available,
182- /// so this function is unstable.
183- #[ doc( hidden) ]
184- pub fn print_feature_cfgs ( ) {
185- print_feature_cfg ( 84 , "const_is_null" ) ;
186- print_feature_cfg ( 85 , "fn_ptr_eq" ) ;
187- print_feature_cfg ( 86 , "from_bytes_with_nul_error" ) ;
188- print_feature_cfg ( 95 , "cfg_select" ) ;
189- }
190-
191149/// Registers `pyo3`s config names as reachable cfg expressions
192150///
193151/// - <https://github.com/rust-lang/cargo/pull/13571>
@@ -328,6 +286,47 @@ pub mod pyo3_build_script_impl {
328286 self . message
329287 }
330288 }
289+
290+ /// Detects features which `pyo3` and `pyo3-ffi` depend upon internally, and prints the appropriate
291+ /// `cargo:rustc-cfg` and `cargo:rustc-check-cfg` directives to enable them.
292+ pub fn print_feature_cfgs ( ) {
293+ print_feature_cfg ( 84 , "const_is_null" ) ;
294+ print_feature_cfg ( 85 , "fn_ptr_eq" ) ;
295+ print_feature_cfg ( 86 , "from_bytes_with_nul_error" ) ;
296+ print_feature_cfg ( 95 , "cfg_select" ) ;
297+ }
298+
299+ /// Helper to print a feature cfg with a minimum rust version required.
300+ fn print_feature_cfg ( minor_version_required : u32 , cfg : & str ) {
301+ println ! ( "cargo:rustc-check-cfg=cfg({cfg})" ) ;
302+
303+ let minor_version = rustc_minor_version ( ) . unwrap_or ( 0 ) ;
304+ if minor_version >= minor_version_required {
305+ println ! ( "cargo:rustc-cfg={cfg}" ) ;
306+ }
307+ }
308+
309+ /// Emit libpython rpath link args if appropriate for the target and interpreter config.
310+ ///
311+ /// This form exists for pyo3-ffi where `get()` cannot be called.
312+ pub fn print_libpython_rpath_link_args (
313+ target : & Triple ,
314+ interpreter_config : & InterpreterConfig ,
315+ ) {
316+ let is_linking_libpython = is_linking_libpython_for_target ( target) ;
317+ let is_wasm = matches ! (
318+ target. architecture,
319+ Architecture :: Wasm32 | Architecture :: Wasm64
320+ ) ;
321+ let is_emscripten = target. operating_system == target_lexicon:: OperatingSystem :: Emscripten ;
322+ // webassembly targets generally don't support rpath, emscripten is the only exception currently aware of:
323+ // https://github.com/emscripten-core/emscripten/issues/22126
324+ if is_linking_libpython && ( !is_wasm || is_emscripten) {
325+ if let Some ( lib_dir) = interpreter_config. lib_dir ( ) {
326+ println ! ( "cargo:rustc-link-arg=-Wl,-rpath,{lib_dir}" ) ;
327+ }
328+ }
329+ }
331330}
332331
333332fn rustc_minor_version ( ) -> Option < u32 > {
0 commit comments