Skip to content

Commit 2cd657c

Browse files
committed
Auto merge of #14358 - weihanglo:build-std, r=epage
fix(build-std): remove hack on creating virtual std workspace
2 parents fa64658 + 7902bf8 commit 2cd657c

File tree

5 files changed

+39
-82
lines changed

5 files changed

+39
-82
lines changed

src/cargo/core/compiler/standard_lib.rs

+3-53
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@ use crate::core::compiler::{CompileKind, CompileMode, RustcTargetData, Unit};
66
use crate::core::profiles::{Profiles, UnitFor};
77
use crate::core::resolver::features::{CliFeatures, FeaturesFor, ResolvedFeatures};
88
use crate::core::resolver::HasDevUnits;
9-
use crate::core::{Dependency, PackageId, PackageSet, Resolve, SourceId, Workspace};
9+
use crate::core::{PackageId, PackageSet, Resolve, Workspace};
1010
use crate::ops::{self, Packages};
1111
use crate::util::errors::CargoResult;
1212
use crate::GlobalContext;
1313
use std::collections::{HashMap, HashSet};
1414
use std::path::PathBuf;
15-
use std::rc::Rc;
1615

1716
use super::BuildConfig;
1817

@@ -74,60 +73,11 @@ pub fn resolve_std<'gctx>(
7473
}
7574

7675
let src_path = detect_sysroot_src_path(target_data)?;
77-
let to_patch = [
78-
"rustc-std-workspace-core",
79-
"rustc-std-workspace-alloc",
80-
"rustc-std-workspace-std",
81-
];
82-
let patches = to_patch
83-
.iter()
84-
.map(|&name| {
85-
let source_path = SourceId::for_path(&src_path.join("library").join(name))?;
86-
let dep = Dependency::parse(name, None, source_path)?;
87-
Ok(dep)
88-
})
89-
.collect::<CargoResult<Vec<_>>>()?;
90-
let crates_io_url = crate::sources::CRATES_IO_INDEX.parse().unwrap();
91-
let patch = HashMap::from([(crates_io_url, patches)]);
92-
let members = vec![
93-
String::from("library/std"),
94-
String::from("library/core"),
95-
String::from("library/alloc"),
96-
String::from("library/sysroot"),
97-
];
98-
let ws_config = crate::core::WorkspaceConfig::Root(crate::core::WorkspaceRootConfig::new(
99-
&src_path,
100-
&Some(members),
101-
/*default_members*/ &None,
102-
/*exclude*/ &None,
103-
/*inheritable*/ &None,
104-
/*custom_metadata*/ &None,
105-
));
106-
let virtual_manifest = crate::core::VirtualManifest::new(
107-
Rc::default(),
108-
Rc::new(toml_edit::ImDocument::parse("".to_owned()).expect("empty is valid TOML")),
109-
Rc::default(),
110-
Rc::default(),
111-
/*replace*/ Vec::new(),
112-
patch,
113-
ws_config,
114-
crate::core::Features::default(),
115-
None,
116-
);
117-
76+
let std_ws_manifest_path = src_path.join("library").join("Cargo.toml");
11877
let gctx = ws.gctx();
119-
// This is a delicate hack. In order for features to resolve correctly,
120-
// the resolver needs to run a specific "current" member of the workspace.
121-
// Thus, in order to set the features for `std`, we need to set `sysroot`
122-
// to be the "current" member. `sysroot` is the root, and all other
123-
// standard library crates are dependencies from there. Since none of the
124-
// other crates need to alter their features, this should be fine, for
125-
// now. Perhaps in the future features will be decoupled from the resolver
126-
// and it will be easier to control feature selection.
127-
let current_manifest = src_path.join("library/sysroot/Cargo.toml");
12878
// TODO: Consider doing something to enforce --locked? Or to prevent the
12979
// lock file from being written, such as setting ephemeral.
130-
let mut std_ws = Workspace::new_virtual(src_path, current_manifest, virtual_manifest, gctx)?;
80+
let mut std_ws = Workspace::new(&std_ws_manifest_path, gctx)?;
13181
// Don't require optional dependencies in this workspace, aka std's own
13282
// `[dev-dependencies]`. No need for us to generate a `Resolve` which has
13383
// those included because we'll never use them anyway.

src/cargo/core/workspace.rs

-19
Original file line numberDiff line numberDiff line change
@@ -244,25 +244,6 @@ impl<'gctx> Workspace<'gctx> {
244244
}
245245
}
246246

247-
pub fn new_virtual(
248-
root_path: PathBuf,
249-
current_manifest: PathBuf,
250-
manifest: VirtualManifest,
251-
gctx: &'gctx GlobalContext,
252-
) -> CargoResult<Workspace<'gctx>> {
253-
let mut ws = Workspace::new_default(current_manifest, gctx);
254-
ws.root_manifest = Some(root_path.join("Cargo.toml"));
255-
ws.target_dir = gctx.target_dir()?;
256-
ws.packages
257-
.packages
258-
.insert(root_path, MaybePackage::Virtual(manifest));
259-
ws.find_members()?;
260-
ws.set_resolve_behavior()?;
261-
// TODO: validation does not work because it walks up the directory
262-
// tree looking for the root which is a fake file that doesn't exist.
263-
Ok(ws)
264-
}
265-
266247
/// Creates a "temporary workspace" from one package which only contains
267248
/// that package.
268249
///

tests/build-std/main.rs

+25-2
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,31 @@ fn basic() {
119119
120120
"#]])
121121
.run();
122-
p.cargo("run").build_std().target_host().run();
123-
p.cargo("test").build_std().target_host().run();
122+
p.cargo("run")
123+
.build_std()
124+
.target_host()
125+
.with_stderr_data(str![[r#"
126+
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
127+
[RUNNING] `target/[HOST_TARGET]/debug/foo`
128+
129+
"#]])
130+
.run();
131+
p.cargo("test")
132+
.build_std()
133+
.target_host()
134+
.with_stderr_data(str![[r#"
135+
[COMPILING] rustc-std-workspace-std [..]
136+
...
137+
[COMPILING] test v0.0.0 ([..])
138+
[COMPILING] foo v0.0.1 ([ROOT]/foo)
139+
[FINISHED] `test` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
140+
[RUNNING] unittests src/lib.rs (target/[HOST_TARGET]/debug/deps/foo-[HASH])
141+
[RUNNING] unittests src/main.rs (target/[HOST_TARGET]/debug/deps/foo-[HASH])
142+
[RUNNING] tests/smoke.rs (target/[HOST_TARGET]/debug/deps/smoke-[HASH])
143+
[DOCTEST] foo
144+
145+
"#]])
146+
.run();
124147

125148
// Check for hack that removes dylibs.
126149
let deps_dir = Path::new("target")

tests/testsuite/mock-std/Cargo.toml

-8
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[workspace]
2+
resolver = "1"
3+
members = [
4+
"std",
5+
"sysroot",
6+
]
7+
8+
[patch.crates-io]
9+
rustc-std-workspace-core = { path = 'rustc-std-workspace-core' }
10+
rustc-std-workspace-alloc = { path = 'rustc-std-workspace-alloc' }
11+
rustc-std-workspace-std = { path = 'rustc-std-workspace-std' }

0 commit comments

Comments
 (0)