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 Aug 30, 2020
1 parent f816786 commit 00274f1
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 @@ -48,6 +48,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Link against libpython on android with `extension-module` set. [#1095](https://github.com/PyO3/pyo3/pull/1095)
- Fix support for both `__add__` and `__radd__` in the `+` operator when both are defined in `PyNumberProtocol`
(and similar for all other reversible operators). [#1107](https://github.com/PyO3/pyo3/pull/1107)
- Fix `cargo test` with `extension-module` feature. (Requires `cargo +nightly -Zextra-link-arg test` for now.) #[1123](https://github.com/PyO3/pyo3/pull/1123)

## [0.11.1] - 2020-06-30
### Added
Expand Down
33 changes: 20 additions & 13 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -568,8 +568,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 @@ -730,19 +730,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 00274f1

Please sign in to comment.