cargo_emit/
rustc_link_search.rs

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