Skip to content

Commit

Permalink
Generate FISH_BUILD_VERSION from build.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
henrikhorluck committed Aug 18, 2023
1 parent 603c5b7 commit 421ad21
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 7 deletions.
110 changes: 110 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Expand Up @@ -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]
Expand All @@ -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 = []
bsd = []
23 changes: 23 additions & 0 deletions 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
Expand Down Expand Up @@ -115,6 +119,25 @@ fn main() {
rsconf::rebuild_if_paths_changed(&source_files);
}

fn find_version() -> Result<String, Box<dyn std::error::Error>> {
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.
///
Expand Down
3 changes: 1 addition & 2 deletions fish-rust/src/env/environment.rs
Expand Up @@ -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);

Expand Down
1 change: 0 additions & 1 deletion fish-rust/src/ffi.rs
Expand Up @@ -77,7 +77,6 @@ include_cpp! {
generate!("set_cloexec")

generate!("init_input")
generate!("get_fish_version")

generate_pod!("pipes_ffi_t")
generate!("environment_t")
Expand Down
4 changes: 1 addition & 3 deletions fish-rust/src/fish.rs
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
Expand Down
2 changes: 2 additions & 0 deletions fish-rust/src/lib.rs
Expand Up @@ -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;

Expand Down

0 comments on commit 421ad21

Please sign in to comment.