Skip to content

Commit 9346fd0

Browse files
Add capi support to rustpython executable
1 parent ce47be6 commit 9346fd0

7 files changed

Lines changed: 62 additions & 26 deletions

File tree

‎Cargo.lock‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ repository.workspace = true
1010
license.workspace = true
1111

1212
[features]
13+
capi = ["dep:rustpython-capi", "threading"]
1314
default = ["threading", "stdlib", "stdio", "importlib", "ssl-rustls", "host_env"]
1415
host_env = ["rustpython-vm/host_env", "rustpython-stdlib?/host_env"]
1516
importlib = ["rustpython-vm/importlib"]
@@ -31,6 +32,7 @@ tkinter = ["rustpython-stdlib/tkinter"]
3132
winresource = "0.1"
3233

3334
[dependencies]
35+
rustpython-capi = { workspace = true, optional = true }
3436
rustpython-compiler = { workspace = true }
3537
rustpython-pylib = { workspace = true, optional = true }
3638
rustpython-stdlib = { workspace = true, optional = true, features = ["compiler"] }
@@ -141,6 +143,7 @@ repository = "https://github.com/RustPython/RustPython"
141143
license = "MIT"
142144

143145
[workspace.dependencies]
146+
rustpython-capi = { path = "crates/capi", version = "0.5.0" }
144147
rustpython-compiler-core = { path = "crates/compiler-core", version = "0.5.0" }
145148
rustpython-compiler = { path = "crates/compiler", version = "0.5.0" }
146149
rustpython-codegen = { path = "crates/codegen", version = "0.5.0" }

‎build.rs‎

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,29 @@
11
fn main() {
2-
if std::env::var("CARGO_CFG_TARGET_OS").unwrap() == "windows" {
3-
println!("cargo:rerun-if-changed=logo.ico");
4-
let mut res = winresource::WindowsResource::new();
5-
if std::path::Path::new("logo.ico").exists() {
6-
res.set_icon("logo.ico");
7-
} else {
8-
println!("cargo:warning=logo.ico not found, skipping icon embedding");
9-
return;
2+
let target = std::env::var("CARGO_CFG_TARGET_OS").unwrap();
3+
let capi_enabled = std::env::var_os("CARGO_FEATURE_CAPI").is_some();
4+
5+
match target.as_str() {
6+
"linux" if capi_enabled => {
7+
println!("cargo:rustc-link-arg-bin=rustpython=-Wl,--export-dynamic");
108
}
11-
res.compile()
12-
.map_err(|e| {
13-
println!("cargo:warning=Failed to compile Windows resources: {e}");
14-
})
15-
.ok();
9+
"macos" if capi_enabled => {
10+
println!("cargo:rustc-link-arg-bin=rustpython=-Wl,-export_dynamic");
11+
}
12+
"windows" => {
13+
println!("cargo:rerun-if-changed=logo.ico");
14+
let mut res = winresource::WindowsResource::new();
15+
if std::path::Path::new("logo.ico").exists() {
16+
res.set_icon("logo.ico");
17+
} else {
18+
println!("cargo:warning=logo.ico not found, skipping icon embedding");
19+
return;
20+
}
21+
res.compile()
22+
.map_err(|e| {
23+
println!("cargo:warning=Failed to compile Windows resources: {e}");
24+
})
25+
.ok();
26+
}
27+
_ => {}
1628
}
1729
}

‎crates/capi/Cargo.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ repository.workspace = true
99
license.workspace = true
1010

1111
[lib]
12-
crate-type = ["cdylib"]
12+
crate-type = ["cdylib", "rlib"]
1313

1414
[dependencies]
1515
rustpython-vm = { workspace = true, features = ["threading"] }

‎crates/capi/src/lib.rs‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11
#![allow(clippy::missing_safety_doc)]
2+
3+
use crate::pylifecycle::MAIN_INTERP;
4+
use rustpython_vm::Interpreter;
25
pub use rustpython_vm::PyObject;
6+
use std::sync::MutexGuard;
37

48
extern crate alloc;
59

610
pub mod pylifecycle;
711
pub mod pystate;
812
pub mod refcount;
13+
14+
/// Get main interpreter of this process. Will be None of it has not been initialized yet.
15+
pub fn get_main_interpreter() -> MutexGuard<'static, Option<Interpreter>> {
16+
MAIN_INTERP
17+
.lock()
18+
.expect("Failed to lock interpreter mutex")
19+
}

‎crates/capi/src/pylifecycle.rs‎

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
1+
use crate::get_main_interpreter;
12
use crate::pystate::attach_vm_to_thread;
23
use core::ffi::c_int;
34
use rustpython_vm::Interpreter;
45
use rustpython_vm::vm::thread::ThreadedVirtualMachine;
5-
use std::sync::{Mutex, OnceLock};
6+
use std::sync::Mutex;
67

7-
static MAIN_INTERP: OnceLock<Mutex<Interpreter>> = OnceLock::new();
8+
pub(crate) static MAIN_INTERP: Mutex<Option<Interpreter>> = Mutex::new(None);
89

910
/// Request a thread local vm from the main interpreter
1011
pub(crate) fn request_vm_from_interpreter() -> ThreadedVirtualMachine {
11-
MAIN_INTERP
12-
.get()
13-
.expect("Interpreter is not initialized")
14-
.lock()
15-
.expect("Failed to lock interpreter mutex")
12+
get_main_interpreter()
13+
.as_ref()
14+
.expect("Interpreter not initialized")
1615
.enter(|vm| vm.new_thread())
1716
}
1817

1918
#[unsafe(no_mangle)]
2019
pub extern "C" fn Py_IsInitialized() -> c_int {
21-
MAIN_INTERP.get().is_some() as c_int
20+
get_main_interpreter().is_none() as c_int
2221
}
2322

2423
#[unsafe(no_mangle)]
@@ -28,9 +27,11 @@ pub extern "C" fn Py_Initialize() {
2827

2928
#[unsafe(no_mangle)]
3029
pub extern "C" fn Py_InitializeEx(_initsigs: c_int) {
31-
MAIN_INTERP.get_or_init(|| Interpreter::with_init(Default::default(), |_vm| {}).into());
32-
33-
attach_vm_to_thread();
30+
let mut interp = get_main_interpreter();
31+
if interp.is_none() {
32+
*interp = Interpreter::with_init(Default::default(), |_vm| {}).into();
33+
attach_vm_to_thread();
34+
}
3435
}
3536

3637
#[unsafe(no_mangle)]

‎src/lib.rs‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,15 @@ pub fn run(mut builder: InterpreterBuilder) -> ExitCode {
118118
builder = builder.settings(settings);
119119

120120
let interp = builder.interpreter();
121-
let exitcode = interp.run(move |vm| run_rustpython(vm, run_mode));
121+
let exitcode = cfg_select! {
122+
feature = "capi" => {{
123+
let local_vm = interp.enter(|vm| vm.new_thread());
124+
*(rustpython_capi::get_main_interpreter()) = Some(interp);
125+
let result = local_vm.run(|vm| run_rustpython(vm, run_mode));
126+
rustpython_capi::get_main_interpreter().take().unwrap().finalize(result.err())
127+
}},
128+
_ => interp.run(move |vm| run_rustpython(vm, run_mode)),
129+
};
122130

123131
rustpython_vm::host_env::os::exit_code(exitcode)
124132
}

0 commit comments

Comments
 (0)