cargo_emit/
rustc_link_arg_bin.rs

1/// Tells Cargo to pass the `-C link-arg=$flag` option to the compiler,
2/// but only when building the binary target with name `$bin`.
3/// Its usage is highly platform specific.
4/// It is useful to set a linker script or other linker options.
5///
6/// This is equivalent to:
7///
8/// ```
9/// println!("cargo:rustc-link-arg-bin=$bin=$flag");
10/// ```
11///
12/// # Examples
13///
14/// ```
15/// cargo_emit::rustc_link_arg_bin!(
16///     "hello_world" => "-Wall"
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_arg_bin!(
25///     to: stdout,
26///     "hello_world" => "-Wall"
27/// );
28/// ```
29#[macro_export]
30macro_rules! rustc_link_arg_bin {
31    (to: $stream:expr, $bin:expr => $flags:expr $(,)?) => {
32        $crate::pair!(to: $stream, "rustc-link-arg-bin", "{}={}", $bin, $flags);
33    };
34    (to: $stream:expr, $($bin:expr=> $flags:expr),+ $(,)?) => { {
35        $($crate::rustc_link_arg_bin!(to: $stream, $bin => $flags);)+
36    } };
37    ($bin:expr => $flags:expr $(,)?) => {
38        $crate::rustc_link_arg_bin!(to: std::io::stdout(), $bin => $flags);
39    };
40    ($($bin:expr=> $flags:expr),+ $(,)?) => { {
41        $crate::rustc_link_arg_bin!(to: std::io::stdout(), $($bin=> $flags),+);
42    } };
43}
44
45#[cfg(test)]
46mod tests {
47    #[test]
48    fn single() {
49        insta::assert_display_snapshot!(
50            crate::capture_output(|output| {
51                crate::rustc_link_arg_bin!(
52                    to: output,
53                    "BIN" => "FLAGS"
54                );
55            }),
56            @"cargo:rustc-link-arg-bin=BIN=FLAGS
57        "
58        );
59    }
60
61    #[test]
62    fn multiple() {
63        insta::assert_display_snapshot!(
64            crate::capture_output(|output| {
65                crate::rustc_link_arg_bin!(
66                    to: output,
67                    "BIN1" => "FLAGS1",
68                    "BIN2" => "FLAGS2",
69                );
70            }),
71            @r###"
72        cargo:rustc-link-arg-bin=BIN1=FLAGS1
73        cargo:rustc-link-arg-bin=BIN2=FLAGS2
74        "###
75        );
76    }
77}