diff options
author | Jonas Schievink <[email protected]> | 2021-06-09 16:16:52 +0100 |
---|---|---|
committer | Jonas Schievink <[email protected]> | 2021-06-09 16:16:52 +0100 |
commit | 05b3a4bc93cf6555857bfc68b5e85830a0da80dc (patch) | |
tree | 4bd5ad6b0b9a6a4a653ad8511122fa542021f588 /crates | |
parent | 5f592f4f58a6e1e1db0f920af34a2f569b65017c (diff) |
Build test-macros in a build script
Diffstat (limited to 'crates')
-rw-r--r-- | crates/proc_macro_srv/src/tests/utils.rs | 29 | ||||
-rw-r--r-- | crates/proc_macro_test/Cargo.toml | 6 | ||||
-rw-r--r-- | crates/proc_macro_test/build.rs | 48 | ||||
-rw-r--r-- | crates/proc_macro_test/imp/.gitignore | 2 | ||||
-rw-r--r-- | crates/proc_macro_test/imp/Cargo.toml | 17 | ||||
-rw-r--r-- | crates/proc_macro_test/imp/src/lib.rs | 48 | ||||
-rw-r--r-- | crates/proc_macro_test/src/lib.rs | 48 |
7 files changed, 123 insertions, 75 deletions
diff --git a/crates/proc_macro_srv/src/tests/utils.rs b/crates/proc_macro_srv/src/tests/utils.rs index 2e950c113..2c093aa0a 100644 --- a/crates/proc_macro_srv/src/tests/utils.rs +++ b/crates/proc_macro_srv/src/tests/utils.rs | |||
@@ -7,35 +7,8 @@ use proc_macro_api::ListMacrosTask; | |||
7 | use std::str::FromStr; | 7 | use std::str::FromStr; |
8 | 8 | ||
9 | pub mod fixtures { | 9 | pub mod fixtures { |
10 | use cargo_metadata::Message; | ||
11 | use std::path::PathBuf; | ||
12 | use std::process::Command; | ||
13 | |||
14 | // Use current project metadata to get the proc-macro dylib path | ||
15 | pub fn proc_macro_test_dylib_path() -> std::path::PathBuf { | 10 | pub fn proc_macro_test_dylib_path() -> std::path::PathBuf { |
16 | let name = "proc_macro_test"; | 11 | proc_macro_test::PROC_MACRO_TEST_LOCATION.into() |
17 | let version = "0.0.0"; | ||
18 | let command = Command::new(toolchain::cargo()) | ||
19 | .args(&["check", "--tests", "--message-format", "json"]) | ||
20 | .output() | ||
21 | .unwrap() | ||
22 | .stdout; | ||
23 | |||
24 | for message in Message::parse_stream(command.as_slice()) { | ||
25 | match message.unwrap() { | ||
26 | Message::CompilerArtifact(artifact) => { | ||
27 | if artifact.target.kind.contains(&"proc-macro".to_string()) { | ||
28 | let repr = format!("{} {}", name, version); | ||
29 | if artifact.package_id.repr.starts_with(&repr) { | ||
30 | return PathBuf::from(&artifact.filenames[0]); | ||
31 | } | ||
32 | } | ||
33 | } | ||
34 | _ => (), // Unknown message | ||
35 | } | ||
36 | } | ||
37 | |||
38 | panic!("No proc-macro dylib for {} found!", name); | ||
39 | } | 12 | } |
40 | } | 13 | } |
41 | 14 | ||
diff --git a/crates/proc_macro_test/Cargo.toml b/crates/proc_macro_test/Cargo.toml index 753443be2..1a88e361e 100644 --- a/crates/proc_macro_test/Cargo.toml +++ b/crates/proc_macro_test/Cargo.toml | |||
@@ -8,4 +8,8 @@ publish = false | |||
8 | 8 | ||
9 | [lib] | 9 | [lib] |
10 | doctest = false | 10 | doctest = false |
11 | proc-macro = true | 11 | |
12 | [build-dependencies] | ||
13 | proc_macro_test_impl = { path = "imp", version = "0.0.0" } | ||
14 | toolchain = { path = "../toolchain", version = "0.0.0" } | ||
15 | cargo_metadata = "0.13" | ||
diff --git a/crates/proc_macro_test/build.rs b/crates/proc_macro_test/build.rs new file mode 100644 index 000000000..4653a93dd --- /dev/null +++ b/crates/proc_macro_test/build.rs | |||
@@ -0,0 +1,48 @@ | |||
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 | |||
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 output = Command::new(toolchain::cargo()) | ||
21 | .current_dir("imp") | ||
22 | .args(&["build", "-p", "proc_macro_test_impl", "--message-format", "json"]) | ||
23 | .output() | ||
24 | .unwrap(); | ||
25 | assert!(output.status.success()); | ||
26 | |||
27 | let mut artifact_path = None; | ||
28 | for message in Message::parse_stream(output.stdout.as_slice()) { | ||
29 | match message.unwrap() { | ||
30 | Message::CompilerArtifact(artifact) => { | ||
31 | if artifact.target.kind.contains(&"proc-macro".to_string()) { | ||
32 | let repr = format!("{} {}", name, version); | ||
33 | if artifact.package_id.repr.starts_with(&repr) { | ||
34 | artifact_path = Some(PathBuf::from(&artifact.filenames[0])); | ||
35 | } | ||
36 | } | ||
37 | } | ||
38 | _ => (), // Unknown message | ||
39 | } | ||
40 | } | ||
41 | |||
42 | let src_path = artifact_path.expect("no dylib for proc_macro_test_impl found"); | ||
43 | let dest_path = out_dir.join(src_path.file_name().unwrap()); | ||
44 | fs::copy(src_path, &dest_path).unwrap(); | ||
45 | |||
46 | let info_path = out_dir.join("proc_macro_test_location.txt"); | ||
47 | fs::write(info_path, dest_path.to_str().unwrap()).unwrap(); | ||
48 | } | ||
diff --git a/crates/proc_macro_test/imp/.gitignore b/crates/proc_macro_test/imp/.gitignore new file mode 100644 index 000000000..2c96eb1b6 --- /dev/null +++ b/crates/proc_macro_test/imp/.gitignore | |||
@@ -0,0 +1,2 @@ | |||
1 | target/ | ||
2 | Cargo.lock | ||
diff --git a/crates/proc_macro_test/imp/Cargo.toml b/crates/proc_macro_test/imp/Cargo.toml new file mode 100644 index 000000000..1c2e75401 --- /dev/null +++ b/crates/proc_macro_test/imp/Cargo.toml | |||
@@ -0,0 +1,17 @@ | |||
1 | [package] | ||
2 | name = "proc_macro_test_impl" | ||
3 | version = "0.0.0" | ||
4 | license = "MIT OR Apache-2.0" | ||
5 | authors = ["rust-analyzer developers"] | ||
6 | edition = "2018" | ||
7 | publish = false | ||
8 | |||
9 | [lib] | ||
10 | doctest = false | ||
11 | proc-macro = true | ||
12 | |||
13 | [workspace] | ||
14 | |||
15 | [dependencies] | ||
16 | # this crate should not have any dependencies, since it uses its own workspace, | ||
17 | # and its own `Cargo.lock` | ||
diff --git a/crates/proc_macro_test/imp/src/lib.rs b/crates/proc_macro_test/imp/src/lib.rs new file mode 100644 index 000000000..4b26d2472 --- /dev/null +++ b/crates/proc_macro_test/imp/src/lib.rs | |||
@@ -0,0 +1,48 @@ | |||
1 | //! Exports a few trivial procedural macros for testing. | ||
2 | |||
3 | use proc_macro::TokenStream; | ||
4 | |||
5 | #[proc_macro] | ||
6 | pub fn fn_like_noop(args: TokenStream) -> TokenStream { | ||
7 | args | ||
8 | } | ||
9 | |||
10 | #[proc_macro] | ||
11 | pub fn fn_like_panic(args: TokenStream) -> TokenStream { | ||
12 | panic!("fn_like_panic!({})", args); | ||
13 | } | ||
14 | |||
15 | #[proc_macro] | ||
16 | pub fn fn_like_error(args: TokenStream) -> TokenStream { | ||
17 | format!("compile_error!(\"fn_like_error!({})\");", args).parse().unwrap() | ||
18 | } | ||
19 | |||
20 | #[proc_macro_attribute] | ||
21 | pub fn attr_noop(_args: TokenStream, item: TokenStream) -> TokenStream { | ||
22 | item | ||
23 | } | ||
24 | |||
25 | #[proc_macro_attribute] | ||
26 | pub fn attr_panic(args: TokenStream, item: TokenStream) -> TokenStream { | ||
27 | panic!("#[attr_panic {}] {}", args, item); | ||
28 | } | ||
29 | |||
30 | #[proc_macro_attribute] | ||
31 | pub fn attr_error(args: TokenStream, item: TokenStream) -> TokenStream { | ||
32 | format!("compile_error!(\"#[attr_error({})] {}\");", args, item).parse().unwrap() | ||
33 | } | ||
34 | |||
35 | #[proc_macro_derive(DeriveEmpty)] | ||
36 | pub fn derive_empty(_item: TokenStream) -> TokenStream { | ||
37 | TokenStream::new() | ||
38 | } | ||
39 | |||
40 | #[proc_macro_derive(DerivePanic)] | ||
41 | pub fn derive_panic(item: TokenStream) -> TokenStream { | ||
42 | panic!("#[derive(DerivePanic)] {}", item); | ||
43 | } | ||
44 | |||
45 | #[proc_macro_derive(DeriveError)] | ||
46 | pub fn derive_error(item: TokenStream) -> TokenStream { | ||
47 | format!("compile_error!(\"#[derive(DeriveError)] {}\");", item).parse().unwrap() | ||
48 | } | ||
diff --git a/crates/proc_macro_test/src/lib.rs b/crates/proc_macro_test/src/lib.rs index 4b26d2472..2edf23a63 100644 --- a/crates/proc_macro_test/src/lib.rs +++ b/crates/proc_macro_test/src/lib.rs | |||
@@ -1,48 +1,4 @@ | |||
1 | //! Exports a few trivial procedural macros for testing. | 1 | //! Exports a few trivial procedural macros for testing. |
2 | 2 | ||
3 | use proc_macro::TokenStream; | 3 | pub static PROC_MACRO_TEST_LOCATION: &str = |
4 | 4 | include_str!(concat!(env!("OUT_DIR"), "/proc_macro_test_location.txt")); | |
5 | #[proc_macro] | ||
6 | pub fn fn_like_noop(args: TokenStream) -> TokenStream { | ||
7 | args | ||
8 | } | ||
9 | |||
10 | #[proc_macro] | ||
11 | pub fn fn_like_panic(args: TokenStream) -> TokenStream { | ||
12 | panic!("fn_like_panic!({})", args); | ||
13 | } | ||
14 | |||
15 | #[proc_macro] | ||
16 | pub fn fn_like_error(args: TokenStream) -> TokenStream { | ||
17 | format!("compile_error!(\"fn_like_error!({})\");", args).parse().unwrap() | ||
18 | } | ||
19 | |||
20 | #[proc_macro_attribute] | ||
21 | pub fn attr_noop(_args: TokenStream, item: TokenStream) -> TokenStream { | ||
22 | item | ||
23 | } | ||
24 | |||
25 | #[proc_macro_attribute] | ||
26 | pub fn attr_panic(args: TokenStream, item: TokenStream) -> TokenStream { | ||
27 | panic!("#[attr_panic {}] {}", args, item); | ||
28 | } | ||
29 | |||
30 | #[proc_macro_attribute] | ||
31 | pub fn attr_error(args: TokenStream, item: TokenStream) -> TokenStream { | ||
32 | format!("compile_error!(\"#[attr_error({})] {}\");", args, item).parse().unwrap() | ||
33 | } | ||
34 | |||
35 | #[proc_macro_derive(DeriveEmpty)] | ||
36 | pub fn derive_empty(_item: TokenStream) -> TokenStream { | ||
37 | TokenStream::new() | ||
38 | } | ||
39 | |||
40 | #[proc_macro_derive(DerivePanic)] | ||
41 | pub fn derive_panic(item: TokenStream) -> TokenStream { | ||
42 | panic!("#[derive(DerivePanic)] {}", item); | ||
43 | } | ||
44 | |||
45 | #[proc_macro_derive(DeriveError)] | ||
46 | pub fn derive_error(item: TokenStream) -> TokenStream { | ||
47 | format!("compile_error!(\"#[derive(DeriveError)] {}\");", item).parse().unwrap() | ||
48 | } | ||