run_make_support/
artifact_names.rs

1//! A collection of helpers to construct artifact names, such as names of dynamic or static
2//! librarys which are target-dependent.
3
4// FIXME(jieyouxu): convert these to return `PathBuf`s instead of strings!
5
6use crate::targets::is_msvc;
7
8/// Construct the static library name based on the target.
9#[must_use]
10pub fn static_lib_name(name: &str) -> String {
11    // See tools.mk (irrelevant lines omitted):
12    //
13    // ```makefile
14    // ifeq ($(UNAME),Darwin)
15    //     STATICLIB = $(TMPDIR)/lib$(1).a
16    // else
17    //     ifdef IS_WINDOWS
18    //         ifdef IS_MSVC
19    //             STATICLIB = $(TMPDIR)/$(1).lib
20    //         else
21    //             STATICLIB = $(TMPDIR)/lib$(1).a
22    //         endif
23    //     else
24    //         STATICLIB = $(TMPDIR)/lib$(1).a
25    //     endif
26    // endif
27    // ```
28    assert!(!name.contains(char::is_whitespace), "static library name cannot contain whitespace");
29
30    if is_msvc() { format!("{name}.lib") } else { format!("lib{name}.a") }
31}
32
33/// Construct the dynamic library name based on the target.
34#[must_use]
35pub fn dynamic_lib_name(name: &str) -> String {
36    assert!(!name.contains(char::is_whitespace), "dynamic library name cannot contain whitespace");
37
38    format!("{}{name}.{}", std::env::consts::DLL_PREFIX, std::env::consts::DLL_EXTENSION)
39}
40
41/// Construct the name of the import library for the dynamic library, exclusive to MSVC and
42/// accepted by link.exe.
43#[track_caller]
44#[must_use]
45pub fn msvc_import_dynamic_lib_name(name: &str) -> String {
46    assert!(is_msvc(), "this function is exclusive to MSVC");
47    assert!(!name.contains(char::is_whitespace), "import library name cannot contain whitespace");
48
49    format!("{name}.dll.lib")
50}
51
52/// Construct the dynamic library extension based on the target.
53#[must_use]
54pub fn dynamic_lib_extension() -> &'static str {
55    std::env::consts::DLL_EXTENSION
56}
57
58/// Construct the name of a rust library (rlib).
59#[must_use]
60pub fn rust_lib_name(name: &str) -> String {
61    format!("lib{name}.rlib")
62}
63
64/// Construct the binary (executable) name based on the target.
65#[must_use]
66pub fn bin_name(name: &str) -> String {
67    format!("{name}{}", std::env::consts::EXE_SUFFIX)
68}