|
| 1 | +use mdbook::book::{Book, Chapter}; |
| 2 | +use mdbook::errors::Error; |
| 3 | +use mdbook::preprocess::{CmdPreprocessor, Preprocessor, PreprocessorContext}; |
| 4 | +use mdbook::BookItem; |
| 5 | +use std::{env, io, path::Path, process::Command}; |
| 6 | + |
| 7 | +fn main() { |
| 8 | + let mut args = std::env::args().skip(1); |
| 9 | + match args.next().as_deref() { |
| 10 | + Some("supports") => { |
| 11 | + // Supports all renderers. |
| 12 | + return; |
| 13 | + } |
| 14 | + Some(arg) => { |
| 15 | + eprintln!("unknown argument: {arg}"); |
| 16 | + std::process::exit(1); |
| 17 | + } |
| 18 | + None => {} |
| 19 | + } |
| 20 | + |
| 21 | + if let Err(e) = handle_preprocessing() { |
| 22 | + eprintln!("{}", e); |
| 23 | + std::process::exit(1); |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +// Plot the Kani metrics. |
| 28 | +// The mdbook builder reads the postprocessed book from stdout, |
| 29 | +// so suppress all Command output to avoid having its output interpreted as part of the book |
| 30 | +fn run_kani_metrics_script() -> Result<(), Error> { |
| 31 | + // This will be the absolute path to the "doc/" folder |
| 32 | + let original_dir = env::current_dir()?; |
| 33 | + let tools_path = original_dir.join(Path::new("doc/src/tools")); |
| 34 | + |
| 35 | + let kani_std_analysis_path = Path::new("scripts/kani-std-analysis"); |
| 36 | + env::set_current_dir(kani_std_analysis_path)?; |
| 37 | + |
| 38 | + Command::new("pip") |
| 39 | + .args(&["install", "-r", "requirements.txt"]) |
| 40 | + .stdout(std::process::Stdio::null()) |
| 41 | + .stderr(std::process::Stdio::null()) |
| 42 | + .status()?; |
| 43 | + |
| 44 | + Command::new("python") |
| 45 | + .args(&[ |
| 46 | + "kani_std_analysis.py", |
| 47 | + "--plot-only", |
| 48 | + "--plot-dir", |
| 49 | + &tools_path.to_string_lossy(), |
| 50 | + ]) |
| 51 | + .stdout(std::process::Stdio::null()) |
| 52 | + .stderr(std::process::Stdio::null()) |
| 53 | + .status()?; |
| 54 | + |
| 55 | + env::set_current_dir(&original_dir)?; |
| 56 | + |
| 57 | + Ok(()) |
| 58 | +} |
| 59 | + |
| 60 | +struct AddKaniGraphs; |
| 61 | + |
| 62 | +impl Preprocessor for AddKaniGraphs { |
| 63 | + fn name(&self) -> &str { |
| 64 | + "add-metrics" |
| 65 | + } |
| 66 | + |
| 67 | + fn run(&self, _ctx: &PreprocessorContext, mut book: Book) -> Result<Book, Error> { |
| 68 | + book.for_each_mut(|item| { |
| 69 | + if let BookItem::Chapter(ch) = item { |
| 70 | + if ch.name == "Kani" { |
| 71 | + add_graphs(ch); |
| 72 | + return; |
| 73 | + } |
| 74 | + } |
| 75 | + }); |
| 76 | + Ok(book) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +fn add_graphs(chapter: &mut Chapter) { |
| 81 | + let new_content = r#" |
| 82 | +## Kani Metrics |
| 83 | +
|
| 84 | +Note that these metrics are for x86-64 architectures. |
| 85 | +
|
| 86 | +## `core` |
| 87 | + |
| 88 | +
|
| 89 | + |
| 90 | +
|
| 91 | + |
| 92 | +"#; |
| 93 | + |
| 94 | + chapter.content.push_str(new_content); |
| 95 | +} |
| 96 | + |
| 97 | +pub fn handle_preprocessing() -> Result<(), Error> { |
| 98 | + run_kani_metrics_script()?; |
| 99 | + let pre = AddKaniGraphs; |
| 100 | + let (ctx, book) = CmdPreprocessor::parse_input(io::stdin())?; |
| 101 | + |
| 102 | + let processed_book = pre.run(&ctx, book)?; |
| 103 | + serde_json::to_writer(io::stdout(), &processed_book)?; |
| 104 | + |
| 105 | + Ok(()) |
| 106 | +} |
0 commit comments