mirror of
https://github.com/fish-shell/fish-shell
synced 2024-11-14 17:07:44 +00:00
971d257e67
The translation is fairly direct though it adds some duplication, for example there are multiple "match" statements that mimic function overloading. Rust has no overloading, and we cannot have generic methods in the Node trait (due to a Rust limitation, the error is like "cannot be made into an object") so we include the type name in method names. Give clients like "indent_visitor_t" a Rust companion ("IndentVisitor") that takes care of the AST traversal while the AST consumption remains in C++ for now. In future, "IndentVisitor" should absorb the entirety of "indent_visitor_t". This pattern requires that "fish_indent" be exposed includable header to the CXX bridge. Alternatively, we could define FFI wrappers for recursive AST traversal. Rust requires we separate the AST visitors for "mut" and "const" scenarios. Take this opportunity to concretize both visitors: The only client that requires mutable access is the populator. To match the structure of the C++ populator which makes heavy use of function overloading, we need to add a bunch of functions to the trait. Since there is no other mutable visit, this seems acceptable. The "const" visitors never use "will_visit_fields_of()" or "did_visit_fields_of()", so remove them (though this is debatable). Like in the C++ implementation, the AST nodes themselves are largely defined via macros. Union fields like "Statement" and "ArgumentOrRedirection" do currently not use macros but may in future. This commit also introduces a precedent for a type that is defined in one CXX bridge and used in another one - "ParseErrorList". To make this work we need to manually define "ExternType". There is one annoyance with CXX: functions that take explicit lifetime parameters require to be marked as unsafe. This makes little sense because functions that return `&Foo` with implicit lifetime can be misused the same way on the C++ side. One notable change is that we cannot directly port "find_block_open_keyword()" (which is used to compute an error) because it relies on the stack of visited nodes. We cannot modify a stack of node references while we do the "mut" walk. Happily, an idiomatic solution is easy: we can tell the AST visitor to backtrack to the parent node and create the error there. Since "node_t::accept_base" is no longer a template we don't need the "node_visitation_t" trampoline anymore. The added copying at the FFI boundary makes things slower (memcpy dominates the profile) but it's not unusable, which is good news: $ hyperfine ./fish.{old,new}" -c 'source ../share/completions/git.fish'" Benchmark 1: ./fish.old -c 'source ../share/completions/git.fish' Time (mean ± σ): 195.5 ms ± 2.9 ms [User: 190.1 ms, System: 4.4 ms] Range (min … max): 193.2 ms … 205.1 ms 15 runs Benchmark 2: ./fish.new -c 'source ../share/completions/git.fish' Time (mean ± σ): 677.5 ms ± 62.0 ms [User: 665.4 ms, System: 10.0 ms] Range (min … max): 611.7 ms … 805.5 ms 10 runs Summary './fish.old -c 'source ../share/completions/git.fish'' ran 3.47 ± 0.32 times faster than './fish.new -c 'source ../share/completions/git.fish'' Leftovers: - Enum variants are still snakecase; I didn't get around to changing this yet. - "ast_type_to_string()" still returns a snakecase name. This could be changed since it's not user visible.
123 lines
4.7 KiB
Rust
123 lines
4.7 KiB
Rust
use miette::miette;
|
|
|
|
fn main() -> miette::Result<()> {
|
|
cc::Build::new().file("src/compat.c").compile("libcompat.a");
|
|
|
|
let rust_dir = std::env::var("CARGO_MANIFEST_DIR").expect("Env var CARGO_MANIFEST_DIR missing");
|
|
let target_dir =
|
|
std::env::var("FISH_RUST_TARGET_DIR").unwrap_or(format!("{}/{}", rust_dir, "target/"));
|
|
let fish_src_dir = format!("{}/{}", rust_dir, "../src/");
|
|
|
|
// Where cxx emits its header.
|
|
let cxx_include_dir = format!("{}/{}", target_dir, "cxxbridge/rust/");
|
|
|
|
// If FISH_BUILD_DIR is given by CMake, then use it; otherwise assume it's at ../build.
|
|
let fish_build_dir =
|
|
std::env::var("FISH_BUILD_DIR").unwrap_or(format!("{}/{}", rust_dir, "../build/"));
|
|
|
|
// Where autocxx should put its stuff.
|
|
let autocxx_gen_dir = std::env::var("FISH_AUTOCXX_GEN_DIR")
|
|
.unwrap_or(format!("{}/{}", fish_build_dir, "fish-autocxx-gen/"));
|
|
|
|
detect_features();
|
|
|
|
// Emit cxx junk.
|
|
// This allows "Rust to be used from C++"
|
|
// This must come before autocxx so that cxx can emit its cxx.h header.
|
|
let source_files = vec![
|
|
"src/abbrs.rs",
|
|
"src/ast.rs",
|
|
"src/event.rs",
|
|
"src/common.rs",
|
|
"src/fd_monitor.rs",
|
|
"src/fd_readable_set.rs",
|
|
"src/fds.rs",
|
|
"src/ffi_init.rs",
|
|
"src/ffi_tests.rs",
|
|
"src/fish_indent.rs",
|
|
"src/future_feature_flags.rs",
|
|
"src/highlight.rs",
|
|
"src/job_group.rs",
|
|
"src/parse_constants.rs",
|
|
"src/parse_tree.rs",
|
|
"src/parse_util.rs",
|
|
"src/redirection.rs",
|
|
"src/smoke.rs",
|
|
"src/termsize.rs",
|
|
"src/timer.rs",
|
|
"src/tokenizer.rs",
|
|
"src/topic_monitor.rs",
|
|
"src/trace.rs",
|
|
"src/util.rs",
|
|
"src/wait_handle.rs",
|
|
"src/builtins/shared.rs",
|
|
];
|
|
cxx_build::bridges(&source_files)
|
|
.flag_if_supported("-std=c++11")
|
|
.include(&fish_src_dir)
|
|
.include(&fish_build_dir) // For config.h
|
|
.include(&cxx_include_dir) // For cxx.h
|
|
.flag("-Wno-comment")
|
|
.compile("fish-rust");
|
|
|
|
// Emit autocxx junk.
|
|
// This allows "C++ to be used from Rust."
|
|
let include_paths = [&fish_src_dir, &fish_build_dir, &cxx_include_dir];
|
|
let mut builder = autocxx_build::Builder::new("src/ffi.rs", include_paths);
|
|
// Use autocxx's custom output directory unless we're being called by `rust-analyzer` and co.,
|
|
// in which case stick to the default target directory so code intelligence continues to work.
|
|
if std::env::var("RUSTC_WRAPPER").map_or(true, |wrapper| {
|
|
!(wrapper.contains("rust-analyzer") || wrapper.contains("intellij-rust-native-helper"))
|
|
}) {
|
|
// We need this reassignment because of how the builder pattern works
|
|
builder = builder.custom_gendir(autocxx_gen_dir.into());
|
|
}
|
|
let mut b = builder.build()?;
|
|
b.flag_if_supported("-std=c++11")
|
|
.flag("-Wno-comment")
|
|
.compile("fish-rust-autocxx");
|
|
for file in source_files {
|
|
println!("cargo:rerun-if-changed={file}");
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
/// Dynamically enables certain features at build-time, without their having to be explicitly
|
|
/// enabled in the `cargo build --features xxx` invocation.
|
|
///
|
|
/// This can be used to enable features that we check for and conditionally compile according to in
|
|
/// our own codebase, but [can't be used to pull in dependencies](0) even if they're gated (in
|
|
/// `Cargo.toml`) behind a feature we just enabled.
|
|
///
|
|
/// [0]: https://github.com/rust-lang/cargo/issues/5499
|
|
fn detect_features() {
|
|
for (feature, detector) in [
|
|
// Ignore the first line, it just sets up the type inference. Model new entries after the
|
|
// second line.
|
|
("", &(|| Ok(false)) as &dyn Fn() -> miette::Result<bool>),
|
|
("bsd", &detect_bsd),
|
|
] {
|
|
match detector() {
|
|
Err(e) => eprintln!("{feature} detect: {e}"),
|
|
Ok(true) => println!("cargo:rustc-cfg=feature=\"{feature}\""),
|
|
Ok(false) => (),
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Detect if we're being compiled on a BSD-derived OS. Does not yet play nicely with
|
|
/// cross-compilation.
|
|
///
|
|
/// Rust offers fine-grained conditional compilation per-os for the popular operating systems, but
|
|
/// doesn't necessarily include less-popular forks nor does it group them into families more
|
|
/// specific than "windows" vs "unix" so we can conditionally compile code for BSD systems.
|
|
fn detect_bsd() -> miette::Result<bool> {
|
|
let uname = std::process::Command::new("uname")
|
|
.output()
|
|
.map_err(|_| miette!("Error executing uname!"))?;
|
|
Ok(std::str::from_utf8(&uname.stdout)
|
|
.map(|s| s.to_ascii_lowercase())
|
|
.map(|s| s.contains("bsd"))
|
|
.unwrap_or(false))
|
|
}
|