Skip to content

Commit 4c69907

Browse files
authored
Make rustworkx build and run with pyiodide (#1447)
* Pyodide: force only one thread * Add .cargo/config.toml * Unstable feature that makes rayon work * Newline * Document pyodide * Add release note for pyodide * Fix separator * Consolidate the bare minimum of flags * Add emscripten-wasm-eh flag * Solve nit * Remove flags forcing emscripten builds from requiring nightly
1 parent f52e9b8 commit 4c69907

File tree

5 files changed

+44
-1
lines changed

5 files changed

+44
-1
lines changed

.cargo/config.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,6 @@ rustflags = [
99
"-C", "link-arg=-undefined",
1010
"-C", "link-arg=dynamic_lookup",
1111
]
12+
13+
[unstable]
14+
build-std = ["std", "panic_abort"]

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,5 +75,5 @@ default-features = false
7575
features = ["multi_thread"]
7676

7777
[profile.release]
78-
lto = 'fat'
78+
lto = "fat"
7979
codegen-units = 1

docs/source/install.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ source.
113113
- i686 or x86_64
114114
- :ref:`tier-4`
115115
-
116+
* - Pyodide
117+
- WASM (Emscripten)
118+
- :ref:`tier-experimental`
119+
-
116120

117121

118122
.. _manylinux 2014: https://peps.python.org/pep-0599/>
@@ -166,6 +170,18 @@ functioning Python environment and may require a C/C++ compiler or additional
166170
programs to build dependencies from source as part of the installation process.
167171
Support for these platforms are best effort only.
168172

173+
.. _tier-experimental:
174+
175+
Tier Experimental
176+
-----------------
177+
178+
Tier Experimental platforms are not tested upstream as part of the development process.
179+
Pre-compiled binaries are built by the external community in separate repositories. Not all of rustworkx might compile for
180+
platforms of this tier and features can be removed. Often, platforms in this tier use unstable features
181+
from the Rust compiler and might break at any time. Support for these platforms are best effort only.
182+
183+
Currently, the only platform in this tier is Pyodide, which is a port of Python that can run in the browser and on Node.js.
184+
169185
Using rustworkx
170186
===============
171187

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
features:
3+
- |
4+
`rustworkx` now has experimental support for `Pyodide <https://pyodide.org/en/stable/>`__.
5+
Pyodide is a Python distribution for WebAssembly that runs in the browser.
6+
This is the first release that compiles with Pyodide and will allow users to run `rustworkx`
7+
in web applications. Because Pyodide wheels are not available in PyPI,
8+
we are working with the Pyodide team to publish them in the Pyodide package index.

src/lib.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,5 +710,21 @@ fn rustworkx(py: Python<'_>, m: &Bound<PyModule>) -> PyResult<()> {
710710
m.add_class::<Type>()?;
711711
m.add_class::<KeySpec>()?;
712712
m.add_wrapped(wrap_pymodule!(generators::generators))?;
713+
#[cfg(target_os = "emscripten")]
714+
setup_rayon_for_pyodide();
713715
Ok(())
714716
}
717+
718+
#[cfg(target_os = "emscripten")]
719+
static PYODIDE_INIT: std::sync::Once = std::sync::Once::new();
720+
721+
#[cfg(target_os = "emscripten")]
722+
pub fn setup_rayon_for_pyodide() {
723+
PYODIDE_INIT.call_once(|| {
724+
rayon::ThreadPoolBuilder::new()
725+
.num_threads(1)
726+
.use_current_thread()
727+
.build_global()
728+
.expect("failing setting up threads for pyodide");
729+
});
730+
}

0 commit comments

Comments
 (0)