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_windows_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_windows_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 if target.contains("aix") {
39 "a"
40 } else {
41 "so"
42 }
43}
44
45/// Construct the name of the import library for the dynamic library, exclusive to MSVC and accepted
46/// by link.exe.
47#[track_caller]
48#[must_use]
49pub fn msvc_import_dynamic_lib_name(name: &str) -> String {
50 assert!(is_windows_msvc(), "this function is exclusive to MSVC");
51 assert!(!name.contains(char::is_whitespace), "import library name cannot contain whitespace");
52
53 format!("{name}.dll.lib")
54}
55
56/// Construct the name of a rust library (rlib).
57#[track_caller]
58#[must_use]
59pub fn rust_lib_name(name: &str) -> String {
60 format!("lib{name}.rlib")
61}
62
63/// Construct the binary (executable) name based on the target.
64#[track_caller]
65#[must_use]
66pub fn bin_name(name: &str) -> String {
67 let target = target();
68
69 if target.contains("windows") {
70 format!("{name}.exe")
71 } else if target.contains("uefi") {
72 format!("{name}.efi")
73 } else if target.contains("wasm") {
74 format!("{name}.wasm")
75 } else if target.contains("nvptx") {
76 format!("{name}.ptx")
77 } else {
78 name.to_string()
79 }
80}