| 17 | } |
| 18 | |
| 19 | fn main() { |
| 20 | println!("cargo:rerun-if-changed=build.rs"); |
| 21 | println!("cargo:rerun-if-env-changed=ATOMIC_BUILD_CHANNEL"); |
| 22 | println!("cargo:rerun-if-env-changed=ATOMIC_BUILD_COMMIT"); |
| 23 | let version = env::var("CARGO_PKG_VERSION").expect("Cargo supplies the package version"); |
| 24 | let channel = env::var("ATOMIC_BUILD_CHANNEL").unwrap_or_else(|_| "dev".to_string()); |
| 25 | if channel == "release" { |
| 26 | println!("cargo:rustc-env=ATOMIC_BUILD_VERSION={version}"); |
| 27 | return; |
| 28 | } |
| 29 | assert_eq!( |
| 30 | channel, "dev", |
| 31 | "ATOMIC_BUILD_CHANNEL must be 'dev' or 'release'" |
| 32 | ); |
| 33 | let dir = PathBuf::from( |
| 34 | env::var_os("CARGO_MANIFEST_DIR").expect("Cargo supplies the manifest directory"), |
| 35 | ); |
| 36 | let root = git(&dir, &["rev-parse", "--show-toplevel"]).map(|path| PathBuf::from(path.trim())); |
| 37 | let mut dirty = false; |
| 38 | if let Some(root) = &root { |
| 39 | for name in ["HEAD", "index", "packed-refs", "refs/heads"] { |
| 40 | if let Some(path) = git(root, &["rev-parse", "--git-path", name]) { |
| 41 | let path = root.join(path.trim()); |
| 42 | if path.exists() { |
| 43 | println!("cargo:rerun-if-changed={}", path.display()); |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | if let Some(files) = git(root, &["ls-files", "-z"]) { |
| 48 | for file in files.split('\0').filter(|file| !file.is_empty()) { |
| 49 | println!("cargo:rerun-if-changed={}", root.join(file).display()); |
| 50 | } |
| 51 | } |
| 52 | dirty = git(root, &["status", "--porcelain", "--untracked-files=no"]) |
| 53 | .is_some_and(|status| !status.trim().is_empty()); |
| 54 | } |
| 55 | let supplied_commit = env::var("ATOMIC_BUILD_COMMIT").ok(); |
| 56 | let commit = supplied_commit |
| 57 | .clone() |
| 58 | .or_else(|| { |
| 59 | root.as_deref() |
| 60 | .and_then(|root| git(root, &["rev-parse", "--verify", "HEAD"])) |
| 61 | }) |
| 62 | .unwrap_or_else(|| "unknown".to_string()); |
| 63 | let commit = commit.trim(); |
| 64 | let commit = if commit.len() >= 12 |
| 65 | && commit.len() <= 64 |
| 66 | && commit.bytes().all(|b| b.is_ascii_hexdigit()) |
| 67 | { |
| 68 | &commit[..12] |
| 69 | } else { |
| 70 | assert!( |
| 71 | supplied_commit.is_none(), |
| 72 | "ATOMIC_BUILD_COMMIT must contain a 12 to 64 character hexadecimal commit" |
| 73 | ); |
| 74 | "unknown" |
| 75 | }; |
| 76 | let dirty = if dirty { "-dirty" } else { "" }; |