@@ -36,13 +36,13 @@ use charon_lib::{
3636 logger,
3737 options:: { CHARON_ARGS , CliOpts , SerializationFormat } ,
3838 pretty:: FmtWithCtx ,
39- utils:: arg_value,
39+ utils:: { arg_value, arg_values } ,
4040} ;
4141use clap:: Parser ;
4242use cli:: { Charon , Cli } ;
4343use itertools:: Itertools ;
44- use std:: { env, process:: ExitStatus } ;
45- use toolchain:: { toolchain_path, toolchain_version} ;
44+ use std:: { env, path :: PathBuf , process:: ExitStatus } ;
45+ use toolchain:: { CHARON_SYSROOT_VAR , setup_miri_sysroot , toolchain_path, toolchain_version} ;
4646
4747#[ macro_use]
4848extern crate charon_lib;
@@ -102,6 +102,22 @@ pub fn main() -> Result<()> {
102102 println ! ( "{}" , path. display( ) ) ;
103103 ExitStatus :: default ( )
104104 }
105+ Charon :: PrintSysroot ( args) => {
106+ let target = match args. target {
107+ Some ( target) => target,
108+ None => get_rustc_version ( ) ?. host ,
109+ } ;
110+ // Mirrors how `charon-driver` picks a sysroot.
111+ let sysroot = match miri_sysroot ( args. sysroot . as_deref ( ) , & target) {
112+ Some ( sysroot) => sysroot,
113+ None => match args. sysroot . as_deref ( ) {
114+ None | Some ( "default" | "miri" ) => toolchain_path ( ) ?,
115+ Some ( sysroot) => sysroot. into ( ) ,
116+ } ,
117+ } ;
118+ println ! ( "{}" , sysroot. display( ) ) ;
119+ ExitStatus :: default ( )
120+ }
105121 Charon :: ToolchainVersion => {
106122 println ! ( "{}" , toolchain_version( ) ) ;
107123 ExitStatus :: default ( )
@@ -208,11 +224,24 @@ fn translate_with_cargo(
208224 cmd. env_remove ( "CARGO_PRIMARY_PACKAGE" ) ;
209225 cmd. env ( CHARON_ARGS , serde_json:: to_string ( & options) . unwrap ( ) ) ;
210226 cmd. arg ( "build" ) ;
211- if arg_value ( & cargo_args, "--target" ) . is_none ( ) {
212- // Make sure the build target is explicitly set. This is needed to detect which crates are
213- // proc-macro/build-script in `charon-driver`.
214- cmd. arg ( "--target" ) ;
215- cmd. arg ( & get_rustc_version ( ) ?. host ) ;
227+ let target = match arg_values ( & cargo_args, "--target" ) . collect_vec ( ) . as_slice ( ) {
228+ [ target] => target. to_string ( ) ,
229+ // We resolve a single sysroot for all the crates we build.
230+ [ _, _, ..] => anyhow:: bail!(
231+ "passing several `--target` flags to cargo isn't supported; \
232+ use charon's `--targets` option instead"
233+ ) ,
234+ [ ] => {
235+ // Make sure the build target is explicitly set. This is needed to detect which crates
236+ // are proc-macro/build-script in `charon-driver`.
237+ let host = get_rustc_version ( ) ?. host ;
238+ cmd. arg ( "--target" ) ;
239+ cmd. arg ( & host) ;
240+ host
241+ }
242+ } ;
243+ if let Some ( sysroot) = miri_sysroot ( options. sysroot . as_deref ( ) , & target) {
244+ cmd. env ( CHARON_SYSROOT_VAR , sysroot) ;
216245 }
217246 cmd. args ( cargo_args) ;
218247 trace ! ( "running {cmd:?}" ) ;
@@ -227,15 +256,19 @@ fn translate_without_cargo(mut options: CliOpts) -> anyhow::Result<ExitStatus> {
227256 ensure_rustup ( ) ;
228257 options. validate ( ) ?;
229258 let mut cmd = toolchain:: driver_cmd ( ) ?;
230- let is_specified = |arg| {
231- let mut iter = options. rustc_args . iter ( ) ;
232- iter. any ( |input| input. starts_with ( arg) )
259+ let target = match arg_value ( & options. rustc_args , "--target" ) {
260+ Some ( target) => target. to_owned ( ) ,
261+ None => {
262+ // Make sure the build target is explicitly set. This is needed to detect which crates
263+ // are proc-macro/build-script in `charon-driver`.
264+ let host = get_rustc_version ( ) ?. host ;
265+ cmd. arg ( "--target" ) ;
266+ cmd. arg ( & host) ;
267+ host
268+ }
233269 } ;
234- if !is_specified ( "--target" ) {
235- // Make sure the build target is explicitly set. This is needed to detect which crates are
236- // proc-macro/build-script in `charon-driver`.
237- cmd. arg ( "--target" ) ;
238- cmd. arg ( & get_rustc_version ( ) ?. host ) ;
270+ if let Some ( sysroot) = miri_sysroot ( options. sysroot . as_deref ( ) , & target) {
271+ cmd. env ( CHARON_SYSROOT_VAR , sysroot) ;
239272 }
240273 cmd. args ( std:: mem:: take ( & mut options. rustc_args ) ) ;
241274 cmd. env ( CHARON_ARGS , serde_json:: to_string ( & options) . unwrap ( ) ) ;
@@ -246,6 +279,16 @@ fn translate_without_cargo(mut options: CliOpts) -> anyhow::Result<ExitStatus> {
246279 . expect ( "failed to wait for charon-driver?" ) )
247280}
248281
282+ /// In the default case, or with `--sysroot=miri`, we ask Miri to build a full-MIR sysroot for the
283+ /// given target. Returns `None` if the user chose another sysroot, or if Miri's setup failed.
284+ fn miri_sysroot ( sysroot : Option < & str > , target : & str ) -> Option < PathBuf > {
285+ match sysroot {
286+ // TODO: add non-miri sysroot we build ourselves, see #1475
287+ None | Some ( "miri" ) => setup_miri_sysroot ( target) ,
288+ Some ( _) => None ,
289+ }
290+ }
291+
249292fn get_rustc_version ( ) -> anyhow:: Result < rustc_version:: VersionMeta > {
250293 let cmd = toolchain:: driver_cmd ( ) ?;
251294 let rustc_version = rustc_version:: VersionMeta :: for_command ( cmd) . unwrap_or_else ( |err| {
0 commit comments