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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
|
//! Tests for build.rs rerun-if-env-changed and rustc-env
use cargo_test_support::basic_manifest;
use cargo_test_support::prelude::*;
use cargo_test_support::project;
use cargo_test_support::sleep_ms;
use cargo_test_support::str;
#[cargo_test]
fn rerun_if_env_changes() {
let p = project()
.file("src/main.rs", "fn main() {}")
.file(
"build.rs",
r#"
fn main() {
println!("cargo::rerun-if-env-changed=FOO");
}
"#,
)
.build();
p.cargo("check")
.with_stderr_data(str![[r#"
[COMPILING] foo v0.0.1 ([ROOT]/foo)
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
"#]])
.run();
p.cargo("check")
.env("FOO", "bar")
.with_stderr_data(str![[r#"
[COMPILING] foo v0.0.1 ([ROOT]/foo)
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
"#]])
.run();
p.cargo("check")
.env("FOO", "baz")
.with_stderr_data(str![[r#"
[COMPILING] foo v0.0.1 ([ROOT]/foo)
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
"#]])
.run();
p.cargo("check")
.env("FOO", "baz")
.with_stderr_data(str![[r#"
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
"#]])
.run();
p.cargo("check")
.with_stderr_data(str![[r#"
[COMPILING] foo v0.0.1 ([ROOT]/foo)
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
"#]])
.run();
}
#[cargo_test]
fn rerun_if_env_or_file_changes() {
let p = project()
.file("src/main.rs", "fn main() {}")
.file(
"build.rs",
r#"
fn main() {
println!("cargo::rerun-if-env-changed=FOO");
println!("cargo::rerun-if-changed=foo");
}
"#,
)
.file("foo", "")
.build();
p.cargo("check")
.with_stderr_data(str![[r#"
[COMPILING] foo v0.0.1 ([ROOT]/foo)
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
"#]])
.run();
p.cargo("check")
.env("FOO", "bar")
.with_stderr_data(str![[r#"
[COMPILING] foo v0.0.1 ([ROOT]/foo)
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
"#]])
.run();
p.cargo("check")
.env("FOO", "bar")
.with_stderr_data(str![[r#"
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
"#]])
.run();
sleep_ms(1000);
p.change_file("foo", "// modified");
p.cargo("check")
.env("FOO", "bar")
.with_stderr_data(str![[r#"
[COMPILING] foo v0.0.1 ([ROOT]/foo)
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
"#]])
.run();
}
#[cargo_test]
fn rustc_bootstrap() {
let build_rs = r#"
fn main() {
println!("cargo::rustc-env=RUSTC_BOOTSTRAP=1");
}
"#;
let p = project()
.file("Cargo.toml", &basic_manifest("has-dashes", "0.0.1"))
.file(
"src/lib.rs",
"#![allow(internal_features)] #![feature(rustc_attrs)]",
)
.file("build.rs", build_rs)
.build();
// RUSTC_BOOTSTRAP unset on stable should error
p.cargo("check")
.with_stderr_data(str![[r#"
[COMPILING] has-dashes v0.0.1 ([ROOT]/foo)
[ERROR] Cannot set `RUSTC_BOOTSTRAP=1` from build script of `has-dashes v0.0.1 ([ROOT]/foo)`.
[NOTE] Crates cannot set `RUSTC_BOOTSTRAP` themselves, as doing so would subvert the stability guarantees of Rust for your project.
[HELP] If you're sure you want to do this in your project, set the environment variable `RUSTC_BOOTSTRAP=has_dashes` before running cargo instead.
"#]])
.with_status(101)
.run();
// nightly should warn whether or not RUSTC_BOOTSTRAP is set
p.cargo("check")
.masquerade_as_nightly_cargo(&["RUSTC_BOOTSTRAP"])
// NOTE: uses RUSTC_BOOTSTRAP so it will be propagated to rustc
// (this matters when tests are being run with a beta or stable cargo)
.env("RUSTC_BOOTSTRAP", "1")
.with_stderr_data(str![[r#"
[COMPILING] has-dashes v0.0.1 ([ROOT]/foo)
[WARNING] has-dashes@0.0.1: Cannot set `RUSTC_BOOTSTRAP=1` from build script of `has-dashes v0.0.1 ([ROOT]/foo)`.
[NOTE] Crates cannot set `RUSTC_BOOTSTRAP` themselves, as doing so would subvert the stability guarantees of Rust for your project.
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
"#]])
.run();
// RUSTC_BOOTSTRAP set to the name of the library should warn
p.cargo("check")
.env("RUSTC_BOOTSTRAP", "has_dashes")
.with_stderr_data(str![[r#"
[WARNING] has-dashes@0.0.1: Cannot set `RUSTC_BOOTSTRAP=1` from build script of `has-dashes v0.0.1 ([ROOT]/foo)`.
[NOTE] Crates cannot set `RUSTC_BOOTSTRAP` themselves, as doing so would subvert the stability guarantees of Rust for your project.
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
"#]])
.run();
// RUSTC_BOOTSTRAP set to some random value should error
p.cargo("check")
.env("RUSTC_BOOTSTRAP", "bar")
.with_stderr_data(str![[r#"
[ERROR] Cannot set `RUSTC_BOOTSTRAP=1` from build script of `has-dashes v0.0.1 ([ROOT]/foo)`.
[NOTE] Crates cannot set `RUSTC_BOOTSTRAP` themselves, as doing so would subvert the stability guarantees of Rust for your project.
[HELP] If you're sure you want to do this in your project, set the environment variable `RUSTC_BOOTSTRAP=has_dashes` before running cargo instead.
"#]])
.with_status(101)
.run();
// Tests for binaries instead of libraries
let p = project()
.file("Cargo.toml", &basic_manifest("foo", "0.0.1"))
.file(
"src/main.rs",
"#![allow(internal_features)] #![feature(rustc_attrs)] fn main() {}",
)
.file("build.rs", build_rs)
.build();
// nightly should warn when there's no library whether or not RUSTC_BOOTSTRAP is set
p.cargo("check")
.masquerade_as_nightly_cargo(&["RUSTC_BOOTSTRAP"])
// NOTE: uses RUSTC_BOOTSTRAP so it will be propagated to rustc
// (this matters when tests are being run with a beta or stable cargo)
.env("RUSTC_BOOTSTRAP", "1")
.with_stderr_data(str![[r#"
[COMPILING] foo v0.0.1 ([ROOT]/foo)
[WARNING] foo@0.0.1: Cannot set `RUSTC_BOOTSTRAP=1` from build script of `foo v0.0.1 ([ROOT]/foo)`.
[NOTE] Crates cannot set `RUSTC_BOOTSTRAP` themselves, as doing so would subvert the stability guarantees of Rust for your project.
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
"#]])
.run();
// RUSTC_BOOTSTRAP conditionally set when there's no library should error (regardless of the value)
p.cargo("check")
.env("RUSTC_BOOTSTRAP", "foo")
.with_stderr_data(str![[r#"
[ERROR] Cannot set `RUSTC_BOOTSTRAP=1` from build script of `foo v0.0.1 ([ROOT]/foo)`.
[NOTE] Crates cannot set `RUSTC_BOOTSTRAP` themselves, as doing so would subvert the stability guarantees of Rust for your project.
[HELP] If you're sure you want to do this in your project, set the environment variable `RUSTC_BOOTSTRAP=1` before running cargo instead.
"#]])
.with_status(101)
.run();
}
#[cargo_test]
fn build_script_env_verbose() {
let build_rs = r#"
fn main() {}
"#;
let p = project()
.file("Cargo.toml", &basic_manifest("verbose-build", "0.0.1"))
.file("src/lib.rs", "")
.file("build.rs", build_rs)
.build();
p.cargo("check -vv")
.with_stderr_data(
"\
...
[RUNNING] `[..]CARGO=[..]build-script-build`
...",
)
.run();
}
#[cargo_test]
#[cfg(target_arch = "x86_64")]
fn build_script_sees_cfg_target_feature() {
let build_rs = r#"
fn main() {
let cfg = std::env::var("CARGO_CFG_TARGET_FEATURE").unwrap();
eprintln!("CARGO_CFG_TARGET_FEATURE={cfg}");
}
"#;
let configs = [
r#"
[build]
rustflags = ["-Ctarget-feature=+sse4.1,+sse4.2"]
"#,
r#"
[target.'cfg(target_arch = "x86_64")']
rustflags = ["-Ctarget-feature=+sse4.1,+sse4.2"]
"#,
];
for config in configs {
let p = project()
.file(".cargo/config.toml", config)
.file("src/lib.rs", r#""#)
.file("build.rs", build_rs)
.build();
p.cargo("check -vv")
.with_stderr_data(
"\
...
[foo 0.0.1] CARGO_CFG_TARGET_FEATURE=[..]sse4.2[..]
...
[..]-Ctarget-feature=[..]+sse4.2[..]
...",
)
.run();
}
}
/// In this test, the cfg is self-contradictory. There's no *right* answer as to
/// what the value of `RUSTFLAGS` should be in this case. We chose to give a
/// warning. However, no matter what we do, it's important that build scripts
/// and rustc see a consistent picture
#[cargo_test]
fn cfg_paradox() {
let build_rs = r#"
fn main() {
let cfg = std::env::var("CARGO_CFG_BERTRAND").is_ok();
eprintln!("cfg!(bertrand)={cfg}");
}
"#;
let config = r#"
[target.'cfg(not(bertrand))']
rustflags = ["--cfg=bertrand"]
"#;
let p = project()
.file(".cargo/config.toml", config)
.file("src/lib.rs", r#""#)
.file("build.rs", build_rs)
.build();
p.cargo("check -vv")
.with_stderr_data(
"\
[WARNING] non-trivial mutual dependency between target-specific configuration and RUSTFLAGS
...
[foo 0.0.1] cfg!(bertrand)=true
...
[..]--cfg=bertrand[..]
...",
)
.run();
}
/// This test checks how Cargo handles rustc cfgs which are defined both with
/// and without a value. The expected behavior is that the environment variable
/// is going to contain all the values.
///
/// For example, this configuration:
/// ```
/// target_has_atomic
/// target_has_atomic="16"
/// target_has_atomic="32"
/// target_has_atomic="64"
/// target_has_atomic="8"
/// target_has_atomic="ptr"
/// ```
///
/// Should result in the following environment variable:
///
/// ```
/// CARGO_CFG_TARGET_HAS_ATOMIC=16,32,64,8,ptr
/// ```
///
/// On the other hand, configuration symbols without any value should result in
/// an empty string.
///
/// For example, this configuration:
///
/// ```
/// target_thread_local
/// ```
///
/// Should result in the following environment variable:
///
/// ```
/// CARGO_CFG_TARGET_THREAD_LOCAL=
/// ```
#[cargo_test(nightly, reason = "affected rustc cfg is unstable")]
#[cfg(target_arch = "x86_64")]
fn rustc_cfg_with_and_without_value() {
let build_rs = r#"
fn main() {
let cfg = std::env::var("CARGO_CFG_TARGET_HAS_ATOMIC");
eprintln!("CARGO_CFG_TARGET_HAS_ATOMIC={cfg:?}");
let cfg = std::env::var("CARGO_CFG_WINDOWS");
eprintln!("CARGO_CFG_WINDOWS={cfg:?}");
let cfg = std::env::var("CARGO_CFG_UNIX");
eprintln!("CARGO_CFG_UNIX={cfg:?}");
}
"#;
let p = project()
.file("src/lib.rs", r#""#)
.file("build.rs", build_rs)
.build();
let mut check = p.cargo("check -vv");
#[cfg(target_has_atomic = "64")]
check.with_stderr_data(
"\
...
[foo 0.0.1] CARGO_CFG_TARGET_HAS_ATOMIC=Ok(\"[..]64[..]\")
...",
);
#[cfg(windows)]
check.with_stderr_data(
"\
...
[foo 0.0.1] CARGO_CFG_WINDOWS=Ok(\"\")
...",
);
#[cfg(unix)]
check.with_stderr_data(
"\
...
[foo 0.0.1] CARGO_CFG_UNIX=Ok(\"\")
...",
);
check.run();
}
|