Skip to content

Use pdl-compiler directly #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might as well check that the cargo build works 🤷


- name: Install dependencies
run: |
cargo install pdl-compiler --version 0.2.2
cargo install pdl-compiler --version 0.2.3
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm assuming it's desirable for pdl-compiler to be up to date -- if you'd rather stick with 0.2.2 that's fine too of course


- 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") {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it even desirable to maintain this code path? If someone is using Cargo vs the Blaze build, when would they want to use the external pdlc? Or perhaps should it be present to allow hypothetical debugging of issues specific to a pdlc binary, but only used if an env var is set?

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());
}