Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 41 additions & 21 deletions crates/rust-analyzer/build.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
//! Just embed git-hash to `--version`
//! Construct version in the `commit-hash date chanel` format

use std::{env, path::PathBuf, process::Command};

fn main() {
set_rerun();

let rev =
env::var("RUST_ANALYZER_REV").ok().or_else(rev).unwrap_or_else(|| "???????".to_string());
println!("cargo:rustc-env=REV={}", rev)
println!("cargo:rustc-env=REV={}", rev())
}

fn set_rerun() {
Expand All @@ -18,29 +15,52 @@ fn set_rerun() {
);

while manifest_dir.parent().is_some() {
if manifest_dir.join(".git/HEAD").exists() {
let git_dir = manifest_dir.join(".git");

println!("cargo:rerun-if-changed={}", git_dir.join("HEAD").display());
// current branch ref
if let Ok(output) =
Command::new("git").args(&["rev-parse", "--symbolic-full-name", "HEAD"]).output()
{
if let Ok(ref_link) = String::from_utf8(output.stdout) {
println!("cargo:rerun-if-changed={}", git_dir.join(ref_link).display());
}
}
let head_ref = manifest_dir.join(".git/HEAD");
if head_ref.exists() {
println!("cargo:rerun-if-changed={}", head_ref.display());
return;
}

manifest_dir.pop();
}

println!("cargo:warning=Could not find `.git/HEAD` from manifest dir!");
}

fn rev() -> Option<String> {
let output =
Command::new("git").args(&["describe", "--tags", "--exclude", "nightly"]).output().ok()?;
fn rev() -> String {
if let Ok(rev) = env::var("RUST_ANALYZER_REV") {
return rev;
}

if let Some(commit_hash) = commit_hash() {
let mut buf = commit_hash;

if let Some(date) = build_date() {
buf.push(' ');
buf.push_str(&date);
}

let channel = env::var("RUST_ANALYZER_CHANNEL").unwrap_or_else(|_| "dev".to_string());
buf.push(' ');
buf.push_str(&channel);

return buf;
}

"???????".to_string()
}

fn commit_hash() -> Option<String> {
output_to_string("git rev-parse --short HEAD")
}

fn build_date() -> Option<String> {
output_to_string("date --iso --utc")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this'll work for all hosts, but we are doing a similar thing in dist anyways, so, somehow, it seems to work?

}

fn output_to_string(command: &str) -> Option<String> {
let args = command.split_ascii_whitespace().collect::<Vec<_>>();
let output = Command::new(args[0]).args(&args[1..]).output().ok()?;
let stdout = String::from_utf8(output.stdout).ok()?;
Some(stdout)
Some(stdout.trim().to_string())
}
8 changes: 5 additions & 3 deletions xtask/src/dist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{

use anyhow::Result;
use flate2::{write::GzEncoder, Compression};
use xshell::{cmd, cp, mkdir_p, pushd, read_file, rm_rf, write_file};
use xshell::{cmd, cp, mkdir_p, pushd, pushenv, read_file, rm_rf, write_file};

use crate::{date_iso, project_root};

Expand All @@ -26,7 +26,8 @@ impl DistCmd {
let release_tag = if self.nightly { "nightly".to_string() } else { date_iso()? };
dist_client(&version, &release_tag)?;
}
dist_server()?;
let release_channel = if self.nightly { "nightly" } else { "stable" };
dist_server(release_channel)?;
Ok(())
}
}
Expand Down Expand Up @@ -59,7 +60,8 @@ fn dist_client(version: &str, release_tag: &str) -> Result<()> {
Ok(())
}

fn dist_server() -> Result<()> {
fn dist_server(release_channel: &str) -> Result<()> {
let _e = pushenv("RUST_ANALYZER_CHANNEL", release_channel);
let target = get_target();
if target.contains("-linux-gnu") || target.contains("-linux-musl") {
env::set_var("CC", "clang");
Expand Down