Merge pull request #2538 from davidhewitt/emscripten-link-args

pyo3-build-config: add link args for wasm32-unknown-emscripten
This commit is contained in:
messense 2022-08-07 10:57:56 +08:00 committed by GitHub
commit 8bc86c39eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 12 deletions

View File

@ -52,6 +52,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `PyCapsule::set_context` no longer takes a `py: Python<'_>` argument.
- `PyCapsule::name` now returns `PyResult<Option<&CStr>>` instead of `&CStr`.
- `FromPyObject::extract` now raises an error if source type is `PyString` and target type is `Vec<T>`. [#2500](https://github.com/PyO3/pyo3/pull/2500)
- `pyo3_build_config::add_extension_module_link_args()` now also emits linker arguments for `wasm32-unknown-emscripten`. [#2500](https://github.com/PyO3/pyo3/pull/2500)
### Removed

View File

@ -16,7 +16,7 @@ use std::{
path::{Path, PathBuf},
};
use std::{env, process::Command};
use std::{env, process::Command, str::FromStr};
#[cfg(feature = "resolve-config")]
use once_cell::sync::OnceCell;
@ -27,6 +27,7 @@ pub use impl_::{
BuildFlag, BuildFlags, CrossCompileConfig, InterpreterConfig, PythonImplementation,
PythonVersion, Triple,
};
use target_lexicon::OperatingSystem;
/// Adds all the [`#[cfg]` flags](index.html) to the current compilation.
///
@ -46,23 +47,27 @@ pub fn use_pyo3_cfgs() {
get().emit_pyo3_cfgs();
}
/// Adds linker arguments (for macOS) suitable for PyO3's `extension-module` feature.
/// Adds linker arguments suitable for PyO3's `extension-module` feature.
///
/// This should be called from a build script.
///
/// This is currently a no-op on non-macOS platforms, however may emit additional linker arguments
/// in future if deemed necessarys.
/// The following link flags are added:
/// - macOS: `-undefined dynamic_lookup`
/// - wasm32-unknown-emscripten: `-sSIDE_MODULE=2 -sWASM_BIGINT`
///
/// All other platforms currently are no-ops, however this may change as necessary
/// in future.
pub fn add_extension_module_link_args() {
_add_extension_module_link_args(
&impl_::cargo_env_var("CARGO_CFG_TARGET_OS").unwrap(),
std::io::stdout(),
)
_add_extension_module_link_args(&impl_::target_triple_from_env(), std::io::stdout())
}
fn _add_extension_module_link_args(target_os: &str, mut writer: impl std::io::Write) {
if target_os == "macos" {
fn _add_extension_module_link_args(triple: &Triple, mut writer: impl std::io::Write) {
if triple.operating_system == OperatingSystem::Darwin {
writeln!(writer, "cargo:rustc-cdylib-link-arg=-undefined").unwrap();
writeln!(writer, "cargo:rustc-cdylib-link-arg=dynamic_lookup").unwrap();
} else if triple == &Triple::from_str("wasm32-unknown-emscripten").unwrap() {
writeln!(writer, "cargo:rustc-cdylib-link-arg=-sSIDE_MODULE=2").unwrap();
writeln!(writer, "cargo:rustc-cdylib-link-arg=-sWASM_BIGINT").unwrap();
}
}
@ -220,14 +225,31 @@ mod tests {
let mut buf = Vec::new();
// Does nothing on non-mac
_add_extension_module_link_args("windows", &mut buf);
_add_extension_module_link_args(
&Triple::from_str("x86_64-pc-windows-msvc").unwrap(),
&mut buf,
);
assert_eq!(buf, Vec::new());
_add_extension_module_link_args("macos", &mut buf);
_add_extension_module_link_args(
&Triple::from_str("x86_64-apple-darwin").unwrap(),
&mut buf,
);
assert_eq!(
std::str::from_utf8(&buf).unwrap(),
"cargo:rustc-cdylib-link-arg=-undefined\n\
cargo:rustc-cdylib-link-arg=dynamic_lookup\n"
);
buf.clear();
_add_extension_module_link_args(
&Triple::from_str("wasm32-unknown-emscripten").unwrap(),
&mut buf,
);
assert_eq!(
std::str::from_utf8(&buf).unwrap(),
"cargo:rustc-cdylib-link-arg=-sSIDE_MODULE=2\n\
cargo:rustc-cdylib-link-arg=-sWASM_BIGINT\n"
);
}
}