summary refs log tree commit diff
path: root/tests/run-make/musl-default-linking/rmake.rs
blob: 017444cfcddcbc0a19db3d2ec90b47681f462fbb (plain)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
use run_make_support::{rustc, serde_json};

// Please do NOT add more targets to this list!
// Per https://github.com/rust-lang/compiler-team/issues/422,
// we should be trying to move these targets to dynamically link
// musl libc by default.
static LEGACY_STATIC_LINKING_TARGETS: &[&'static str] = &[
    "aarch64-unknown-linux-musl",
    "arm-unknown-linux-musleabi",
    "arm-unknown-linux-musleabihf",
    "armv5te-unknown-linux-musleabi",
    "armv7-unknown-linux-musleabi",
    "armv7-unknown-linux-musleabihf",
    "i586-unknown-linux-musl",
    "i686-unknown-linux-musl",
    "mips64-unknown-linux-musl",
    "mips64-unknown-linux-muslabi64",
    "mips64el-unknown-linux-muslabi64",
    "powerpc-unknown-linux-musl",
    "powerpc-unknown-linux-muslspe",
    "powerpc64-unknown-linux-musl",
    "powerpc64le-unknown-linux-musl",
    "riscv32gc-unknown-linux-musl",
    "s390x-unknown-linux-musl",
    "thumbv7neon-unknown-linux-musleabihf",
    "x86_64-unknown-linux-musl",
];

fn main() {
    let targets = rustc().print("target-list").run().stdout_utf8();

    for target in targets.lines() {
        let abi = target.split('-').last().unwrap();

        if !abi.starts_with("musl") {
            continue;
        }

        let target_spec_json = rustc()
            .print("target-spec-json")
            .target(target)
            .arg("-Zunstable-options")
            .run()
            .stdout_utf8();

        let target_spec: serde_json::Value =
            serde_json::from_str(&target_spec_json).expect("failed to parse target-spec-json");

        let target_families = &target_spec["target-family"];
        // WebAssembly doesn't support dynamic linking yet; all musl targets
        // need to be statically linked.
        if target_families
            .as_array()
            .expect("target-family wasn't an array")
            .iter()
            .filter_map(|x| x.as_str())
            .any(|family| family == "wasm")
        {
            continue;
        }

        let default = &target_spec["crt-static-default"];
        // If the value is `null`, then the default to dynamically link from
        // musl_base was not overridden.
        if default.is_null() {
            continue;
        }

        if default.as_bool().expect("wasn't a boolean")
            && !LEGACY_STATIC_LINKING_TARGETS.contains(&target)
        {
            panic!("{target} statically links musl libc when it should dynamically link it");
        }
    }
}