forked from model-checking/verify-rust-std
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
make metrics a mdbook preprocessor instead
- Loading branch information
1 parent
31aed2a
commit 864be39
Showing
8 changed files
with
133 additions
and
37 deletions.
There are no files selected for viewing
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
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 @@ | ||
target |
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,10 @@ | ||
[package] | ||
name = "mdbook-kani-metrics" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
mdbook = { version = "^0.4" } | ||
pulldown-cmark = { version = "0.12.2", default-features = false } | ||
pulldown-cmark-to-cmark = "18.0.0" | ||
serde_json = "1.0.132" |
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,106 @@ | ||
use mdbook::book::{Book, Chapter}; | ||
use mdbook::errors::Error; | ||
use mdbook::preprocess::{CmdPreprocessor, Preprocessor, PreprocessorContext}; | ||
use mdbook::BookItem; | ||
use std::{env, io, path::Path, process::Command}; | ||
|
||
fn main() { | ||
let mut args = std::env::args().skip(1); | ||
match args.next().as_deref() { | ||
Some("supports") => { | ||
// Supports all renderers. | ||
return; | ||
} | ||
Some(arg) => { | ||
eprintln!("unknown argument: {arg}"); | ||
std::process::exit(1); | ||
} | ||
None => {} | ||
} | ||
|
||
if let Err(e) = handle_preprocessing() { | ||
eprintln!("{}", e); | ||
std::process::exit(1); | ||
} | ||
} | ||
|
||
// Plot the Kani metrics. | ||
// The mdbook builder reads the postprocessed book from stdout, | ||
// so suppress all Command output to avoid having its output interpreted as part of the book | ||
fn run_kani_metrics_script() -> Result<(), Error> { | ||
// This will be the absolute path to the "doc/" folder | ||
let original_dir = env::current_dir()?; | ||
let tools_path = original_dir.join(Path::new("src/tools")); | ||
|
||
let kani_std_analysis_path = Path::new("../scripts/kani-std-analysis"); | ||
env::set_current_dir(kani_std_analysis_path)?; | ||
|
||
Command::new("pip") | ||
.args(&["install", "-r", "requirements.txt"]) | ||
.stdout(std::process::Stdio::null()) | ||
.stderr(std::process::Stdio::null()) | ||
.status()?; | ||
|
||
Command::new("python") | ||
.args(&[ | ||
"kani_std_analysis.py", | ||
"--plot-only", | ||
"--plot-dir", | ||
&tools_path.to_string_lossy(), | ||
]) | ||
.stdout(std::process::Stdio::null()) | ||
.stderr(std::process::Stdio::null()) | ||
.status()?; | ||
|
||
env::set_current_dir(&original_dir)?; | ||
|
||
Ok(()) | ||
} | ||
|
||
struct AddKaniGraphs; | ||
|
||
impl Preprocessor for AddKaniGraphs { | ||
fn name(&self) -> &str { | ||
"add-metrics" | ||
} | ||
|
||
fn run(&self, _ctx: &PreprocessorContext, mut book: Book) -> Result<Book, Error> { | ||
book.for_each_mut(|item| { | ||
if let BookItem::Chapter(ch) = item { | ||
if ch.name == "Kani" { | ||
add_graphs(ch); | ||
return; | ||
} | ||
} | ||
}); | ||
Ok(book) | ||
} | ||
} | ||
|
||
fn add_graphs(chapter: &mut Chapter) { | ||
let new_content = r#" | ||
## Kani Metrics | ||
Note that these metrics are for x86-64 architectures. | ||
## `core` | ||
![Unsafe Metrics](core_unsafe_metrics.png) | ||
![Safe Abstractions Metrics](core_safe_abstractions_metrics.png) | ||
![Safe Metrics](core_safe_metrics.png) | ||
"#; | ||
|
||
chapter.content.push_str(new_content); | ||
} | ||
|
||
pub fn handle_preprocessing() -> Result<(), Error> { | ||
run_kani_metrics_script()?; | ||
let pre = AddKaniGraphs; | ||
let (ctx, book) = CmdPreprocessor::parse_input(io::stdin())?; | ||
|
||
let processed_book = pre.run(&ctx, book)?; | ||
serde_json::to_writer(io::stdout(), &processed_book)?; | ||
|
||
Ok(()) | ||
} |
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 was deleted.
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