Skip to content

Commit

Permalink
Make sr-api-macros and srml-support renaming of crates (paritytec…
Browse files Browse the repository at this point in the history
…h#1757)

Procedural do not support `$crate` to get access to the crate where the
macro is defined. We use a hack to re-export the crate under a known
name. With rust edition 2018, people started to rename their crates in
`Cargo.toml`, but that breaks the re-export. This commit introduces
`proc-maco-crate` that supports finding the requested crate name, even
if it was renamed.
  • Loading branch information
bkchr authored Feb 11, 2019
1 parent cf6bd26 commit 9bb2125
Show file tree
Hide file tree
Showing 16 changed files with 135 additions and 35 deletions.
20 changes: 20 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions core/sr-api-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ quote = "0.6"
syn = { version = "^0.15.22", features = [ "full", "fold", "extra-traits", "visit" ] }
proc-macro2 = "0.4"
blake2-rfc = "0.2"
proc-macro-crate = "0.1.3"

[dev-dependencies]
substrate-client = { path = "../client" }
Expand Down
1 change: 1 addition & 0 deletions core/sr-api-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ extern crate proc_macro2;
extern crate quote;
extern crate syn;
extern crate blake2_rfc;
extern crate proc_macro_crate;

use proc_macro::TokenStream;

Expand Down
21 changes: 16 additions & 5 deletions core/sr-api-macros/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use proc_macro2::{TokenStream, Span};
use syn::{Result, Ident, FnDecl, parse_quote, Type, Pat, spanned::Spanned, FnArg, Error};
use quote::quote;
use std::env;
use proc_macro_crate::crate_name;

/// Unwrap the given result, if it is an error, `compile_error!` will be generated.
pub fn unwrap_or_error(res: Result<TokenStream>) -> TokenStream {
Expand All @@ -34,12 +35,22 @@ pub fn generate_hidden_includes(unique_id: &'static str) -> TokenStream {
TokenStream::new()
} else {
let mod_name = generate_hidden_includes_mod_name(unique_id);
quote!(
#[doc(hidden)]
mod #mod_name {
pub extern crate substrate_client as sr_api_client;
match crate_name("substrate-client") {
Ok(client_name) => {
let client_name = Ident::new(&client_name, Span::call_site());
quote!(
#[doc(hidden)]
mod #mod_name {
pub extern crate #client_name as sr_api_client;
}
)
},
Err(e) => {
let err = Error::new(Span::call_site(), &e).to_compile_error();
quote!( #err )
}
)
}

}.into()
}

Expand Down
20 changes: 20 additions & 0 deletions core/test-runtime/wasm/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
8 changes: 4 additions & 4 deletions node-template/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ parity-codec-derive = { version = "3.0", default-features = false }
rstd = { package = "sr-std", path = "../../core/sr-std", default_features = false }
runtime-io = { package = "sr-io", path = "../../core/sr-io", default_features = false }
version = { package = "sr-version", path = "../../core/sr-version", default_features = false }
srml-support = { path = "../../srml/support", default_features = false }
support = { package = "srml-support", path = "../../srml/support", default_features = false }
primitives = { package = "substrate-primitives", path = "../../core/primitives", default_features = false }
balances = { package = "srml-balances", path = "../../srml/balances", default_features = false }
consensus = { package = "srml-consensus", path = "../../srml/consensus", default_features = false }
Expand All @@ -24,7 +24,7 @@ system = { package = "srml-system", path = "../../srml/system", default_features
timestamp = { package = "srml-timestamp", path = "../../srml/timestamp", default_features = false }
sudo = { package = "srml-sudo", path = "../../srml/sudo", default_features = false }
runtime-primitives = { package = "sr-primitives", path = "../../core/sr-primitives", default_features = false }
substrate-client = { path = "../../core/client", default_features = false }
client = { package = "substrate-client", path = "../../core/client", default_features = false }
consensus-aura = { package = "substrate-consensus-aura-primitives", path = "../../core/consensus/aura/primitives", default_features = false }

[features]
Expand All @@ -33,10 +33,10 @@ std = [
"parity-codec/std",
"parity-codec-derive/std",
"primitives/std",
"substrate-client/std",
"client/std",
"rstd/std",
"runtime-io/std",
"srml-support/std",
"support/std",
"balances/std",
"executive/std",
"aura/std",
Expand Down
4 changes: 1 addition & 3 deletions node-template/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
#[macro_use]
extern crate serde_derive;

use substrate_client as client;

#[macro_use]
extern crate parity_codec_derive;

Expand Down Expand Up @@ -38,7 +36,7 @@ pub use timestamp::Call as TimestampCall;
pub use balances::Call as BalancesCall;
pub use runtime_primitives::{Permill, Perbill};
pub use timestamp::BlockPeriod;
pub use srml_support::{StorageValue, construct_runtime};
pub use support::{StorageValue, construct_runtime};

/// Alias to Ed25519 pubkey that identifies an account on the chain.
pub type AccountId = primitives::H256;
Expand Down
20 changes: 20 additions & 0 deletions node-template/runtime/wasm/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions node/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ safe-mix = { version = "1.0", default-features = false }
parity-codec-derive = { version = "3.0" }
parity-codec = { version = "3.0", default-features = false }
substrate-primitives = { path = "../../core/primitives", default-features = false }
substrate-client = { path = "../../core/client", default-features = false }
client = { package = "substrate-client", path = "../../core/client", default-features = false }
rstd = { package = "sr-std", path = "../../core/sr-std", default-features = false }
runtime_primitives = { package = "sr-primitives", path = "../../core/sr-primitives", default-features = false }
version = { package = "sr-version", path = "../../core/sr-version", default-features = false }
srml-support = { path = "../../srml/support", default-features = false }
support = { package = "srml-support", path = "../../srml/support", default-features = false }
aura = { package = "srml-aura", path = "../../srml/aura", default-features = false }
balances = { package = "srml-balances", path = "../../srml/balances", default-features = false }
consensus = { package = "srml-consensus", path = "../../srml/consensus", default-features = false }
Expand Down Expand Up @@ -45,7 +45,7 @@ std = [
"substrate-primitives/std",
"rstd/std",
"runtime_primitives/std",
"srml-support/std",
"support/std",
"balances/std",
"consensus/std",
"contract/std",
Expand All @@ -65,7 +65,7 @@ std = [
"node-primitives/std",
"serde/std",
"safe-mix/std",
"substrate-client/std",
"client/std",
"consensus_aura/std",
"rustc-hex",
"hex-literal",
Expand Down
11 changes: 5 additions & 6 deletions node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,16 @@ extern crate runtime_primitives;
use rstd::prelude::*;
use parity_codec_derive::{Encode, Decode};
#[cfg(feature = "std")]
use srml_support::{Serialize, Deserialize};
use srml_support::construct_runtime;
use support::{Serialize, Deserialize};
use support::construct_runtime;
use substrate_primitives::u32_trait::{_2, _4};
use node_primitives::{
AccountId, AccountIndex, Balance, BlockNumber, Hash, Index, SessionKey, Signature
};
use grandpa::fg_primitives::{self, ScheduledChange};
use substrate_client::impl_runtime_apis;
use substrate_client::{
use client::{
block_builder::api::{self as block_builder_api, InherentData, CheckInherentsResult},
runtime_api as client_api,
runtime_api as client_api, impl_runtime_apis
};
use runtime_primitives::ApplyResult;
use runtime_primitives::transaction_validity::TransactionValidity;
Expand All @@ -58,7 +57,7 @@ pub use consensus::Call as ConsensusCall;
pub use timestamp::Call as TimestampCall;
pub use balances::Call as BalancesCall;
pub use runtime_primitives::{Permill, Perbill};
pub use srml_support::StorageValue;
pub use support::StorageValue;

/// Runtime version.
pub const VERSION: RuntimeVersion = RuntimeVersion {
Expand Down
Loading

0 comments on commit 9bb2125

Please sign in to comment.