diff --git a/Cargo.lock b/Cargo.lock index 629693d500cb..371490bc76b4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -337,6 +337,7 @@ dependencies = [ "cxx-gen", "errno 0.2.8", "fast-float", + "git2", "hexponent", "inventory", "lazy_static", @@ -357,6 +358,15 @@ dependencies = [ "widestring-suffix", ] +[[package]] +name = "form_urlencoded" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a62bc1cf6f830c2ec14a513a9fb124d0a213a629668a4186f329db21fe045652" +dependencies = [ + "percent-encoding", +] + [[package]] name = "getrandom" version = "0.2.10" @@ -368,6 +378,19 @@ dependencies = [ "wasi", ] +[[package]] +name = "git2" +version = "0.17.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b989d6a7ca95a362cf2cfc5ad688b3a467be1f87e480b8dad07fee8c79b0044" +dependencies = [ + "bitflags 1.3.2", + "libc", + "libgit2-sys", + "log", + "url", +] + [[package]] name = "glob" version = "0.3.1" @@ -415,6 +438,16 @@ version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" +[[package]] +name = "idna" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d20d6b07bfbc108882d88ed8e37d39636dcc260e15e30c45e6ba089610b917c" +dependencies = [ + "unicode-bidi", + "unicode-normalization", +] + [[package]] name = "indexmap" version = "1.9.3" @@ -489,6 +522,18 @@ version = "0.2.147" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3" +[[package]] +name = "libgit2-sys" +version = "0.15.2+1.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a80df2e11fb4a61f4ba2ab42dbe7f74468da143f1a75c74e11dee7c813f694fa" +dependencies = [ + "cc", + "libc", + "libz-sys", + "pkg-config", +] + [[package]] name = "libloading" version = "0.7.4" @@ -499,6 +544,18 @@ dependencies = [ "winapi", ] +[[package]] +name = "libz-sys" +version = "1.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d97137b25e321a73eef1418d1d5d2eda4d77e12813f8e6dead84bc52c5870a7b" +dependencies = [ + "cc", + "libc", + "pkg-config", + "vcpkg", +] + [[package]] name = "link-cplusplus" version = "1.0.9" @@ -648,6 +705,12 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "19b17cddbe7ec3f8bc800887bab5e717348c95ea2ca0b1bf0837fb964dc67099" +[[package]] +name = "percent-encoding" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b2a4787296e9989611394c33f193f676704af1686e70b8f8033ab5ba9a35a94" + [[package]] name = "pkg-config" version = "0.3.27" @@ -969,12 +1032,42 @@ dependencies = [ "once_cell", ] +[[package]] +name = "tinyvec" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87cc5ceb3875bb20c2890005a4e226a4651264a5c75edb2421b52861a0a0cb50" +dependencies = [ + "tinyvec_macros", +] + +[[package]] +name = "tinyvec_macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" + +[[package]] +name = "unicode-bidi" +version = "0.3.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92888ba5573ff080736b3648696b70cafad7d250551175acbaa4e0385b3e1460" + [[package]] name = "unicode-ident" version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "301abaae475aa91687eb82514b328ab47a211a533026cb25fc3e519b86adfc3c" +[[package]] +name = "unicode-normalization" +version = "0.1.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c5713f0fc4b5db668a2ac63cdb7bb4469d8c9fed047b1d0292cc7b0ce2ba921" +dependencies = [ + "tinyvec", +] + [[package]] name = "unicode-width" version = "0.1.10" @@ -990,6 +1083,23 @@ dependencies = [ "libc", ] +[[package]] +name = "url" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50bff7831e19200a85b17131d085c25d7811bc4e186efdaf54bbd132994a88cb" +dependencies = [ + "form_urlencoded", + "idna", + "percent-encoding", +] + +[[package]] +name = "vcpkg" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" + [[package]] name = "version_check" version = "0.9.4" diff --git a/Cargo.toml b/Cargo.toml index 1718f07ffbbd..06e83b3672cb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -66,6 +66,7 @@ autocxx-build = "0.23.1" cc = { git = "https://github.com/mqudsi/cc-rs", branch = "fish" } cxx-build = { git = "https://github.com/fish-shell/cxx", branch = "fish" } cxx-gen = { git = "https://github.com/fish-shell/cxx", branch = "fish" } +git2 = { version = "0.17", default-features = false } rsconf = { git = "https://github.com/mqudsi/rsconf", branch = "master" } [lib] @@ -80,4 +81,4 @@ fish-ffi-tests = ["inventory"] # The following features are auto-detected by the build-script and should not be enabled manually. asan = [] -bsd = [] \ No newline at end of file +bsd = [] diff --git a/fish-rust/build.rs b/fish-rust/build.rs index 3aab2bf79a1e..c734fca0279f 100644 --- a/fish-rust/build.rs +++ b/fish-rust/build.rs @@ -1,8 +1,12 @@ use rsconf::{LinkType, Target}; use std::env; use std::error::Error; +use std::path::PathBuf; fn main() { + let version = find_version().unwrap_or("unknown".into()); + println!("cargo:rustc-env=FISH_BUILD_VERSION={version}"); + for key in ["DOCDIR", "DATADIR", "SYSCONFDIR", "BINDIR"] { if let Ok(val) = env::var(key) { // Forward some CMake config @@ -115,6 +119,25 @@ fn main() { rsconf::rebuild_if_paths_changed(&source_files); } +fn find_version() -> Result> { + let base_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")); + let version_file = base_dir.join("version"); + let version = if version_file.exists() { + // First see if there is a version file (included in release tarballs), + std::fs::read_to_string(version_file)? + } else { + // then try git-describe + let repo = git2::Repository::open(base_dir)?; + let mut opts = git2::DescribeOptions::new(); + let desc = repo.describe(opts.show_commit_oid_as_fallback(true))?; + desc.format(Some( + git2::DescribeFormatOptions::new().dirty_suffix("-dirty"), + ))? + }; + + Ok(version) +} + /// Dynamically enables certain features at build-time, without their having to be explicitly /// enabled in the `cargo build --features xxx` invocation. /// diff --git a/fish-rust/src/env/environment.rs b/fish-rust/src/env/environment.rs index ed2ebd4abf72..2198d4f3a17d 100644 --- a/fish-rust/src/env/environment.rs +++ b/fish-rust/src/env/environment.rs @@ -603,8 +603,7 @@ pub fn env_init(paths: Option<&ConfigPaths>, do_uvars: bool, default_paths: bool vars.set_one(L!("CMD_DURATION"), EnvMode::UNEXPORT, "0".into()); // Set up the version variable. - let v = unsafe { CStr::from_ptr(ffi::get_fish_version()) }; - let version = str2wcstring(v.to_bytes()); + let version: WString = crate::BUILD_VERSION.into(); vars.set_one(L!("version"), EnvMode::GLOBAL, version.clone()); vars.set_one(L!("FISH_VERSION"), EnvMode::GLOBAL, version); diff --git a/fish-rust/src/ffi.rs b/fish-rust/src/ffi.rs index 65df48449c9c..47c7745fa001 100644 --- a/fish-rust/src/ffi.rs +++ b/fish-rust/src/ffi.rs @@ -77,7 +77,6 @@ include_cpp! { generate!("set_cloexec") generate!("init_input") - generate!("get_fish_version") generate_pod!("pipes_ffi_t") generate!("environment_t") diff --git a/fish-rust/src/fish.rs b/fish-rust/src/fish.rs index 46254c625e5a..05a380398829 100644 --- a/fish-rust/src/fish.rs +++ b/fish-rust/src/fish.rs @@ -57,8 +57,6 @@ use std::path::{Path, PathBuf}; use std::sync::atomic::Ordering; use std::sync::Arc; -// FIXME: generate this in `build.rs` so we can read this with `env!("FISH_BUILD_VERSION")` -const FISH_BUILD_VERSION: &str = "3.6.1-99999-dirty"; // FIXME: when the crate is actually called fish and not fish-rust, read this from cargo // See: https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-crates // for reference @@ -469,7 +467,7 @@ fn fish_parse_opt(args: &mut [&wstr], opts: &mut FishCmdOpts) -> usize { 'v' => { // FIXME: this was _(L"%s, version %s\n"), but rust-fwprintf! takes a literal instead of an expr // and appears to not print anything - println!("{}, version {}", PACKAGE_NAME, FISH_BUILD_VERSION); + println!("{}, version {}", PACKAGE_NAME, crate::BUILD_VERSION); std::process::exit(0); } diff --git a/fish-rust/src/lib.rs b/fish-rust/src/lib.rs index fde6f9e728e7..d83759372dbc 100644 --- a/fish-rust/src/lib.rs +++ b/fish-rust/src/lib.rs @@ -10,6 +10,8 @@ #![allow(clippy::ptr_arg)] #![allow(clippy::field_reassign_with_default)] +pub const BUILD_VERSION: &str = env!("FISH_BUILD_VERSION"); + #[macro_use] mod common;