From 4556d3935340f19c3b9ce8dc6cd3d0290441a447 Mon Sep 17 00:00:00 2001 From: Marshall Pierce Date: Wed, 30 Aug 2023 11:14:49 -0600 Subject: [PATCH] Use pdl-compiler directly If the `pdlc` binary is not available, use `pdl_compiler` as a library instead. Also do some misc tidying of the build script, since it looks like the output path was being derived in different ways with the same result. --- .github/workflows/build.yml | 8 +++-- .gitignore | 1 + rust/Cargo.toml | 3 ++ rust/build.rs | 65 ++++++++++++++++++++----------------- 4 files changed, 45 insertions(+), 32 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 51a2059..6214247 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -26,9 +26,13 @@ jobs: with: submodules: recursive + - name: Check Cargo build + run: | + cargo test + - name: Install dependencies run: | - cargo install pdl-compiler --version 0.2.2 + cargo install pdl-compiler --version 0.2.3 - name: Build run: | @@ -65,7 +69,7 @@ jobs: - name: Install dependencies run: | - cargo install pdl-compiler --version 0.2.2 + cargo install pdl-compiler --version 0.2.3 python3 -m pip install hatch - name: Set VERSION diff --git a/.gitignore b/.gitignore index fd2305f..8e2a49f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ Cargo.lock target/ bazel-* +/.idea diff --git a/rust/Cargo.toml b/rust/Cargo.toml index 1510574..ad004de 100644 --- a/rust/Cargo.toml +++ b/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.2.3" + [lib] path="src/lib.rs" crate-type = ["staticlib"] diff --git a/rust/build.rs b/rust/build.rs index 793e1f9..e6518dd 100644 --- a/rust/build.rs +++ b/rust/build.rs @@ -16,6 +16,7 @@ use std::env; use std::fs::File; use std::path::{Path, PathBuf}; use std::process::{Command, Stdio}; +use std::io::Write; fn main() { install_generated_module( @@ -41,47 +42,51 @@ fn install_generated_module(module_name: &str, prebuilt_var: &str, pdl_name: &Pa Err(_) => PathBuf::from(module_name), }; - if Path::new(module_prebuilt.as_os_str()).exists() { - let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap()); - std::fs::copy( - module_prebuilt.as_os_str().to_str().unwrap(), - out_dir.join(module_name).as_os_str().to_str().unwrap(), - ) - .unwrap(); + let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()).join(module_name); + + if module_prebuilt.exists() { + std::fs::copy(module_prebuilt, &out_path).unwrap(); } else { - generate_module(pdl_name); + generate_module(pdl_name, &out_path); } + + // set var for use in `include!` + println!("cargo:rustc-env={}={}", prebuilt_var, out_path.to_str().unwrap()); } -fn generate_module(in_file: &PathBuf) { - let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap()); - let out_file = - File::create(out_dir.join(in_file.file_name().unwrap()).with_extension("rs")).unwrap(); +fn generate_module(in_file: &Path, out_path: &Path) { + let mut out_file = File::create(out_path).unwrap(); // Find the pdl tool. Expecting it at CARGO_HOME/bin - let pdl = match env::var("CARGO_HOME") { + let pdlc = 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()); - } + if pdlc.exists() { + let output = Command::new(pdlc.as_os_str().to_str().unwrap()) + .arg("--output-format") + .arg("rust") + .arg(in_file) + .stdout(Stdio::from(out_file)) + .output() + .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()) + ); - println!( - "Status: {}, stderr: {}", - output.status, - String::from_utf8_lossy(output.stderr.as_slice()) - ); + assert!(output.status.success()); + } else { + // use pdl_compiler as a library + 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").to_string()).expect("PDL parse failed"); + let analyzed_file = pdl_compiler::analyzer::analyze(&parsed_file).expect("PDL analysis failed"); + let rust_source = pdl_compiler::backends::rust::generate(&sources, &analyzed_file); + out_file.write_all(rust_source.as_bytes()).expect("Could not write to output file"); + } - assert!(output.status.success()); + println!("cargo:rerun-if-changed={}", in_file.display()); }