Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build proc-macro-test-impl out-of-tree #12831

Merged
merged 5 commits into from
Jul 20, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 38 additions & 2 deletions crates/proc-macro-test/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,43 @@ use std::{
use cargo_metadata::Message;

fn main() {
println!("cargo:rerun-if-changed=imp");

let out_dir = env::var_os("OUT_DIR").unwrap();
let out_dir = Path::new(&out_dir);

let name = "proc-macro-test-impl";
let version = "0.0.0";

let imp_dir = std::env::current_dir().unwrap().join("imp");

let staging_dir = out_dir.join("proc-macro-test-imp-staging");
// this'll error out if the staging dir didn't previously exist. using
// `std::fs::exists` would suffer from TOCTOU so just do our best to
// wipe it and ignore errors.
let _ = std::fs::remove_dir_all(&staging_dir);

println!("Creating {}", staging_dir.display());
std::fs::create_dir_all(&staging_dir).unwrap();

let src_dir = staging_dir.join("src");
println!("Creating {}", src_dir.display());
std::fs::create_dir_all(src_dir).unwrap();

for item_els in [&["Cargo.toml"][..], &["src", "lib.rs"]] {
let mut src = imp_dir.clone();
let mut dst = staging_dir.clone();
for el in item_els {
src.push(el);
dst.push(el);
}
println!("Copying {} to {}", src.display(), dst.display());
std::fs::copy(src, dst).unwrap();
}

let target_dir = out_dir.join("target");
let output = Command::new(toolchain::cargo())
.current_dir("imp")
.current_dir(&staging_dir)
.args(&["build", "-p", "proc-macro-test-impl", "--message-format", "json"])
// Explicit override the target directory to avoid using the same one which the parent
// cargo is using, or we'll deadlock.
Expand All @@ -29,7 +58,14 @@ fn main() {
.arg(&target_dir)
.output()
.unwrap();
assert!(output.status.success());
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");
}

let mut artifact_path = None;
for message in Message::parse_stream(output.stdout.as_slice()) {
Expand Down