|
1 | 1 | //! Tests for -Zextra-link-arg.
|
2 | 2 |
|
3 |
| -use cargo_test_support::{basic_bin_manifest, project}; |
| 3 | +// NOTE: Many of these tests use `without_status()` when passing bogus flags |
| 4 | +// because MSVC link.exe just gives a warning on unknown flags (how helpful!), |
| 5 | +// and other linkers will return an error. |
| 6 | + |
| 7 | +use cargo_test_support::registry::Package; |
| 8 | +use cargo_test_support::{basic_bin_manifest, basic_manifest, project}; |
4 | 9 |
|
5 | 10 | #[cargo_test]
|
6 | 11 | fn build_script_extra_link_arg_bin() {
|
@@ -125,14 +130,16 @@ fn link_arg_missing_target() {
|
125 | 130 | )
|
126 | 131 | .build();
|
127 | 132 |
|
128 |
| - p.cargo("check") |
129 |
| - .with_status(101) |
130 |
| - .with_stderr("\ |
131 |
| -[COMPILING] foo [..] |
132 |
| -error: invalid instruction `cargo:rustc-link-arg-cdylib` from build script of `foo v0.0.1 ([ROOT]/foo)` |
133 |
| -The package foo v0.0.1 ([ROOT]/foo) does not have a cdylib target. |
134 |
| -") |
135 |
| - .run(); |
| 133 | + // TODO: Uncomment this if cdylib restriction is re-added (see |
| 134 | + // cdylib_link_arg_transitive below). |
| 135 | + // p.cargo("check") |
| 136 | + // .with_status(101) |
| 137 | + // .with_stderr("\ |
| 138 | + // [COMPILING] foo [..] |
| 139 | + // error: invalid instruction `cargo:rustc-link-arg-cdylib` from build script of `foo v0.0.1 ([ROOT]/foo)` |
| 140 | + // The package foo v0.0.1 ([ROOT]/foo) does not have a cdylib target. |
| 141 | + // ") |
| 142 | + // .run(); |
136 | 143 |
|
137 | 144 | p.change_file(
|
138 | 145 | "build.rs",
|
@@ -183,3 +190,113 @@ The instruction should have the form cargo:rustc-link-arg-bin=BIN=ARG
|
183 | 190 | )
|
184 | 191 | .run();
|
185 | 192 | }
|
| 193 | + |
| 194 | +#[cargo_test] |
| 195 | +fn cdylib_link_arg_transitive() { |
| 196 | + // There was an unintended regression in 1.50 where rustc-link-arg-cdylib |
| 197 | + // arguments from dependencies were being applied in the parent package. |
| 198 | + // Previously it was silently ignored. |
| 199 | + // See https://github.com/rust-lang/cargo/issues/9562 |
| 200 | + let p = project() |
| 201 | + .file( |
| 202 | + "Cargo.toml", |
| 203 | + r#" |
| 204 | + [package] |
| 205 | + name = "foo" |
| 206 | + version = "0.1.0" |
| 207 | +
|
| 208 | + [lib] |
| 209 | + crate-type = ["cdylib"] |
| 210 | +
|
| 211 | + [dependencies] |
| 212 | + bar = {path="bar"} |
| 213 | + "#, |
| 214 | + ) |
| 215 | + .file("src/lib.rs", "") |
| 216 | + .file("bar/Cargo.toml", &basic_manifest("bar", "1.0.0")) |
| 217 | + .file("bar/src/lib.rs", "") |
| 218 | + .file( |
| 219 | + "bar/build.rs", |
| 220 | + r#" |
| 221 | + fn main() { |
| 222 | + println!("cargo:rustc-link-arg-cdylib=--bogus"); |
| 223 | + } |
| 224 | + "#, |
| 225 | + ) |
| 226 | + .build(); |
| 227 | + p.cargo("build -v") |
| 228 | + .without_status() |
| 229 | + .with_stderr_contains( |
| 230 | + "\ |
| 231 | +[COMPILING] bar v1.0.0 [..] |
| 232 | +[RUNNING] `rustc --crate-name build_script_build bar/build.rs [..] |
| 233 | +[RUNNING] `[..]build-script-build[..] |
| 234 | +warning: cargo:rustc-link-arg-cdylib was specified in the build script of bar v1.0.0 \ |
| 235 | +([ROOT]/foo/bar), but that package does not contain a cdylib target |
| 236 | +
|
| 237 | +Allowing this was an unintended change in the 1.50 release, and may become an error in \ |
| 238 | +the future. For more information, see <https://github.com/rust-lang/cargo/issues/9562>. |
| 239 | +[RUNNING] `rustc --crate-name bar bar/src/lib.rs [..] |
| 240 | +[COMPILING] foo v0.1.0 [..] |
| 241 | +[RUNNING] `rustc --crate-name foo src/lib.rs [..]-C link-arg=--bogus[..]` |
| 242 | +", |
| 243 | + ) |
| 244 | + .run(); |
| 245 | +} |
| 246 | + |
| 247 | +#[cargo_test] |
| 248 | +fn link_arg_transitive_not_allowed() { |
| 249 | + // Verify that transitive dependencies don't pass link args. |
| 250 | + // |
| 251 | + // Note that rustc-link-arg doesn't have any errors or warnings when it is |
| 252 | + // unused. Perhaps that could be more aggressive, but it is difficult |
| 253 | + // since it could be used for test binaries. |
| 254 | + Package::new("bar", "1.0.0") |
| 255 | + .file("src/lib.rs", "") |
| 256 | + .file( |
| 257 | + "build.rs", |
| 258 | + r#" |
| 259 | + fn main() { |
| 260 | + println!("cargo:rustc-link-arg=--bogus"); |
| 261 | + } |
| 262 | + "#, |
| 263 | + ) |
| 264 | + .publish(); |
| 265 | + |
| 266 | + let p = project() |
| 267 | + .file( |
| 268 | + "Cargo.toml", |
| 269 | + r#" |
| 270 | + [package] |
| 271 | + name = "foo" |
| 272 | + version = "0.1.0" |
| 273 | +
|
| 274 | + [lib] |
| 275 | + crate-type = ["cdylib"] |
| 276 | +
|
| 277 | + [dependencies] |
| 278 | + bar = "1.0" |
| 279 | + "#, |
| 280 | + ) |
| 281 | + .file("src/lib.rs", "") |
| 282 | + .build(); |
| 283 | + |
| 284 | + p.cargo("build -v -Zextra-link-arg") |
| 285 | + .masquerade_as_nightly_cargo() |
| 286 | + .with_stderr( |
| 287 | + "\ |
| 288 | +[UPDATING] [..] |
| 289 | +[DOWNLOADING] [..] |
| 290 | +[DOWNLOADED] [..] |
| 291 | +[COMPILING] bar v1.0.0 |
| 292 | +[RUNNING] `rustc --crate-name build_script_build [..] |
| 293 | +[RUNNING] `[..]/build-script-build[..] |
| 294 | +[RUNNING] `rustc --crate-name bar [..] |
| 295 | +[COMPILING] foo v0.1.0 [..] |
| 296 | +[RUNNING] `rustc --crate-name foo src/lib.rs [..] |
| 297 | +[FINISHED] dev [..] |
| 298 | +", |
| 299 | + ) |
| 300 | + .with_stderr_does_not_contain("--bogus") |
| 301 | + .run(); |
| 302 | +} |
0 commit comments