Run cargo fmt on the whole code base (#9394)

* Run cargo fmt on the whole code base

* Second run

* Add CI check

* Fix compilation

* More unnecessary braces

* Handle weights

* Use --all

* Use correct attributes...

* Fix UI tests

* AHHHHHHHHH

* 🤦

* Docs

* Fix compilation

* 🤷

* Please stop

* 🤦 x 2

* More

* make rustfmt.toml consistent with polkadot

Co-authored-by: André Silva <andrerfosilva@gmail.com>
This commit is contained in:
Bastian Köcher
2021-07-21 16:32:32 +02:00
committed by GitHub
parent d451c38c1c
commit 7b56ab15b4
1010 changed files with 53339 additions and 51208 deletions
+18 -22
View File
@@ -15,7 +15,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use std::{env, path::{PathBuf, Path}, process};
use std::{
env,
path::{Path, PathBuf},
process,
};
/// Returns the manifest dir from the `CARGO_MANIFEST_DIR` env.
fn get_manifest_dir() -> PathBuf {
@@ -50,10 +54,7 @@ impl WasmBuilderSelectProject {
/// Use the given `path` as project for building the WASM binary.
///
/// Returns an error if the given `path` does not points to a `Cargo.toml`.
pub fn with_project(
self,
path: impl Into<PathBuf>,
) -> Result<WasmBuilder, &'static str> {
pub fn with_project(self, path: impl Into<PathBuf>) -> Result<WasmBuilder, &'static str> {
let path = path.into();
if path.ends_with("Cargo.toml") && path.exists() {
@@ -97,9 +98,7 @@ pub struct WasmBuilder {
impl WasmBuilder {
/// Create a new instance of the builder.
pub fn new() -> WasmBuilderSelectProject {
WasmBuilderSelectProject {
_ignore: (),
}
WasmBuilderSelectProject { _ignore: () }
}
/// Enable exporting `__heap_base` as global variable in the WASM binary.
@@ -147,9 +146,8 @@ impl WasmBuilder {
/// Build the WASM binary.
pub fn build(self) {
let out_dir = PathBuf::from(env::var("OUT_DIR").expect("`OUT_DIR` is set by cargo!"));
let file_path = out_dir.join(
self.file_name.clone().unwrap_or_else(|| "wasm_binary.rs".into()),
);
let file_path =
out_dir.join(self.file_name.clone().unwrap_or_else(|| "wasm_binary.rs".into()));
if check_skip_build() {
// If we skip the build, we still want to make sure to be called when an env variable
@@ -158,7 +156,7 @@ impl WasmBuilder {
provide_dummy_wasm_binary_if_not_exist(&file_path);
return;
return
}
build_project(
@@ -179,13 +177,17 @@ impl WasmBuilder {
fn generate_crate_skip_build_env_name() -> String {
format!(
"SKIP_{}_WASM_BUILD",
env::var("CARGO_PKG_NAME").expect("Package name is set").to_uppercase().replace('-', "_"),
env::var("CARGO_PKG_NAME")
.expect("Package name is set")
.to_uppercase()
.replace('-', "_"),
)
}
/// Checks if the build of the WASM binary should be skipped.
fn check_skip_build() -> bool {
env::var(crate::SKIP_BUILD_ENV).is_ok() || env::var(generate_crate_skip_build_env_name()).is_ok()
env::var(crate::SKIP_BUILD_ENV).is_ok() ||
env::var(generate_crate_skip_build_env_name()).is_ok()
}
/// Provide a dummy WASM binary if there doesn't exist one.
@@ -243,15 +245,9 @@ fn build_project(
);
let (wasm_binary, wasm_binary_bloaty) = if let Some(wasm_binary) = wasm_binary {
(
wasm_binary.wasm_binary_path_escaped(),
bloaty.wasm_binary_bloaty_path_escaped(),
)
(wasm_binary.wasm_binary_path_escaped(), bloaty.wasm_binary_bloaty_path_escaped())
} else {
(
bloaty.wasm_binary_bloaty_path_escaped(),
bloaty.wasm_binary_bloaty_path_escaped(),
)
(bloaty.wasm_binary_bloaty_path_escaped(), bloaty.wasm_binary_bloaty_path_escaped())
};
crate::write_file_if_changed(
+13 -13
View File
@@ -96,7 +96,12 @@
//! as well. For example if installing the rust nightly from 20.02.2020 using `rustup install nightly-2020-02-20`,
//! the wasm target needs to be installed as well `rustup target add wasm32-unknown-unknown --toolchain nightly-2020-02-20`.
use std::{env, fs, path::{PathBuf, Path}, process::Command, io::BufRead};
use std::{
env, fs,
io::BufRead,
path::{Path, PathBuf},
process::Command,
};
mod builder;
mod prerequisites;
@@ -144,18 +149,16 @@ fn copy_file_if_changed(src: PathBuf, dst: PathBuf) {
let dst_file = fs::read_to_string(&dst).ok();
if src_file != dst_file {
fs::copy(&src, &dst)
.unwrap_or_else(
|_| panic!("Copying `{}` to `{}` can not fail; qed", src.display(), dst.display())
);
fs::copy(&src, &dst).unwrap_or_else(|_| {
panic!("Copying `{}` to `{}` can not fail; qed", src.display(), dst.display())
});
}
}
/// Get a cargo command that compiles with nightly
fn get_nightly_cargo() -> CargoCommand {
let env_cargo = CargoCommand::new(
&env::var("CARGO").expect("`CARGO` env variable is always set by cargo"),
);
let env_cargo =
CargoCommand::new(&env::var("CARGO").expect("`CARGO` env variable is always set by cargo"));
let default_cargo = CargoCommand::new("cargo");
let rustup_run_nightly = CargoCommand::new_with_args("rustup", &["run", "nightly", "cargo"]);
let wasm_toolchain = env::var(WASM_BUILD_TOOLCHAIN).ok();
@@ -197,7 +200,7 @@ fn get_rustup_nightly(selected: Option<String>) -> Option<CargoCommand> {
}
latest_nightly?.trim_end_matches(&host).into()
}
},
};
Some(CargoCommand::new_with_args("rustup", &["run", &version, "cargo"]))
@@ -253,10 +256,7 @@ struct CargoCommandVersioned {
impl CargoCommandVersioned {
fn new(command: CargoCommand, version: String) -> Self {
Self {
command,
version,
}
Self { command, version }
}
/// Returns the `rustc` version.
@@ -15,12 +15,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use crate::{CargoCommandVersioned, CargoCommand, write_file_if_changed};
use crate::{write_file_if_changed, CargoCommand, CargoCommandVersioned};
use std::{fs, path::Path};
use tempfile::tempdir;
use ansi_term::Color;
use tempfile::tempdir;
/// Print an error message.
fn print_error_message(message: &str) -> String {
@@ -95,7 +95,7 @@ fn create_check_toolchain_project(project_dir: &Path) {
rustc_version.unwrap_or_else(|| "unknown rustc version".into()),
);
}
"#
"#,
);
// Just prints the `RURSTC_VERSION` environment variable that is being created by the
// `build.rs` script.
@@ -105,7 +105,7 @@ fn create_check_toolchain_project(project_dir: &Path) {
fn main() {
println!("{}", env!("RUSTC_VERSION"));
}
"#
"#,
);
}
@@ -120,7 +120,12 @@ fn check_wasm_toolchain_installed(
let manifest_path = temp.path().join("Cargo.toml").display().to_string();
let mut build_cmd = cargo_command.command();
build_cmd.args(&["build", "--target=wasm32-unknown-unknown", "--manifest-path", &manifest_path]);
build_cmd.args(&[
"build",
"--target=wasm32-unknown-unknown",
"--manifest-path",
&manifest_path,
]);
if super::color_output_enabled() {
build_cmd.arg("--color=always");
@@ -133,33 +138,27 @@ fn check_wasm_toolchain_installed(
build_cmd.env_remove("CARGO_TARGET_DIR");
run_cmd.env_remove("CARGO_TARGET_DIR");
build_cmd
.output()
.map_err(|_| err_msg.clone())
.and_then(|s|
if s.status.success() {
let version = run_cmd.output().ok().and_then(|o| String::from_utf8(o.stdout).ok());
Ok(CargoCommandVersioned::new(
cargo_command,
version.unwrap_or_else(|| "unknown rustc version".into()),
))
} else {
match String::from_utf8(s.stderr) {
Ok(ref err) if err.contains("linker `rust-lld` not found") => {
Err(print_error_message("`rust-lld` not found, please install it!"))
},
Ok(ref err) => Err(
format!(
"{}\n\n{}\n{}\n{}{}\n",
err_msg,
Color::Yellow.bold().paint("Further error information:"),
Color::Yellow.bold().paint("-".repeat(60)),
err,
Color::Yellow.bold().paint("-".repeat(60)),
)
),
Err(_) => Err(err_msg),
}
build_cmd.output().map_err(|_| err_msg.clone()).and_then(|s| {
if s.status.success() {
let version = run_cmd.output().ok().and_then(|o| String::from_utf8(o.stdout).ok());
Ok(CargoCommandVersioned::new(
cargo_command,
version.unwrap_or_else(|| "unknown rustc version".into()),
))
} else {
match String::from_utf8(s.stderr) {
Ok(ref err) if err.contains("linker `rust-lld` not found") =>
Err(print_error_message("`rust-lld` not found, please install it!")),
Ok(ref err) => Err(format!(
"{}\n\n{}\n{}\n{}{}\n",
err_msg,
Color::Yellow.bold().paint("Further error information:"),
Color::Yellow.bold().paint("-".repeat(60)),
err,
Color::Yellow.bold().paint("-".repeat(60)),
)),
Err(_) => Err(err_msg),
}
)
}
})
}
@@ -18,15 +18,20 @@
use crate::{write_file_if_changed, CargoCommandVersioned};
use std::{
fs, path::{Path, PathBuf}, borrow::ToOwned, process, env, collections::HashSet,
hash::{Hash, Hasher}, ops::Deref,
borrow::ToOwned,
collections::HashSet,
env, fs,
hash::{Hash, Hasher},
ops::Deref,
path::{Path, PathBuf},
process,
};
use toml::value::Table;
use build_helper::rerun_if_changed;
use cargo_metadata::{MetadataCommand, Metadata};
use cargo_metadata::{Metadata, MetadataCommand};
use walkdir::WalkDir;
@@ -114,19 +119,16 @@ pub(crate) fn create_and_compile(
);
build_project(&project, default_rustflags, cargo_cmd);
let (wasm_binary, wasm_binary_compressed, bloaty) = compact_wasm_file(
&project,
project_cargo_toml,
wasm_binary_name,
);
let (wasm_binary, wasm_binary_compressed, bloaty) =
compact_wasm_file(&project, project_cargo_toml, wasm_binary_name);
wasm_binary.as_ref().map(|wasm_binary|
copy_wasm_to_target_directory(project_cargo_toml, wasm_binary)
);
wasm_binary
.as_ref()
.map(|wasm_binary| copy_wasm_to_target_directory(project_cargo_toml, wasm_binary));
wasm_binary_compressed.as_ref().map(|wasm_binary_compressed|
wasm_binary_compressed.as_ref().map(|wasm_binary_compressed| {
copy_wasm_to_target_directory(project_cargo_toml, wasm_binary_compressed)
);
});
generate_rerun_if_changed_instructions(project_cargo_toml, &project, &wasm_workspace);
@@ -144,17 +146,17 @@ fn find_cargo_lock(cargo_manifest: &Path) -> Option<PathBuf> {
}
if !path.pop() {
return None;
return None
}
}
}
if let Some(path) = find_impl(build_helper::out_dir()) {
return Some(path);
return Some(path)
}
if let Some(path) = find_impl(cargo_manifest.to_path_buf()) {
return Some(path);
return Some(path)
}
build_helper::warning!(
@@ -169,15 +171,20 @@ fn find_cargo_lock(cargo_manifest: &Path) -> Option<PathBuf> {
/// Extract the crate name from the given `Cargo.toml`.
fn get_crate_name(cargo_manifest: &Path) -> String {
let cargo_toml: Table = toml::from_str(
&fs::read_to_string(cargo_manifest).expect("File exists as checked before; qed")
).expect("Cargo manifest is a valid toml file; qed");
&fs::read_to_string(cargo_manifest).expect("File exists as checked before; qed"),
)
.expect("Cargo manifest is a valid toml file; qed");
let package = cargo_toml
.get("package")
.and_then(|t| t.as_table())
.expect("`package` key exists in valid `Cargo.toml`; qed");
package.get("name").and_then(|p| p.as_str()).map(ToOwned::to_owned).expect("Package name exists; qed")
package
.get("name")
.and_then(|p| p.as_str())
.map(ToOwned::to_owned)
.expect("Package name exists; qed")
}
/// Returns the name for the wasm binary.
@@ -192,9 +199,10 @@ fn get_wasm_workspace_root() -> PathBuf {
loop {
match out_dir.parent() {
Some(parent) if out_dir.ends_with("build") => return parent.to_path_buf(),
_ => if !out_dir.pop() {
break;
}
_ =>
if !out_dir.pop() {
break
},
}
}
@@ -210,10 +218,10 @@ fn create_project_cargo_toml(
enabled_features: impl Iterator<Item = String>,
) {
let mut workspace_toml: Table = toml::from_str(
&fs::read_to_string(
workspace_root_path.join("Cargo.toml"),
).expect("Workspace root `Cargo.toml` exists; qed")
).expect("Workspace root `Cargo.toml` is a valid toml file; qed");
&fs::read_to_string(workspace_root_path.join("Cargo.toml"))
.expect("Workspace root `Cargo.toml` exists; qed"),
)
.expect("Workspace root `Cargo.toml` is a valid toml file; qed");
let mut wasm_workspace_toml = Table::new();
@@ -232,25 +240,25 @@ fn create_project_cargo_toml(
wasm_workspace_toml.insert("profile".into(), profile.into());
// Add patch section from the project root `Cargo.toml`
while let Some(mut patch) = workspace_toml.remove("patch")
.and_then(|p| p.try_into::<Table>().ok()) {
while let Some(mut patch) =
workspace_toml.remove("patch").and_then(|p| p.try_into::<Table>().ok())
{
// Iterate over all patches and make the patch path absolute from the workspace root path.
patch.iter_mut()
.filter_map(|p|
patch
.iter_mut()
.filter_map(|p| {
p.1.as_table_mut().map(|t| t.iter_mut().filter_map(|t| t.1.as_table_mut()))
)
})
.flatten()
.for_each(|p|
p.iter_mut()
.filter(|(k, _)| k == &"path")
.for_each(|(_, v)| {
if let Some(path) = v.as_str().map(PathBuf::from) {
if path.is_relative() {
*v = workspace_root_path.join(path).display().to_string().into();
}
.for_each(|p| {
p.iter_mut().filter(|(k, _)| k == &"path").for_each(|(_, v)| {
if let Some(path) = v.as_str().map(PathBuf::from) {
if path.is_relative() {
*v = workspace_root_path.join(path).display().to_string().into();
}
})
);
}
})
});
wasm_workspace_toml.insert("patch".into(), patch.into());
}
@@ -296,7 +304,8 @@ fn find_package_by_manifest_path<'a>(
manifest_path: &Path,
crate_metadata: &'a cargo_metadata::Metadata,
) -> &'a cargo_metadata::Package {
crate_metadata.packages
crate_metadata
.packages
.iter()
.find(|p| p.manifest_path == manifest_path)
.expect("Wasm project exists in its own metadata; qed")
@@ -309,18 +318,19 @@ fn project_enabled_features(
) -> Vec<String> {
let package = find_package_by_manifest_path(cargo_manifest, crate_metadata);
let mut enabled_features = package.features.keys()
let mut enabled_features = package
.features
.keys()
.filter(|f| {
let mut feature_env = f.replace("-", "_");
feature_env.make_ascii_uppercase();
// 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"
&& *f != "default"
&& env::var(format!("CARGO_FEATURE_{}", feature_env))
.map(|v| v == "1")
.unwrap_or_default()
*f != "std" &&
*f != "default" && env::var(format!("CARGO_FEATURE_{}", feature_env))
.map(|v| v == "1")
.unwrap_or_default()
})
.cloned()
.collect::<Vec<_>>();
@@ -418,7 +428,8 @@ fn build_project(project: &Path, default_rustflags: &str, cargo_cmd: CargoComman
env::var(crate::WASM_BUILD_RUSTFLAGS_ENV).unwrap_or_default(),
);
build_cmd.args(&["rustc", "--target=wasm32-unknown-unknown"])
build_cmd
.args(&["rustc", "--target=wasm32-unknown-unknown"])
.arg(format!("--manifest-path={}", manifest_path.display()))
.env("RUSTFLAGS", rustflags)
// Unset the `CARGO_TARGET_DIR` to prevent a cargo deadlock (cargo locks a target dir exclusive).
@@ -456,17 +467,16 @@ fn compact_wasm_file(
let is_release_build = is_release_build();
let target = if is_release_build { "release" } else { "debug" };
let default_wasm_binary_name = get_wasm_binary_name(cargo_manifest);
let wasm_file = project.join("target/wasm32-unknown-unknown")
let wasm_file = project
.join("target/wasm32-unknown-unknown")
.join(target)
.join(format!("{}.wasm", default_wasm_binary_name));
let wasm_compact_file = if is_release_build {
let wasm_compact_file = project.join(
format!(
"{}.compact.wasm",
wasm_binary_name.clone().unwrap_or_else(|| default_wasm_binary_name.clone()),
)
);
let wasm_compact_file = project.join(format!(
"{}.compact.wasm",
wasm_binary_name.clone().unwrap_or_else(|| default_wasm_binary_name.clone()),
));
wasm_gc::garbage_collect_file(&wasm_file, &wasm_compact_file)
.expect("Failed to compact generated WASM binary.");
Some(WasmBinary(wasm_compact_file))
@@ -474,24 +484,19 @@ fn compact_wasm_file(
None
};
let wasm_compact_compressed_file = wasm_compact_file.as_ref()
.and_then(|compact_binary| {
let file_name = wasm_binary_name.clone()
.unwrap_or_else(|| default_wasm_binary_name.clone());
let wasm_compact_compressed_file = wasm_compact_file.as_ref().and_then(|compact_binary| {
let file_name =
wasm_binary_name.clone().unwrap_or_else(|| default_wasm_binary_name.clone());
let wasm_compact_compressed_file = project.join(
format!(
"{}.compact.compressed.wasm",
file_name,
)
);
let wasm_compact_compressed_file =
project.join(format!("{}.compact.compressed.wasm", file_name,));
if compress_wasm(&compact_binary.0, &wasm_compact_compressed_file) {
Some(WasmBinary(wasm_compact_compressed_file))
} else {
None
}
});
if compress_wasm(&compact_binary.0, &wasm_compact_compressed_file) {
Some(WasmBinary(wasm_compact_compressed_file))
} else {
None
}
});
let bloaty_file_name = if let Some(name) = wasm_binary_name {
format!("{}.wasm", name)
@@ -502,24 +507,14 @@ fn compact_wasm_file(
let bloaty_file = project.join(bloaty_file_name);
fs::copy(wasm_file, &bloaty_file).expect("Copying the bloaty file to the project dir.");
(
wasm_compact_file,
wasm_compact_compressed_file,
WasmBinaryBloaty(bloaty_file),
)
(wasm_compact_file, wasm_compact_compressed_file, WasmBinaryBloaty(bloaty_file))
}
fn compress_wasm(
wasm_binary_path: &Path,
compressed_binary_out_path: &Path,
) -> bool {
fn compress_wasm(wasm_binary_path: &Path, compressed_binary_out_path: &Path) -> bool {
use sp_maybe_compressed_blob::CODE_BLOB_BOMB_LIMIT;
let data = fs::read(wasm_binary_path).expect("Failed to read WASM binary");
if let Some(compressed) = sp_maybe_compressed_blob::compress(
&data,
CODE_BLOB_BOMB_LIMIT,
) {
if let Some(compressed) = sp_maybe_compressed_blob::compress(&data, CODE_BLOB_BOMB_LIMIT) {
fs::write(compressed_binary_out_path, &compressed[..])
.expect("Failed to write WASM binary");
@@ -590,7 +585,8 @@ fn generate_rerun_if_changed_instructions(
.exec()
.expect("`cargo metadata` can not fail!");
let package = metadata.packages
let package = metadata
.packages
.iter()
.find(|p| p.manifest_path == cargo_manifest)
.expect("The crate package is contained in its own metadata; qed");
@@ -603,12 +599,11 @@ fn generate_rerun_if_changed_instructions(
packages.insert(DeduplicatePackage::from(package));
while let Some(dependency) = dependencies.pop() {
let path_or_git_dep = dependency.source
.as_ref()
.map(|s| s.starts_with("git+"))
.unwrap_or(true);
let path_or_git_dep =
dependency.source.as_ref().map(|s| s.starts_with("git+")).unwrap_or(true);
let package = metadata.packages
let package = metadata
.packages
.iter()
.filter(|p| !p.manifest_path.starts_with(wasm_workspace))
.find(|p| {
@@ -649,9 +644,7 @@ fn package_rerun_if_changed(package: &DeduplicatePackage) {
// Ignore this entry if it is a directory that contains a `Cargo.toml` that is not the
// `Cargo.toml` related to the current package. This is done to ignore sub-crates of a crate.
// If such a sub-crate is a dependency, it will be processed independently anyway.
p.path() == manifest_path
|| !p.path().is_dir()
|| !p.path().join("Cargo.toml").exists()
p.path() == manifest_path || !p.path().is_dir() || !p.path().join("Cargo.toml").exists()
})
.filter_map(|p| p.ok().map(|p| p.into_path()))
.filter(|p| {
@@ -681,5 +674,6 @@ fn copy_wasm_to_target_directory(cargo_manifest: &Path, wasm_binary: &WasmBinary
fs::copy(
wasm_binary.wasm_binary_path(),
target_dir.join(format!("{}.wasm", get_wasm_binary_name(cargo_manifest))),
).expect("Copies WASM binary to `WASM_TARGET_DIRECTORY`.");
)
.expect("Copies WASM binary to `WASM_TARGET_DIRECTORY`.");
}