|
31 | 31 | //! [`CompileMode::RunCustomBuild`]: super::CompileMode
|
32 | 32 | //! [instructions]: https://doc.rust-lang.org/cargo/reference/build-scripts.html#outputs-of-the-build-script
|
33 | 33 |
|
34 |
| -use super::{fingerprint, Context, Job, LinkType, Unit, Work}; |
| 34 | +use super::{fingerprint, Context, Job, Unit, Work}; |
35 | 35 | use crate::core::compiler::artifact;
|
36 | 36 | use crate::core::compiler::context::Metadata;
|
37 | 37 | use crate::core::compiler::job_queue::JobState;
|
@@ -62,7 +62,7 @@ pub struct BuildOutput {
|
62 | 62 | /// Names and link kinds of libraries, suitable for the `-l` flag.
|
63 | 63 | pub library_links: Vec<String>,
|
64 | 64 | /// Linker arguments suitable to be passed to `-C link-arg=<args>`
|
65 |
| - pub linker_args: Vec<(LinkType, String)>, |
| 65 | + pub linker_args: Vec<(ExtraLinkArgFor, String)>, |
66 | 66 | /// Various `--cfg` flags to pass to the compiler.
|
67 | 67 | pub cfgs: Vec<String>,
|
68 | 68 | /// Various `--check-cfg` flags to pass to the compiler.
|
@@ -146,6 +146,47 @@ pub struct BuildDeps {
|
146 | 146 | pub rerun_if_env_changed: Vec<String>,
|
147 | 147 | }
|
148 | 148 |
|
| 149 | +/// Represents one of the instructions from `cargo:rustc-link-arg-*` build |
| 150 | +/// script instruction family. |
| 151 | +/// |
| 152 | +/// In other words, indicates targets that custom linker arguments applies to. |
| 153 | +/// |
| 154 | +/// See the [build script documentation][1] for more. |
| 155 | +/// |
| 156 | +/// [1]: https://doc.rust-lang.org/nightly/cargo/reference/build-scripts.html#cargorustc-link-argflag |
| 157 | +#[derive(Clone, Hash, Debug, PartialEq, Eq)] |
| 158 | +pub enum ExtraLinkArgFor { |
| 159 | + /// Represents `cargo:rustc-link-arg=FLAG`. |
| 160 | + All, |
| 161 | + /// Represents `cargo:rustc-cdylib-link-arg=FLAG`. |
| 162 | + Cdylib, |
| 163 | + /// Represents `cargo:rustc-link-arg-bins=FLAG`. |
| 164 | + Bin, |
| 165 | + /// Represents `cargo:rustc-link-arg-bin=BIN=FLAG`. |
| 166 | + SingleBin(String), |
| 167 | + /// Represents `cargo:rustc-link-arg-tests=FLAG`. |
| 168 | + Test, |
| 169 | + /// Represents `cargo:rustc-link-arg-benches=FLAG`. |
| 170 | + Bench, |
| 171 | + /// Represents `cargo:rustc-link-arg-examples=FLAG`. |
| 172 | + Example, |
| 173 | +} |
| 174 | + |
| 175 | +impl ExtraLinkArgFor { |
| 176 | + /// Checks if this link type applies to a given [`Target`]. |
| 177 | + pub fn applies_to(&self, target: &Target) -> bool { |
| 178 | + match self { |
| 179 | + ExtraLinkArgFor::All => true, |
| 180 | + ExtraLinkArgFor::Cdylib => target.is_cdylib(), |
| 181 | + ExtraLinkArgFor::Bin => target.is_bin(), |
| 182 | + ExtraLinkArgFor::SingleBin(name) => target.is_bin() && target.name() == name, |
| 183 | + ExtraLinkArgFor::Test => target.is_test(), |
| 184 | + ExtraLinkArgFor::Bench => target.is_bench(), |
| 185 | + ExtraLinkArgFor::Example => target.is_exe_example(), |
| 186 | + } |
| 187 | + } |
| 188 | +} |
| 189 | + |
149 | 190 | /// Prepares a `Work` that executes the target as a custom build script.
|
150 | 191 | pub fn prepare(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Job> {
|
151 | 192 | let _p = profile::start(format!(
|
@@ -711,10 +752,10 @@ impl BuildOutput {
|
711 | 752 | key, pkg_descr
|
712 | 753 | ));
|
713 | 754 | }
|
714 |
| - linker_args.push((LinkType::Cdylib, value)) |
| 755 | + linker_args.push((ExtraLinkArgFor::Cdylib, value)) |
715 | 756 | }
|
716 | 757 | "rustc-link-arg-bins" => {
|
717 |
| - check_and_add_target!("bin", Target::is_bin, LinkType::Bin); |
| 758 | + check_and_add_target!("bin", Target::is_bin, ExtraLinkArgFor::Bin); |
718 | 759 | }
|
719 | 760 | "rustc-link-arg-bin" => {
|
720 | 761 | let mut parts = value.splitn(2, '=');
|
@@ -742,19 +783,19 @@ impl BuildOutput {
|
742 | 783 | bin_name
|
743 | 784 | );
|
744 | 785 | }
|
745 |
| - linker_args.push((LinkType::SingleBin(bin_name), arg.to_string())); |
| 786 | + linker_args.push((ExtraLinkArgFor::SingleBin(bin_name), arg.to_string())); |
746 | 787 | }
|
747 | 788 | "rustc-link-arg-tests" => {
|
748 |
| - check_and_add_target!("test", Target::is_test, LinkType::Test); |
| 789 | + check_and_add_target!("test", Target::is_test, ExtraLinkArgFor::Test); |
749 | 790 | }
|
750 | 791 | "rustc-link-arg-benches" => {
|
751 |
| - check_and_add_target!("benchmark", Target::is_bench, LinkType::Bench); |
| 792 | + check_and_add_target!("benchmark", Target::is_bench, ExtraLinkArgFor::Bench); |
752 | 793 | }
|
753 | 794 | "rustc-link-arg-examples" => {
|
754 |
| - check_and_add_target!("example", Target::is_example, LinkType::Example); |
| 795 | + check_and_add_target!("example", Target::is_example, ExtraLinkArgFor::Example); |
755 | 796 | }
|
756 | 797 | "rustc-link-arg" => {
|
757 |
| - linker_args.push((LinkType::All, value)); |
| 798 | + linker_args.push((ExtraLinkArgFor::All, value)); |
758 | 799 | }
|
759 | 800 | "rustc-cfg" => cfgs.push(value.to_string()),
|
760 | 801 | "rustc-check-cfg" => {
|
|
0 commit comments