Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How to get git hash in build.rs ? #1566

Answered by enomado
enomado asked this question in Q&A
Discussion options

Hi

Here is my Cross.toml


[target.x86_64-pc-windows-gnu]
pre-build = [
 "dpkg --add-architecture $CROSS_DEB_ARCH",
 "apt update",
 # tls
 "apt install --assume-yes nasm",
 # protobuf
 "apt install --assume-yes protobuf-compiler:$CROSS_DEB_ARCH",
 "apt install --assume-yes openssl",
 "apt install --assume-yes git",
 
]

I'm trying

 let hash = Command::new("git").args(&["rev-parse", "HEAD"]).output()?;
 let git_hash = String::from_utf8(hash.stdout)?;
 p!("BUILD.rs -> git hash ...{}", git_hash);

It emits empty string.

I also tried vergen_git2 and apt install --assume-yes libgit2, but result is pretty the same - it cant find repo, or so.

You must be logged in to vote

Looks like a found a problem.

stderr was fatal: not a git repository (or any parent up to mount point /home/sc/t/bur)

real problem was what my repo is not equal to workspace root.
I have something like

git_repo
 .git
 my_rust_project

when I run cross build, my pwd is git_repo/my_rust_project, so I have mounted only my_rust_project, and ./../.git is not accessible.

Is there any way to set mount point as ./../, or should I reorganize my repository?

Replies: 2 comments 5 replies

Comment options

Im guessing theres some output in stderr? This is probably a file ownership problem. If it is, see #1473 (reply in thread)

You must be logged in to vote
0 replies
Comment options

Looks like a found a problem.

stderr was fatal: not a git repository (or any parent up to mount point /home/sc/t/bur)

real problem was what my repo is not equal to workspace root.
I have something like

git_repo
 .git
 my_rust_project

when I run cross build, my pwd is git_repo/my_rust_project, so I have mounted only my_rust_project, and ./../.git is not accessible.

Is there any way to set mount point as ./../, or should I reorganize my repository?

You must be logged in to vote
5 replies
Comment options

looks like I found an answer #1294

But i cant get how lets say FTL_DIR can help me

Comment options

got it

it'll be mounted in the container at the same path as on the host.

Comment options

Hi @enomado could you share your [build.env] section of Cross.toml? I'm running into the same issue :)

Comment options

@merklefruit its still experimental, but last time worked


[build.env]
# i'm in /home/sc/t/bur/rust_app
volumes = [
 # "A_DIRECTORY=/home/sc/t/bur",
 "A_DIRECTORY=./../",
]
[target.x86_64-pc-windows-gnu]
# image = "ghcr.io/cross-rs/aarch64-unknown-linux-gnu:0.2.4"
# build-std = ["core", "alloc"] 
pre-build = [
 "dpkg --add-architecture $CROSS_DEB_ARCH",
 "apt update",
 # for tls
 "apt install --assume-yes nasm",
 # for protobuf
 "apt install --assume-yes protobuf-compiler:$CROSS_DEB_ARCH",
 "apt install --assume-yes openssl",
 "apt install --assume-yes git",
 # some of them need gfor git yo work
 "git config --system --add safe.directory '*'",
 "git config --system --add safe.directory './../*'",
 "git config --system --add safe.directory './../../*'",
 "git config --system --add safe.directory './../../../*'",
 "git config --system --add safe.directory './../../../../*'",
 "git config --system --add safe.directory './../../../../../*'",
 
]

build.rs


#![allow(dead_code)]
use std::{env, fs, path::PathBuf, process::Command, str::FromStr};
use vergen_git2::{Emitter, Git2Builder};
// use vergen::Emitter;
extern crate winresource;
// use vergen::*;
fn main() {
 if std::env::var("CARGO_CFG_TARGET_OS").unwrap() == "windows" {
 let mut res = winresource::WindowsResource::new();
 res.set_icon("./assets/app_icon.ico");
 res.compile().unwrap();
 }
 // vergen(SHORT_SHA | COMMIT_DATE).unwrap();
 // works in cross https://github.com/cross-rs/cross/discussions/1473#discussioncomment-9128550
 gen_version_wrap();
 // get_version_dirty_way_wrap()
}
macro_rules! p {
 ($($tokens: tt)*) => {
 println!("cargo:warning={}", format!($($tokens)*))
 }
}
fn get_version_dirty_way_wrap() {
 match get_version_dirty_way() {
 Ok(_s) => {}
 Err(_x) => {}
 }
}
fn get_version_dirty_way() -> anyhow::Result<String> {
 let hash = Command::new("git").args(&["rev-parse", "HEAD"]).output()?;
 let git_hash = String::from_utf8(hash.stdout)?;
 let git_hash_err = String::from_utf8(hash.stderr)?;
 let pp = PathBuf::from_str("./../../../")?;
 let paths = fs::read_dir(pp).unwrap();
 for path in paths {
 let ppp = path.unwrap();
 p!("paths, {}", &ppp.path().display());
 }
 let path = PathBuf::from_str("./../../../.git/refs/heads/master")?;
 let contents = fs::read_to_string(path).unwrap();
 p!("BUILD.rs -> git hash2 {}", contents);
 p!(
 "BUILD.rs -> git hash {}, {}, {}...",
 git_hash,
 git_hash_err,
 contents
 );
 Ok(git_hash)
}
fn gen_version_wrap() {
 match gen_version() {
 Ok(_x) => {}
 Err(x) => {
 println!("failed to get git version. {x}")
 }
 }
}
fn gen_version() -> anyhow::Result<()> {
 // let build = BuildBuilder::all_build()?;
 // let git2 = Git2Builder::all_git()?;
 let _path = env::current_dir()?;
 // let repo = path.join("./../../../").canonicalize()?;
 // p!("BUILD.rs -> Starting ...{}", repo.display());
 // println!("The current directory is {}", path.display());
 // let git2 = GitclBuilder::all_git()?;
 let git2 = Git2Builder::all_git()?;
 // VERGEN_GIT_SHA
 // git2.at_path(repo);
 Emitter::default()
 // .add_instructions(&build)?
 .add_instructions(&git2)?
 .emit()?;
 Ok(())
}

main.rs


impl AppGitHash {
 pub fn get() -> Option<String> {
 let hash = option_env!("VERGEN_GIT_SHA");
 hash.map(|s| s[0..8].to_owned())
 }
 // VERGEN_GIT_SHA
}
Comment options

Thank you!

Answer selected by enomado
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /