mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-26 16:57:58 +00:00
Fixes wasm-builder rerun if changed logic (#6144)
There was a bug which related in required files not being tracked of being modified. This pr fixes this bug by making sure we ignore version requirements for path dependencies and git dependencies. This also ensures that we only track `.rs` or `.toml` files. Another improvement is that we only include paths which don't contain a `Cargo.toml` if this `Cargo.toml` does not belongs to the package being processed. This prevents that sub-crates are added to the tracked files, while not being part of the dependencies.
This commit is contained in:
@@ -26,7 +26,7 @@ use toml::value::Table;
|
||||
|
||||
use build_helper::rerun_if_changed;
|
||||
|
||||
use cargo_metadata::MetadataCommand;
|
||||
use cargo_metadata::{MetadataCommand, Metadata};
|
||||
|
||||
use walkdir::WalkDir;
|
||||
|
||||
@@ -342,11 +342,7 @@ fn project_enabled_features(
|
||||
///
|
||||
/// # Returns
|
||||
/// The path to the created project.
|
||||
fn create_project(
|
||||
cargo_manifest: &Path,
|
||||
wasm_workspace: &Path,
|
||||
crate_metadata: &cargo_metadata::Metadata,
|
||||
) -> PathBuf {
|
||||
fn create_project(cargo_manifest: &Path, wasm_workspace: &Path, crate_metadata: &Metadata) -> PathBuf {
|
||||
let crate_name = get_crate_name(cargo_manifest);
|
||||
let crate_path = cargo_manifest.parent().expect("Parent path exists; qed");
|
||||
let wasm_binary = get_wasm_binary_name(cargo_manifest);
|
||||
@@ -519,22 +515,33 @@ fn generate_rerun_if_changed_instructions(
|
||||
.exec()
|
||||
.expect("`cargo metadata` can not fail!");
|
||||
|
||||
// Start with the dependencies of the crate we want to compile for wasm.
|
||||
let mut dependencies = 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")
|
||||
.dependencies
|
||||
.iter()
|
||||
.collect::<Vec<_>>();
|
||||
.expect("The crate package is contained in its own metadata; qed");
|
||||
|
||||
// Start with the dependencies of the crate we want to compile for wasm.
|
||||
let mut dependencies = package.dependencies.iter().collect::<Vec<_>>();
|
||||
|
||||
// Collect all packages by follow the dependencies of all packages we find.
|
||||
let mut packages = HashSet::new();
|
||||
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 package = metadata.packages
|
||||
.iter()
|
||||
.filter(|p| !p.manifest_path.starts_with(wasm_workspace))
|
||||
.find(|p| dependency.req.matches(&p.version) && dependency.name == p.name);
|
||||
.find(|p| {
|
||||
// Check that the name matches and that the version matches or this is
|
||||
// a git or path dep. A git or path dependency can only occur once, so we don't
|
||||
// need to check the version.
|
||||
(path_or_git_dep || dependency.req.matches(&p.version)) && dependency.name == p.name
|
||||
});
|
||||
|
||||
if let Some(package) = package {
|
||||
if packages.insert(DeduplicatePackage::from(package)) {
|
||||
@@ -544,21 +551,7 @@ fn generate_rerun_if_changed_instructions(
|
||||
}
|
||||
|
||||
// Make sure that if any file/folder of a dependency change, we need to rerun the `build.rs`
|
||||
packages.into_iter()
|
||||
.filter(|p| !p.manifest_path.starts_with(wasm_workspace))
|
||||
.for_each(|package| {
|
||||
let mut manifest_path = package.manifest_path.clone();
|
||||
if manifest_path.ends_with("Cargo.toml") {
|
||||
manifest_path.pop();
|
||||
}
|
||||
|
||||
rerun_if_changed(&manifest_path);
|
||||
|
||||
WalkDir::new(manifest_path)
|
||||
.into_iter()
|
||||
.filter_map(|p| p.ok())
|
||||
.for_each(|p| rerun_if_changed(p.path()));
|
||||
});
|
||||
packages.iter().for_each(package_rerun_if_changed);
|
||||
|
||||
// Register our env variables
|
||||
println!("cargo:rerun-if-env-changed={}", crate::SKIP_BUILD_ENV);
|
||||
@@ -568,8 +561,32 @@ fn generate_rerun_if_changed_instructions(
|
||||
println!("cargo:rerun-if-env-changed={}", crate::WASM_BUILD_TOOLCHAIN);
|
||||
}
|
||||
|
||||
/// Copy the WASM binary to the target directory set in `WASM_TARGET_DIRECTORY` environment variable.
|
||||
/// If the variable is not set, this is a no-op.
|
||||
/// Track files and paths related to the given package to rerun `build.rs` on any relevant change.
|
||||
fn package_rerun_if_changed(package: &DeduplicatePackage) {
|
||||
let mut manifest_path = package.manifest_path.clone();
|
||||
if manifest_path.ends_with("Cargo.toml") {
|
||||
manifest_path.pop();
|
||||
}
|
||||
|
||||
WalkDir::new(&manifest_path)
|
||||
.into_iter()
|
||||
.filter_entry(|p| {
|
||||
// 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()
|
||||
})
|
||||
.filter_map(|p| p.ok().map(|p| p.into_path()))
|
||||
.filter(|p| {
|
||||
p.is_dir() || p.extension().map(|e| e == "rs" || e == "toml").unwrap_or_default()
|
||||
})
|
||||
.for_each(|p| rerun_if_changed(p));
|
||||
}
|
||||
|
||||
/// Copy the WASM binary to the target directory set in `WASM_TARGET_DIRECTORY` environment
|
||||
/// variable. If the variable is not set, this is a no-op.
|
||||
fn copy_wasm_to_target_directory(cargo_manifest: &Path, wasm_binary: &WasmBinary) {
|
||||
let target_dir = match env::var(crate::WASM_TARGET_DIRECTORY) {
|
||||
Ok(path) => PathBuf::from(path),
|
||||
|
||||
Reference in New Issue
Block a user