rapx/analysis/utils/
draw_dot.rs

1use std::fs::{File, remove_file};
2use std::io::Write;
3use std::process::Command;
4
5// please ensure 'graphviz' has been installed
6pub fn render_dot_graphs(dot_graphs: Vec<(String, String)>) {
7    Command::new("mkdir")
8        .args(["UPG"])
9        .output()
10        .expect("Failed to create directory");
11
12    for (_index, dot) in dot_graphs.into_iter().enumerate() {
13        let file_name = format!("{}.dot", dot.0);
14        let mut file = File::create(&file_name).expect("Unable to create file");
15        file.write_all(dot.1.as_bytes())
16            .expect("Unable to write data");
17
18        Command::new("dot")
19            .args(["-Tpng", &file_name, "-o", &format!("UPG/{}.png", dot.0)])
20            .output()
21            .expect("Failed to execute Graphviz dot command");
22
23        remove_file(&file_name).expect("Failed to delete .dot file");
24    }
25}
26
27pub fn render_dot_string(name: String, dot_graph: String) {
28    Command::new("mkdir")
29        .args(["MIR_dot_graph"])
30        .output()
31        .expect("Failed to create directory");
32
33    let file_name = format!("{}.dot", name);
34    rap_debug!("render graph {:?}", file_name);
35    let mut file = File::create(&file_name).expect("Unable to create file");
36    file.write_all(dot_graph.as_bytes())
37        .expect("Unable to write data");
38
39    Command::new("dot")
40        .args([
41            "-Tpng",
42            &file_name,
43            "-o",
44            &format!("MIR_dot_graph/{}.png", name),
45        ])
46        .output()
47        .expect("Failed to execute Graphviz dot command");
48
49    remove_file(&file_name).expect("Failed to delete .dot file");
50}