Skip to content

Commit fb45eb1

Browse files
committed
Auto merge of #9292 - ehuss:cargo-util, r=alexcrichton
Split out cargo-util package for cargo-test-support. This splits out code from `cargo` that was being used by `cargo-test-support` in order to remove the dev-dependency cycle on the `cargo` crate. The intent here is to improve development build times. On my system, `touch src/cargo/lib.rs ; cargo check --profile=test` goes from 6.4 seconds to 3.5. I suspect more substantial changes (more than just `touch`) should exhibit even more improvements. This has a substantial downside that it is another package to manage publishing, particularly with making sure the version is updated correctly for crates.io. I'm uncertain if on balance it is worth it, but I lean towards moving forward with it.
2 parents 99320bb + c840160 commit fb45eb1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+656
-508
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ jobs:
6666
- run: cargo test --features 'deny-warnings'
6767
- run: cargo test --features 'deny-warnings' -p cargo-test-support
6868
- run: cargo test -p cargo-platform
69+
- run: cargo test -p cargo-util
6970
- run: cargo test --manifest-path crates/mdman/Cargo.toml
7071
- run: cargo build --manifest-path crates/credential/cargo-credential-1password/Cargo.toml
7172
- run: cargo build --manifest-path crates/credential/cargo-credential-gnome-secret/Cargo.toml

Cargo.toml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ path = "src/cargo/lib.rs"
2222
atty = "0.2"
2323
bytesize = "1.0"
2424
cargo-platform = { path = "crates/cargo-platform", version = "0.1.1" }
25+
cargo-util = { path = "crates/cargo-util", version = "0.1.0" }
2526
crates-io = { path = "crates/crates-io", version = "0.33.0" }
2627
crossbeam-utils = "0.8"
27-
crypto-hash = "0.3.1"
2828
curl = { version = "0.4.23", features = ["http2"] }
2929
curl-sys = "0.4.22"
3030
env_logger = "0.8.1"
@@ -50,12 +50,10 @@ num_cpus = "1.0"
5050
opener = "0.4"
5151
percent-encoding = "2.0"
5252
rustfix = "0.5.0"
53-
same-file = "1"
5453
semver = { version = "0.10", features = ["serde"] }
5554
serde = { version = "1.0.123", features = ["derive"] }
5655
serde_ignored = "0.1.0"
5756
serde_json = { version = "1.0.30", features = ["raw_value"] }
58-
shell-escape = "0.1.4"
5957
strip-ansi-escapes = "0.1.0"
6058
tar = { version = "0.4.26", default-features = false }
6159
tempfile = "3.0"
@@ -75,11 +73,7 @@ im-rc = "15.0.0"
7573
rustc-workspace-hack = "1.0.0"
7674
rand = "0.8.3"
7775

78-
[target.'cfg(target_os = "macos")'.dependencies]
79-
core-foundation = { version = "0.9.0", features = ["mac_os_10_7_support"] }
80-
8176
[target.'cfg(windows)'.dependencies]
82-
miow = "0.3.6"
8377
fwdansi = "1.1.0"
8478

8579
[target.'cfg(windows)'.dependencies.winapi]

crates/cargo-test-support/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ edition = "2018"
99
doctest = false
1010

1111
[dependencies]
12-
cargo = { path = "../.." }
12+
anyhow = "1.0.34"
1313
cargo-test-macro = { path = "../cargo-test-macro" }
14+
cargo-util = { path = "../cargo-util" }
1415
filetime = "0.2"
1516
flate2 = { version = "1.0", default-features = false, features = ["zlib"] }
1617
git2 = "0.13.16"

crates/cargo-test-support/src/cross_compile.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
//! These tests are all disabled on rust-lang/rust's CI, but run in Cargo's CI.
1111
1212
use crate::{basic_manifest, main_file, project};
13-
use cargo::util::ProcessError;
14-
use cargo::CargoResult;
13+
use cargo_util::ProcessError;
1514
use std::env;
1615
use std::fmt::Write;
1716
use std::process::{Command, Output};
@@ -41,7 +40,7 @@ pub fn disabled() -> bool {
4140

4241
let cross_target = alternate();
4342

44-
let run_cross_test = || -> CargoResult<Output> {
43+
let run_cross_test = || -> anyhow::Result<Output> {
4544
let p = project()
4645
.at("cross_test")
4746
.file("Cargo.toml", &basic_manifest("cross_test", "1.0.0"))

crates/cargo-test-support/src/lib.rs

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use std::process::{Command, Output};
1515
use std::str;
1616
use std::time::{self, Duration};
1717

18-
use cargo::util::{is_ci, CargoResult, ProcessBuilder, ProcessError, Rustc};
18+
use cargo_util::{is_ci, ProcessBuilder, ProcessError};
1919
use serde_json::{self, Value};
2020
use url::Url;
2121

@@ -701,7 +701,7 @@ impl Execs {
701701
self
702702
}
703703

704-
pub fn exec_with_output(&mut self) -> CargoResult<Output> {
704+
pub fn exec_with_output(&mut self) -> anyhow::Result<Output> {
705705
self.ran = true;
706706
// TODO avoid unwrap
707707
let p = (&self.process_builder).clone().unwrap();
@@ -1548,33 +1548,52 @@ fn substitute_macros(input: &str) -> String {
15481548

15491549
pub mod install;
15501550

1551-
thread_local!(
1552-
pub static RUSTC: Rustc = Rustc::new(
1553-
PathBuf::from("rustc"),
1554-
None,
1555-
None,
1556-
Path::new("should be path to rustup rustc, but we don't care in tests"),
1557-
None,
1558-
).unwrap()
1559-
);
1551+
struct RustcInfo {
1552+
verbose_version: String,
1553+
host: String,
1554+
}
1555+
1556+
impl RustcInfo {
1557+
fn new() -> RustcInfo {
1558+
let output = ProcessBuilder::new("rustc")
1559+
.arg("-vV")
1560+
.exec_with_output()
1561+
.expect("rustc should exec");
1562+
let verbose_version = String::from_utf8(output.stdout).expect("utf8 output");
1563+
let host = verbose_version
1564+
.lines()
1565+
.filter_map(|line| line.strip_prefix("host: "))
1566+
.next()
1567+
.expect("verbose version has host: field")
1568+
.to_string();
1569+
RustcInfo {
1570+
verbose_version,
1571+
host,
1572+
}
1573+
}
1574+
}
1575+
1576+
lazy_static::lazy_static! {
1577+
static ref RUSTC_INFO: RustcInfo = RustcInfo::new();
1578+
}
15601579

15611580
/// The rustc host such as `x86_64-unknown-linux-gnu`.
1562-
pub fn rustc_host() -> String {
1563-
RUSTC.with(|r| r.host.to_string())
1581+
pub fn rustc_host() -> &'static str {
1582+
&RUSTC_INFO.host
15641583
}
15651584

15661585
pub fn is_nightly() -> bool {
1586+
let vv = &RUSTC_INFO.verbose_version;
15671587
env::var("CARGO_TEST_DISABLE_NIGHTLY").is_err()
1568-
&& RUSTC
1569-
.with(|r| r.verbose_version.contains("-nightly") || r.verbose_version.contains("-dev"))
1588+
&& (vv.contains("-nightly") || vv.contains("-dev"))
15701589
}
15711590

1572-
pub fn process<T: AsRef<OsStr>>(t: T) -> cargo::util::ProcessBuilder {
1591+
pub fn process<T: AsRef<OsStr>>(t: T) -> ProcessBuilder {
15731592
_process(t.as_ref())
15741593
}
15751594

1576-
fn _process(t: &OsStr) -> cargo::util::ProcessBuilder {
1577-
let mut p = cargo::util::process(t);
1595+
fn _process(t: &OsStr) -> ProcessBuilder {
1596+
let mut p = ProcessBuilder::new(t);
15781597

15791598
// In general just clear out all cargo-specific configuration already in the
15801599
// environment. Our tests all assume a "default configuration" unless
@@ -1643,7 +1662,7 @@ pub trait ChannelChanger: Sized {
16431662
fn masquerade_as_nightly_cargo(&mut self) -> &mut Self;
16441663
}
16451664

1646-
impl ChannelChanger for cargo::util::ProcessBuilder {
1665+
impl ChannelChanger for ProcessBuilder {
16471666
fn masquerade_as_nightly_cargo(&mut self) -> &mut Self {
16481667
self.env("__CARGO_TEST_CHANNEL_OVERRIDE_DO_NOT_USE_THIS", "nightly")
16491668
}

crates/cargo-test-support/src/registry.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use crate::git::repo;
22
use crate::paths;
3-
use cargo::sources::CRATES_IO_INDEX;
4-
use cargo::util::Sha256;
3+
use cargo_util::Sha256;
54
use flate2::write::GzEncoder;
65
use flate2::Compression;
76
use std::collections::BTreeMap;
@@ -560,7 +559,7 @@ impl Package {
560559

561560
/// Sets the index schema version for this package.
562561
///
563-
/// See [`cargo::sources::registry::RegistryPackage`] for more information.
562+
/// See `cargo::sources::registry::RegistryPackage` for more information.
564563
pub fn schema_version(&mut self, version: u32) -> &mut Package {
565564
self.v = Some(version);
566565
self
@@ -585,7 +584,9 @@ impl Package {
585584
let registry_url = match (self.alternative, dep.registry.as_deref()) {
586585
(false, None) => None,
587586
(false, Some("alternative")) => Some(alt_registry_url().to_string()),
588-
(true, None) => Some(CRATES_IO_INDEX.to_string()),
587+
(true, None) => {
588+
Some("https://github.com/rust-lang/crates.io-index".to_string())
589+
}
589590
(true, Some("alternative")) => None,
590591
_ => panic!("registry_dep currently only supports `alternative`"),
591592
};

crates/cargo-util/Cargo.toml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
[package]
2+
name = "cargo-util"
3+
version = "0.1.0"
4+
authors = ["The Cargo Project Developers"]
5+
edition = "2018"
6+
license = "MIT OR Apache-2.0"
7+
homepage = "https://github.com/rust-lang/cargo"
8+
repository = "https://github.com/rust-lang/cargo"
9+
description = "Miscellaneous support code used by Cargo."
10+
11+
[dependencies]
12+
anyhow = "1.0.34"
13+
crypto-hash = "0.3.1"
14+
filetime = "0.2.9"
15+
hex = "0.4.2"
16+
jobserver = "0.1.21"
17+
libc = "0.2.88"
18+
log = "0.4.6"
19+
same-file = "1.0.6"
20+
shell-escape = "0.1.4"
21+
tempfile = "3.1.0"
22+
walkdir = "2.3.1"
23+
24+
[target.'cfg(target_os = "macos")'.dependencies]
25+
core-foundation = { version = "0.9.0", features = ["mac_os_10_7_support"] }
26+
27+
[target.'cfg(windows)'.dependencies]
28+
miow = "0.3.6"
29+
winapi = { version = "0.3.9", features = ["consoleapi", "minwindef"] }

crates/cargo-util/LICENSE-APACHE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../LICENSE-APACHE

crates/cargo-util/LICENSE-MIT

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../LICENSE-MIT

crates/cargo-util/src/lib.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//! Miscellaneous support code used by Cargo.
2+
3+
pub use self::read2::read2;
4+
pub use process_builder::ProcessBuilder;
5+
pub use process_error::{exit_status_to_string, is_simple_exit_code, ProcessError};
6+
pub use sha256::Sha256;
7+
8+
pub mod paths;
9+
mod process_builder;
10+
mod process_error;
11+
mod read2;
12+
mod sha256;
13+
14+
/// Whether or not this running in a Continuous Integration environment.
15+
pub fn is_ci() -> bool {
16+
std::env::var("CI").is_ok() || std::env::var("TF_BUILD").is_ok()
17+
}

0 commit comments

Comments
 (0)