-
Notifications
You must be signed in to change notification settings - Fork 462
Support wasm-wasip1 target #927
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| [target.wasm32-wasip1] | ||
| rustflags = [ | ||
| "-L/opt/wasi-sdk/share/wasi-sysroot/lib/wasm32-wasi", | ||
| "-Clink-arg=-lc++", | ||
| "-Clink-arg=-lc++abi" | ||
| ] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -78,6 +78,7 @@ fn read_file_arg(file_arg: &FileArgument) -> std::io::Result<Vec<u8>> { | |
| } | ||
|
|
||
| /// Write a human-friendly description of `e` to stderr. | ||
| #[cfg(not(target_arch = "wasm32"))] | ||
| fn eprint_read_error(file_arg: &FileArgument, e: &std::io::Error) { | ||
| match e.kind() { | ||
| std::io::ErrorKind::NotFound => { | ||
|
|
@@ -95,12 +96,26 @@ fn eprint_read_error(file_arg: &FileArgument, e: &std::io::Error) { | |
| }; | ||
| } | ||
|
|
||
| #[cfg(target_arch = "wasm32")] | ||
| fn eprint_read_error(file_arg: &FileArgument, e: &std::io::Error) { | ||
| // For the browser/WASM demo, fail softly so we can keep running. | ||
| println!("WASM read error on {} ({:?})", file_arg, e.kind()); | ||
| } | ||
|
|
||
| pub(crate) fn read_or_die(path: &Path) -> Vec<u8> { | ||
| match fs::read(path) { | ||
| Ok(src) => src, | ||
| Err(e) => { | ||
| eprint_read_error(&FileArgument::NamedPath(path.to_path_buf()), &e); | ||
| std::process::exit(EXIT_BAD_ARGUMENTS); | ||
| #[cfg(target_arch = "wasm32")] | ||
| { | ||
| // Return empty content in WASM to avoid aborting the demo. | ||
| return Vec::new(); | ||
|
Comment on lines
+112
to
+113
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is fine for a demo but IMO failing silently is not acceptable |
||
| } | ||
| #[cfg(not(target_arch = "wasm32"))] | ||
| { | ||
| std::process::exit(EXIT_BAD_ARGUMENTS); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,7 +8,6 @@ use std::{ | |
| }; | ||
|
|
||
| use clap::{crate_authors, crate_description, value_parser, Arg, ArgAction, Command}; | ||
| use crossterm::tty::IsTty; | ||
| use owo_colors::OwoColorize as _; | ||
|
|
||
| use crate::{ | ||
|
|
@@ -51,6 +50,30 @@ pub(crate) struct DisplayOptions { | |
|
|
||
| pub(crate) const DEFAULT_TERMINAL_WIDTH: usize = 80; | ||
|
|
||
| #[cfg(not(target_arch = "wasm32"))] | ||
| fn stdout_is_tty() -> bool { | ||
| use crossterm::tty::IsTty; | ||
| std::io::stdout().is_tty() | ||
| } | ||
|
|
||
| #[cfg(target_arch = "wasm32")] | ||
| fn stdout_is_tty() -> bool { | ||
| false | ||
| } | ||
|
Comment on lines
+59
to
+62
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's a use case for running WASI programs in a terminal, there's no way standard way to detect it though so it's a better default, it would be nice to have a way to force colors/links with flags or environment variables |
||
|
|
||
| #[cfg(not(target_arch = "wasm32"))] | ||
| fn terminal_columns() -> Option<usize> { | ||
| crossterm::terminal::size() | ||
| .ok() | ||
| .map(|(cols, _rows)| cols as usize) | ||
| .filter(|cols| *cols > 0) | ||
| } | ||
|
|
||
| #[cfg(target_arch = "wasm32")] | ||
| fn terminal_columns() -> Option<usize> { | ||
| None | ||
| } | ||
|
|
||
| impl Default for DisplayOptions { | ||
| fn default() -> Self { | ||
| Self { | ||
|
|
@@ -125,7 +148,7 @@ fn app() -> clap::Command { | |
| )); | ||
|
|
||
| after_help.push_str("\n\nSee the full manual at "); | ||
| if std::io::stdout().is_tty() { | ||
| if stdout_is_tty() { | ||
| // Make the link to the manual clickable in terminals that | ||
| // support OSC 8, the ANSI escape code for hyperlinks. | ||
| // | ||
|
|
@@ -596,6 +619,7 @@ fn common_path_suffix(lhs_path: &Path, rhs_path: &Path) -> Option<String> { | |
| } | ||
|
|
||
| /// Does `path` look like "/tmp/git-blob-abcdef/modified_field.txt"? | ||
| #[cfg(not(target_arch = "wasm32"))] | ||
| fn is_git_tmpfile(path: &Path) -> bool { | ||
| let Ok(rel_path) = path.strip_prefix(std::env::temp_dir()) else { | ||
| return false; | ||
|
|
@@ -612,6 +636,12 @@ fn is_git_tmpfile(path: &Path) -> bool { | |
| .starts_with("git-blob-") | ||
| } | ||
|
|
||
| #[cfg(target_arch = "wasm32")] | ||
| fn is_git_tmpfile(_path: &Path) -> bool { | ||
| // WASI environments may not have a writable /tmp; skip special-casing. | ||
| false | ||
| } | ||
|
|
||
| fn build_display_path(lhs_path: &FileArgument, rhs_path: &FileArgument) -> String { | ||
| match (lhs_path, rhs_path) { | ||
| (FileArgument::NamedPath(lhs), FileArgument::NamedPath(rhs)) => { | ||
|
|
@@ -1002,10 +1032,8 @@ pub(crate) fn parse_args() -> Mode { | |
| /// Try to work out the width of the terminal we're on, or fall back | ||
| /// to a sensible default value. | ||
| fn detect_terminal_width() -> usize { | ||
| if let Ok((columns, _rows)) = crossterm::terminal::size() { | ||
| if columns > 0 { | ||
| return columns.into(); | ||
| } | ||
| if let Some(columns) = terminal_columns() { | ||
| return columns; | ||
| } | ||
|
|
||
| // If crossterm couldn't detect the terminal width, use the | ||
|
|
@@ -1036,7 +1064,7 @@ pub(crate) fn should_use_color(color_output: ColorOutput) -> bool { | |
| fn detect_color_support() -> bool { | ||
| // TODO: consider following the env parsing logic in git_config_bool | ||
| // in config.c. | ||
| std::io::stdout().is_tty() || env::var("GIT_PAGER_IN_USE").is_ok() | ||
| stdout_is_tty() || env::var("GIT_PAGER_IN_USE").is_ok() | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.