mirror of
https://github.com/cargo-bins/cargo-binstall.git
synced 2025-04-24 22:30:03 +00:00
Split crates and clean up structure of codebase (#294)
Co-authored-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
parent
bf700f9012
commit
4b00f5f143
88 changed files with 2989 additions and 1423 deletions
13
crates/detect-wasi/src/bin/detect-wasi.rs
Normal file
13
crates/detect-wasi/src/bin/detect-wasi.rs
Normal file
|
@ -0,0 +1,13 @@
|
|||
use std::process::exit;
|
||||
|
||||
use detect_wasi::detect_wasi_runability;
|
||||
|
||||
fn main() {
|
||||
if detect_wasi_runability().unwrap() {
|
||||
println!("WASI is runnable!");
|
||||
exit(0);
|
||||
} else {
|
||||
println!("WASI is not runnable");
|
||||
exit(1);
|
||||
}
|
||||
}
|
42
crates/detect-wasi/src/lib.rs
Normal file
42
crates/detect-wasi/src/lib.rs
Normal file
|
@ -0,0 +1,42 @@
|
|||
use std::{
|
||||
fs::File,
|
||||
io::{Result, Write},
|
||||
process::Command,
|
||||
};
|
||||
#[cfg(unix)]
|
||||
use std::{fs::Permissions, os::unix::fs::PermissionsExt};
|
||||
|
||||
use tempfile::tempdir;
|
||||
|
||||
const WASI_PROGRAM: &[u8] = include_bytes!("miniwasi.wasm");
|
||||
|
||||
/// Detect the ability to run WASI
|
||||
///
|
||||
/// This attempts to run a small embedded WASI program, and returns true if no errors happened.
|
||||
/// Errors returned by the `Result` are I/O errors from the establishment of the context, not
|
||||
/// errors from the run attempt.
|
||||
///
|
||||
/// On Linux, you can configure your system to run WASI programs using a binfmt directive. Under
|
||||
/// systemd, write the below to `/etc/binfmt.d/wasi.conf`, with `/usr/bin/wasmtime` optionally
|
||||
/// replaced with the path to your WASI runtime of choice:
|
||||
///
|
||||
/// ```plain
|
||||
/// :wasi:M::\x00asm::/usr/bin/wasmtime:
|
||||
/// ```
|
||||
pub fn detect_wasi_runability() -> Result<bool> {
|
||||
let progdir = tempdir()?;
|
||||
let prog = progdir.path().join("miniwasi.wasm");
|
||||
|
||||
{
|
||||
let mut progfile = File::create(&prog)?;
|
||||
progfile.write_all(WASI_PROGRAM)?;
|
||||
|
||||
#[cfg(unix)]
|
||||
progfile.set_permissions(Permissions::from_mode(0o777))?;
|
||||
}
|
||||
|
||||
match Command::new(prog).output() {
|
||||
Ok(out) => Ok(out.status.success() && out.stdout.is_empty() && out.stderr.is_empty()),
|
||||
Err(_) => Ok(false),
|
||||
}
|
||||
}
|
BIN
crates/detect-wasi/src/miniwasi.wasm
Executable file
BIN
crates/detect-wasi/src/miniwasi.wasm
Executable file
Binary file not shown.
10
crates/detect-wasi/src/miniwasi.wast
Normal file
10
crates/detect-wasi/src/miniwasi.wast
Normal file
|
@ -0,0 +1,10 @@
|
|||
(module
|
||||
(import "wasi_snapshot_preview1" "proc_exit" (func $exit (param i32)))
|
||||
(memory $0 0)
|
||||
(export "memory" (memory $0))
|
||||
(export "_start" (func $0))
|
||||
(func $0
|
||||
(call $exit (i32.const 0))
|
||||
(unreachable)
|
||||
)
|
||||
)
|
Loading…
Add table
Add a link
Reference in a new issue