|
| 1 | +// Taken from reth [https://github.com/paradigmxyz/reth/blob/main/crates/node/core/build.rs] |
| 2 | +// The MIT License (MIT) |
| 3 | +// |
| 4 | +// Copyright (c) 2022-2025 Reth Contributors |
| 5 | +// |
| 6 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +// of this software and associated documentation files (the "Software"), to deal |
| 8 | +// in the Software without restriction, including without limitation the rights |
| 9 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +// copies of the Software, and to permit persons to whom the Software is |
| 11 | +// furnished to do so, subject to the following conditions: |
| 12 | +// |
| 13 | +// The above copyright notice and this permission notice shall be included in |
| 14 | +// all copies or substantial portions of the Software. |
| 15 | +// |
| 16 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | +// THE SOFTWARE. |
| 23 | + |
| 24 | +use std::{env, error::Error}; |
| 25 | +use vergen::{BuildBuilder, CargoBuilder, Emitter}; |
| 26 | +use vergen_git2::Git2Builder; |
| 27 | + |
| 28 | +fn main() -> Result<(), Box<dyn Error>> { |
| 29 | + let mut emitter = Emitter::default(); |
| 30 | + |
| 31 | + let build_builder = BuildBuilder::default().build_timestamp(true).build()?; |
| 32 | + |
| 33 | + emitter.add_instructions(&build_builder)?; |
| 34 | + |
| 35 | + let cargo_builder = CargoBuilder::default() |
| 36 | + .features(true) |
| 37 | + .target_triple(true) |
| 38 | + .build()?; |
| 39 | + |
| 40 | + emitter.add_instructions(&cargo_builder)?; |
| 41 | + |
| 42 | + let git_builder = Git2Builder::default() |
| 43 | + .describe(false, true, None) |
| 44 | + .dirty(true) |
| 45 | + .sha(false) |
| 46 | + .build()?; |
| 47 | + |
| 48 | + emitter.add_instructions(&git_builder)?; |
| 49 | + |
| 50 | + emitter.emit_and_set()?; |
| 51 | + let sha = env::var("VERGEN_GIT_SHA")?; |
| 52 | + let sha_short = &sha[0..7]; |
| 53 | + |
| 54 | + let is_dirty = env::var("VERGEN_GIT_DIRTY")? == "true"; |
| 55 | + // > git describe --always --tags |
| 56 | + // if not on a tag: v0.2.0-beta.3-82-g1939939b |
| 57 | + // if on a tag: v0.2.0-beta.3 |
| 58 | + let not_on_tag = env::var("VERGEN_GIT_DESCRIBE")?.ends_with(&format!("-g{sha_short}")); |
| 59 | + let version_suffix = if is_dirty || not_on_tag { "-dev" } else { "" }; |
| 60 | + println!("cargo:rustc-env=OP_RBUILDER_VERSION_SUFFIX={version_suffix}"); |
| 61 | + |
| 62 | + // Set short SHA |
| 63 | + println!("cargo:rustc-env=VERGEN_GIT_SHA_SHORT={}", &sha[..8]); |
| 64 | + |
| 65 | + // Set the build profile |
| 66 | + let out_dir = env::var("OUT_DIR").unwrap(); |
| 67 | + let profile = out_dir.rsplit(std::path::MAIN_SEPARATOR).nth(3).unwrap(); |
| 68 | + println!("cargo:rustc-env=OP_RBUILDER_BUILD_PROFILE={profile}"); |
| 69 | + |
| 70 | + // Set formatted version strings |
| 71 | + let pkg_version = env!("CARGO_PKG_VERSION"); |
| 72 | + |
| 73 | + // The short version information for op-rbuilder. |
| 74 | + // - The latest version from Cargo.toml |
| 75 | + // - The short SHA of the latest commit. |
| 76 | + // Example: 0.1.0 (defa64b2) |
| 77 | + println!( |
| 78 | + "cargo:rustc-env=OP_RBUILDER_SHORT_VERSION={pkg_version}{version_suffix} ({sha_short})" |
| 79 | + ); |
| 80 | + |
| 81 | + // LONG_VERSION |
| 82 | + // The long version information for op-rbuilder. |
| 83 | + // |
| 84 | + // - The latest version from Cargo.toml + version suffix (if any) |
| 85 | + // - The full SHA of the latest commit |
| 86 | + // - The build datetime |
| 87 | + // - The build features |
| 88 | + // - The build profile |
| 89 | + // |
| 90 | + // Example: |
| 91 | + // |
| 92 | + // ```text |
| 93 | + // Version: 0.1.0 |
| 94 | + // Commit SHA: defa64b2 |
| 95 | + // Build Timestamp: 2023-05-19T01:47:19.815651705Z |
| 96 | + // Build Features: jemalloc |
| 97 | + // Build Profile: maxperf |
| 98 | + // ``` |
| 99 | + println!("cargo:rustc-env=OP_RBUILDER_LONG_VERSION_0=Version: {pkg_version}{version_suffix}"); |
| 100 | + println!("cargo:rustc-env=OP_RBUILDER_LONG_VERSION_1=Commit SHA: {sha}"); |
| 101 | + println!( |
| 102 | + "cargo:rustc-env=OP_RBUILDER_LONG_VERSION_2=Build Timestamp: {}", |
| 103 | + env::var("VERGEN_BUILD_TIMESTAMP")? |
| 104 | + ); |
| 105 | + println!( |
| 106 | + "cargo:rustc-env=OP_RBUILDER_LONG_VERSION_3=Build Features: {}", |
| 107 | + env::var("VERGEN_CARGO_FEATURES")? |
| 108 | + ); |
| 109 | + println!("cargo:rustc-env=OP_RBUILDER_LONG_VERSION_4=Build Profile: {profile}"); |
| 110 | + |
| 111 | + // The version information for op-rbuilder formatted for P2P (devp2p). |
| 112 | + // - The latest version from Cargo.toml |
| 113 | + // - The target triple |
| 114 | + // |
| 115 | + // Example: op-rbuilder/v0.1.0-alpha.1-428a6dc2f/aarch64-apple-darwin |
| 116 | + println!( |
| 117 | + "cargo:rustc-env=OP_RBUILDER_P2P_CLIENT_VERSION={}", |
| 118 | + format_args!( |
| 119 | + "op-rbuilder/v{pkg_version}-{sha_short}/{}", |
| 120 | + env::var("VERGEN_CARGO_TARGET_TRIPLE")? |
| 121 | + ) |
| 122 | + ); |
| 123 | + |
| 124 | + Ok(()) |
| 125 | +} |
0 commit comments