From 47622d691299f47f4babe75331ee3e8c47ab15e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20K=C3=B6cher?= Date: Mon, 14 Feb 2022 21:12:48 +0100 Subject: [PATCH] wasm-builder: Support latest nightly (#10837) * wasm-builder: Support latest nightly With latest nightly, aka rust version 1.60+ namespaced features are added. This changes the handling of optional dependencies. We currently have features that enable optional dependencies when `std` is enabled. This was before no problem, but now the wasm-builder detects them as enabled. To support the transition period until 1.60 is released as stable, this pr adds an heuristic to not enable these optional crates in the wasm build when they are enabled in the `std` feature. This heuristic fails when someones enables these optional dependencies from the outside as well as via the `std` feature, however we hope that no one is doing this at the moment. When namespaced features are enabled, these dependencies needs to be enabled using `dep:dependency-name` to solve this properly. https://doc.rust-lang.org/cargo/reference/unstable.html#namespaced-features * Remove accidentally added features --- substrate/Cargo.lock | 1 - substrate/bin/node/runtime/Cargo.toml | 4 +--- substrate/primitives/keyring/Cargo.toml | 1 - .../utils/wasm-builder/src/wasm_project.rs | 23 ++++++++++++++++--- 4 files changed, 21 insertions(+), 8 deletions(-) diff --git a/substrate/Cargo.lock b/substrate/Cargo.lock index feb721e609..e97c90e492 100644 --- a/substrate/Cargo.lock +++ b/substrate/Cargo.lock @@ -4916,7 +4916,6 @@ dependencies = [ "sp-core", "sp-inherents", "sp-io", - "sp-keyring", "sp-npos-elections", "sp-offchain", "sp-runtime", diff --git a/substrate/bin/node/runtime/Cargo.toml b/substrate/bin/node/runtime/Cargo.toml index 2aad40b4f1..4f235a5117 100644 --- a/substrate/bin/node/runtime/Cargo.toml +++ b/substrate/bin/node/runtime/Cargo.toml @@ -35,7 +35,6 @@ sp-std = { version = "4.0.0", default-features = false, path = "../../../primiti sp-api = { version = "4.0.0-dev", default-features = false, path = "../../../primitives/api" } sp-runtime = { version = "5.0.0", default-features = false, path = "../../../primitives/runtime" } sp-staking = { version = "4.0.0-dev", default-features = false, path = "../../../primitives/staking" } -sp-keyring = { version = "5.0.0", optional = true, path = "../../../primitives/keyring" } sp-session = { version = "4.0.0-dev", default-features = false, path = "../../../primitives/session" } sp-transaction-pool = { version = "4.0.0-dev", default-features = false, path = "../../../primitives/transaction-pool" } sp-version = { version = "4.0.0-dev", default-features = false, path = "../../../primitives/version" } @@ -153,7 +152,6 @@ std = [ "sp-runtime/std", "sp-staking/std", "pallet-staking/std", - "sp-keyring", "sp-session/std", "pallet-sudo/std", "frame-support/std", @@ -178,7 +176,7 @@ std = [ "log/std", "frame-try-runtime/std", "sp-npos-elections/std", - "sp-io/std", + "sp-io/std", "pallet-child-bounties/std", ] runtime-benchmarks = [ diff --git a/substrate/primitives/keyring/Cargo.toml b/substrate/primitives/keyring/Cargo.toml index 5cf8a57450..1e5a234d39 100644 --- a/substrate/primitives/keyring/Cargo.toml +++ b/substrate/primitives/keyring/Cargo.toml @@ -13,7 +13,6 @@ readme = "README.md" [package.metadata.docs.rs] targets = ["x86_64-unknown-linux-gnu"] - [dependencies] sp-core = { version = "5.0.0", path = "../core" } sp-runtime = { version = "5.0.0", path = "../runtime" } diff --git a/substrate/utils/wasm-builder/src/wasm_project.rs b/substrate/utils/wasm-builder/src/wasm_project.rs index 20e176444c..e94703b610 100644 --- a/substrate/utils/wasm-builder/src/wasm_project.rs +++ b/substrate/utils/wasm-builder/src/wasm_project.rs @@ -341,13 +341,30 @@ fn project_enabled_features( ) -> Vec { let package = find_package_by_manifest_path(cargo_manifest, crate_metadata); + let std_enabled = package.features.get("std"); + let mut enabled_features = package .features - .keys() - .filter(|f| { + .iter() + .filter(|(f, v)| { let mut feature_env = f.replace("-", "_"); feature_env.make_ascii_uppercase(); + // If this is a feature that corresponds only to an optional dependency + // and this feature is enabled by the `std` feature, we assume that this + // is only done through the `std` feature. This is a bad heuristic and should + // be removed after namespaced features are landed: + // https://doc.rust-lang.org/cargo/reference/unstable.html#namespaced-features + // Then we can just express this directly in the `Cargo.toml` and do not require + // this heuristic anymore. However, for the transition phase between now and namespaced + // features already being present in nightly, we need this code to make + // runtimes compile with all the possible rustc versions. + if v.len() == 1 && v.get(0).map_or(false, |v| *v == format!("dep:{}", f)) { + if std_enabled.as_ref().map(|e| e.iter().any(|ef| ef == *f)).unwrap_or(false) { + return false + } + } + // We don't want to enable the `std`/`default` feature for the wasm build and // we need to check if the feature is enabled by checking the env variable. *f != "std" && @@ -355,7 +372,7 @@ fn project_enabled_features( .map(|v| v == "1") .unwrap_or_default() }) - .cloned() + .map(|d| d.0.clone()) .collect::>(); enabled_features.sort();