Skip to content

Commit

Permalink
Add cargo-batch
Browse files Browse the repository at this point in the history
  • Loading branch information
Dirbaio committed Apr 7, 2022
1 parent e2e2ddd commit c67234e
Show file tree
Hide file tree
Showing 8 changed files with 292 additions and 49 deletions.
7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "cargo"
name = "cargo-batch"
version = "0.62.0"
edition = "2021"
license = "MIT OR Apache-2.0"
Expand All @@ -10,9 +10,9 @@ readme = "README.md"
description = """
Cargo, a package manager for Rust.
"""
autobins = false

[lib]
name = "cargo"
path = "src/cargo/lib.rs"

[dependencies]
Expand Down Expand Up @@ -70,6 +70,7 @@ itertools = "0.10.0"
# See the `src/tools/rustc-workspace-hack/README.md` file in `rust-lang/rust`
# for more information.
rustc-workspace-hack = "1.0.0"
cfg-if = "1.0.0"

[target.'cfg(windows)'.dependencies]
fwdansi = "1.1.0"
Expand Down Expand Up @@ -104,7 +105,7 @@ flate2 = { version = "1.0.3", default-features = false, features = ["zlib"] }
tar = { version = "0.4.26", default-features = false }

[[bin]]
name = "cargo"
name = "cargo-batch"
test = false
doc = false

Expand Down
3 changes: 3 additions & 0 deletions src/cargo/core/compiler/build_context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ pub struct BuildContext<'a, 'cfg> {

/// The list of all kinds that are involved in this build
pub all_kinds: HashSet<CompileKind>,

pub unit_export_dirs: HashMap<Unit, PathBuf>,
}

impl<'a, 'cfg> BuildContext<'a, 'cfg> {
Expand Down Expand Up @@ -85,6 +87,7 @@ impl<'a, 'cfg> BuildContext<'a, 'cfg> {
unit_graph,
scrape_units,
all_kinds,
unit_export_dirs: HashMap::new(),
})
}

Expand Down
4 changes: 2 additions & 2 deletions src/cargo/core/compiler/build_context/target_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -751,8 +751,8 @@ pub struct RustcTargetData<'cfg> {

/// Build information for targets that we're building for. This will be
/// empty if the `--target` flag is not passed.
target_config: HashMap<CompileTarget, TargetConfig>,
target_info: HashMap<CompileTarget, TargetInfo>,
pub target_config: HashMap<CompileTarget, TargetConfig>,
pub target_info: HashMap<CompileTarget, TargetInfo>,
}

impl<'cfg> RustcTargetData<'cfg> {
Expand Down
35 changes: 11 additions & 24 deletions src/cargo/core/compiler/context/compilation_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ pub struct CompilationFiles<'a, 'cfg> {
/// The target directory layout for the target (if different from then host).
pub(super) target: HashMap<CompileTarget, Layout>,
/// Additional directory to include a copy of the outputs.
export_dir: Option<PathBuf>,
export_dir: HashMap<Unit, PathBuf>,
/// The root targets requested by the user on the command line (does not
/// include dependencies).
roots: Vec<Unit>,
Expand Down Expand Up @@ -153,7 +153,7 @@ impl<'a, 'cfg: 'a> CompilationFiles<'a, 'cfg> {
ws: cx.bcx.ws,
host,
target,
export_dir: cx.bcx.build_config.export_dir.clone(),
export_dir: cx.bcx.unit_export_dirs.clone(),
roots: cx.bcx.roots.clone(),
metas,
outputs,
Expand Down Expand Up @@ -208,10 +208,12 @@ impl<'a, 'cfg: 'a> CompilationFiles<'a, 'cfg> {
}
}

/*
/// Additional export directory from `--out-dir`.
pub fn export_dir(&self) -> Option<PathBuf> {
self.export_dir.clone()
}
*/

/// Directory name to use for a package in the form `NAME-HASH`.
///
Expand Down Expand Up @@ -404,21 +406,11 @@ impl<'a, 'cfg: 'a> CompilationFiles<'a, 'cfg> {
}

let filename = file_type.uplift_filename(&unit.target);
let uplift_path = if unit.target.is_example() {
// Examples live in their own little world.
self.layout(unit.kind).examples().join(filename)
} else if unit.target.is_custom_build() {
self.build_script_dir(unit).join(filename)
if unit.target.is_custom_build() {
Some(self.build_script_dir(unit).join(filename))
} else {
self.layout(unit.kind).dest().join(filename)
};
if from_path == uplift_path {
// This can happen with things like examples that reside in the
// same directory, do not have a metadata hash (like on Windows),
// and do not have hyphens.
return None;
None
}
Some(uplift_path)
}

fn calc_outputs(
Expand Down Expand Up @@ -515,15 +507,10 @@ impl<'a, 'cfg: 'a> CompilationFiles<'a, 'cfg> {
// If, the `different_binary_name` feature is enabled, the name of the hardlink will
// be the name of the binary provided by the user in `Cargo.toml`.
let hardlink = self.uplift_to(unit, &file_type, &path);
let export_path = if unit.target.is_custom_build() {
None
} else {
self.export_dir.as_ref().and_then(|export_dir| {
hardlink
.as_ref()
.map(|hardlink| export_dir.join(hardlink.file_name().unwrap()))
})
};
let export_path = self
.export_dir
.get(unit)
.map(|export_dir| export_dir.join(file_type.uplift_filename(&unit.target)));
outputs.push(OutputFile {
path,
hardlink,
Expand Down
22 changes: 8 additions & 14 deletions src/cargo/core/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,6 @@ fn rustc(cx: &mut Context<'_, '_>, unit: &Unit, exec: &Arc<dyn Executor>) -> Car
fn link_targets(cx: &mut Context<'_, '_>, unit: &Unit, fresh: bool) -> CargoResult<Work> {
let bcx = cx.bcx;
let outputs = cx.outputs(unit)?;
let export_dir = cx.files().export_dir();
let package_id = unit.pkg.package_id();
let manifest_path = PathBuf::from(unit.pkg.manifest_path());
let profile = unit.profile.clone();
Expand Down Expand Up @@ -482,20 +481,15 @@ fn link_targets(cx: &mut Context<'_, '_>, unit: &Unit, fresh: bool) -> CargoResu
if !src.exists() {
continue;
}
let dst = match output.hardlink.as_ref() {
Some(dst) => dst,
None => {
destinations.push(src.clone());
continue;
}
};
destinations.push(dst.clone());
paths::link_or_copy(src, dst)?;
if let Some(ref path) = output.export_path {
let export_dir = export_dir.as_ref().unwrap();
paths::create_dir_all(export_dir)?;

if let Some(path) = &output.hardlink {
paths::create_dir_all(path.parent().unwrap())?;
paths::link_or_copy(src, path)?;
destinations.push(path.clone());
}
if let Some(path) = &output.export_path {
paths::create_dir_all(path.parent().unwrap())?;
paths::link_or_copy(src, path)?;
destinations.push(path.clone());
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/cargo/core/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,8 @@ impl hash::Hash for Package {
/// This is primarily used to convert a set of `PackageId`s to `Package`s. It
/// will download as needed, or used the cached download if available.
pub struct PackageSet<'cfg> {
packages: HashMap<PackageId, LazyCell<Package>>,
sources: RefCell<SourceMap<'cfg>>,
pub packages: HashMap<PackageId, LazyCell<Package>>,
pub sources: RefCell<SourceMap<'cfg>>,
config: &'cfg Config,
multi: Multi,
/// Used to prevent reusing the PackageSet to download twice.
Expand Down
10 changes: 6 additions & 4 deletions src/cargo/ops/cargo_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -602,10 +602,12 @@ pub fn create_bcx<'a, 'cfg>(
remove_duplicate_doc(build_config, &units, &mut unit_graph);
}

if build_config
.requested_kinds
.iter()
.any(CompileKind::is_host)
// cargo-many: we have to do this always, as we can have dupe units even if
// we're not building for the host.
// if build_config
// .requested_kinds
// .iter()
// .any(CompileKind::is_host)
{
// Rebuild the unit graph, replacing the explicit host targets with
// CompileKind::Host, merging any dependencies shared with build
Expand Down
Loading

0 comments on commit c67234e

Please sign in to comment.