2023-03-20 06:50:32 +08:00
|
|
|
use miette::miette;
|
|
|
|
|
2023-01-15 06:56:24 +08:00
|
|
|
fn main() -> miette::Result<()> {
|
2023-03-26 23:23:05 +08:00
|
|
|
cc::Build::new().file("src/compat.c").compile("libcompat.a");
|
|
|
|
|
2023-01-15 06:56:24 +08:00
|
|
|
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/"));
|
|
|
|
|
2023-03-20 06:50:32 +08:00
|
|
|
detect_features();
|
|
|
|
|
2023-01-15 06:56:24 +08:00
|
|
|
// 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![
|
2023-02-25 00:00:05 +08:00
|
|
|
"src/abbrs.rs",
|
2023-02-12 04:31:08 +08:00
|
|
|
"src/event.rs",
|
2023-03-26 23:23:05 +08:00
|
|
|
"src/common.rs",
|
2023-02-18 09:21:44 +08:00
|
|
|
"src/fd_monitor.rs",
|
2023-01-15 06:56:24 +08:00
|
|
|
"src/fd_readable_set.rs",
|
2023-02-18 09:21:44 +08:00
|
|
|
"src/fds.rs",
|
2023-01-15 06:56:24 +08:00
|
|
|
"src/ffi_init.rs",
|
2023-01-16 06:56:04 +08:00
|
|
|
"src/ffi_tests.rs",
|
2023-02-03 23:34:29 +08:00
|
|
|
"src/future_feature_flags.rs",
|
2023-02-26 06:42:45 +08:00
|
|
|
"src/job_group.rs",
|
2023-02-05 16:35:06 +08:00
|
|
|
"src/parse_constants.rs",
|
2023-02-04 18:21:42 +08:00
|
|
|
"src/redirection.rs",
|
2023-01-15 06:56:24 +08:00
|
|
|
"src/smoke.rs",
|
2023-03-20 06:50:33 +08:00
|
|
|
"src/termsize.rs",
|
2023-02-15 05:54:18 +08:00
|
|
|
"src/timer.rs",
|
2023-02-05 16:35:06 +08:00
|
|
|
"src/tokenizer.rs",
|
2023-01-15 06:56:24 +08:00
|
|
|
"src/topic_monitor.rs",
|
2023-03-28 23:59:51 +08:00
|
|
|
"src/trace.rs",
|
2023-01-31 04:23:01 +08:00
|
|
|
"src/util.rs",
|
2023-03-14 10:23:31 +08:00
|
|
|
"src/wait_handle.rs",
|
2023-01-16 11:52:08 +08:00
|
|
|
"src/builtins/shared.rs",
|
2023-01-15 06:56:24 +08:00
|
|
|
];
|
2023-02-03 14:56:32 +08:00
|
|
|
cxx_build::bridges(&source_files)
|
2023-01-15 06:56:24 +08:00
|
|
|
.flag_if_supported("-std=c++11")
|
|
|
|
.include(&fish_src_dir)
|
|
|
|
.include(&fish_build_dir) // For config.h
|
|
|
|
.include(&cxx_include_dir) // For cxx.h
|
2023-03-04 03:43:59 +08:00
|
|
|
.flag("-Wno-comment")
|
2023-01-15 06:56:24 +08:00
|
|
|
.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];
|
2023-03-13 06:38:39 +08:00
|
|
|
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()?;
|
2023-01-15 06:56:24 +08:00
|
|
|
b.flag_if_supported("-std=c++11")
|
2023-03-04 03:43:59 +08:00
|
|
|
.flag("-Wno-comment")
|
2023-01-15 06:56:24 +08:00
|
|
|
.compile("fish-rust-autocxx");
|
2023-02-03 14:56:32 +08:00
|
|
|
for file in source_files {
|
|
|
|
println!("cargo:rerun-if-changed={file}");
|
|
|
|
}
|
2023-01-15 06:56:24 +08:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2023-03-20 06:50:32 +08:00
|
|
|
|
|
|
|
/// 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 [
|
2023-03-20 07:11:09 +08:00
|
|
|
// 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),
|
|
|
|
] {
|
2023-03-20 06:50:32 +08:00
|
|
|
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> {
|
2023-03-20 07:11:09 +08:00
|
|
|
let uname = std::process::Command::new("uname")
|
|
|
|
.output()
|
2023-03-20 06:50:32 +08:00
|
|
|
.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))
|
|
|
|
}
|