Skip to content

Commit

Permalink
Use pdl-compiler directly
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
marshallpierce committed Mar 12, 2024
1 parent 023c89e commit 4556d39
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 32 deletions.
8 changes: 6 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Cargo.lock
target/
bazel-*
/.idea
3 changes: 3 additions & 0 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
65 changes: 35 additions & 30 deletions rust/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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());
}

0 comments on commit 4556d39

Please sign in to comment.