Skip to content

Commit c45e880

Browse files
authored
chore: clean up build script (#12488)
* chore: clean up build script * doc
1 parent abd4728 commit c45e880

File tree

4 files changed

+50
-44
lines changed

4 files changed

+50
-44
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

benches/Cargo.toml

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
[package]
22
name = "foundry-bench"
3-
version = "0.1.0"
4-
edition = "2024"
5-
license = "Apache-2.0 OR MIT"
3+
description = "Foundry benchmark runner"
4+
5+
version.workspace = true
6+
edition.workspace = true
7+
rust-version.workspace = true
8+
authors.workspace = true
9+
license.workspace = true
10+
homepage.workspace = true
11+
repository.workspace = true
12+
13+
[lints]
14+
workspace = true
615

716
[[bin]]
817
name = "foundry-bench"

benches/src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! Foundry benchmark runner.
2+
13
use crate::results::{HyperfineOutput, HyperfineResult};
24
use eyre::{Result, WrapErr};
35
use foundry_common::{sh_eprintln, sh_println};
@@ -57,7 +59,7 @@ impl FromStr for RepoConfig {
5759
} else {
5860
// Create new config with custom rev or default
5961
// Name should follow the format: org-repo (with hyphen)
60-
RepoConfig {
62+
Self {
6163
name: format!("{org}-{repo}"),
6264
org: org.to_string(),
6365
repo: repo.to_string(),
@@ -159,7 +161,7 @@ impl BenchmarkProject {
159161
Self::install_npm_dependencies(&root_path)?;
160162

161163
sh_println!(" ✅ Project {} setup complete at {}", config.name, root);
162-
Ok(BenchmarkProject { name: config.name.to_string(), root_path, temp_project })
164+
Ok(Self { name: config.name.to_string(), root_path, temp_project })
163165
}
164166

165167
/// Install npm dependencies if package.json exists

crates/common/build.rs

Lines changed: 33 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1-
use std::{env, error::Error};
1+
#![expect(clippy::disallowed_macros)]
22

33
use chrono::DateTime;
4+
use std::{error::Error, path::PathBuf};
45
use vergen::EmitBuilder;
56

6-
#[expect(clippy::disallowed_macros)]
77
fn main() -> Result<(), Box<dyn Error>> {
8-
// Re-run the build script if the build script itself changes or if the
9-
// environment variables change.
108
println!("cargo:rerun-if-changed=build.rs");
11-
println!("cargo:rerun-if-env-changed=TAG_NAME");
12-
println!("cargo:rerun-if-env-changed=PROFILE");
139

1410
EmitBuilder::builder()
1511
.build_date()
@@ -18,38 +14,25 @@ fn main() -> Result<(), Box<dyn Error>> {
1814
.git_sha(false)
1915
.emit_and_set()?;
2016

21-
// Set the Git SHA of the latest commit.
22-
let sha = env::var("VERGEN_GIT_SHA")?;
17+
let sha = env_var("VERGEN_GIT_SHA");
2318
let sha_short = &sha[..10];
2419

25-
// Set the version suffix and whether the version is a nightly build.
26-
// if not on a tag: <BIN> 0.3.0-dev+ba03de0019.1737036656.debug
27-
// if on a tag: <BIN> 0.3.0-stable+ba03de0019.1737036656.release
28-
let tag_name = env::var("TAG_NAME")
29-
.or_else(|_| env::var("CARGO_TAG_NAME"))
30-
.unwrap_or_else(|_| String::from("dev"));
31-
let (is_nightly, version_suffix) = if tag_name.contains("nightly") {
32-
(true, "-nightly".to_string())
33-
} else if let Some((_, rc_number)) = tag_name.split_once("rc") {
34-
(false, format!("-rc{rc_number}"))
35-
} else {
36-
(false, format!("-{tag_name}"))
37-
};
20+
let tag_name = try_env_var("TAG_NAME").unwrap_or_else(|| String::from("dev"));
21+
let is_nightly = tag_name.contains("nightly");
22+
let version_suffix = if is_nightly { "nightly" } else { &tag_name };
3823

39-
// Whether the version is a nightly build.
4024
if is_nightly {
4125
println!("cargo:rustc-env=FOUNDRY_IS_NIGHTLY_VERSION=true");
4226
}
4327

44-
// Set formatted version strings
45-
let pkg_version = env::var("CARGO_PKG_VERSION")?;
28+
let pkg_version = env_var("CARGO_PKG_VERSION");
29+
let version = format!("{pkg_version}-{version_suffix}");
4630

47-
// Append the profile to the version string
48-
let out_dir = env::var("OUT_DIR").unwrap();
49-
let profile = out_dir.rsplit(std::path::MAIN_SEPARATOR).nth(3).unwrap();
31+
// `PROFILE` captures only release or debug. Get the actual name from the out directory.
32+
let out_dir = PathBuf::from(env_var("OUT_DIR"));
33+
let profile = out_dir.components().rev().nth(3).unwrap().as_os_str().to_str().unwrap();
5034

51-
// Set the build timestamp.
52-
let build_timestamp = env::var("VERGEN_BUILD_TIMESTAMP")?;
35+
let build_timestamp = env_var("VERGEN_BUILD_TIMESTAMP");
5336
let build_timestamp_unix = DateTime::parse_from_rfc3339(&build_timestamp)?.timestamp();
5437

5538
// The SemVer compatible version information for Foundry.
@@ -59,16 +42,14 @@ fn main() -> Result<(), Box<dyn Error>> {
5942
// - The build profile.
6043
// Example: forge 0.3.0-nightly+3cb96bde9b.1737036656.debug
6144
println!(
62-
"cargo:rustc-env=FOUNDRY_SEMVER_VERSION={pkg_version}{version_suffix}+{sha_short}.{build_timestamp_unix}.{profile}"
45+
"cargo:rustc-env=FOUNDRY_SEMVER_VERSION={version}+{sha_short}.{build_timestamp_unix}.{profile}"
6346
);
6447

6548
// The short version information for the Foundry CLI.
6649
// - The latest version from Cargo.toml
6750
// - The short SHA of the latest commit.
6851
// Example: 0.3.0-dev (3cb96bde9b)
69-
println!(
70-
"cargo:rustc-env=FOUNDRY_SHORT_VERSION={pkg_version}{version_suffix} ({sha_short} {build_timestamp})"
71-
);
52+
println!("cargo:rustc-env=FOUNDRY_SHORT_VERSION={version} ({sha_short} {build_timestamp})");
7253

7354
// The long version information for the Foundry CLI.
7455
// - The latest version from Cargo.toml.
@@ -85,12 +66,26 @@ fn main() -> Result<(), Box<dyn Error>> {
8566
// Build Timestamp: 2025-01-16T15:04:03.522021223Z (1737039843)
8667
// Build Profile: debug
8768
// ```
88-
println!("cargo:rustc-env=FOUNDRY_LONG_VERSION_0=Version: {pkg_version}{version_suffix}");
89-
println!("cargo:rustc-env=FOUNDRY_LONG_VERSION_1=Commit SHA: {sha}");
90-
println!(
91-
"cargo:rustc-env=FOUNDRY_LONG_VERSION_2=Build Timestamp: {build_timestamp} ({build_timestamp_unix})"
69+
let long_version = format!(
70+
"\
71+
Version: {version}
72+
Commit SHA: {sha}
73+
Build Timestamp: {build_timestamp} ({build_timestamp_unix})
74+
Build Profile: {profile}"
9275
);
93-
println!("cargo:rustc-env=FOUNDRY_LONG_VERSION_3=Build Profile: {profile}");
76+
assert_eq!(long_version.lines().count(), 4);
77+
for (i, line) in long_version.lines().enumerate() {
78+
println!("cargo:rustc-env=FOUNDRY_LONG_VERSION_{i}={line}");
79+
}
9480

9581
Ok(())
9682
}
83+
84+
fn env_var(name: &str) -> String {
85+
try_env_var(name).unwrap()
86+
}
87+
88+
fn try_env_var(name: &str) -> Option<String> {
89+
println!("cargo:rerun-if-env-changed={name}");
90+
std::env::var(name).ok()
91+
}

0 commit comments

Comments
 (0)