2021-06-09 10:16:52 -05:00
|
|
|
//! This will build the proc macro in `imp`, and copy the resulting dylib artifact into the
|
|
|
|
//! `OUT_DIR`.
|
|
|
|
//!
|
2022-04-29 09:29:24 -05:00
|
|
|
//! `proc-macro-test` itself contains only a path to that artifact.
|
2022-07-20 10:43:45 -05:00
|
|
|
//!
|
|
|
|
//! The `PROC_MACRO_TEST_TOOLCHAIN` environment variable can be exported to use
|
|
|
|
//! a specific rustup toolchain: this allows testing against older ABIs (e.g.
|
|
|
|
//! 1.58) and future ABIs (stage1, nightly)
|
2021-06-09 10:16:52 -05:00
|
|
|
|
|
|
|
use std::{
|
|
|
|
env, fs,
|
|
|
|
path::{Path, PathBuf},
|
|
|
|
process::Command,
|
|
|
|
};
|
|
|
|
|
|
|
|
use cargo_metadata::Message;
|
|
|
|
|
|
|
|
fn main() {
|
2022-07-20 09:22:48 -05:00
|
|
|
println!("cargo:rerun-if-changed=imp");
|
2024-01-15 03:40:09 -06:00
|
|
|
|
|
|
|
let has_features = env::var_os("RUSTC_BOOTSTRAP").is_some()
|
|
|
|
|| String::from_utf8(
|
|
|
|
Command::new(toolchain::cargo()).arg("--version").output().unwrap().stdout,
|
|
|
|
)
|
|
|
|
.unwrap()
|
|
|
|
.contains("nightly");
|
2022-07-20 09:22:48 -05:00
|
|
|
|
2021-06-09 10:16:52 -05:00
|
|
|
let out_dir = env::var_os("OUT_DIR").unwrap();
|
|
|
|
let out_dir = Path::new(&out_dir);
|
|
|
|
|
2024-01-15 03:40:09 -06:00
|
|
|
if !has_features {
|
|
|
|
println!("proc-macro-test testing only works on nightly toolchains");
|
|
|
|
let info_path = out_dir.join("proc_macro_test_location.txt");
|
|
|
|
fs::File::create(info_path).unwrap();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-04-29 09:29:24 -05:00
|
|
|
let name = "proc-macro-test-impl";
|
2021-06-09 10:16:52 -05:00
|
|
|
let version = "0.0.0";
|
2022-07-20 09:14:53 -05:00
|
|
|
|
|
|
|
let imp_dir = std::env::current_dir().unwrap().join("imp");
|
2022-07-20 09:22:48 -05:00
|
|
|
|
|
|
|
let staging_dir = out_dir.join("proc-macro-test-imp-staging");
|
2022-07-20 09:56:30 -05:00
|
|
|
// this'll error out if the staging dir didn't previously exist. using
|
2022-07-20 09:22:48 -05:00
|
|
|
// `std::fs::exists` would suffer from TOCTOU so just do our best to
|
2022-07-20 09:56:25 -05:00
|
|
|
// wipe it and ignore errors.
|
2022-07-20 09:22:48 -05:00
|
|
|
let _ = std::fs::remove_dir_all(&staging_dir);
|
|
|
|
|
|
|
|
println!("Creating {}", staging_dir.display());
|
2022-07-20 09:14:53 -05:00
|
|
|
std::fs::create_dir_all(&staging_dir).unwrap();
|
2022-07-20 09:22:48 -05:00
|
|
|
|
|
|
|
let src_dir = staging_dir.join("src");
|
|
|
|
println!("Creating {}", src_dir.display());
|
|
|
|
std::fs::create_dir_all(src_dir).unwrap();
|
2022-07-20 09:14:53 -05:00
|
|
|
|
2022-07-20 09:30:08 -05:00
|
|
|
for item_els in [&["Cargo.toml"][..], &["src", "lib.rs"]] {
|
2022-07-20 09:14:53 -05:00
|
|
|
let mut src = imp_dir.clone();
|
|
|
|
let mut dst = staging_dir.clone();
|
|
|
|
for el in item_els {
|
|
|
|
src.push(el);
|
|
|
|
dst.push(el);
|
|
|
|
}
|
2022-07-20 09:22:48 -05:00
|
|
|
println!("Copying {} to {}", src.display(), dst.display());
|
2022-07-20 09:14:53 -05:00
|
|
|
std::fs::copy(src, dst).unwrap();
|
|
|
|
}
|
|
|
|
|
2021-06-21 14:14:08 -05:00
|
|
|
let target_dir = out_dir.join("target");
|
2022-07-20 10:43:45 -05:00
|
|
|
|
2024-01-15 03:40:09 -06:00
|
|
|
let mut cmd = Command::new(toolchain::cargo());
|
2022-07-26 04:53:50 -05:00
|
|
|
cmd.current_dir(&staging_dir)
|
2023-01-09 12:36:22 -06:00
|
|
|
.args(["build", "-p", "proc-macro-test-impl", "--message-format", "json"])
|
2021-06-21 14:14:08 -05:00
|
|
|
// Explicit override the target directory to avoid using the same one which the parent
|
|
|
|
// cargo is using, or we'll deadlock.
|
|
|
|
// This can happen when `CARGO_TARGET_DIR` is set or global config forces all cargo
|
|
|
|
// instance to use the same target directory.
|
|
|
|
.arg("--target-dir")
|
2022-07-23 15:37:46 -05:00
|
|
|
.arg(&target_dir);
|
|
|
|
|
2023-09-18 04:32:37 -05:00
|
|
|
if let Ok(target) = std::env::var("TARGET") {
|
|
|
|
cmd.args(["--target", &target]);
|
|
|
|
}
|
|
|
|
|
2023-01-09 12:36:22 -06:00
|
|
|
println!("Running {cmd:?}");
|
2022-07-23 15:37:46 -05:00
|
|
|
|
|
|
|
let output = cmd.output().unwrap();
|
2022-07-20 09:14:53 -05:00
|
|
|
if !output.status.success() {
|
|
|
|
println!("proc-macro-test-impl failed to build");
|
|
|
|
println!("============ stdout ============");
|
|
|
|
println!("{}", String::from_utf8_lossy(&output.stdout));
|
|
|
|
println!("============ stderr ============");
|
|
|
|
println!("{}", String::from_utf8_lossy(&output.stderr));
|
|
|
|
panic!("proc-macro-test-impl failed to build");
|
|
|
|
}
|
2021-06-09 10:16:52 -05:00
|
|
|
|
2024-01-18 00:50:06 -06:00
|
|
|
// Old Package ID Spec
|
|
|
|
let repr = format!("{name} {version}");
|
|
|
|
// New Package Id Spec since rust-lang/cargo#13311
|
|
|
|
let pkgid = String::from_utf8(
|
|
|
|
Command::new(toolchain::cargo())
|
|
|
|
.current_dir(&staging_dir)
|
|
|
|
.args(["pkgid", name])
|
|
|
|
.output()
|
|
|
|
.unwrap().stdout,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let pkgid = pkgid.trim();
|
|
|
|
|
2021-06-09 10:16:52 -05:00
|
|
|
let mut artifact_path = None;
|
|
|
|
for message in Message::parse_stream(output.stdout.as_slice()) {
|
2023-01-09 12:36:22 -06:00
|
|
|
if let Message::CompilerArtifact(artifact) = message.unwrap() {
|
|
|
|
if artifact.target.kind.contains(&"proc-macro".to_string()) {
|
2024-01-18 00:50:06 -06:00
|
|
|
if artifact.package_id.repr.starts_with(&repr) || artifact.package_id.repr == pkgid {
|
2023-01-09 12:36:22 -06:00
|
|
|
artifact_path = Some(PathBuf::from(&artifact.filenames[0]));
|
2021-06-09 10:16:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-21 14:14:08 -05:00
|
|
|
// This file is under `target_dir` and is already under `OUT_DIR`.
|
2022-04-29 09:29:24 -05:00
|
|
|
let artifact_path = artifact_path.expect("no dylib for proc-macro-test-impl found");
|
2021-06-09 10:16:52 -05:00
|
|
|
|
|
|
|
let info_path = out_dir.join("proc_macro_test_location.txt");
|
2021-06-21 14:14:08 -05:00
|
|
|
fs::write(info_path, artifact_path.to_str().unwrap()).unwrap();
|
2021-06-09 10:16:52 -05:00
|
|
|
}
|