Skip to content

Commit 649ee27

Browse files
committed
mir
1 parent 6f7bc20 commit 649ee27

File tree

2 files changed

+87
-16
lines changed

2 files changed

+87
-16
lines changed

orchestrator/src/coordinator.rs

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ impl Container {
195195
};
196196

197197
let write_main = self.commander.one(write_main);
198-
let modify_cargo_toml = modify_cargo_toml(&self.commander, request.edition);
198+
let modify_cargo_toml = modify_cargo_toml(&self.commander, &request);
199199

200200
let (write_main, modify_cargo_toml) = join!(write_main, modify_cargo_toml);
201201

@@ -305,30 +305,30 @@ struct Commander {
305305
// Consider making this into a top-level message to the worker to avoid the roundtrip.
306306
async fn modify_cargo_toml(
307307
commander: &Commander,
308-
edition: crate::sandbox::Edition,
308+
request: &CompileRequest,
309309
) -> Result<(), ModifyCargoTomlError> {
310310
use modify_cargo_toml_error::*;
311311

312312
const PATH: &str = "Cargo.toml";
313313

314-
let request = ReadFileRequest {
314+
let read_request = ReadFileRequest {
315315
path: PATH.to_owned(),
316316
};
317-
let cargo_toml = commander.one(request).await.context(CouldNotReadSnafu)?;
317+
let cargo_toml = commander.one(read_request).await.context(CouldNotReadSnafu)?;
318318

319319
let cargo_toml = String::from_utf8(cargo_toml.0)?;
320320
let cargo_toml = toml::from_str(&cargo_toml)?;
321321

322-
let cargo_toml = modify_cargo_toml::set_edition(cargo_toml, edition.to_cargo_toml_key());
322+
let cargo_toml = request.modify_cargo_toml(cargo_toml);
323323

324324
let cargo_toml = toml::to_string(&cargo_toml)?;
325325
let cargo_toml = cargo_toml.into_bytes();
326326

327-
let request = WriteFileRequest {
327+
let write_request = WriteFileRequest {
328328
path: PATH.to_owned(),
329329
content: cargo_toml,
330330
};
331-
commander.one(request).await.context(CouldNotWriteSnafu)?;
331+
commander.one(write_request).await.context(CouldNotWriteSnafu)?;
332332

333333
Ok(())
334334
}
@@ -778,6 +778,10 @@ mod tests {
778778
//Coordinator::new_docker()
779779
}
780780

781+
fn new_compile_request() -> CompileRequest {
782+
new_compile_mir_request()
783+
}
784+
781785
fn new_compile_assembly_request() -> CompileRequest {
782786
CompileRequest {
783787
target: CompileTarget::Assembly(
@@ -802,7 +806,7 @@ mod tests {
802806
fn new_compile_hir_request_for(edition: Edition) -> CompileRequest {
803807
CompileRequest {
804808
target: CompileTarget::Hir,
805-
channel: Channel::Beta,
809+
channel: Channel::Nightly,
806810
crate_type: CrateType::Library(LibraryType::Lib),
807811
mode: Mode::Release,
808812
edition,
@@ -838,8 +842,17 @@ mod tests {
838842
}
839843
}
840844

841-
fn new_compile_request() -> CompileRequest {
842-
new_compile_mir_request()
845+
fn new_compile_wasm_request() -> CompileRequest {
846+
CompileRequest {
847+
target: CompileTarget::Wasm,
848+
channel: Channel::Nightly, // TODO: Can we run this on all channels now?
849+
crate_type: CrateType::Library(LibraryType::Cdylib),
850+
mode: Mode::Release,
851+
edition: Edition::Rust2021,
852+
tests: false,
853+
backtrace: false,
854+
code: r#"#[export_name = "inc"] pub fn inc(a: u8) -> u8 { a + 1 }"#.to_owned(),
855+
}
843856
}
844857

845858
#[tokio::test]
@@ -919,7 +932,6 @@ mod tests {
919932
Ok(())
920933
}
921934

922-
923935
#[tokio::test]
924936
#[snafu::report]
925937
async fn test_compile_assembly() -> Result<()> {
@@ -983,6 +995,26 @@ mod tests {
983995
Ok(())
984996
}
985997

998+
#[tokio::test]
999+
#[snafu::report]
1000+
async fn test_compile_wasm() -> Result<()> {
1001+
// cargo-wasm only exists inside the container
1002+
let coordinator = Coordinator::new_docker()?;
1003+
1004+
let response = coordinator
1005+
.compile(new_compile_wasm_request())
1006+
.with_timeout()
1007+
.await
1008+
.unwrap();
1009+
1010+
assert!(response.success, "stderr: {}", response.stderr);
1011+
assert_contains!(response.code, r#"(func $inc (export "inc") (type $t0) (param $p0 i32) (result i32)"#);
1012+
1013+
coordinator.shutdown().await?;
1014+
1015+
Ok(())
1016+
}
1017+
9861018
trait TimeoutExt: Future + Sized {
9871019
fn with_timeout(
9881020
self,

orchestrator/src/sandbox.rs

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,26 @@ pub enum CrateType {
8484
Library(LibraryType),
8585
}
8686

87+
impl CrateType {
88+
pub(crate) fn to_cargo_toml_key(&self) -> &'static str {
89+
use {CrateType::*, LibraryType::*};
90+
91+
match self {
92+
Binary => "bin",
93+
Library(Lib) => "lib",
94+
Library(Dylib) => "dylib",
95+
Library(Rlib) => "rlib",
96+
Library(Staticlib) => "staticlib",
97+
Library(Cdylib) => "cdylib",
98+
Library(ProcMacro) => "proc-macro",
99+
}
100+
}
101+
102+
pub(crate) fn to_library_cargo_toml_key(&self) -> Option<&'static str> {
103+
if *self == Self::Binary { None } else { Some(self.to_cargo_toml_key()) }
104+
}
105+
}
106+
87107
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
88108
pub enum LibraryType {
89109
Lib,
@@ -123,7 +143,7 @@ impl CompileRequest {
123143
use CompileTarget::*;
124144

125145
let mut args = if let Wasm = self.target {
126-
vec!["wasm", "build"]
146+
vec!["wasm"]
127147
} else {
128148
vec!["rustc"]
129149
};
@@ -133,8 +153,6 @@ impl CompileRequest {
133153

134154
match self.target {
135155
Assembly(flavor, _, _) => {
136-
use crate::sandbox::AssemblyFlavor::*;
137-
138156
args.extend(&["--", "--emit", "asm=compilation"]);
139157

140158
// Enable extra assembly comments for nightly builds
@@ -145,8 +163,8 @@ impl CompileRequest {
145163

146164
args.push("-C");
147165
match flavor {
148-
Att => args.push("llvm-args=-x86-asm-syntax=att"),
149-
Intel => args.push("llvm-args=-x86-asm-syntax=intel"),
166+
AssemblyFlavor::Att => args.push("llvm-args=-x86-asm-syntax=att"),
167+
AssemblyFlavor::Intel => args.push("llvm-args=-x86-asm-syntax=intel"),
150168
}
151169
}
152170
LlvmIr => args.extend(&["--", "--emit", "llvm-ir=compilation"]),
@@ -167,6 +185,27 @@ impl CompileRequest {
167185
}
168186
}
169187

188+
pub(crate) fn modify_cargo_toml(&self, mut cargo_toml: toml::Value) -> toml::Value {
189+
cargo_toml = modify_cargo_toml::set_edition(cargo_toml, self.edition.to_cargo_toml_key());
190+
191+
// TODO: When we boot the container, grab information like
192+
// `Cargo.toml` (and the `rustc` version, dependency
193+
// versions?). Then we always start from that version.
194+
195+
// TODO: We need to undo these changes when switching back from a library
196+
if let Some(crate_type) = self.crate_type.to_library_cargo_toml_key() {
197+
cargo_toml = modify_cargo_toml::set_crate_type(cargo_toml, crate_type);
198+
}
199+
200+
// TODO: We need to undo these changes when switching back from Wasm
201+
if CompileTarget::Wasm == self.target {
202+
cargo_toml = modify_cargo_toml::remove_dependencies(cargo_toml);
203+
cargo_toml = modify_cargo_toml::set_release_lto(cargo_toml, true);
204+
}
205+
206+
cargo_toml
207+
}
208+
170209
pub(crate) fn postprocess_result(&self, mut code: String) -> String {
171210
if let CompileTarget::Assembly(_, demangle, process) = self.target {
172211
if demangle == DemangleAssembly::Demangle {

0 commit comments

Comments
 (0)