1 //! A Cargo build script binary used in unit tests for the Bazel `cargo_build_script` rule
2
3 /// `cargo_build_script` should always set `CARGO_ENCODED_RUSTFLAGS`
test_encoded_rustflags()4 fn test_encoded_rustflags() {
5 let encoded_rustflags = std::env::var("CARGO_ENCODED_RUSTFLAGS").unwrap();
6
7 let flags: Vec<String> = encoded_rustflags
8 .split('\x1f')
9 .map(str::to_string)
10 .collect();
11 assert_eq!(flags.len(), 2);
12
13 assert!(flags[0].starts_with("--sysroot"));
14
15 // Ensure the `pwd` template has been resolved
16 assert!(!flags[0].contains("${pwd}"));
17
18 assert_eq!(flags[1], "--verbose");
19 }
20
21 /// Ensure Make variables provided by the `toolchains` attribute are expandable.
test_toolchain_var()22 fn test_toolchain_var() {
23 let tool = std::env::var("EXPANDED_TOOLCHAIN_VAR").unwrap();
24 if cfg!(target_os = "windows") {
25 assert!(tool.ends_with("rustc.exe"));
26 } else {
27 assert!(tool.ends_with("rustc"));
28 }
29 eprintln!("{}", std::env::current_dir().unwrap().display());
30 let tool_path = std::path::PathBuf::from(tool);
31 assert!(tool_path.exists(), "{} does not exist", tool_path.display());
32 }
33
main()34 fn main() {
35 // Perform some unit testing
36 test_encoded_rustflags();
37 test_toolchain_var();
38
39 // Pass the TOOL_PATH along to the rust_test so we can assert on it.
40 println!(
41 "cargo:rustc-env=TOOL_PATH={}",
42 std::env::var("TOOL").unwrap()
43 );
44
45 // Assert that the CC, CXX and LD env vars existed and were executable.
46 // We don't assert what happens when they're executed (in particular, we don't check for a
47 // non-zero exit code), but this asserts that it's an existing file which is executable.
48 for env_var in &["CC", "CXX", "LD"] {
49 let path = std::env::var(env_var)
50 .unwrap_or_else(|err| panic!("Error getting {}: {}", env_var, err));
51 std::process::Command::new(path).status().unwrap();
52 }
53
54 // Assert that some env variables are set.
55 for env_var in &["CFLAGS", "CXXFLAGS", "LDFLAGS"] {
56 assert!(std::env::var(env_var).is_ok());
57 }
58
59 assert_eq!(std::env::var("CARGO_MANIFEST_LINKS").unwrap(), "beep");
60 }
61