Skip to content

Commit 15de03b

Browse files
committed
Write tgt IR in build/rules
1 parent 8eb3f89 commit 15de03b

2 files changed

Lines changed: 17 additions & 10 deletions

File tree

rule-preprocessor/src/ir.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -221,19 +221,23 @@ pub struct RulesIR {
221221
}
222222

223223
impl RulesIR {
224-
pub fn write_ir(&self) {
225-
let crate_root = self.crate_root.canonicalize().unwrap();
224+
pub fn write_ir(&self, out_dir: &Path) {
226225
for (rule_path, file_ir) in &self.all_ir {
227226
let rule_path = Path::new(rule_path);
228-
let file_name = rule_path.file_name().unwrap().to_str().unwrap();
229-
let json_name = file_name.replace("tgt_", "ir_").replace(".rs", ".json");
230-
let json_path = rule_path.parent().unwrap().join(json_name);
231-
227+
let rule_name = rule_path.parent().unwrap().file_name().unwrap();
228+
let json_name = rule_path
229+
.file_name()
230+
.unwrap()
231+
.to_str()
232+
.unwrap()
233+
.replace("tgt_", "ir_")
234+
.replace(".rs", ".json");
235+
let json_path = out_dir.join(rule_name).join(json_name);
236+
237+
std::fs::create_dir_all(json_path.parent().unwrap()).unwrap();
232238
let json = serde_json::to_string_pretty(file_ir).unwrap();
233239
std::fs::write(&json_path, format!("{json}\n")).unwrap();
234-
235-
let json_rel = json_path.strip_prefix(&crate_root).unwrap_or(&json_path);
236-
println!("{}", json_rel.display());
240+
println!("{}", json_path.display());
237241
}
238242
}
239243
}

rule-preprocessor/src/main.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@ use semantic::SemanticAnalysis;
1717
use syntactic::SyntacticAnalysis;
1818

1919
fn main() {
20+
let out_dir = std::env::args()
21+
.nth(1)
22+
.expect("usage: rule-preprocessor <out-dir>");
2023
SemanticAnalysis::run(SyntacticAnalysis::run(
2124
&std::fs::canonicalize("../rules").unwrap(),
2225
))
23-
.write_ir();
26+
.write_ir(&std::path::PathBuf::from(out_dir));
2427
}

0 commit comments

Comments
 (0)