|
1 | | -use std::fs; |
| 1 | +use std::{env, fs}; |
2 | 2 |
|
3 | 3 | extern crate napi_build; |
4 | 4 |
|
5 | | -fn main() { |
| 5 | +fn main() -> anyhow::Result<()> { |
| 6 | + println!("cargo:rerun-if-env-changed=CI"); |
| 7 | + let is_ci = env::var("CI").is_ok_and(|value| !value.is_empty()); |
| 8 | + |
6 | 9 | // Generates, stores build-time information as static values. |
7 | 10 | // There are some places relying on correct values for this (i.e telemetry), |
8 | 11 | // So failing build if this fails. |
9 | | - shadow_rs::ShadowBuilder::builder() |
10 | | - .build() |
11 | | - .expect("Should able to generate build time information"); |
| 12 | + let cargo = vergen_git2::CargoBuilder::default() |
| 13 | + .target_triple(true) |
| 14 | + .build()?; |
| 15 | + // We use the git dirty state to disable persistent caching (persistent caching relies on a |
| 16 | + // commit hash to be safe). One tradeoff of this is that we must invalidate the rust build more |
| 17 | + // often. |
| 18 | + // |
| 19 | + // This invalidates the build if any untracked files change. That's sufficient for the case |
| 20 | + // where we transition from dirty to clean. |
| 21 | + // |
| 22 | + // There's an edge-case here where the repository could be newly dirty, but we can't know |
| 23 | + // because our build hasn't been invalidated, since the untracked files weren't untracked last |
| 24 | + // time we ran. That will cause us to incorrectly report ourselves as clean. |
| 25 | + // |
| 26 | + // However, in practice that shouldn't be much of an issue: If no other dependency of this |
| 27 | + // top-level crate has changed (which would've triggered our rebuild), then the resulting binary |
| 28 | + // must be equivalent to a clean build anyways. Therefore, persistent caching using the HEAD |
| 29 | + // commit hash as a version is okay. |
| 30 | + let git = vergen_git2::Git2Builder::default() |
| 31 | + .dirty(/* include_untracked */ true) |
| 32 | + .describe( |
| 33 | + /* tags */ true, |
| 34 | + /* dirty */ !is_ci, // suppress the dirty suffix in CI |
| 35 | + /* matches */ Some("v[0-9]*"), // find the last version tag |
| 36 | + ) |
| 37 | + .build()?; |
| 38 | + vergen_git2::Emitter::default() |
| 39 | + .add_instructions(&cargo)? |
| 40 | + .add_instructions(&git)? |
| 41 | + .fail_on_error() |
| 42 | + .emit()?; |
12 | 43 |
|
13 | 44 | let git_head = fs::read_to_string("../../.git/HEAD").unwrap_or_default(); |
14 | 45 | if !git_head.is_empty() && !git_head.starts_with("ref: ") { |
@@ -36,4 +67,6 @@ fn main() { |
36 | 67 |
|
37 | 68 | #[cfg(not(target_arch = "wasm32"))] |
38 | 69 | turbo_tasks_build::generate_register(); |
| 70 | + |
| 71 | + Ok(()) |
39 | 72 | } |
0 commit comments