Skip to content

Commit 5b80f19

Browse files
authored
Unrolled build for #156062
Rollup merge of #156062 - arjunr2:wali-support-clargs, r=nia-e Added command-line argument support for `wasm32-wali-linux-musl`
2 parents e95e732 + 9ef7541 commit 5b80f19

3 files changed

Lines changed: 62 additions & 3 deletions

File tree

library/std/src/os/linux/raw.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ pub use self::arch::{blkcnt_t, blksize_t, ino_t, nlink_t, off_t, stat, time_t};
3131
target_arch = "powerpc",
3232
target_arch = "sparc",
3333
target_arch = "arm",
34-
target_arch = "wasm32"
3534
))]
3635
mod arch {
3736
use crate::os::raw::{c_long, c_short, c_uint};
@@ -311,7 +310,9 @@ mod arch {
311310
}
312311
}
313312

314-
#[cfg(any(target_arch = "x86_64", target_arch = "powerpc64"))]
313+
#[cfg(any(target_arch = "x86_64", target_arch = "powerpc64",
314+
// `wasm32-wali-linux-musl` uses ABI similar to x86_64
315+
target_arch = "wasm32"))]
315316
mod arch {
316317
use crate::os::raw::{c_int, c_long};
317318

library/std/src/sys/args/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ mod common;
1515

1616
cfg_select! {
1717
any(
18-
all(target_family = "unix", not(any(target_os = "espidf", target_os = "vita"))),
18+
all(target_family = "unix", not(any(all(target_family = "wasm", target_os = "linux"), target_os = "espidf", target_os = "vita"))),
1919
target_os = "hermit",
2020
) => {
2121
mod unix;
@@ -45,6 +45,10 @@ cfg_select! {
4545
mod wasi;
4646
pub use wasi::*;
4747
}
48+
all(target_family = "wasm", target_os = "linux") => {
49+
mod wali;
50+
pub use wali::*;
51+
}
4852
target_os = "xous" => {
4953
mod xous;
5054
pub use xous::*;

library/std/src/sys/args/wali.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
pub use super::common::Args;
2+
3+
/// One-time global initialization.
4+
pub unsafe fn init(argc: isize, argv: *const *const u8) {
5+
unsafe { imp::init(argc, argv) }
6+
}
7+
8+
/// Returns the command line arguments
9+
pub fn args() -> Args {
10+
imp::args()
11+
}
12+
13+
mod imp {
14+
use super::Args;
15+
use crate::ffi::{CString, OsString};
16+
use crate::os::raw::{c_uint as WaliArgIdx, c_uint};
17+
use crate::os::unix::prelude::*;
18+
use crate::sync::OnceLock;
19+
20+
#[link(wasm_import_module = "wali")]
21+
unsafe extern "C" {
22+
pub fn __cl_get_argc() -> WaliArgIdx;
23+
pub fn __cl_get_argv_len(offset: WaliArgIdx) -> c_uint;
24+
pub fn __cl_copy_argv(buf: *mut i8, offset: WaliArgIdx) -> c_uint;
25+
}
26+
27+
static ARGS: OnceLock<Vec<OsString>> = OnceLock::new();
28+
29+
pub unsafe fn init(_argc: isize, _argv: *const *const u8) {
30+
// Uses the WALI arguments API
31+
ARGS.set(argc_argv()).ok();
32+
}
33+
34+
unsafe fn load_arg(idx: c_uint) -> OsString {
35+
let arg_len = unsafe { __cl_get_argv_len(idx) };
36+
let arg_buf = CString::new(vec![b'x'; arg_len as usize]).unwrap();
37+
let ptr = arg_buf.into_raw();
38+
let arg_buf = unsafe {
39+
__cl_copy_argv(ptr, idx);
40+
CString::from_raw(ptr)
41+
};
42+
OsStringExt::from_vec(arg_buf.into_bytes())
43+
}
44+
45+
fn argc_argv() -> Vec<OsString> {
46+
let argc = unsafe { __cl_get_argc() };
47+
(0..argc).map(|x| unsafe { load_arg(x) }).collect()
48+
}
49+
50+
pub fn args() -> Args {
51+
let cached = ARGS.get().cloned().unwrap_or_default();
52+
Args::new(cached)
53+
}
54+
}

0 commit comments

Comments
 (0)