Skip to content

Commit 30a01ed

Browse files
committed
WIP add HostSandbox/wasm_sandbox
1 parent 475a23e commit 30a01ed

File tree

12 files changed

+113
-26
lines changed

12 files changed

+113
-26
lines changed

src/cargo/core/compiler/build_context/mod.rs

+21-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ use crate::util::{profile, Cfg, Config, Rustc};
1515
mod target_info;
1616
pub use self::target_info::{FileFlavor, TargetInfo};
1717

18+
pub const HOST_SANDBOX_TARGET: &str = "wasm32-unknown-unknown";
19+
1820
/// The build context, containing all information about a build task.
1921
///
2022
/// It is intended that this is mostly static information. Stuff that mutates
@@ -38,10 +40,13 @@ pub struct BuildContext<'a, 'cfg> {
3840
pub rustc: Rustc,
3941
/// Build information for the host arch.
4042
pub host_config: TargetConfig,
43+
/// Build information for the host sandbox arch (wasm).
44+
pub host_sandbox_config: TargetConfig,
4145
/// Build information for the target.
4246
pub target_config: TargetConfig,
4347
pub target_info: TargetInfo,
4448
pub host_info: TargetInfo,
49+
pub host_sandbox_info: TargetInfo,
4550
pub units: &'a UnitInterner<'a>,
4651
}
4752

@@ -59,18 +64,21 @@ impl<'a, 'cfg> BuildContext<'a, 'cfg> {
5964
let rustc = config.load_global_rustc(Some(ws))?;
6065

6166
let host_config = TargetConfig::new(config, &rustc.host)?;
67+
let host_sandbox_config = TargetConfig::new(config, HOST_SANDBOX_TARGET)?;
6268
let target_config = match build_config.requested_target.as_ref() {
6369
Some(triple) => TargetConfig::new(config, triple)?,
6470
None => host_config.clone(),
6571
};
66-
let (host_info, target_info) = {
72+
let (host_info, host_sandbox_info, target_info) = {
6773
let _p = profile::start("BuildContext::probe_target_info");
6874
debug!("probe_target_info");
6975
let host_info =
7076
TargetInfo::new(config, &build_config.requested_target, &rustc, Kind::Host)?;
77+
let host_sandbox_info =
78+
TargetInfo::new(config, &build_config.requested_target, &rustc, Kind::HostSandbox)?;
7179
let target_info =
7280
TargetInfo::new(config, &build_config.requested_target, &rustc, Kind::Target)?;
73-
(host_info, target_info)
81+
(host_info, host_sandbox_info, target_info)
7482
};
7583

7684
Ok(BuildContext {
@@ -82,7 +90,9 @@ impl<'a, 'cfg> BuildContext<'a, 'cfg> {
8290
target_config,
8391
target_info,
8492
host_config,
93+
host_sandbox_config,
8594
host_info,
95+
host_sandbox_info,
8696
build_config,
8797
profiles,
8898
extra_compiler_args,
@@ -111,6 +121,7 @@ impl<'a, 'cfg> BuildContext<'a, 'cfg> {
111121
};
112122
let (name, info) = match kind {
113123
Kind::Host => (self.host_triple(), &self.host_info),
124+
Kind::HostSandbox => (self.host_sandbox_triple(), &self.host_sandbox_info),
114125
Kind::Target => (self.target_triple(), &self.target_info),
115126
};
116127
platform.matches(name, info.cfg())
@@ -130,6 +141,7 @@ impl<'a, 'cfg> BuildContext<'a, 'cfg> {
130141
pub fn cfg(&self, kind: Kind) -> &[Cfg] {
131142
let info = match kind {
132143
Kind::Host => &self.host_info,
144+
Kind::HostSandbox => &self.host_sandbox_info,
133145
Kind::Target => &self.target_info,
134146
};
135147
info.cfg()
@@ -145,6 +157,10 @@ impl<'a, 'cfg> BuildContext<'a, 'cfg> {
145157
&self.rustc.host
146158
}
147159

160+
pub fn host_sandbox_triple(&self) -> &str {
161+
HOST_SANDBOX_TARGET
162+
}
163+
148164
pub fn target_triple(&self) -> &str {
149165
self.build_config
150166
.requested_target
@@ -157,6 +173,7 @@ impl<'a, 'cfg> BuildContext<'a, 'cfg> {
157173
fn target_config(&self, kind: Kind) -> &TargetConfig {
158174
match kind {
159175
Kind::Host => &self.host_config,
176+
Kind::HostSandbox => &self.host_sandbox_config,
160177
Kind::Target => &self.target_config,
161178
}
162179
}
@@ -181,6 +198,7 @@ impl<'a, 'cfg> BuildContext<'a, 'cfg> {
181198
fn info(&self, kind: Kind) -> &TargetInfo {
182199
match kind {
183200
Kind::Host => &self.host_info,
201+
Kind::HostSandbox => &self.host_sandbox_info,
184202
Kind::Target => &self.target_info,
185203
}
186204
}
@@ -196,6 +214,7 @@ impl<'a, 'cfg> BuildContext<'a, 'cfg> {
196214
pub fn script_override(&self, lib_name: &str, kind: Kind) -> Option<&BuildOutput> {
197215
match kind {
198216
Kind::Host => self.host_config.overrides.get(lib_name),
217+
Kind::HostSandbox => self.host_sandbox_config.overrides.get(lib_name),
199218
Kind::Target => self.target_config.overrides.get(lib_name),
200219
}
201220
}

src/cargo/core/compiler/build_context/target_info.rs

+16-3
Original file line numberDiff line numberDiff line change
@@ -110,16 +110,22 @@ impl TargetInfo {
110110
let mut pipelining_test = process.clone();
111111
pipelining_test.args(&["--error-format=json", "--json=artifacts"]);
112112
let supports_pipelining = match kind {
113-
Kind::Host => Some(rustc.cached_output(&pipelining_test).is_ok()),
113+
Kind::Host | Kind::HostSandbox => Some(rustc.cached_output(&pipelining_test).is_ok()),
114114
Kind::Target => None,
115115
};
116116

117117
let target_triple = requested_target
118118
.as_ref()
119119
.map(|s| s.as_str())
120120
.unwrap_or(&rustc.host);
121-
if kind == Kind::Target {
122-
process.arg("--target").arg(target_triple);
121+
match kind {
122+
Kind::Host => {}
123+
Kind::HostSandbox => {
124+
process.arg("--target").arg(super::HOST_SANDBOX_TARGET);
125+
}
126+
Kind::Target => {
127+
process.arg("--target").arg(target_triple);
128+
}
123129
}
124130

125131
let crate_type_process = process.clone();
@@ -161,6 +167,13 @@ impl TargetInfo {
161167
}
162168
rustlib
163169
}
170+
Kind::HostSandbox => {
171+
rustlib.push("lib");
172+
rustlib.push("rustlib");
173+
rustlib.push(super::HOST_SANDBOX_TARGET);
174+
rustlib.push("lib");
175+
rustlib
176+
}
164177
Kind::Target => {
165178
rustlib.push("lib");
166179
rustlib.push("rustlib");

src/cargo/core/compiler/context/compilation_files.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ impl fmt::Display for Metadata {
5353
pub struct CompilationFiles<'a, 'cfg> {
5454
/// The target directory layout for the host (and target if it is the same as host).
5555
pub(super) host: Layout,
56+
/// The target directory layout for the host sandbox architecture
57+
pub(super) host_sandbox: Layout,
5658
/// The target directory layout for the target (if different from then host).
5759
pub(super) target: Option<Layout>,
5860
/// Additional directory to include a copy of the outputs.
@@ -93,6 +95,7 @@ impl<'a, 'cfg: 'a> CompilationFiles<'a, 'cfg> {
9395
pub(super) fn new(
9496
roots: &[Unit<'a>],
9597
host: Layout,
98+
host_sandbox: Layout,
9699
target: Option<Layout>,
97100
export_dir: Option<PathBuf>,
98101
ws: &'a Workspace<'cfg>,
@@ -110,6 +113,7 @@ impl<'a, 'cfg: 'a> CompilationFiles<'a, 'cfg> {
110113
CompilationFiles {
111114
ws,
112115
host,
116+
host_sandbox,
113117
target,
114118
export_dir,
115119
roots: roots.to_vec(),
@@ -122,6 +126,7 @@ impl<'a, 'cfg: 'a> CompilationFiles<'a, 'cfg> {
122126
pub fn layout(&self, kind: Kind) -> &Layout {
123127
match kind {
124128
Kind::Host => &self.host,
129+
Kind::HostSandbox => &self.host_sandbox,
125130
Kind::Target => self.target.as_ref().unwrap_or(&self.host),
126131
}
127132
}
@@ -345,10 +350,10 @@ impl<'a, 'cfg: 'a> CompilationFiles<'a, 'cfg> {
345350

346351
let out_dir = self.out_dir(unit);
347352
let link_stem = self.link_stem(unit);
348-
let info = if unit.kind == Kind::Host {
349-
&bcx.host_info
350-
} else {
351-
&bcx.target_info
353+
let info = match unit.kind {
354+
Kind::Host => &bcx.host_info,
355+
Kind::HostSandbox => &bcx.host_sandbox_info,
356+
Kind::Target => &bcx.target_info,
352357
};
353358
let file_stem = self.file_stem(unit);
354359

src/cargo/core/compiler/context/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
298298
"debug"
299299
};
300300
let host_layout = Layout::new(self.bcx.ws, None, dest)?;
301+
let host_sandbox_layout = Layout::new(self.bcx.ws, Some(super::build_context::HOST_SANDBOX_TARGET), dest)?;
301302
let target_layout = match self.bcx.build_config.requested_target.as_ref() {
302303
Some(target) => Some(Layout::new(self.bcx.ws, Some(target), dest)?),
303304
None => None,
@@ -309,6 +310,7 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
309310
let files = CompilationFiles::new(
310311
units,
311312
host_layout,
313+
host_sandbox_layout,
312314
target_layout,
313315
export_dir,
314316
self.bcx.ws,
@@ -327,6 +329,10 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
327329
.host
328330
.prepare()
329331
.chain_err(|| internal("couldn't prepare build directories"))?;
332+
self.files_mut()
333+
.host_sandbox
334+
.prepare()
335+
.chain_err(|| internal("couldn't prepare build directories"))?;
330336
if let Some(ref mut target) = self.files.as_mut().unwrap().target {
331337
target
332338
.prepare()

src/cargo/core/compiler/context/unit_dependencies.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ fn compute_deps<'a, 'cfg, 'tmp>(
187187
{
188188
let unit = new_unit(bcx, pkg, lib, dep_unit_for, Kind::Target, mode);
189189
ret.push((unit, dep_unit_for));
190-
let unit = new_unit(bcx, pkg, lib, dep_unit_for, Kind::Host, mode);
190+
let unit = new_unit(bcx, pkg, lib, dep_unit_for, unit.kind.for_target(lib), mode);
191191
ret.push((unit, dep_unit_for));
192192
} else {
193193
let unit = new_unit(bcx, pkg, lib, dep_unit_for, unit.kind.for_target(lib), mode);
@@ -199,6 +199,7 @@ fn compute_deps<'a, 'cfg, 'tmp>(
199199
// all we need. If this isn't a build script, then it depends on the
200200
// build script if there is one.
201201
if unit.target.is_custom_build() {
202+
assert_eq!(unit.kind, Kind::Host, "unit.target {:?}", unit.target);
202203
return Ok(ret);
203204
}
204205
ret.extend(dep_build_script(unit, bcx));

src/cargo/core/compiler/custom_build.rs

+1
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ fn build_work<'a, 'cfg>(cx: &mut Context<'a, 'cfg>, unit: &Unit<'a>) -> CargoRes
163163
"TARGET",
164164
&match unit.kind {
165165
Kind::Host => bcx.host_triple(),
166+
Kind::HostSandbox => bcx.host_sandbox_triple(),
166167
Kind::Target => bcx.target_triple(),
167168
},
168169
)

src/cargo/core/compiler/mod.rs

+35-11
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,11 @@ use crate::util::{internal, join_paths, profile};
5151
/// These will be the same unless cross-compiling.
5252
#[derive(PartialEq, Eq, Hash, Debug, Clone, Copy, PartialOrd, Ord, Serialize)]
5353
pub enum Kind {
54+
/// Actual native host architecture
5455
Host,
56+
/// For host, but in a wasm sandbox
57+
HostSandbox,
58+
/// Target architecture
5559
Target,
5660
}
5761

@@ -947,16 +951,27 @@ fn build_base_args<'a, 'cfg>(
947951
}
948952
}
949953

950-
if unit.kind == Kind::Target {
951-
opt(
952-
cmd,
953-
"--target",
954-
"",
955-
bcx.build_config
956-
.requested_target
957-
.as_ref()
958-
.map(|s| s.as_ref()),
959-
);
954+
match unit.kind {
955+
Kind::Host => {}
956+
Kind::HostSandbox => {
957+
opt(
958+
cmd,
959+
"--target",
960+
"",
961+
Some(cx.bcx.host_sandbox_triple().as_ref()),
962+
);
963+
}
964+
Kind::Target => {
965+
opt(
966+
cmd,
967+
"--target",
968+
"",
969+
bcx.build_config
970+
.requested_target
971+
.as_ref()
972+
.map(|s| s.as_ref()),
973+
);
974+
}
960975
}
961976

962977
opt(cmd, "-C", "ar=", bcx.ar(unit.kind).map(|s| s.as_ref()));
@@ -1109,7 +1124,16 @@ impl Kind {
11091124
// that needs to be on the host we lift ourselves up to `Host`.
11101125
match self {
11111126
Kind::Host => Kind::Host,
1112-
Kind::Target if target.for_host() => Kind::Host,
1127+
Kind::HostSandbox if target.is_custom_build() => Kind::Host,
1128+
Kind::HostSandbox => Kind::HostSandbox,
1129+
Kind::Target if target.for_host() => {
1130+
if target.wasm_sandbox() {
1131+
assert!(!target.is_custom_build(), "target {:?}", target);
1132+
Kind::HostSandbox
1133+
} else {
1134+
Kind::Host
1135+
}
1136+
}
11131137
Kind::Target => Kind::Target,
11141138
}
11151139
}

src/cargo/core/compiler/unit.rs

+3
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ impl<'a> UnitInterner<'a> {
140140
kind: Kind,
141141
mode: CompileMode,
142142
) -> Unit<'a> {
143+
// building custom_build implies Host
144+
assert!(!(target.is_custom_build() && mode == CompileMode::Build) || kind == Kind::Host,
145+
"target {:?} kind {:?} mode {:?}", target, kind, mode);
143146
let inner = self.intern_inner(&UnitInner {
144147
pkg,
145148
target,

src/cargo/core/manifest.rs

+12
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ pub struct Target {
241241
harness: bool, // whether to use the test harness (--test)
242242
for_host: bool,
243243
proc_macro: bool,
244+
wasm_sandbox: bool,
244245
edition: Edition,
245246
}
246247

@@ -296,6 +297,7 @@ struct SerializedTarget<'a> {
296297
/// Corresponds to `--crate-type` compiler attribute.
297298
/// See https://doc.rust-lang.org/reference/linkage.html
298299
crate_types: Vec<&'a str>,
300+
wasm_sandbox: bool,
299301
name: &'a str,
300302
src_path: Option<&'a PathBuf>,
301303
edition: &'a str,
@@ -315,6 +317,7 @@ impl ser::Serialize for Target {
315317
SerializedTarget {
316318
kind: &self.kind,
317319
crate_types: self.rustc_crate_types(),
320+
wasm_sandbox: self.wasm_sandbox,
318321
name: &self.name,
319322
src_path,
320323
edition: &self.edition.to_string(),
@@ -384,6 +387,7 @@ compact_debug! {
384387
harness
385388
for_host
386389
proc_macro
390+
wasm_sandbox
387391
edition
388392
)]
389393
}
@@ -614,6 +618,7 @@ impl Target {
614618
harness: true,
615619
for_host: false,
616620
proc_macro: false,
621+
wasm_sandbox: false,
617622
edition,
618623
tested: true,
619624
benched: true,
@@ -768,6 +773,9 @@ impl Target {
768773
pub fn proc_macro(&self) -> bool {
769774
self.proc_macro
770775
}
776+
pub fn wasm_sandbox(&self) -> bool {
777+
self.wasm_sandbox
778+
}
771779
pub fn edition(&self) -> Edition {
772780
self.edition
773781
}
@@ -908,6 +916,10 @@ impl Target {
908916
self.proc_macro = proc_macro;
909917
self
910918
}
919+
pub fn set_wasm_sandbox(&mut self, wasm_sandbox: bool) -> &mut Target {
920+
self.wasm_sandbox = wasm_sandbox;
921+
self
922+
}
911923
pub fn set_edition(&mut self, edition: Edition) -> &mut Target {
912924
self.edition = edition;
913925
self

0 commit comments

Comments
 (0)