Skip to content

Commit

Permalink
Add commit information to roc versions built from source
Browse files Browse the repository at this point in the history
  • Loading branch information
Hasnep committed Sep 25, 2024
1 parent 3215a8f commit 4170f47
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 25 deletions.
41 changes: 28 additions & 13 deletions Cargo.lock

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

12 changes: 11 additions & 1 deletion crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
name = "roc_cli"
description = "The Roc binary that brings together all functionality in the Roc toolset."
default-run = "roc"
build = "build.rs"

authors.workspace = true
edition.workspace = true
Expand Down Expand Up @@ -30,7 +31,13 @@ target-wasm32 = ["roc_build/target-wasm32"]
target-x86 = ["roc_build/target-x86", "roc_repl_cli/target-x86"]
target-x86_64 = ["roc_build/target-x86_64", "roc_repl_cli/target-x86_64"]

target-all = ["target-aarch64", "target-arm", "target-x86", "target-x86_64", "target-wasm32"]
target-all = [
"target-aarch64",
"target-arm",
"target-x86",
"target-x86_64",
"target-wasm32",
]

sanitizers = ["roc_build/sanitizers"]

Expand Down Expand Up @@ -93,6 +100,9 @@ parking_lot.workspace = true
pretty_assertions.workspace = true
serial_test.workspace = true

[build-dependencies]
chrono.workspace = true

[[bench]]
name = "time_bench"
harness = false
71 changes: 71 additions & 0 deletions crates/cli/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use chrono::prelude::*;
use std::fs;
use std::process::Command;
use std::str;

fn main() {
// Rebuild if this build.rs file changes
println!("cargo:rerun-if-changed=build.rs");

// The version file is located at the root of the repository
let version_file_path = "../../version.txt";

// Rebuild if version file changes
println!("cargo:rerun-if-changed={}", version_file_path);

// Read the version file
let version_file_contents = fs::read_to_string(version_file_path).unwrap();

// If the version is "built-from-source", replace it with the git commit information
let version = match version_file_contents.trim() {
"built-from-source" => {
let git_head_file_path = "../../.git/HEAD";
// Rebuild if a new Git commit is made
println!("cargo:rerun-if-changed={}", git_head_file_path);

// Check if the .git/HEAD file exists
let git_head_exists = fs::metadata(git_head_file_path).is_ok();
if git_head_exists {
// Get the hash of the current commit
let git_describe_output = Command::new("git")
.arg("describe")
.arg("--always")
.arg("--dirty= with additional changes") // Add a suffix if the working directory is dirty
.output()
.expect("Failed to execute git describe command");
println!("git_describe_output: {:?}", git_describe_output);
let git_commit_hash = str::from_utf8(&git_describe_output.stdout)
.expect("Failed to parse git describe output")
.trim();

// Get the datetime of the last commit
let git_show_output = Command::new("git")
.arg("show")
.arg("--no-patch")
.arg("--format=%ct") // Outputting a UNIX timestamp is the only way to always use UTC
.output()
.expect("Failed to execute git show command");
println!("git_show_output: {:?}", git_show_output);
let git_commit_timestamp = {
let timestamp = str::from_utf8(&git_show_output.stdout)
.expect("Failed to parse git show output as a string")
.trim()
.parse::<i64>()
.expect("Failed to parse timestamp as an integer");
DateTime::from_timestamp(timestamp, 0)
.expect("Failed to parse timestamp")
.format("%Y-%m-%d %H:%M:%S")
};
format!(
"built from commit {git_commit_hash}, committed at {git_commit_timestamp} UTC"
)
} else {
// If the .git/HEAD file does not exist, e.g. in a Nix build, use a generic message
"built from source".to_string()
}
}
_ => version_file_contents.trim().to_string(),
};
// Emit the version to a build-time environment variable
println!("cargo:rustc-env=ROC_VERSION={}", version);
}
4 changes: 2 additions & 2 deletions crates/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ pub const FLAG_PP_HOST: &str = "host";
pub const FLAG_PP_PLATFORM: &str = "platform";
pub const FLAG_PP_DYLIB: &str = "lib";

const VERSION: &str = include_str!("../../../version.txt");
pub const VERSION: &str = env!("ROC_VERSION");
const DEFAULT_GENERATED_DOCS_DIR: &str = "generated-docs";

pub fn build_app() -> Command {
Expand Down Expand Up @@ -182,7 +182,7 @@ pub fn build_app() -> Command {
PossibleValuesParser::new(Target::iter().map(Into::<&'static str>::into));

Command::new("roc")
.version(concatcp!(VERSION, "\n"))
.version(VERSION)
.about("Run the given .roc file, if there are no compilation errors.\nYou can use one of the SUBCOMMANDS below to do something else!")
.args_conflicts_with_subcommands(true)
.subcommand(Command::new(CMD_BUILD)
Expand Down
11 changes: 2 additions & 9 deletions crates/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use roc_cli::{
CMD_RUN, CMD_TEST, CMD_VERSION, DIRECTORY_OR_FILES, FLAG_CHECK, FLAG_DEV, FLAG_LIB, FLAG_MAIN,
FLAG_NO_COLOR, FLAG_NO_HEADER, FLAG_NO_LINK, FLAG_OUTPUT, FLAG_PP_DYLIB, FLAG_PP_HOST,
FLAG_PP_PLATFORM, FLAG_STDIN, FLAG_STDOUT, FLAG_TARGET, FLAG_TIME, GLUE_DIR, GLUE_SPEC,
ROC_FILE,
ROC_FILE, VERSION,
};
use roc_docs::generate_docs_html;
use roc_error_macros::user_error;
Expand All @@ -23,9 +23,6 @@ use std::path::{Path, PathBuf};
use std::str::FromStr;
use target_lexicon::Triple;

#[macro_use]
extern crate const_format;

#[global_allocator]
static ALLOC: mimalloc::MiMalloc = mimalloc::MiMalloc;

Expand Down Expand Up @@ -364,11 +361,7 @@ fn main() -> io::Result<()> {
Ok(format_exit_code)
}
Some((CMD_VERSION, _)) => {
print!(
"{}",
concatcp!("roc ", include_str!("../../../version.txt"))
);

println!("roc {}", VERSION);
Ok(0)
}
_ => unreachable!(),
Expand Down

0 comments on commit 4170f47

Please sign in to comment.