Skip to content

Commit

Permalink
Fix cargo test with extension-module feature.
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhewitt committed Nov 19, 2020
1 parent d3f993a commit b23cd69
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Remove unused `python3` feature. [#1235](https://github.com/PyO3/pyo3/pull/1235)

### Fixed
- Fix `cargo test` with `extension-module` feature. (Requires `cargo +nightly -Zextra-link-arg test` for now.) #[1123](https://github.com/PyO3/pyo3/pull/1123)
- Fix missing field in `PyCodeObject` struct (`co_posonlyargcount`) - caused invalid access to other fields in Python >3.7. [#1260](https://github.com/PyO3/pyo3/pull/1260)
- Fix building for `x86_64-unknown-linux-musl` target from `x86_65-unknown-linux-gnu` host. [#1267](https://github.com/PyO3/pyo3/pull/1267)

Expand Down
33 changes: 20 additions & 13 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -594,8 +594,8 @@ fn get_library_link_name(version: &PythonVersion, ld_version: &str) -> String {
}
}

fn get_rustc_link_lib(config: &InterpreterConfig) -> Result<String> {
match env::var("CARGO_CFG_TARGET_OS").unwrap().as_str() {
fn get_rustc_link_lib(config: &InterpreterConfig, target_os: &str) -> Result<String> {
match target_os {
"windows" => get_rustc_link_lib_windows(config),
"macos" => get_rustc_link_lib_macos(config),
_ => get_rustc_link_lib_unix(config),
Expand Down Expand Up @@ -762,19 +762,26 @@ fn configure(interpreter_config: &InterpreterConfig) -> Result<String> {
}

check_target_architecture(interpreter_config)?;
let target_os = env::var_os("CARGO_CFG_TARGET_OS").unwrap();
let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap();

if let Some(libdir) = &interpreter_config.libdir {
println!("cargo:rustc-link-search=native={}", libdir);
} else if target_os == "windows" {
println!(
"cargo:rustc-link-search=native={}\\libs",
interpreter_config.base_prefix
);
}

let is_extension_module = env::var_os("CARGO_FEATURE_EXTENSION_MODULE").is_some();
if !is_extension_module || target_os == "windows" || target_os == "android" {
println!("{}", get_rustc_link_lib(&interpreter_config)?);
if let Some(libdir) = &interpreter_config.libdir {
println!("cargo:rustc-link-search=native={}", libdir);
} else if target_os == "windows" {
println!(
"cargo:rustc-link-search=native={}\\libs",
interpreter_config.base_prefix
);
}
if is_extension_module && !(target_os == "windows" || target_os == "android") {
// Extension module on unix - only link non-lib targets
let lib_name =
get_library_link_name(&interpreter_config.version, &interpreter_config.ld_version);
println!("cargo:rustc-link-arg-bins=-l{}", lib_name);
} else {
// Not extension module, or is windows or android - always link to libpython.
println!("{}", get_rustc_link_lib(&interpreter_config, &target_os)?);
}

let mut flags = String::new();
Expand Down
17 changes: 9 additions & 8 deletions guide/src/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@ PyO3 provides a struct [`GILOnceCell`] which works equivalently to `OnceCell` bu

[`GILOnceCell`]: https://docs.rs/pyo3/latest/pyo3/once_cell/struct.GILOnceCell.html

## I can't run `cargo test`: I'm having linker issues like "Symbol not found" or "Undefined reference to _PyExc_SystemError"!
## I can't run `cargo test` or `cargo run`: I'm having linker issues like "Symbol not found" or "Undefined reference to _PyExc_SystemError"!

Currently, [#341](https://github.com/PyO3/pyo3/issues/341) causes `cargo test` to fail with linking errors when the `extension-module` feature is activated. For now you can work around this by making the `extension-module` feature optional and running the tests with `cargo test --no-default-features`:
On unix operating systems the `extension-module` feature is required to disable linking against libpython to meet criteria of how Python extension modules should be built.

```toml
[dependencies.pyo3]
version = "*"
PyO3 is able to re-enable linking for binaries and tests in the project, but it requires a nightly cargo feature. To use this feature, you must opt into it, e.g.:

[features]
extension-module = ["pyo3/extension-module"]
default = ["extension-module"]
```
# For cargo test
cargo +nightly -Zextra-link-arg test
# For cargo run
cargo +nightly -Zextra-link-arg run
```

0 comments on commit b23cd69

Please sign in to comment.