diff options
author | 2024-11-25 17:01:57 -0800 | |
---|---|---|
committer | 2024-11-25 18:09:41 -0800 | |
commit | c5132bb980e9d9858027505c6c98fef694cdd322 (patch) | |
tree | fa2c5bd53fde8769921ab88eb371f8c6a86ba6b3 /tools/rootcanal | |
parent | 3a9f725de1ba8e4f1a2513b4e3c63b07b986885d (diff) |
RootCanal: Update build.rs to directly use the pdl_compiler library
Bug: 279665786
Test: cargo -C tools/rootcanal/rust build
Test: m rootcanal
Flag: EXEMPT build change
Change-Id: I20ae4a4678b457cf6a1614ddcf5c56342bea0311
Diffstat (limited to 'tools/rootcanal')
-rw-r--r-- | tools/rootcanal/rust/Cargo.toml | 3 | ||||
-rw-r--r-- | tools/rootcanal/rust/build.rs | 39 |
2 files changed, 15 insertions, 27 deletions
diff --git a/tools/rootcanal/rust/Cargo.toml b/tools/rootcanal/rust/Cargo.toml index 993836ac98..4eaf4cc32a 100644 --- a/tools/rootcanal/rust/Cargo.toml +++ b/tools/rootcanal/rust/Cargo.toml @@ -31,6 +31,9 @@ pin-utils = "0.1.0" rand = "0.8.3" thiserror = "1.0.23" +[build-dependencies] +pdl-compiler = "0.3.0" + [lib] path="src/lib.rs" crate-type = ["staticlib"] diff --git a/tools/rootcanal/rust/build.rs b/tools/rootcanal/rust/build.rs index 793e1f93e1..f9d5f7a310 100644 --- a/tools/rootcanal/rust/build.rs +++ b/tools/rootcanal/rust/build.rs @@ -14,8 +14,8 @@ use std::env; use std::fs::File; +use std::io::Write; use std::path::{Path, PathBuf}; -use std::process::{Command, Stdio}; fn main() { install_generated_module( @@ -53,35 +53,20 @@ fn install_generated_module(module_name: &str, prebuilt_var: &str, pdl_name: &Pa } } -fn generate_module(in_file: &PathBuf) { +fn generate_module(in_file: &Path) { let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap()); - let out_file = + let mut out_file = File::create(out_dir.join(in_file.file_name().unwrap()).with_extension("rs")).unwrap(); - // Find the pdl tool. Expecting it at CARGO_HOME/bin - let pdl = match env::var("CARGO_HOME") { - Ok(dir) => PathBuf::from(dir).join("bin").join("pdlc"), - Err(_) => PathBuf::from("pdlc"), - }; - - if !Path::new(pdl.as_os_str()).exists() { - panic!("pdl not found in the current environment: {:?}", pdl.as_os_str().to_str().unwrap()); - } - println!("cargo:rerun-if-changed={}", in_file.display()); - let output = Command::new(pdl.as_os_str().to_str().unwrap()) - .arg("--output-format") - .arg("rust") - .arg(in_file) - .stdout(Stdio::from(out_file)) - .output() - .unwrap(); - - println!( - "Status: {}, stderr: {}", - output.status, - String::from_utf8_lossy(output.stderr.as_slice()) - ); - assert!(output.status.success()); + let mut sources = pdl_compiler::ast::SourceDatabase::new(); + let parsed_file = pdl_compiler::parser::parse_file( + &mut sources, + in_file.to_str().expect("Filename is not UTF-8"), + ) + .expect("PDL parse failed"); + let analyzed_file = pdl_compiler::analyzer::analyze(&parsed_file).expect("PDL analysis failed"); + let rust_source = pdl_compiler::backends::rust_legacy::generate(&sources, &analyzed_file); + out_file.write_all(rust_source.as_bytes()).expect("Could not write to output file"); } |