From 6db586295bd3d01bf3fb9c28934fd64e6390aadb Mon Sep 17 00:00:00 2001 From: Tianjiao Huang Date: Wed, 9 Jun 2021 23:53:47 -0700 Subject: [PATCH] add placeholders for interface attributes the interface attributes are only recognized by the redIDL compiler but not the Rust compiler. The Rust compiler will spit out errors if there's no placeholder implementations for them --- interface/Cargo.lock | 4 +-- interface/Makefile | 23 +++++++++++-- interface/placeholder_dependencies.toml | 2 ++ interface/src/lib.rs | 2 ++ .../Cargo.lock | 33 ++++++++++++++++++ .../Cargo.toml | 14 ++++++++ .../src/lib.rs | 34 +++++++++++++++++++ tools/redIDL | 2 +- 8 files changed, 108 insertions(+), 6 deletions(-) create mode 100644 interface/placeholder_dependencies.toml create mode 100644 lib/external/interface_attribute_placeholder/Cargo.lock create mode 100644 lib/external/interface_attribute_placeholder/Cargo.toml create mode 100644 lib/external/interface_attribute_placeholder/src/lib.rs diff --git a/interface/Cargo.lock b/interface/Cargo.lock index 436bf268..6b00af05 100644 --- a/interface/Cargo.lock +++ b/interface/Cargo.lock @@ -133,9 +133,9 @@ dependencies = [ [[package]] name = "quote" -version = "1.0.7" +version = "1.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aa563d17ecb180e500da1cfd2b028310ac758de548efdd203e18f283af693f37" +checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7" dependencies = [ "proc-macro2", ] diff --git a/interface/Makefile b/interface/Makefile index 94b84bf0..211d4364 100644 --- a/interface/Makefile +++ b/interface/Makefile @@ -8,17 +8,33 @@ DOMAIN_CREATE_OUTPUT_PATH = ../kernel/src/generated_domain_create.rs .PHONY: all all: $(LIB_RS) $(MANIFEST_PATH) -# Copy Cargo.toml and fix dependency path +# Copy Cargo.toml, and fix dependency path, remove `interface_attribute_placeholder`, and add +# missing dependencies. $(MANIFEST_PATH): Cargo.toml missing_dependencies.toml mkdir -p $(OUTPUT_DIR) sed -E 's/path\s*=\s*"/path = "..\//' $(word 1,$^) > $@ cat $(word 2,$^) >> $@ -# Merge all files and inject macro for expansion +# Add placeholder dependencies and run cargo check. +# Then restore the Cargo.toml. +# TODO(issue #49): move the operation to a temp folder to prevent contaminating the source folder. +.PHONY: compiler_check +compiler_check: + cp Cargo.toml Cargo.toml.backup + cat Cargo.toml.backup placeholder_dependencies.toml > Cargo.toml + cargo check + mv Cargo.toml.backup Cargo.toml + +# Merge all files and inject macro for expansion. +# Since the placeholder removes the interface attributes from the traits, we need to remove +# them from the file. .PHONY: $(OUTPUT_DIR)/merged.rs -$(OUTPUT_DIR)/merged.rs: +$(OUTPUT_DIR)/merged.rs: compiler_check mkdir -p $(OUTPUT_DIR) + cp src/lib.rs src/lib.rs.backup + tac src/lib.rs.backup | sed '/extern crate interface_attribute_placeholder;/I,+1 d' | tac > src/lib.rs cargo expand > $@ + mv src/lib.rs.backup src/lib.rs # Inject use statements $(LIB_RS): $(OUTPUT_DIR)/merged.rs @@ -27,6 +43,7 @@ $(LIB_RS): $(OUTPUT_DIR)/merged.rs .PHONY: clean clean: + cargo clean -rm -rf $(OUTPUT_DIR) -rm -f $(DOMAIN_CREATE_OUTPUT_PATH) diff --git a/interface/placeholder_dependencies.toml b/interface/placeholder_dependencies.toml new file mode 100644 index 00000000..6b09b8d7 --- /dev/null +++ b/interface/placeholder_dependencies.toml @@ -0,0 +1,2 @@ +[dependencies.interface_attribute_placeholder] +path = "../lib/external/interface_attribute_placeholder" diff --git a/interface/src/lib.rs b/interface/src/lib.rs index 898618b3..1159228a 100644 --- a/interface/src/lib.rs +++ b/interface/src/lib.rs @@ -19,6 +19,8 @@ extern crate alloc; extern crate num_derive; #[macro_use] extern crate bitflags; +#[macro_use] +extern crate interface_attribute_placeholder; pub mod bdev; pub mod dom_c; diff --git a/lib/external/interface_attribute_placeholder/Cargo.lock b/lib/external/interface_attribute_placeholder/Cargo.lock new file mode 100644 index 00000000..de8381b8 --- /dev/null +++ b/lib/external/interface_attribute_placeholder/Cargo.lock @@ -0,0 +1,33 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "interface_attribute_placeholder" +version = "0.1.0" +dependencies = [ + "proc-macro2", + "quote", +] + +[[package]] +name = "proc-macro2" +version = "1.0.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0d8caf72986c1a598726adc988bb5984792ef84f5ee5aa50209145ee8077038" +dependencies = [ + "unicode-xid", +] + +[[package]] +name = "quote" +version = "1.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "unicode-xid" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" diff --git a/lib/external/interface_attribute_placeholder/Cargo.toml b/lib/external/interface_attribute_placeholder/Cargo.toml new file mode 100644 index 00000000..02b32fb6 --- /dev/null +++ b/lib/external/interface_attribute_placeholder/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "interface_attribute_placeholder" +version = "0.1.0" +authors = ["Redleaf team "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[lib] +proc-macro = true + +[dependencies] +quote = "1.0.9" +proc-macro2 = "1.0" diff --git a/lib/external/interface_attribute_placeholder/src/lib.rs b/lib/external/interface_attribute_placeholder/src/lib.rs new file mode 100644 index 00000000..e45c046d --- /dev/null +++ b/lib/external/interface_attribute_placeholder/src/lib.rs @@ -0,0 +1,34 @@ +///! Include multiple attributes that +use proc_macro::TokenStream; + +macro_rules! generate_placeholder_attributes { + () => {}; + ($attr:ident) => { + #[doc = "Placeholder attribute; noop besides removing the attribute itself."] + #[proc_macro_attribute] + pub fn $attr(_attr: TokenStream, item: TokenStream) -> TokenStream { + item + } + }; + ($attr:ident, $($attrs:tt)*) => { + generate_placeholder_attributes!($attr); + generate_placeholder_attributes!($($attrs)*); + }; +} + +generate_placeholder_attributes! { + placeholder, + interface, + domain_create, + domain_create_blob, +} +// #[proc_macro_attribute] +// pub fn a(mut attr: TokenStream, item: TokenStream) -> TokenStream { +// let attr = proc_macro2::TokenStream::from(attr); +// let item = proc_macro2::TokenStream::from(item); +// let output = quote::quote! { +// #[$attr(#attr)] +// #item +// }; +// output.into() +// } \ No newline at end of file diff --git a/tools/redIDL b/tools/redIDL index 1a17b657..2655791d 160000 --- a/tools/redIDL +++ b/tools/redIDL @@ -1 +1 @@ -Subproject commit 1a17b657167d2bbb66ea1b40e0ef9ab3de6f3a88 +Subproject commit 2655791d20acf194416008cc54d0ea2f4371b07c