Skip to content

Commit 141f778

Browse files
committed
feat(cli): split cli options to isolate the ones for the exporter
1 parent 54aa9e2 commit 141f778

7 files changed

Lines changed: 103 additions & 36 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ Changes to the frontend:
3030
- Add infrastructure to get a monomorphized `FullDef`; this is used in charon to monomorphize a crate graph (#1559)
3131
- Fix a regression affecting projection predicates (#1678)
3232

33+
Change to cargo-hax:
34+
- Improve the caching of rustc when using `cargo hax` commands (#1719)
35+
3336
Changes to hax-lib:
3437
- New behavior for `hax_lib::include`: it now forces inclusion when in contradiction with `-i` flag.
3538

cli/driver/src/callbacks_wrapper.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use hax_types::cli_options::{Command, ENV_VAR_OPTIONS_FRONTEND, Options};
1+
use hax_types::cli_options::{ENV_VAR_OPTIONS_FRONTEND, ExporterOptions};
22

33
use rustc_ast::Crate;
44
use rustc_driver::{Callbacks, Compilation};
@@ -10,7 +10,7 @@ use rustc_span::symbol::Symbol;
1010
/// configuration in the `config` phase of rustc
1111
pub struct CallbacksWrapper<'a> {
1212
pub sub: &'a mut (dyn Callbacks + Send + 'a),
13-
pub options: Options,
13+
pub options: ExporterOptions,
1414
}
1515
impl<'a> Callbacks for CallbacksWrapper<'a> {
1616
fn config(&mut self, config: &mut interface::Config) {

cli/driver/src/driver.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ mod features;
3737
use callbacks_wrapper::*;
3838
use features::*;
3939

40-
use hax_types::cli_options::{BackendOptions, Command, ENV_VAR_OPTIONS_FRONTEND};
40+
use hax_types::cli_options::{ENV_VAR_OPTIONS_FRONTEND, ExporterOptions};
4141

4242
use rustc_driver::{Callbacks, Compilation};
4343
use rustc_interface::interface;
@@ -74,7 +74,7 @@ const HAX_VANILLA_RUSTC: &str = "HAX_VANILLA_RUSTC";
7474
fn main() {
7575
setup_logging();
7676

77-
let options: hax_types::cli_options::Options = serde_json::from_str(
77+
let options: ExporterOptions = serde_json::from_str(
7878
&std::env::var(ENV_VAR_OPTIONS_FRONTEND).unwrap_or_else(|_| {
7979
panic!(
8080
"Cannot find environnement variable {}",
@@ -124,7 +124,7 @@ fn main() {
124124
!vanilla_rustc && !is_build_script && (options.deps || is_primary_package);
125125
let mut callbacks: Box<dyn Callbacks + Send> = if translate_package {
126126
Box::new(exporter::ExtractionCallbacks {
127-
body_types: options.command.body_kinds(),
127+
body_kinds: options.body_kinds.clone(),
128128
})
129129
} else {
130130
struct CallbacksNoop;
@@ -149,11 +149,9 @@ fn main() {
149149
"--cfg".into(),
150150
hax_lib_macros_types::HAX_CFG_OPTION_NAME.into(),
151151
])
152-
.chain(match &options.command {
153-
Command::Backend(BackendOptions { backend, .. }) => {
154-
vec!["--cfg".into(), format!("hax_backend_{backend}")]
155-
}
156-
_ => vec![],
152+
.chain(match &options.backend {
153+
Some(backend) => vec!["--cfg".into(), format!("hax_backend_{backend}")],
154+
None => vec![],
157155
})
158156
.chain(features.into_iter().map(|s| format!("-Zcrate-attr={}", s)))
159157
.chain(rustc_args[1..].iter().cloned())

cli/driver/src/exporter.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use hax_frontend_exporter::SInto;
22
use hax_frontend_exporter::state::LocalContextS;
3-
use hax_types::cli_options::{Backend, ENV_VAR_OPTIONS_FRONTEND, PathOrDash};
3+
use hax_types::cli_options::PathOrDash;
44
use rustc_driver::{Callbacks, Compilation};
55
use rustc_interface::interface;
66
use rustc_interface::interface::Compiler;
@@ -64,7 +64,7 @@ fn convert_thir<'tcx, Body: hax_frontend_exporter::IsBody>(
6464
/// Callback for extraction
6565
#[derive(Debug, Clone, Serialize)]
6666
pub(crate) struct ExtractionCallbacks {
67-
pub body_types: Vec<hax_types::cli_options::ExportBodyKind>,
67+
pub body_kinds: Vec<hax_types::cli_options::ExportBodyKind>,
6868
}
6969

7070
impl From<ExtractionCallbacks> for hax_frontend_exporter_options::Options {
@@ -122,7 +122,7 @@ impl Callbacks for ExtractionCallbacks {
122122

123123
use hax_types::driver_api::{HaxMeta, with_kind_type};
124124
with_kind_type!(
125-
self.body_types.clone(),
125+
self.body_kinds.clone(),
126126
<Body>|| {
127127
let (spans, def_ids, impl_infos, items, cache_map) =
128128
convert_thir(&self.clone().into(), tcx);

cli/driver/src/features.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,10 @@ impl Features {
7979
}
8080
/// Runs Rustc with a driver that only collects which unstable
8181
/// Rustc features are enabled
82-
pub fn detect(options: &hax_types::cli_options::Options, rustc_args: &Vec<String>) -> Self {
82+
pub fn detect(
83+
options: &hax_types::cli_options::ExporterOptions,
84+
rustc_args: &Vec<String>,
85+
) -> Self {
8386
struct CollectFeatures {
8487
features: Features,
8588
}

cli/subcommands/src/cargo_hax.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,7 @@ fn get_hax_rustc_driver_path() -> PathBuf {
510510
/// in `TARGET`. One `haxmeta` file is produced by crate. Each
511511
/// `haxmeta` file contains the full AST of one crate.
512512
fn compute_haxmeta_files(options: &Options) -> (Vec<EmitHaxMetaMessage>, i32) {
513+
let frontend_options = ExporterOptions::from(options);
513514
let mut cmd = {
514515
let mut cmd = process::Command::new("cargo");
515516
if let Some(toolchain) = toolchain() {
@@ -536,7 +537,7 @@ fn compute_haxmeta_files(options: &Options) -> (Vec<EmitHaxMetaMessage>, i32) {
536537
.env("HAX_CARGO_CACHE_KEY", get_hax_version())
537538
.env(
538539
ENV_VAR_OPTIONS_FRONTEND,
539-
serde_json::to_string(&options)
540+
serde_json::to_string(&frontend_options)
540541
.expect("Options could not be converted to a JSON string"),
541542
);
542543
cmd
@@ -660,20 +661,20 @@ fn run_command(options: &Options, haxmeta_files: Vec<EmitHaxMetaMessage>) -> boo
660661
fn main() {
661662
let args: Vec<String> = get_args("hax");
662663
let mut options = match &args[..] {
663-
[_, kw] if kw == "__json" => serde_json::from_str(
664-
&std::env::var(ENV_VAR_OPTIONS_FRONTEND).unwrap_or_else(|_| {
664+
[_, kw] if kw == "__json" => {
665+
serde_json::from_str(&std::env::var(ENV_VAR_OPTIONS_FULL).unwrap_or_else(|_| {
665666
panic!(
666667
"Cannot find environnement variable {}",
667-
ENV_VAR_OPTIONS_FRONTEND
668+
ENV_VAR_OPTIONS_FULL
668669
)
669-
}),
670-
)
671-
.unwrap_or_else(|_| {
672-
panic!(
673-
"Invalid value for the environnement variable {}",
674-
ENV_VAR_OPTIONS_FRONTEND
675-
)
676-
}),
670+
}))
671+
.unwrap_or_else(|_| {
672+
panic!(
673+
"Invalid value for the environnement variable {}",
674+
ENV_VAR_OPTIONS_FULL
675+
)
676+
})
677+
}
677678
_ => Options::parse_from(args.iter()),
678679
};
679680
options.normalize_paths();

hax-types/src/cli_options/mod.rs

Lines changed: 72 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -195,16 +195,7 @@ pub enum Backend<E: Extension> {
195195

196196
impl fmt::Display for Backend<()> {
197197
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
198-
match self {
199-
Backend::Fstar { .. } => write!(f, "fstar"),
200-
Backend::Coq { .. } => write!(f, "coq"),
201-
Backend::Ssprove { .. } => write!(f, "ssprove"),
202-
Backend::Easycrypt { .. } => write!(f, "easycrypt"),
203-
Backend::ProVerif { .. } => write!(f, "proverif"),
204-
Backend::Lean { .. } => write!(f, "lean"),
205-
Backend::Rust { .. } => write!(f, "rust"),
206-
Backend::GenerateRustEngineNames { .. } => write!(f, "generate_rust_engine_names"),
207-
}
198+
BackendName::from(self).fmt(f)
208199
}
209200
}
210201

@@ -537,4 +528,75 @@ impl From<Options> for hax_frontend_exporter_options::Options {
537528
}
538529
}
539530

531+
/// The subset of `Options` the frontend is sensible to.
532+
#[derive_group(Serializers)]
533+
#[derive(JsonSchema, Debug, Clone)]
534+
pub struct ExporterOptions {
535+
pub deps: bool,
536+
pub force_cargo_build: ForceCargoBuild,
537+
pub backend: Option<BackendName>,
538+
pub body_kinds: Vec<ExportBodyKind>,
539+
}
540+
541+
#[derive_group(Serializers)]
542+
#[derive(JsonSchema, Debug, Clone, Copy)]
543+
pub enum BackendName {
544+
Fstar,
545+
Coq,
546+
Ssprove,
547+
Easycrypt,
548+
ProVerif,
549+
Lean,
550+
Rust,
551+
GenerateRustEngineNames,
552+
}
553+
554+
impl fmt::Display for BackendName {
555+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
556+
let name = match self {
557+
BackendName::Fstar => "fstar",
558+
BackendName::Coq => "coq",
559+
BackendName::Ssprove => "ssprove",
560+
BackendName::Easycrypt => "easycrypt",
561+
BackendName::ProVerif => "proverif",
562+
BackendName::Lean => "lean",
563+
BackendName::Rust => "rust",
564+
BackendName::GenerateRustEngineNames => "generate_rust_engine_names",
565+
};
566+
write!(f, "{name}")
567+
}
568+
}
569+
570+
impl From<&Options> for ExporterOptions {
571+
fn from(options: &Options) -> Self {
572+
let backend = match &options.command {
573+
Command::Backend(backend_options) => Some((&backend_options.backend).into()),
574+
_ => None,
575+
};
576+
let body_kinds = options.command.body_kinds();
577+
ExporterOptions {
578+
deps: options.deps,
579+
force_cargo_build: options.force_cargo_build.clone(),
580+
backend,
581+
body_kinds,
582+
}
583+
}
584+
}
585+
586+
impl From<&Backend<()>> for BackendName {
587+
fn from(backend: &Backend<()>) -> Self {
588+
match backend {
589+
Backend::Fstar { .. } => BackendName::Fstar,
590+
Backend::Coq { .. } => BackendName::Coq,
591+
Backend::Ssprove { .. } => BackendName::Ssprove,
592+
Backend::Easycrypt { .. } => BackendName::Easycrypt,
593+
Backend::ProVerif { .. } => BackendName::ProVerif,
594+
Backend::Lean { .. } => BackendName::Lean,
595+
Backend::Rust { .. } => BackendName::Rust,
596+
Backend::GenerateRustEngineNames { .. } => BackendName::GenerateRustEngineNames,
597+
}
598+
}
599+
}
600+
540601
pub const ENV_VAR_OPTIONS_FRONTEND: &str = "DRIVER_HAX_FRONTEND_OPTS";
602+
pub const ENV_VAR_OPTIONS_FULL: &str = "DRIVER_HAX_FRONTEND_FULL_OPTS";

0 commit comments

Comments
 (0)