cargo_emit/
rustc_link_lib.rs

1/// Tells Cargo to pass `$lib` to the compiler as a `-l` flag.
2///
3/// This is equivalent to:
4///
5/// ```
6/// println!("cargo:rustc-link-lib=[$kind=]$name");
7/// ```
8///
9/// # Examples
10///
11/// Useful for telling the linker what libraries should be linked.
12///
13/// ```
14/// cargo_emit::rustc_link_lib!(
15///     "ssl", // same as `=> "dylib"`
16///     "ruby" => "static",
17///     "CoreFoundation" => "framework",
18/// );
19/// ```
20///
21/// or, in case you want it to emit to a custom stream:
22///
23/// ```
24/// let mut stdout = std::io::stdout();
25/// cargo_emit::rustc_link_lib!(
26///     to: stdout,
27///     "ssl", // same as `=> "dylib"`
28///     "ruby" => "static",
29///     "CoreFoundation" => "framework",
30/// );
31/// ```
32#[macro_export]
33macro_rules! rustc_link_lib {
34    (to: $stream:expr, $name:expr $(,)?) => {
35        $crate::pair!(to: $stream, "rustc-link-lib", "{}", $name);
36    };
37    (to: $stream:expr, $name:expr => $kind:expr $(,)?) => {
38        $crate::pair!(to: $stream, "rustc-link-lib", "{}={}", $kind, $name);
39    };
40    (to: $stream:expr, $($name:expr $(=> $kind:expr)?),+ $(,)?) => { {
41        $($crate::rustc_link_lib!(to: $stream, $name $(=> $kind)?);)+
42    } };
43    ($name:expr $(,)?) => {
44        $crate::rustc_link_lib!(to: std::io::stdout(), $name);
45    };
46    ($name:expr => $kind:expr $(,)?) => {
47        $crate::rustc_link_lib!(to: std::io::stdout(), $name => $kind);
48    };
49    ($($name:expr $(=> $kind:expr)?),+ $(,)?) => { {
50        $crate::rustc_link_lib!(to: std::io::stdout(), $($name $(=> $kind)?),+);
51    } };
52}
53
54#[cfg(test)]
55mod tests {
56    #[test]
57    fn single_name_literal() {
58        insta::assert_display_snapshot!(
59            crate::capture_output(|output| {
60                crate::rustc_link_lib!(
61                    to: output,
62                    "NAME"
63                );
64            }),
65            @"cargo:rustc-link-lib=NAME\n"
66        );
67    }
68
69    #[test]
70    fn single_name_expression() {
71        insta::assert_display_snapshot!(
72            crate::capture_output(|output| {
73                let name = "NAME";
74                crate::rustc_link_lib!(
75                    to: output,
76                    name
77                );
78            }),
79            @"cargo:rustc-link-lib=NAME\n"
80        );
81    }
82
83    #[test]
84    fn single_name_literal_with_kind() {
85        insta::assert_display_snapshot!(
86            crate::capture_output(|output| {
87                crate::rustc_link_lib!(
88                    to: output,
89                    "NAME" => "KIND"
90                );
91            }),
92            @"cargo:rustc-link-lib=KIND=NAME\n"
93        );
94    }
95
96    #[test]
97    fn single_name_expression_with_kind() {
98        insta::assert_display_snapshot!(
99            crate::capture_output(|output| {
100                let name = "NAME";
101                let kind = "KIND";
102                crate::rustc_link_lib!(
103                    to: output,
104                    name => kind
105                );
106            }),
107            @"cargo:rustc-link-lib=KIND=NAME\n"
108        );
109    }
110
111    #[test]
112    fn multiple_name_expression_with_kind() {
113        insta::assert_display_snapshot!(
114            crate::capture_output(|output| {
115                let name2 = "NAME2";
116                let kind2 = "KIND2";
117                crate::rustc_link_lib!(
118                    to: output,
119                    "NAME1" => "KIND1",
120                    name2 => kind2,
121                );
122            }),
123            @"cargo:rustc-link-lib=KIND1=NAME1\n\
124              cargo:rustc-link-lib=KIND2=NAME2\n"
125        );
126    }
127}