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

pretty-close-to-build-rs #188

Merged
merged 4 commits into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ TESTNAME=log_global_ref_and_chained_calls just test
```

### Debugging
Duchess looks for the `DUCHESS_DEBUG` environment variable during proc-macro expansion. When this variable is set, if it is `true` or `1`, **all** generated code will be formatted and dumped to a directory. Clickable links are printed to stderr:
Duchess looks for the `DUCHESS_DEBUG` environment variable during proc-macro expansion and `build.rs` execution. When this variable is set, if it is `true` or `1`, **all** generated code will be formatted and dumped to a directory. Additionally, this emits output from the buildscript as `warning` lines for cargo so they become visible.

For proc macro expansion, clickable links are printed to stderr:

For example:
```
Expand Down
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ tracing = "0.1.37"
java-locator = { version = "0.1.3", optional = true }
libloading = { version = "0.8.0", optional = true }
derive-where = "1.2.1"
serde = { version = "1.0.214", features = ["derive"] }

[build-dependencies]
duchess-build-rs = { path = "duchess-build-rs" }


[features]
Expand Down
6 changes: 6 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
fn main() {
duchess_build_rs::DuchessBuildRs::new()
.with_src_path("src/".into())
.execute()
.unwrap();
}
2 changes: 1 addition & 1 deletion duchess-build-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ documentation.workspace = true

[dependencies]
anyhow = "1.0.86"
duchess-reflect = { version = "0.3.0", path = "../duchess-reflect" }
duchess-reflect = { version = "0.3.0", path = "../duchess-reflect", features = ["javap-reflection"] }
lazy_static = "1.5.0"
proc-macro2 = "1.0.86"
quote = "1.0.36"
Expand Down
66 changes: 66 additions & 0 deletions duchess-build-rs/src/derive_java.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use duchess_reflect::{
argument::MethodSelector,
parse::{Parse, Parser},
};
use proc_macro2::Span;
use syn::{parse::ParseStream, Attribute};

use crate::{log, re};

/// Process a file and reflect any `#[java(...)]` attributes that were found
pub(crate) fn process_file(
rs_file: &crate::files::File,
reflector: &mut duchess_reflect::reflect::JavapReflector,
) -> anyhow::Result<bool> {
let mut watch_file = false;
for capture in re::java_derive().captures_iter(&rs_file.contents) {
let std::ops::Range { start, end: _ } = capture.get(0).unwrap().range();
log!(
"Found derive(java) in {}:{}",
rs_file.path.display(),
rs_file.contents[..start].lines().count()
);
let derive_java_attr: DeriveJavaAttr = match syn::parse_str(rs_file.rust_slice_from(start))
{
Ok(attr) => attr,
Err(e) => {
log!("Error: failed to parse derive(java) attribute: {}", e);
return Ok(true);
}
};
reflector.reflect_and_cache(
&derive_java_attr.method_selector.class_name(),
Span::call_site(),
)?;
watch_file = true;
}
Ok(watch_file)
}

/// Representation of attributes like `#[java(java.lang.Long)]`
#[derive(Debug)]
struct DeriveJavaAttr {
method_selector: MethodSelector,
}

impl syn::parse::Parse for DeriveJavaAttr {
fn parse(input: ParseStream) -> syn::Result<Self> {
let attributes = input.call(Attribute::parse_outer)?;
for attr in attributes {
if !attr.path().is_ident("java") {
continue;
}
let derive_tokens = attr.meta.require_list()?.tokens.clone();
let mut parser: Parser = derive_tokens.into();
let method_selector = MethodSelector::parse(&mut parser)?.ok_or(syn::Error::new(
input.span(),
"expected a class in the attribute",
))?;
return Ok(DeriveJavaAttr { method_selector });
}
Err(syn::Error::new(
input.span(),
"expected #[java(...)] attribute",
))
}
}
27 changes: 23 additions & 4 deletions duchess-build-rs/src/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ use std::path::{Path, PathBuf};

use walkdir::WalkDir;

use crate::log;

pub(crate) struct File {
pub(crate) path: PathBuf,
pub(crate) contents: String,
}

pub fn rs_files(path: &Path) -> impl Iterator<Item = anyhow::Result<File>> {
pub fn rs_files(path: impl AsRef<Path>) -> impl Iterator<Item = anyhow::Result<File>> {
WalkDir::new(path)
.into_iter()
.filter_map(|entry| -> Option<anyhow::Result<File>> {
Expand Down Expand Up @@ -51,24 +53,41 @@ impl File {
/// This is used when we are preprocessing and we find
/// some kind of macro invocation. We want to grab all
/// the text that may be part of it and pass it into `syn`.
// TODO: this should actually return an error, its basically never right to return the whole file
pub fn rust_slice_from(&self, offset: usize) -> &str {
let mut counter = 0;
let terminator = self.contents[offset..].char_indices().find(|&(_, c)| {
if c == '{' || c == '[' || c == '(' {
counter += 1;
} else if c == '}' || c == ']' || c == ')' {
counter -= 1;

if counter == 0 {
return true;
}

counter -= 1;
}

false
});
if terminator.is_none() {
log!("rust slice ran to end of file {counter}");
}
match terminator {
Some((i, _)) => &self.contents[offset..offset + i],
Some((i, _)) => &self.contents[offset..offset + i + 1],
None => &self.contents[offset..],
}
}
}

#[cfg(test)]
mod test {
#[test]
fn test_rust_slice() {
for file in super::rs_files("test-files") {
let file = file.unwrap();
for offset in file.contents.char_indices().map(|(i, _)| i) {
let _ = file.rust_slice_from(offset);
}
}
}
}
11 changes: 7 additions & 4 deletions duchess-build-rs/src/impl_java_trait.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use duchess_reflect::{class_info::ClassRef, reflect::Reflector};
use duchess_reflect::{
class_info::ClassRef,
reflect::{JavapReflector, Reflect},
};
use proc_macro2::{Span, TokenStream};
use syn::spanned::Spanned;

use crate::{files::File, java_compiler::JavaCompiler, shim_writer::ShimWriter};
use crate::{files::File, java_compiler::JavaCompiler, log, shim_writer::ShimWriter};

pub fn process_impl(compiler: &JavaCompiler, file: &File, offset: usize) -> anyhow::Result<()> {
let the_impl: JavaInterfaceImpl = syn::parse_str(file.rust_slice_from(offset))?;
Expand All @@ -29,7 +32,7 @@ impl syn::parse::Parse for JavaInterfaceImpl {

impl JavaInterfaceImpl {
fn generate_shim(&self, compiler: &JavaCompiler) -> anyhow::Result<()> {
let reflector = Reflector::new(compiler.configuration());
let mut reflector = JavapReflector::new(compiler.configuration());
let (java_interface_ref, java_interface_span) = self.java_interface()?;
let java_interface_info =
reflector.reflect(&java_interface_ref.name, java_interface_span)?;
Expand All @@ -45,7 +48,7 @@ impl JavaInterfaceImpl {

compiler.compile_to_rs_file(&java_file)?;

eprintln!("compiled to {}", java_file.rs_path.display());
log!("compiled to {}", java_file.rs_path.display());

Ok(())
}
Expand Down
97 changes: 84 additions & 13 deletions duchess-build-rs/src/java_package_macro.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,60 @@
use std::time::Instant;

use anyhow::Context;
use duchess_reflect::{argument::DuchessDeclaration, parse::Parser, reflect::Reflector};
use proc_macro2::TokenStream;
use duchess_reflect::{argument::DuchessDeclaration, parse::Parser, reflect::JavapReflector};
use proc_macro2::{Span, TokenStream};

use crate::{files::File, java_package_macro, log, re};

use crate::{files::File, java_compiler::JavaCompiler};
pub fn process_file(rs_file: &File, reflector: &mut JavapReflector) -> anyhow::Result<bool> {
let mut watch_file = false;
for capture in re::java_package().captures_iter(&rs_file.contents) {
let std::ops::Range { start, end: _ } = capture.get(0).unwrap().range();
log!(
"Found `java_package!` macro at {}:{}",
rs_file.path.display(),
rs_file.contents[..start].lines().count()
);
java_package_macro::process_macro(reflector, &rs_file, start)
.with_context(|| format!("failed to process macro {}", rs_file.slug(start)))?;
watch_file = true;
}
Ok(watch_file)
}

pub fn process_macro(compiler: &JavaCompiler, file: &File, offset: usize) -> anyhow::Result<()> {
let the_impl: JavaPackageMacro = syn::parse_str(file.rust_slice_from(offset))
.with_context(|| format!("{} failed to parse java_package macro", file.slug(offset),))?;
fn process_macro(reflector: &mut JavapReflector, file: &File, offset: usize) -> anyhow::Result<()> {
let the_impl: JavaPackageMacro = match syn::parse_str(file.rust_slice_from(offset))
.with_context(|| {
format!(
"{} failed to parse java_package macro as Rust code",
file.slug(offset),
)
})
.with_context(|| format!("full contents:\n>>>>{}<<<", file.rust_slice_from(offset)))
{
Ok(package) => package,
Err(e) => {
// we'll let rustc deal with this later
log!(
"Warning: failed to parse java_package macro as Rust code, ignoring it. Error: {}",
e
);
return Ok(());
}
};

the_impl.parse_contents(compiler)?;
let contents = match the_impl.parse_contents() {
Ok(decl) => decl,
Err(e) => {
// we'll let rustc deal with this later
log!(
"Warning: failed to parse java_package macro as Duchess code, ignoring it. Error: {:?}",
e
);
return Ok(());
}
};
cache_all_classes(contents, reflector).with_context(|| "failed to execute javap")?;
Ok(())
}

Expand All @@ -30,12 +76,37 @@ impl syn::parse::Parse for JavaPackageMacro {
}

impl JavaPackageMacro {
fn parse_contents(self, compiler: &JavaCompiler) -> anyhow::Result<()> {
fn parse_contents(self) -> anyhow::Result<DuchessDeclaration> {
let input = self.invocation.mac.tokens;
let decl = Parser::from(input).parse::<DuchessDeclaration>()?;
let mut reflector = Reflector::new(compiler.configuration());
let _root_map = decl.to_root_map(&mut reflector)?;
// FIXME: next step is to dump the data from the reflector into OUT_DIR
Ok(())
Ok(Parser::from(input).parse::<DuchessDeclaration>()?)
}
}

fn cache_all_classes(
decl: DuchessDeclaration,
reflector: &mut JavapReflector,
) -> anyhow::Result<()> {
let _root_map = decl.to_root_map(reflector)?;
for class in _root_map.class_names() {
// forcibly reflect every class
let now = Instant::now();
reflector.reflect_and_cache(&class, Span::call_site())?;
log!("Reflecting {} took {:?}", class, now.elapsed());
}
Ok(())
}

#[cfg(test)]
mod test {
use duchess_reflect::{config::Configuration, reflect::JavapReflector};

#[test]
fn process_file() {
let mut compiler = JavapReflector::new(&Configuration::new());
let rs_file = crate::files::File {
path: "test-files/java_package_1.rs".into(),
contents: include_str!("../test-files/java_package_1.rs").to_string(),
};
super::process_file(&rs_file, &mut compiler).unwrap();
}
}
Loading
Loading