cargo_emit/
rustc_link_search.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
/// Tells Cargo to pass `$path` to the compiler as a `-L` flag.
///
/// This is equivalent to:
///
/// ```
/// println!("cargo:rustc-link-search=[$kind=]$path");
/// ```
///
/// # Examples
///
/// Useful for telling the linker where a path can be found.
///
/// ```
/// cargo_emit::rustc_link_search!(
///     "path/to/ssl/lib/", // same as `=> "all"`
///     "path/to/ruby/lib/" => "native",
/// );
/// ```
///
/// or, in case you want it to emit to a custom stream:
///
/// ```
/// let mut stdout = std::io::stdout();
/// cargo_emit::rustc_link_search!(
///     to: stdout,
///     "path/to/ssl/lib/", // same as `=> "all"`
///     "path/to/ruby/lib/" => "native",
/// );
/// ```
#[macro_export]
macro_rules! rustc_link_search {
    (to: $stream:expr, $path:expr $(,)?) => {
        $crate::pair!(to: $stream, "rustc-link-search", "{}", $path);
    };
    (to: $stream:expr, $path:expr => $kind:expr $(,)?) => {
        $crate::pair!(to: $stream, "rustc-link-search", "{}={}", $kind, $path);
    };
    (to: $stream:expr, $($path:expr $(=> $kind:expr)?),+ $(,)?) => { {
        $($crate::rustc_link_search!(to: $stream, $path $(=> $kind)?);)+
    } };
    ($path:expr $(,)?) => {
        $crate::rustc_link_search!(to: std::io::stdout(), $path);
    };
    ($path:expr => $kind:expr $(,)?) => {
        $crate::rustc_link_search!(to: std::io::stdout(), $path => $kind);
    };
    ($($path:expr $(=> $kind:expr)?),+ $(,)?) => { {
        $crate::rustc_link_search!(to: std::io::stdout(), $($path $(=> $kind)?),+);
    } };
}

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

    #[test]
    fn single_name_expression() {
        insta::assert_display_snapshot!(
            crate::capture_output(|output| {
                let path = "PATH";
                crate::rustc_link_search!(
                    to: output,
                    path
                );
            }),
            @"cargo:rustc-link-search=PATH\n"
        );
    }

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

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

    #[test]
    fn multiple_name_expression_with_kind() {
        insta::assert_display_snapshot!(
            crate::capture_output(|output| {
                let path2 = "PATH2";
                let kind2 = "KIND2";
                crate::rustc_link_search!(
                    to: output,
                    "PATH1" => "KIND1",
                    path2 => kind2,
                );
            }),
            @"cargo:rustc-link-search=KIND1=PATH1\n\
              cargo:rustc-link-search=KIND2=PATH2\n"
        );
    }
}