-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #997 from laurentder/issue_943
Issue 943
- Loading branch information
Showing
6 changed files
with
297 additions
and
15 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
use std::path::PathBuf; | ||
|
||
use anyhow::anyhow; | ||
use cargo_metadata::{self, Package}; | ||
pub type Result<T> = anyhow::Result<T>; | ||
|
||
pub(crate) fn make_cargo_metadata() -> Result<cargo_metadata::Metadata> { | ||
let mut args = std::env::args().skip_while(|val| !val.starts_with("--manifest-path")); | ||
|
||
let mut cmd = cargo_metadata::MetadataCommand::new(); | ||
match args.next() { | ||
Some(ref p) if p == "--manifest-path" => { | ||
cmd.manifest_path(args.next().unwrap()); | ||
} | ||
Some(p) => { | ||
cmd.manifest_path(p.trim_start_matches("--manifest-path=")); | ||
} | ||
None => {} | ||
}; | ||
|
||
cmd.exec().map_err(|e| e.into()) | ||
} | ||
|
||
fn select_root_crate(m: &cargo_metadata::Metadata) -> Result<&Package> { | ||
if m.workspace_default_members.is_empty() { | ||
return Err(anyhow!("can't create mlcfg file, no default workspace")); | ||
} | ||
|
||
// cargo metadata doesn't specify one particular crate. | ||
// We consider that the first crate in the list of workspace_default_members will give the name of the file | ||
let crate_id = &m.workspace_default_members[0]; | ||
let Some(package) = m.packages.iter().find(|p| &p.id == crate_id) else { | ||
return Err(anyhow!("can't create mlcfg file, no default package")); | ||
}; | ||
|
||
Ok(package) | ||
} | ||
|
||
pub(crate) fn make_mlcfg_filename(m: &cargo_metadata::Metadata) -> Result<PathBuf> { | ||
let root_crate = select_root_crate(m)?; | ||
|
||
// to specify the crate kind in the file name: lib, bin | ||
if root_crate.targets.is_empty() || root_crate.targets[0].kind.is_empty() { | ||
return Err(anyhow!("can't create mlcfg file, default package without target")); | ||
} | ||
|
||
let crate_type = &root_crate.targets[0].kind[0]; | ||
let filename = format!("{}-{}.mlcfg", root_crate.name, crate_type); | ||
|
||
// put the file at the root of the target directory | ||
Ok(m.target_directory.join(filename).into()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
use std::{ | ||
path::{Path, PathBuf}, | ||
process::Command, | ||
}; | ||
|
||
use super::helpers::Result; | ||
use anyhow::anyhow; | ||
use include_dir::{include_dir, Dir}; | ||
|
||
static PRELUDE: Dir<'static> = include_dir!("$CARGO_MANIFEST_DIR/../prelude"); | ||
|
||
#[derive(Copy, Clone, Debug)] | ||
pub enum Why3Mode { | ||
Ide, | ||
} | ||
|
||
impl std::fmt::Display for Why3Mode { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
match self { | ||
Why3Mode::Ide => f.write_str("ide"), | ||
} | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct Why3Launcher { | ||
mode: Why3Mode, | ||
why3_path: Option<PathBuf>, | ||
config_file: Option<PathBuf>, | ||
args: Option<String>, | ||
output_file: PathBuf, | ||
} | ||
|
||
impl Why3Launcher { | ||
pub fn new( | ||
mode: Why3Mode, | ||
why3_path: Option<PathBuf>, | ||
config_file: Option<PathBuf>, | ||
args: Option<String>, | ||
output_file: PathBuf, | ||
) -> Self { | ||
Self { mode, why3_path, config_file, args, output_file } | ||
} | ||
|
||
pub fn make(&self, temp_dir: &Path) -> Result<Command> { | ||
let mode = self.mode.to_string(); | ||
PRELUDE.extract(temp_dir).expect("can't launch why3, could extract prelude into temp dir"); | ||
|
||
let mut command = | ||
if let Some(p) = &self.why3_path { Command::new(p) } else { Command::new("why3") }; | ||
command | ||
.args([ | ||
"--warn-off=unused_variable", | ||
"--warn-off=clone_not_abstract", | ||
"--warn-off=axiom_abstract", | ||
&mode, | ||
"-L", | ||
]) | ||
.arg(temp_dir.as_os_str()) | ||
.arg(&self.output_file); | ||
if let Some(cfg) = &self.config_file { | ||
command.arg("-C").arg(cfg); | ||
} | ||
if let Some(args) = &self.args { | ||
if !args.is_empty() { | ||
command.args(args.split_ascii_whitespace()); | ||
} | ||
} | ||
|
||
Ok(command) | ||
} | ||
} | ||
|
||
pub struct Why3LauncherBuilder { | ||
mode: Why3Mode, | ||
why3_path: Option<PathBuf>, | ||
config_file: Option<PathBuf>, | ||
args: Option<String>, | ||
output_file: Option<PathBuf>, | ||
} | ||
|
||
impl Why3LauncherBuilder { | ||
pub fn new() -> Self { | ||
Self { | ||
mode: Why3Mode::Ide, | ||
why3_path: None, | ||
config_file: None, | ||
args: None, | ||
output_file: None, | ||
} | ||
} | ||
|
||
pub fn mode(&mut self, m: Why3Mode) -> &mut Self { | ||
self.mode = m; | ||
self | ||
} | ||
|
||
pub fn why3_path(&mut self, p: PathBuf) -> &mut Self { | ||
self.why3_path = Some(p); | ||
self | ||
} | ||
|
||
pub fn config_file(&mut self, p: PathBuf) -> &mut Self { | ||
self.config_file = Some(p); | ||
self | ||
} | ||
|
||
pub fn args(&mut self, a: String) -> &mut Self { | ||
self.args = Some(a); | ||
self | ||
} | ||
|
||
pub fn output_file(&mut self, p: PathBuf) -> &mut Self { | ||
self.output_file = Some(p); | ||
self | ||
} | ||
|
||
pub fn build(self) -> Result<Why3Launcher> { | ||
let Some(mlcfg_file) = self.output_file else { | ||
return Err(anyhow!("can't launch why3, no mlcfg_file specify")); | ||
}; | ||
|
||
Ok(Why3Launcher::new(self.mode, self.why3_path, self.config_file, self.args, mlcfg_file)) | ||
} | ||
} |
Oops, something went wrong.