-
Notifications
You must be signed in to change notification settings - Fork 413
Windows build does not link with Advapi32.lib #1142
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Just for reference, that is the use std::{env, fs, path::PathBuf};
use anyhow::{Context, Result};
use git2::{DescribeOptions, ErrorCode, Repository};
fn get_current_branch(repo: &Repository) -> Result<Option<String>> {
// git branch --show-current
let head = match repo.head() {
Ok(head) => Some(head),
Err(err) => match err.code() {
ErrorCode::NotFound | ErrorCode::UnbornBranch => None,
_ => return Err(err.into()),
},
};
Ok(head.as_ref().and_then(|h| h.shorthand()).map(String::from))
}
fn rerun_if_git_ref_changed(repo: &Repository) -> Result<()> {
let git_dir = repo.path();
let head_path = git_dir.join("HEAD");
if head_path.exists() {
println!("cargo:rerun-if-changed={}", head_path.display());
}
if let Some(current_branch) = get_current_branch(repo)? {
let git_current_branch_ref = git_dir.join("refs").join("heads").join(current_branch);
if git_current_branch_ref.exists() {
println!(
"cargo:rerun-if-changed={}",
git_current_branch_ref.display()
);
}
}
let tags_path = git_dir.join("refs").join("tags");
if tags_path.exists() {
println!("cargo:rerun-if-changed={}", tags_path.display());
}
Ok(())
}
fn git_describe_tags(repo: &Repository) -> Result<String> {
// git describe --tags
Ok(repo
.describe(DescribeOptions::new().describe_tags())?
.format(None)?)
}
fn git_rev_parse_commit_hash(repo: &Repository) -> Result<String> {
// git rev-parse --short HEAD^{commit}
let commit_hash = repo
.revparse_ext("HEAD^{commit}")?
.0
.describe(DescribeOptions::new().show_commit_oid_as_fallback(true))?
.format(None)?;
Ok(commit_hash)
}
fn get_version() -> Result<String> {
if let Ok(repo) = Repository::discover(".") {
rerun_if_git_ref_changed(&repo)?;
if let Ok(tag) = git_describe_tags(&repo) {
return Ok(tag);
}
if let Ok(hash) = git_rev_parse_commit_hash(&repo) {
return Ok(hash);
}
}
Ok("undefined".to_owned())
}
fn main() -> Result<()> {
let out_dir = PathBuf::from(env::var_os("OUT_DIR").context("OUT_DIR is set by cargo")?);
fs::write(out_dir.join("version.txt"), get_version()?)?;
Ok(())
} This is a close rewrite of this code https://github.com/mullvad/mullvadvpn-app/blob/main/mullvad-version/build.rs So not only me could benefit from the fix to this issue. |
Thanks for the report! I have opened a fix at #1143 |
Thanks for the fix! I haven't checked that you have recreated build script entirely, and not using |
I tried to use
git2
in mybuild.rs
script and encountered a series of errors:Turn out
libgit2
uses some WinAPI calls from Advapi32, likeGetCurrentProcess
in https://github.com/libgit2/libgit2/blob/21a351b0ed207d0871cb23e09c027d1ee42eae98/src/util/fs_path.c#L1832 (MSDN docs for this call https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-openprocesstoken say that linkage withAdvapi32.lib
is required for the call to work).The problem is that there is no linkage with Advapi32 in CMake scripts. Looks like Advapi32 should be added in here https://github.com/libgit2/libgit2/blob/21a351b0ed207d0871cb23e09c027d1ee42eae98/src/CMakeLists.txt#L125-L128
I created an issue for this in the
libgit2
repo libgit2/libgit2#7053 Just wanted to let you know, so new release is published when someone fixes this issue.The text was updated successfully, but these errors were encountered: