1 //! This will build the proc macro in `imp`, and copy the resulting dylib artifact into the
4 //! `proc-macro-test` itself contains only a path to that artifact.
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)
12 path::{Path, PathBuf},
16 use cargo_metadata::Message;
19 println!("cargo:rerun-if-changed=imp");
20 println!("cargo:rerun-if-env-changed=PROC_MACRO_TEST_TOOLCHAIN");
22 let out_dir = env::var_os("OUT_DIR").unwrap();
23 let out_dir = Path::new(&out_dir);
25 let name = "proc-macro-test-impl";
26 let version = "0.0.0";
28 let imp_dir = std::env::current_dir().unwrap().join("imp");
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);
36 println!("Creating {}", staging_dir.display());
37 std::fs::create_dir_all(&staging_dir).unwrap();
39 let src_dir = staging_dir.join("src");
40 println!("Creating {}", src_dir.display());
41 std::fs::create_dir_all(src_dir).unwrap();
43 for item_els in [&["Cargo.toml"][..], &["src", "lib.rs"]] {
44 let mut src = imp_dir.clone();
45 let mut dst = staging_dir.clone();
50 println!("Copying {} to {}", src.display(), dst.display());
51 std::fs::copy(src, dst).unwrap();
54 let target_dir = out_dir.join("target");
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}"));
62 Command::new(toolchain::cargo())
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.
75 println!("Running {:?}", cmd);
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");
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]));
98 _ => (), // Unknown message
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");
105 let info_path = out_dir.join("proc_macro_test_location.txt");
106 fs::write(info_path, artifact_path.to_str().unwrap()).unwrap();