diff --git a/bin/node/runtime/Cargo.toml b/bin/node/runtime/Cargo.toml index 66e9dece177bb..975581d6a0f12 100644 --- a/bin/node/runtime/Cargo.toml +++ b/bin/node/runtime/Cargo.toml @@ -35,7 +35,7 @@ sp-version = { version = "2.0.0-alpha.2", default-features = false, path = "../. # frame dependencies frame-executive = { version = "2.0.0-alpha.2", default-features = false, path = "../../../frame/executive" } -frame-benchmarking = { version = "2.0.0-alpha.2", default-features = false, path = "../../../frame/benchmarking" } +frame-benchmarking = { version = "2.0.0-alpha.2", default-features = false, path = "../../../frame/benchmarking", optional = true } frame-support = { version = "2.0.0-alpha.2", default-features = false, path = "../../../frame/support" } frame-system = { version = "2.0.0-alpha.2", default-features = false, path = "../../../frame/system" } frame-system-rpc-runtime-api = { version = "2.0.0-alpha.2", default-features = false, path = "../../../frame/system/rpc/runtime-api/" } @@ -132,3 +132,4 @@ std = [ "pallet-recovery/std", "pallet-vesting/std", ] +runtime-benchmarks = ["frame-benchmarking"] diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs index 85889a50c20b9..c2ad0d871e905 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -83,7 +83,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { // implementation changes and behavior does not, then leave spec_version as // is and increment impl_version. spec_version: 227, - impl_version: 0, + impl_version: 1, apis: RUNTIME_API_VERSIONS, }; @@ -816,6 +816,7 @@ impl_runtime_apis! { } } + #[cfg(feature = "runtime-benchmarks")] impl frame_benchmarking::Benchmark for Runtime { fn dispatch_benchmark( module: Vec, diff --git a/frame/balances/Cargo.toml b/frame/balances/Cargo.toml index c4d963a2f4599..b4576468be512 100644 --- a/frame/balances/Cargo.toml +++ b/frame/balances/Cargo.toml @@ -14,7 +14,7 @@ codec = { package = "parity-scale-codec", version = "1.0.0", default-features = sp-std = { version = "2.0.0-alpha.2", default-features = false, path = "../../primitives/std" } sp-io = { version = "2.0.0-alpha.2", default-features = false, path = "../../primitives/io" } sp-runtime = { version = "2.0.0-alpha.2", default-features = false, path = "../../primitives/runtime" } -frame-benchmarking = { version = "2.0.0-alpha.2", default-features = false, path = "../benchmarking" } +frame-benchmarking = { version = "2.0.0-alpha.2", default-features = false, path = "../benchmarking", optional = true } frame-support = { version = "2.0.0-alpha.2", default-features = false, path = "../support" } frame-system = { version = "2.0.0-alpha.2", default-features = false, path = "../system" } @@ -34,3 +34,4 @@ std = [ "frame-support/std", "frame-system/std", ] +runtime-benchmarks = ["frame-benchmarking"] diff --git a/frame/balances/src/lib.rs b/frame/balances/src/lib.rs index 4dbaf4b8a886d..b4a1b810df325 100644 --- a/frame/balances/src/lib.rs +++ b/frame/balances/src/lib.rs @@ -155,6 +155,7 @@ mod tests_composite; #[cfg(test)] #[macro_use] mod tests; +#[cfg(feature = "runtime-benchmarks")] mod benchmarking; use sp_std::prelude::*; diff --git a/frame/identity/Cargo.toml b/frame/identity/Cargo.toml index 4f6bc9ca4a66d..f8fa907c4730d 100644 --- a/frame/identity/Cargo.toml +++ b/frame/identity/Cargo.toml @@ -15,7 +15,7 @@ enumflags2 = { version = "0.6.2" } sp-std = { version = "2.0.0-alpha.2", default-features = false, path = "../../primitives/std" } sp-io = { version = "2.0.0-alpha.2", default-features = false, path = "../../primitives/io" } sp-runtime = { version = "2.0.0-alpha.2", default-features = false, path = "../../primitives/runtime" } -frame-benchmarking = { version = "2.0.0-alpha.2", default-features = false, path = "../benchmarking" } +frame-benchmarking = { version = "2.0.0-alpha.2", default-features = false, path = "../benchmarking", optional = true } frame-support = { version = "2.0.0-alpha.2", default-features = false, path = "../support" } frame-system = { version = "2.0.0-alpha.2", default-features = false, path = "../system" } @@ -35,3 +35,4 @@ std = [ "frame-support/std", "frame-system/std", ] +runtime-benchmarks = ["frame-benchmarking"] diff --git a/frame/identity/src/lib.rs b/frame/identity/src/lib.rs index 68b7905961da8..cb2071d5d9ea8 100644 --- a/frame/identity/src/lib.rs +++ b/frame/identity/src/lib.rs @@ -78,6 +78,7 @@ use frame_support::{ }; use frame_system::{self as system, ensure_signed, ensure_root}; +#[cfg(feature = "runtime-benchmarks")] pub mod benchmarking; type BalanceOf = <::Currency as Currency<::AccountId>>::Balance; diff --git a/frame/system/Cargo.toml b/frame/system/Cargo.toml index cb5b52b8ca25c..cfd149c4aa7aa 100644 --- a/frame/system/Cargo.toml +++ b/frame/system/Cargo.toml @@ -36,6 +36,7 @@ std = [ "sp-runtime/std", "sp-version/std", ] +runtime-benchmarks = [] [[bench]] name = "bench" diff --git a/frame/system/src/lib.rs b/frame/system/src/lib.rs index f89c9efbd2962..6cac08117afcf 100644 --- a/frame/system/src/lib.rs +++ b/frame/system/src/lib.rs @@ -885,7 +885,7 @@ impl Module { /// Set the block number to something in particular. Can be used as an alternative to /// `initialize` for tests that don't need to bother with the other environment entries. - #[cfg(any(feature = "std", test))] + #[cfg(any(feature = "std", feature = "runtime-benchmarks", test))] pub fn set_block_number(n: T::BlockNumber) { >::put(n); } diff --git a/frame/timestamp/Cargo.toml b/frame/timestamp/Cargo.toml index 1c8dfa1dfba49..ccfdbef292b19 100644 --- a/frame/timestamp/Cargo.toml +++ b/frame/timestamp/Cargo.toml @@ -14,10 +14,10 @@ documentation = "https://docs.rs/pallet-timestamp" serde = { version = "1.0.101", optional = true } codec = { package = "parity-scale-codec", version = "1.0.0", default-features = false, features = ["derive"] } sp-std = { version = "2.0.0-alpha.2", default-features = false, path = "../../primitives/std" } -sp-io = { version = "2.0.0-alpha.2", default-features = false, path = "../../primitives/io" } +sp-io = { version = "2.0.0-alpha.2", default-features = false, path = "../../primitives/io", optional = true } sp-runtime = { version = "2.0.0-alpha.2", default-features = false, path = "../../primitives/runtime" } sp-inherents = { version = "2.0.0-alpha.2", default-features = false, path = "../../primitives/inherents" } -frame-benchmarking = { version = "2.0.0-alpha.2", default-features = false, path = "../benchmarking" } +frame-benchmarking = { version = "2.0.0-alpha.2", default-features = false, path = "../benchmarking", optional = true } frame-support = { version = "2.0.0-alpha.2", default-features = false, path = "../support" } frame-system = { version = "2.0.0-alpha.2", default-features = false, path = "../system" } sp-timestamp = { version = "2.0.0-alpha.2", default-features = false, path = "../../primitives/timestamp" } @@ -40,3 +40,4 @@ std = [ "frame-system/std", "sp-timestamp/std" ] +runtime-benchmarks = ["frame-benchmarking", "sp-io"] diff --git a/frame/timestamp/src/lib.rs b/frame/timestamp/src/lib.rs index 1c3fec2112a08..2a37dfdddb62a 100644 --- a/frame/timestamp/src/lib.rs +++ b/frame/timestamp/src/lib.rs @@ -90,6 +90,7 @@ #![cfg_attr(not(feature = "std"), no_std)] +#[cfg(feature = "runtime-benchmarks")] mod benchmarking; use sp_std::{result, cmp}; diff --git a/primitives/api/proc-macro/src/impl_runtime_apis.rs b/primitives/api/proc-macro/src/impl_runtime_apis.rs index 770a843bfa6c1..1c476e17f6b62 100644 --- a/primitives/api/proc-macro/src/impl_runtime_apis.rs +++ b/primitives/api/proc-macro/src/impl_runtime_apis.rs @@ -27,7 +27,7 @@ use proc_macro2::{Span, TokenStream}; use quote::quote; use syn::{ - spanned::Spanned, parse_macro_input, Ident, Type, ItemImpl, Path, Signature, + spanned::Spanned, parse_macro_input, Ident, Type, ItemImpl, Path, Signature, Attribute, ImplItem, parse::{Parse, ParseStream, Result, Error}, PathArguments, GenericArgument, TypePath, fold::{self, Fold}, parse_quote, }; @@ -141,7 +141,7 @@ fn extract_runtime_block_ident(trait_: &Path) -> Result<&TypePath> { fn generate_impl_calls( impls: &[ItemImpl], input: &Ident -) -> Result> { +) -> Result)>> { let mut impl_calls = Vec::new(); for impl_ in impls { @@ -162,9 +162,12 @@ fn generate_impl_calls( &impl_trait )?; - impl_calls.push( - (impl_trait_ident.clone(), method.sig.ident.clone(), impl_call) - ); + impl_calls.push(( + impl_trait_ident.clone(), + method.sig.ident.clone(), + impl_call, + filter_cfg_attrs(&impl_.attrs), + )); } } } @@ -178,9 +181,12 @@ fn generate_dispatch_function(impls: &[ItemImpl]) -> Result { let c = generate_crate_access(HIDDEN_INCLUDES_ID); let impl_calls = generate_impl_calls(impls, &data)? .into_iter() - .map(|(trait_, fn_name, impl_)| { + .map(|(trait_, fn_name, impl_, attrs)| { let name = prefix_function_with_trait(&trait_, &fn_name); - quote!( #name => Some(#c::Encode::encode(&{ #impl_ })), ) + quote!( + #( #attrs )* + #name => Some(#c::Encode::encode(&{ #impl_ })), + ) }); Ok(quote!( @@ -200,13 +206,14 @@ fn generate_wasm_interface(impls: &[ItemImpl]) -> Result { let c = generate_crate_access(HIDDEN_INCLUDES_ID); let impl_calls = generate_impl_calls(impls, &input)? .into_iter() - .map(|(trait_, fn_name, impl_)| { + .map(|(trait_, fn_name, impl_, attrs)| { let fn_name = Ident::new( &prefix_function_with_trait(&trait_, &fn_name), Span::call_site() ); quote!( + #( #attrs )* #[cfg(not(feature = "std"))] #[no_mangle] pub fn #fn_name(input_data: *mut u8, input_len: usize) -> u64 { @@ -447,6 +454,7 @@ fn generate_api_impl_for_runtime(impls: &[ItemImpl]) -> Result { let trait_ = extend_with_runtime_decl_path(trait_); impl_.trait_.as_mut().unwrap().1 = trait_; + impl_.attrs = filter_cfg_attrs(&impl_.attrs); impls_prepared.push(impl_); } @@ -622,6 +630,8 @@ impl<'a> Fold for ApiRuntimeImplToApiRuntimeApiImpl<'a> { } ); + input.attrs = filter_cfg_attrs(&input.attrs); + // The implementation for the `RuntimeApiImpl` is only required when compiling with // the feature `std` or `test`. input.attrs.push(parse_quote!( #[cfg(any(feature = "std", test))] )); @@ -695,8 +705,12 @@ fn generate_runtime_api_versions(impls: &[ItemImpl]) -> Result { let id: Path = parse_quote!( #path ID ); let version: Path = parse_quote!( #path VERSION ); + let attrs = filter_cfg_attrs(&impl_.attrs); - result.push(quote!( (#id, #version) )); + result.push(quote!( + #( #attrs )* + (#id, #version) + )); } let c = generate_crate_access(HIDDEN_INCLUDES_ID); @@ -745,3 +759,32 @@ fn impl_runtime_apis_impl_inner(api_impls: &[ItemImpl]) -> Result { ) ) } + +// Filters all attributes except the cfg ones. +fn filter_cfg_attrs(attrs: &[Attribute]) -> Vec { + attrs.into_iter().filter(|a| a.path.is_ident("cfg")).cloned().collect() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn filter_non_cfg_attributes() { + let cfg_std: Attribute = parse_quote!(#[cfg(feature = "std")]); + let cfg_benchmarks: Attribute = parse_quote!(#[cfg(feature = "runtime-benchmarks")]); + + let attrs = vec![ + cfg_std.clone(), + parse_quote!(#[derive(Debug)]), + parse_quote!(#[test]), + cfg_benchmarks.clone(), + parse_quote!(#[allow(non_camel_case_types)]), + ]; + + let filtered = filter_cfg_attrs(&attrs); + assert_eq!(filtered.len(), 2); + assert_eq!(cfg_std, filtered[0]); + assert_eq!(cfg_benchmarks, filtered[1]); + } +}