From b1207f3114028406f12d35f7511a52dbfebdb3ad Mon Sep 17 00:00:00 2001 From: Jacob Lifshay Date: Thu, 20 May 2021 18:47:48 -0700 Subject: [PATCH] convert proc-macro for generating Context trait to a build script in hopes of getting rust-analyzer to not hang --- Cargo.toml | 7 +- build.rs | 36 +++++++++ src/traits.rs | 8 +- .../Cargo.toml | 6 +- .../src/lib.rs | 76 ++++++------------- 5 files changed, 67 insertions(+), 66 deletions(-) create mode 100644 build.rs rename {vector-math-proc-macro => vector-math-build-helpers}/Cargo.toml (64%) rename {vector-math-proc-macro => vector-math-build-helpers}/src/lib.rs (88%) diff --git a/Cargo.toml b/Cargo.toml index a5b7fd4..d709c7a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,10 @@ license = "MIT OR Apache-2.0" half = { version = "1.7.1", optional = true } typed-arena = { version = "2.0.1", optional = true } core_simd = { version = "0.1.0", git = "https://github.com/rust-lang/stdsimd", optional = true } -vector-math-proc-macro = { version = "=0.1.0", path = "vector-math-proc-macro" } + +[build-dependencies] +vector-math-build-helpers = { version = "=0.1.0", path = "vector-math-build-helpers" } +which = "3" [features] default = ["fma"] @@ -26,4 +29,4 @@ rug = "1.12.0" az = "1.1.1" [workspace] -members = [".", "vector-math-proc-macro"] +members = [".", "vector-math-build-helpers"] diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..33d01bc --- /dev/null +++ b/build.rs @@ -0,0 +1,36 @@ +use std::{ + env, fs, + io::{self, Read, Write}, + path::Path, + process::{Child, Command, Stdio}, + thread, +}; +use vector_math_build_helpers::make_context_types; + +fn format_source(source: String) -> String { + let rustfmt_path = which::which("rustfmt").unwrap(); + let mut command = Command::new(rustfmt_path) + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .spawn() + .unwrap(); + let stdin = command.stdin.take().unwrap(); + let reader_thread = thread::spawn(move || -> io::Result<(String, Child)> { + let mut output = String::new(); + command.stdout.take().unwrap().read_to_string(&mut output)?; + Ok((output, command)) + }); + { stdin }.write_all(source.as_bytes()).unwrap(); + let (output, mut command) = reader_thread.join().unwrap().unwrap(); + let exit_status = command.wait().unwrap(); + assert!(exit_status.success()); + output +} + +fn main() { + fs::write( + Path::new(&env::var_os("OUT_DIR").unwrap()).join("context_trait.rs"), + format_source(make_context_types().to_string()), + ) + .unwrap(); +} diff --git a/src/traits.rs b/src/traits.rs index c02b609..e923e1a 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -7,13 +7,7 @@ use core::ops::{ Mul, MulAssign, Neg, Not, Rem, RemAssign, Shl, ShlAssign, Shr, ShrAssign, Sub, SubAssign, }; -/// reference used to build IR for Kazan; an empty type for `core::simd` -pub trait Context: Copy { - vector_math_proc_macro::make_context_types!(); - fn make>(self, v: T::Prim) -> T { - T::make(self, v) - } -} +include!(concat!(env!("OUT_DIR"), "/context_trait.rs")); pub trait Make: Copy { type Prim: Copy; diff --git a/vector-math-proc-macro/Cargo.toml b/vector-math-build-helpers/Cargo.toml similarity index 64% rename from vector-math-proc-macro/Cargo.toml rename to vector-math-build-helpers/Cargo.toml index f65c085..842264b 100644 --- a/vector-math-proc-macro/Cargo.toml +++ b/vector-math-build-helpers/Cargo.toml @@ -1,14 +1,10 @@ [package] -name = "vector-math-proc-macro" +name = "vector-math-build-helpers" version = "0.1.0" authors = ["Jacob Lifshay "] edition = "2018" license = "MIT OR Apache-2.0" -[lib] -proc-macro = true - [dependencies] quote = "1.0" proc-macro2 = "1.0" -syn = { version = "1.0", features = [] } diff --git a/vector-math-proc-macro/src/lib.rs b/vector-math-build-helpers/src/lib.rs similarity index 88% rename from vector-math-proc-macro/src/lib.rs rename to vector-math-build-helpers/src/lib.rs index 89d2bc4..3aa9e5f 100644 --- a/vector-math-proc-macro/src/lib.rs +++ b/vector-math-build-helpers/src/lib.rs @@ -1,24 +1,11 @@ +use proc_macro2::{Ident, Span, TokenStream}; +use quote::{quote, ToTokens}; use std::{ cmp::Ordering, collections::{BTreeSet, HashMap}, hash::Hash, }; -use proc_macro2::{Ident, Span, TokenStream}; -use quote::{quote, ToTokens}; -use syn::{ - parse::{Parse, ParseStream}, - parse_macro_input, -}; - -struct Input {} - -impl Parse for Input { - fn parse(_input: ParseStream) -> syn::Result { - Ok(Input {}) - } -} - macro_rules! make_enum { ( $vis:vis enum $ty:ident { @@ -341,46 +328,31 @@ impl TraitSets { } } -impl Input { - fn to_tokens(&self) -> syn::Result { - let mut types = Vec::new(); - let mut trait_sets = TraitSets::default(); - trait_sets.fill(); - for &bits in TypeBits::VALUES { - for &type_kind in TypeKind::VALUES { - for &vector_scalar in VectorScalar::VALUES { - if !type_kind.is_valid(bits, vector_scalar) { - continue; - } - let ty = type_kind.ty(bits, vector_scalar); - let traits = trait_sets.get(type_kind, bits, vector_scalar); - types.push(quote! { - type #ty: #(#traits)+*; - }); +pub fn make_context_types() -> TokenStream { + let mut types = Vec::new(); + let mut trait_sets = TraitSets::default(); + trait_sets.fill(); + for &bits in TypeBits::VALUES { + for &type_kind in TypeKind::VALUES { + for &vector_scalar in VectorScalar::VALUES { + if !type_kind.is_valid(bits, vector_scalar) { + continue; } + let ty = type_kind.ty(bits, vector_scalar); + let traits = trait_sets.get(type_kind, bits, vector_scalar); + types.push(quote! { + type #ty: #(#traits)+*; + }); } } - Ok(quote! {#(#types)*}) - } -} - -#[proc_macro] -pub fn make_context_types(input: proc_macro::TokenStream) -> proc_macro::TokenStream { - let input = parse_macro_input!(input as Input); - match input.to_tokens() { - Ok(retval) => retval, - Err(err) => err.to_compile_error(), } - .into() -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test() -> syn::Result<()> { - Input {}.to_tokens()?; - Ok(()) + quote! { + /// reference used to build IR for Kazan; an empty type for `core::simd` + pub trait Context: Copy { + #(#types)* + fn make>(self, v: T::Prim) -> T { + T::make(self, v) + } + } } } -- 2.30.2