]> git.lizzy.rs Git - rust.git/blob - crates/proc-macro-test/build.rs
Merge remote-tracking branch 'origin/master' into sync-from-rust-2
[rust.git] / crates / proc-macro-test / build.rs
1 //! This will build the proc macro in `imp`, and copy the resulting dylib artifact into the
2 //! `OUT_DIR`.
3 //!
4 //! `proc-macro-test` itself contains only a path to that artifact.
5 //!
6 //! The `PROC_MACRO_TEST_TOOLCHAIN` environment variable can be exported to use
7 //! a specific rustup toolchain: this allows testing against older ABIs (e.g.
8 //! 1.58) and future ABIs (stage1, nightly)
9
10 use std::{
11     env, fs,
12     path::{Path, PathBuf},
13     process::Command,
14 };
15
16 use cargo_metadata::Message;
17
18 fn main() {
19     println!("cargo:rerun-if-changed=imp");
20     println!("cargo:rerun-if-env-changed=PROC_MACRO_TEST_TOOLCHAIN");
21
22     let out_dir = env::var_os("OUT_DIR").unwrap();
23     let out_dir = Path::new(&out_dir);
24
25     let name = "proc-macro-test-impl";
26     let version = "0.0.0";
27
28     let imp_dir = std::env::current_dir().unwrap().join("imp");
29
30     let staging_dir = out_dir.join("proc-macro-test-imp-staging");
31     // this'll error out if the staging dir didn't previously exist. using
32     // `std::fs::exists` would suffer from TOCTOU so just do our best to
33     // wipe it and ignore errors.
34     let _ = std::fs::remove_dir_all(&staging_dir);
35
36     println!("Creating {}", staging_dir.display());
37     std::fs::create_dir_all(&staging_dir).unwrap();
38
39     let src_dir = staging_dir.join("src");
40     println!("Creating {}", src_dir.display());
41     std::fs::create_dir_all(src_dir).unwrap();
42
43     for item_els in [&["Cargo.toml"][..], &["src", "lib.rs"]] {
44         let mut src = imp_dir.clone();
45         let mut dst = staging_dir.clone();
46         for el in item_els {
47             src.push(el);
48             dst.push(el);
49         }
50         println!("Copying {} to {}", src.display(), dst.display());
51         std::fs::copy(src, dst).unwrap();
52     }
53
54     let target_dir = out_dir.join("target");
55
56     let mut cmd = if let Ok(toolchain) = std::env::var("PROC_MACRO_TEST_TOOLCHAIN") {
57         // leverage rustup to find user-specific toolchain
58         let mut cmd = Command::new("cargo");
59         cmd.arg(format!("+{toolchain}"));
60         cmd
61     } else {
62         Command::new(toolchain::cargo())
63     };
64
65     cmd
66         .current_dir(&staging_dir)
67         .args(&["build", "-p", "proc-macro-test-impl", "--message-format", "json"])
68         // Explicit override the target directory to avoid using the same one which the parent
69         // cargo is using, or we'll deadlock.
70         // This can happen when `CARGO_TARGET_DIR` is set or global config forces all cargo
71         // instance to use the same target directory.
72         .arg("--target-dir")
73         .arg(&target_dir);
74
75     println!("Running {:?}", cmd);
76
77     let output = cmd.output().unwrap();
78     if !output.status.success() {
79         println!("proc-macro-test-impl failed to build");
80         println!("============ stdout ============");
81         println!("{}", String::from_utf8_lossy(&output.stdout));
82         println!("============ stderr ============");
83         println!("{}", String::from_utf8_lossy(&output.stderr));
84         panic!("proc-macro-test-impl failed to build");
85     }
86
87     let mut artifact_path = None;
88     for message in Message::parse_stream(output.stdout.as_slice()) {
89         match message.unwrap() {
90             Message::CompilerArtifact(artifact) => {
91                 if artifact.target.kind.contains(&"proc-macro".to_string()) {
92                     let repr = format!("{} {}", name, version);
93                     if artifact.package_id.repr.starts_with(&repr) {
94                         artifact_path = Some(PathBuf::from(&artifact.filenames[0]));
95                     }
96                 }
97             }
98             _ => (), // Unknown message
99         }
100     }
101
102     // This file is under `target_dir` and is already under `OUT_DIR`.
103     let artifact_path = artifact_path.expect("no dylib for proc-macro-test-impl found");
104
105     let info_path = out_dir.join("proc_macro_test_location.txt");
106     fs::write(info_path, artifact_path.to_str().unwrap()).unwrap();
107 }