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 use std::{
7 env, fs,
8 path::{Path, PathBuf},
9 process::Command,
10 };
11
12 use cargo_metadata::Message;
13
main()14 fn main() {
15 let out_dir = env::var_os("OUT_DIR").unwrap();
16 let out_dir = Path::new(&out_dir);
17
18 let name = "proc_macro_test_impl";
19 let version = "0.0.0";
20 let target_dir = out_dir.join("target");
21 let output = Command::new(toolchain::cargo())
22 .current_dir("imp")
23 .args(&["build", "-p", "proc_macro_test_impl", "--message-format", "json"])
24 // Explicit override the target directory to avoid using the same one which the parent
25 // cargo is using, or we'll deadlock.
26 // This can happen when `CARGO_TARGET_DIR` is set or global config forces all cargo
27 // instance to use the same target directory.
28 .arg("--target-dir")
29 .arg(&target_dir)
30 .output()
31 .unwrap();
32 assert!(output.status.success());
33
34 let mut artifact_path = None;
35 for message in Message::parse_stream(output.stdout.as_slice()) {
36 match message.unwrap() {
37 Message::CompilerArtifact(artifact) => {
38 if artifact.target.kind.contains(&"proc-macro".to_string()) {
39 let repr = format!("{} {}", name, version);
40 if artifact.package_id.repr.starts_with(&repr) {
41 artifact_path = Some(PathBuf::from(&artifact.filenames[0]));
42 }
43 }
44 }
45 _ => (), // Unknown message
46 }
47 }
48
49 // This file is under `target_dir` and is already under `OUT_DIR`.
50 let artifact_path = artifact_path.expect("no dylib for proc_macro_test_impl found");
51
52 let info_path = out_dir.join("proc_macro_test_location.txt");
53 fs::write(info_path, artifact_path.to_str().unwrap()).unwrap();
54 }
55