File: build.rs

package info (click to toggle)
rustc 1.85.0%2Bdfsg3-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, sid, trixie
  • size: 893,396 kB
  • sloc: xml: 158,127; python: 35,830; javascript: 19,497; cpp: 19,002; sh: 17,245; ansic: 13,127; asm: 4,376; makefile: 1,051; perl: 29; lisp: 29; ruby: 19; sql: 11
file content (44 lines) | stat: -rw-r--r-- 2,011 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// HACK(eddyb) easier dep-tracking if we let `rustc` do it.
const SRC_LIB_RS_CONTENTS: &str = include_str!("src/lib.rs");

const EXPECTED_SRC_LIB_RS_PREFIX: &str = "\
//! Port of LLVM's APFloat software floating-point implementation from the
//! following C++ sources (please update commit hash when backporting):
//! <https://github.com/llvm/llvm-project/commit/";

fn main() {
    // HACK(eddyb) disable the default of re-running the build script on *any*
    // change to *the entire source tree* (i.e. the default is roughly `./`).
    println!("cargo:rerun-if-changed=build.rs");

    let llvm_commit_hash = SRC_LIB_RS_CONTENTS
        .strip_prefix(EXPECTED_SRC_LIB_RS_PREFIX)
        .ok_or(())
        .map_err(|_| format!("expected `src/lib.rs` to start with:\n\n{EXPECTED_SRC_LIB_RS_PREFIX}"))
        .and_then(|commit_hash_plus_rest_of_file| {
            commit_hash_plus_rest_of_file
                .split_once('\n')
                .ok_or("expected `src/lib.rs` to have more than 3 lines")?
                .0
                .strip_suffix(">")
                .ok_or("expected trailing hyperlink anchor `>`".into())
        })
        .and_then(|commit_hash| {
            if commit_hash.len() != 40 || !commit_hash.chars().all(|c| matches!(c, '0'..='9'|'a'..='f')) {
                Err(format!("expected `src/lib.rs` to have a valid commit hash, found {commit_hash:?}"))
            } else {
                Ok(commit_hash)
            }
        })
        .unwrap_or_else(|e| {
            eprintln!("\n{e}\n");
            panic!("failed to validate `src/lib.rs`'s commit hash (see above)")
        });

    let expected_version_metadata = format!("+llvm-{}", &llvm_commit_hash[..12]);
    let actual_version = env!("CARGO_PKG_VERSION");
    if !actual_version.ends_with(&expected_version_metadata) {
        eprintln!("\nexpected version ending in `{expected_version_metadata}`, found `{actual_version}`\n");
        panic!("failed to validate Cargo package version (see above)");
    }
}