Hello,
I am compiling some code using the cargo crate. The code is very basic and looks like this:
fn compile_application(app_path: &str) -> anyhow::Result<(String, Vec<PathBuf>)> {
let app_path = std::path::absolute(app_path)?;
let context = GlobalContext::default()?;
context.shell().set_verbosity(Verbosity::Normal);
let manifest_path = if let Some(file) = app_path.file_name()
&& file == OsStr::new("Cargo.toml")
{
app_path
} else {
app_path.join("Cargo.toml")
};
let workspace = Workspace::new(&manifest_path, &context).unwrap();
let workspace_name = workspace
.root()
.file_name()
.unwrap()
.to_str()
.unwrap()
.to_string();
let wasm_target = CompileTarget::new("wasm32-unknown-unknown")?;
let mut compile_opts = CompileOptions::new(&context, CompileMode::Build).unwrap();
compile_opts.spec = Packages::All;
compile_opts.build_config.requested_kinds = vec![CompileKind::Target(wasm_target)];
compile_opts.build_config.message_format = MessageFormat::Human;
match compile(&workspace, &compile_opts) {
Ok(comp) => {
let binaries = comp.binaries.iter().map(|u| u.path.clone()).collect();
Ok((workspace_name, binaries))
}
Err(err) => bail!("Failed to compile the application: {err}"),
}
}
It works fine, but always uses my default toolchain (unless I set RUSTUP_TOOLCHAIN
). How can I programmatically set the toolchain, or simply use whatever is in the workspace's rust-toolchain.toml
file?
I cannot find anything about this in the cargo crate's documentation...