cargo_emit/
rustc_link_lib.rs

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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/// Tells Cargo to pass `$lib` to the compiler as a `-l` flag.
///
/// This is equivalent to:
///
/// ```
/// println!("cargo:rustc-link-lib=[$kind=]$name");
/// ```
///
/// # Examples
///
/// Useful for telling the linker what libraries should be linked.
///
/// ```
/// cargo_emit::rustc_link_lib!(
///     "ssl", // same as `=> "dylib"`
///     "ruby" => "static",
///     "CoreFoundation" => "framework",
/// );
/// ```
///
/// or, in case you want it to emit to a custom stream:
///
/// ```
/// let mut stdout = std::io::stdout();
/// cargo_emit::rustc_link_lib!(
///     to: stdout,
///     "ssl", // same as `=> "dylib"`
///     "ruby" => "static",
///     "CoreFoundation" => "framework",
/// );
/// ```
#[macro_export]
macro_rules! rustc_link_lib {
    (to: $stream:expr, $name:expr $(,)?) => {
        $crate::pair!(to: $stream, "rustc-link-lib", "{}", $name);
    };
    (to: $stream:expr, $name:expr => $kind:expr $(,)?) => {
        $crate::pair!(to: $stream, "rustc-link-lib", "{}={}", $kind, $name);
    };
    (to: $stream:expr, $($name:expr $(=> $kind:expr)?),+ $(,)?) => { {
        $($crate::rustc_link_lib!(to: $stream, $name $(=> $kind)?);)+
    } };
    ($name:expr $(,)?) => {
        $crate::rustc_link_lib!(to: std::io::stdout(), $name);
    };
    ($name:expr => $kind:expr $(,)?) => {
        $crate::rustc_link_lib!(to: std::io::stdout(), $name => $kind);
    };
    ($($name:expr $(=> $kind:expr)?),+ $(,)?) => { {
        $crate::rustc_link_lib!(to: std::io::stdout(), $($name $(=> $kind)?),+);
    } };
}

#[cfg(test)]
mod tests {
    #[test]
    fn single_name_literal() {
        insta::assert_display_snapshot!(
            crate::capture_output(|output| {
                crate::rustc_link_lib!(
                    to: output,
                    "NAME"
                );
            }),
            @"cargo:rustc-link-lib=NAME\n"
        );
    }

    #[test]
    fn single_name_expression() {
        insta::assert_display_snapshot!(
            crate::capture_output(|output| {
                let name = "NAME";
                crate::rustc_link_lib!(
                    to: output,
                    name
                );
            }),
            @"cargo:rustc-link-lib=NAME\n"
        );
    }

    #[test]
    fn single_name_literal_with_kind() {
        insta::assert_display_snapshot!(
            crate::capture_output(|output| {
                crate::rustc_link_lib!(
                    to: output,
                    "NAME" => "KIND"
                );
            }),
            @"cargo:rustc-link-lib=KIND=NAME\n"
        );
    }

    #[test]
    fn single_name_expression_with_kind() {
        insta::assert_display_snapshot!(
            crate::capture_output(|output| {
                let name = "NAME";
                let kind = "KIND";
                crate::rustc_link_lib!(
                    to: output,
                    name => kind
                );
            }),
            @"cargo:rustc-link-lib=KIND=NAME\n"
        );
    }

    #[test]
    fn multiple_name_expression_with_kind() {
        insta::assert_display_snapshot!(
            crate::capture_output(|output| {
                let name2 = "NAME2";
                let kind2 = "KIND2";
                crate::rustc_link_lib!(
                    to: output,
                    "NAME1" => "KIND1",
                    name2 => kind2,
                );
            }),
            @"cargo:rustc-link-lib=KIND1=NAME1\n\
              cargo:rustc-link-lib=KIND2=NAME2\n"
        );
    }
}