Skip to content

Commit 8bc86c3

Browse files
authored
Merge pull request #2538 from davidhewitt/emscripten-link-args
pyo3-build-config: add link args for wasm32-unknown-emscripten
2 parents 480fe7e + 49246be commit 8bc86c3

File tree

2 files changed

+35
-12
lines changed

2 files changed

+35
-12
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
5252
- `PyCapsule::set_context` no longer takes a `py: Python<'_>` argument.
5353
- `PyCapsule::name` now returns `PyResult<Option<&CStr>>` instead of `&CStr`.
5454
- `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)
55+
- `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)
5556

5657
### Removed
5758

pyo3-build-config/src/lib.rs

+34-12
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use std::{
1616
path::{Path, PathBuf},
1717
};
1818

19-
use std::{env, process::Command};
19+
use std::{env, process::Command, str::FromStr};
2020

2121
#[cfg(feature = "resolve-config")]
2222
use once_cell::sync::OnceCell;
@@ -27,6 +27,7 @@ pub use impl_::{
2727
BuildFlag, BuildFlags, CrossCompileConfig, InterpreterConfig, PythonImplementation,
2828
PythonVersion, Triple,
2929
};
30+
use target_lexicon::OperatingSystem;
3031

3132
/// Adds all the [`#[cfg]` flags](index.html) to the current compilation.
3233
///
@@ -46,23 +47,27 @@ pub fn use_pyo3_cfgs() {
4647
get().emit_pyo3_cfgs();
4748
}
4849

49-
/// Adds linker arguments (for macOS) suitable for PyO3's `extension-module` feature.
50+
/// Adds linker arguments suitable for PyO3's `extension-module` feature.
5051
///
5152
/// This should be called from a build script.
5253
///
53-
/// This is currently a no-op on non-macOS platforms, however may emit additional linker arguments
54-
/// in future if deemed necessarys.
54+
/// The following link flags are added:
55+
/// - macOS: `-undefined dynamic_lookup`
56+
/// - wasm32-unknown-emscripten: `-sSIDE_MODULE=2 -sWASM_BIGINT`
57+
///
58+
/// All other platforms currently are no-ops, however this may change as necessary
59+
/// in future.
5560
pub fn add_extension_module_link_args() {
56-
_add_extension_module_link_args(
57-
&impl_::cargo_env_var("CARGO_CFG_TARGET_OS").unwrap(),
58-
std::io::stdout(),
59-
)
61+
_add_extension_module_link_args(&impl_::target_triple_from_env(), std::io::stdout())
6062
}
6163

62-
fn _add_extension_module_link_args(target_os: &str, mut writer: impl std::io::Write) {
63-
if target_os == "macos" {
64+
fn _add_extension_module_link_args(triple: &Triple, mut writer: impl std::io::Write) {
65+
if triple.operating_system == OperatingSystem::Darwin {
6466
writeln!(writer, "cargo:rustc-cdylib-link-arg=-undefined").unwrap();
6567
writeln!(writer, "cargo:rustc-cdylib-link-arg=dynamic_lookup").unwrap();
68+
} else if triple == &Triple::from_str("wasm32-unknown-emscripten").unwrap() {
69+
writeln!(writer, "cargo:rustc-cdylib-link-arg=-sSIDE_MODULE=2").unwrap();
70+
writeln!(writer, "cargo:rustc-cdylib-link-arg=-sWASM_BIGINT").unwrap();
6671
}
6772
}
6873

@@ -220,14 +225,31 @@ mod tests {
220225
let mut buf = Vec::new();
221226

222227
// Does nothing on non-mac
223-
_add_extension_module_link_args("windows", &mut buf);
228+
_add_extension_module_link_args(
229+
&Triple::from_str("x86_64-pc-windows-msvc").unwrap(),
230+
&mut buf,
231+
);
224232
assert_eq!(buf, Vec::new());
225233

226-
_add_extension_module_link_args("macos", &mut buf);
234+
_add_extension_module_link_args(
235+
&Triple::from_str("x86_64-apple-darwin").unwrap(),
236+
&mut buf,
237+
);
227238
assert_eq!(
228239
std::str::from_utf8(&buf).unwrap(),
229240
"cargo:rustc-cdylib-link-arg=-undefined\n\
230241
cargo:rustc-cdylib-link-arg=dynamic_lookup\n"
231242
);
243+
244+
buf.clear();
245+
_add_extension_module_link_args(
246+
&Triple::from_str("wasm32-unknown-emscripten").unwrap(),
247+
&mut buf,
248+
);
249+
assert_eq!(
250+
std::str::from_utf8(&buf).unwrap(),
251+
"cargo:rustc-cdylib-link-arg=-sSIDE_MODULE=2\n\
252+
cargo:rustc-cdylib-link-arg=-sWASM_BIGINT\n"
253+
);
232254
}
233255
}

0 commit comments

Comments
 (0)