run_make_support/
artifact_names.rs

1//! A collection of helpers to construct artifact names, such as names of dynamic or static
2//! libraries which are target-dependent.
3
4use crate::target;
5use crate::targets::is_msvc;
6
7/// Construct the static library name based on the target.
8#[track_caller]
9#[must_use]
10pub fn static_lib_name(name: &str) -> String {
11    assert!(!name.contains(char::is_whitespace), "static library name cannot contain whitespace");
12
13    if is_msvc() { format!("{name}.lib") } else { format!("lib{name}.a") }
14}
15
16/// Construct the dynamic library name based on the target.
17#[track_caller]
18#[must_use]
19pub fn dynamic_lib_name(name: &str) -> String {
20    assert!(!name.contains(char::is_whitespace), "dynamic library name cannot contain whitespace");
21
22    format!("{}{name}.{}", dynamic_lib_prefix(), dynamic_lib_extension())
23}
24
25fn dynamic_lib_prefix() -> &'static str {
26    if target().contains("windows") { "" } else { "lib" }
27}
28
29/// Construct the dynamic library extension based on the target.
30#[must_use]
31pub fn dynamic_lib_extension() -> &'static str {
32    let target = target();
33
34    if target.contains("apple") {
35        "dylib"
36    } else if target.contains("windows") {
37        "dll"
38    } else {
39        "so"
40    }
41}
42
43/// Construct the name of the import library for the dynamic library, exclusive to MSVC and accepted
44/// by link.exe.
45#[track_caller]
46#[must_use]
47pub fn msvc_import_dynamic_lib_name(name: &str) -> String {
48    assert!(is_msvc(), "this function is exclusive to MSVC");
49    assert!(!name.contains(char::is_whitespace), "import library name cannot contain whitespace");
50
51    format!("{name}.dll.lib")
52}
53
54/// Construct the name of a rust library (rlib).
55#[track_caller]
56#[must_use]
57pub fn rust_lib_name(name: &str) -> String {
58    format!("lib{name}.rlib")
59}
60
61/// Construct the binary (executable) name based on the target.
62#[track_caller]
63#[must_use]
64pub fn bin_name(name: &str) -> String {
65    let target = target();
66
67    if target.contains("windows") {
68        format!("{name}.exe")
69    } else if target.contains("uefi") {
70        format!("{name}.efi")
71    } else if target.contains("wasm") {
72        format!("{name}.wasm")
73    } else if target.contains("nvptx") {
74        format!("{name}.ptx")
75    } else {
76        name.to_string()
77    }
78}