Skip to content

Commit

Permalink
Add rustc_link_arg_bin!($bin => $arg) macro equivalent to `cargo:ru…
Browse files Browse the repository at this point in the history
…stc-link-arg-bin=$bin=$arg`
  • Loading branch information
regexident committed Aug 31, 2021
1 parent a29c413 commit e8b614b
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ This crate exposes the following macros:
| [`rustc_env!($key, $value)`] | `cargo:rustc-env=$key=$value` |
| [`rustc_flags!($flags)`] | `cargo:rustc-flags=$flags` |
| [`rustc_link_arg!($arg)`] | `cargo:rustc-link-arg=$arg` |
| [`rustc_link_arg_bin!($bin => $arg)`] | `cargo:rustc-link-arg-bin=$bin=$arg` |
| [`rustc_link_lib!($name => $kind)`] | `cargo:rustc-link-lib=$kind=$name` |
| [`rustc_link_search!($path => $kind)`] | `cargo:rustc-link-search=$kind=$path` |
| [`warning!($message)`] | `cargo:warning=$message` |
Expand All @@ -68,6 +69,7 @@ This crate exposes the following macros:
[`rustc_env!($key, $value)`]: https://docs.rs/cargo-emit/0.1.0/cargo_emit/macro.rustc_env.html
[`rustc_flags!($flags)`]: https://docs.rs/cargo-emit/0.1.0/cargo_emit/macro.rustc_flags.html
[`rustc_link_arg!($arg)`]: https://docs.rs/cargo-emit/0.1.0/cargo_emit/macro.rustc_link_arg.html
[`rustc_link_arg_bin!($bin => $arg)`]: https://docs.rs/cargo-emit/0.1.0/cargo_emit/macro.rustc_link_arg_bin.html
[`rustc_link_lib!($name => $kind)`]: https://docs.rs/cargo-emit/0.1.0/cargo_emit/macro.rustc_link_lib.html
[`rustc_link_search!($path => $kind)`]: https://docs.rs/cargo-emit/0.1.0/cargo_emit/macro.rustc_link_search.html
[`warning!($message)`]: https://docs.rs/cargo-emit/0.1.0/cargo_emit/macro.warning.html
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ mod rustc_cfg;
mod rustc_env;
mod rustc_flags;
mod rustc_link_arg;
mod rustc_link_arg_bin;
mod rustc_link_lib;
mod rustc_link_search;
mod warning;
Expand Down
77 changes: 77 additions & 0 deletions src/rustc_link_arg_bin.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/// Tells Cargo to pass the `-C link-arg=$flag` option to the compiler,
/// but only when building the binary target with name `$bin`.
/// Its usage is highly platform specific.
/// It is useful to set a linker script or other linker options.
///
/// This is equivalent to:
///
/// ```
/// println!("cargo:rustc-link-arg-bin=$bin=$flag");
/// ```
///
/// # Examples
///
/// ```
/// cargo_emit::rustc_link_arg_bin!(
/// "hello_world" => "-Wall"
/// );
/// ```
///
/// or, in case you want it to emit to a custom stream:
///
/// ```
/// let mut stdout = std::io::stdout();
/// cargo_emit::rustc_link_arg_bin!(
/// to: stdout,
/// "hello_world" => "-Wall"
/// );
/// ```
#[macro_export]
macro_rules! rustc_link_arg_bin {
(to: $stream:expr, $bin:expr => $flags:expr $(,)?) => {
$crate::pair!(to: $stream, "rustc-link-arg-bin", "{}={}", $bin, $flags);
};
(to: $stream:expr, $($bin:expr=> $flags:expr),+ $(,)?) => { {
$($crate::rustc_link_arg_bin!(to: $stream, $bin => $flags);)+
} };
($bin:expr => $flags:expr $(,)?) => {
$crate::rustc_link_arg_bin!(to: std::io::stdout(), $bin => $flags);
};
($($bin:expr=> $flags:expr),+ $(,)?) => { {
$crate::rustc_link_arg_bin!(to: std::io::stdout(), $($bin=> $flags),+);
} };
}

#[cfg(test)]
mod tests {
#[test]
fn single() {
insta::assert_display_snapshot!(
crate::capture_output(|output| {
crate::rustc_link_arg_bin!(
to: output,
"BIN" => "FLAGS"
);
}),
@"cargo:rustc-link-arg-bin=BIN=FLAGS
"
);
}

#[test]
fn multiple() {
insta::assert_display_snapshot!(
crate::capture_output(|output| {
crate::rustc_link_arg_bin!(
to: output,
"BIN1" => "FLAGS1",
"BIN2" => "FLAGS2",
);
}),
@r###"
cargo:rustc-link-arg-bin=BIN1=FLAGS1
cargo:rustc-link-arg-bin=BIN2=FLAGS2
"###
);
}
}

0 comments on commit e8b614b

Please sign in to comment.