1- use std :: { env , error :: Error } ;
1+ #! [ expect ( clippy :: disallowed_macros ) ]
22
33use chrono:: DateTime ;
4+ use std:: { error:: Error , path:: PathBuf } ;
45use vergen:: EmitBuilder ;
56
6- #[ expect( clippy:: disallowed_macros) ]
77fn 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