Skip to content

Commit 6ba7380

Browse files
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.
1 parent 6d07a52 commit 6ba7380

File tree

3 files changed

+39
-30
lines changed

3 files changed

+39
-30
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
Cargo.lock
22
target/
33
bazel-*
4+
/.idea

rust/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ pin-utils = "0.1.0"
3030
rand = "0.8.3"
3131
thiserror = "1.0.23"
3232

33+
[build-dependencies]
34+
pdl-compiler = "0.1.6"
35+
3336
[lib]
3437
path="src/lib.rs"
3538
crate-type = ["staticlib"]

rust/build.rs

+35-30
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use std::env;
1616
use std::fs::File;
1717
use std::path::{Path, PathBuf};
1818
use std::process::{Command, Stdio};
19+
use std::io::Write;
1920

2021
fn main() {
2122
install_generated_module(
@@ -41,47 +42,51 @@ fn install_generated_module(module_name: &str, prebuilt_var: &str, pdl_name: &Pa
4142
Err(_) => PathBuf::from(module_name),
4243
};
4344

44-
if Path::new(module_prebuilt.as_os_str()).exists() {
45-
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
46-
std::fs::copy(
47-
module_prebuilt.as_os_str().to_str().unwrap(),
48-
out_dir.join(module_name).as_os_str().to_str().unwrap(),
49-
)
50-
.unwrap();
45+
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()).join(module_name);
46+
47+
if module_prebuilt.exists() {
48+
std::fs::copy(module_prebuilt, &out_path).unwrap();
5149
} else {
52-
generate_module(pdl_name);
50+
generate_module(pdl_name, &out_path);
5351
}
52+
53+
// set var for use in `include!`
54+
println!("cargo:rustc-env={}={}", prebuilt_var, out_path.to_str().unwrap());
5455
}
5556

56-
fn generate_module(in_file: &PathBuf) {
57-
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
58-
let out_file =
59-
File::create(out_dir.join(in_file.file_name().unwrap()).with_extension("rs")).unwrap();
57+
fn generate_module(in_file: &Path, out_path: &Path) {
58+
let mut out_file = File::create(out_path).unwrap();
6059

6160
// Find the pdl tool. Expecting it at CARGO_HOME/bin
62-
let pdl = match env::var("CARGO_HOME") {
61+
let pdlc = match env::var("CARGO_HOME") {
6362
Ok(dir) => PathBuf::from(dir).join("bin").join("pdlc"),
6463
Err(_) => PathBuf::from("pdlc"),
6564
};
6665

67-
if !Path::new(pdl.as_os_str()).exists() {
68-
panic!("pdl not found in the current environment: {:?}", pdl.as_os_str().to_str().unwrap());
69-
}
66+
if pdlc.exists() {
67+
let output = Command::new(pdlc.as_os_str().to_str().unwrap())
68+
.arg("--output-format")
69+
.arg("rust")
70+
.arg(in_file)
71+
.stdout(Stdio::from(out_file))
72+
.output()
73+
.unwrap();
7074

71-
println!("cargo:rerun-if-changed={}", in_file.display());
72-
let output = Command::new(pdl.as_os_str().to_str().unwrap())
73-
.arg("--output-format")
74-
.arg("rust")
75-
.arg(in_file)
76-
.stdout(Stdio::from(out_file))
77-
.output()
78-
.unwrap();
75+
println!(
76+
"Status: {}, stderr: {}",
77+
output.status,
78+
String::from_utf8_lossy(output.stderr.as_slice())
79+
);
7980

80-
println!(
81-
"Status: {}, stderr: {}",
82-
output.status,
83-
String::from_utf8_lossy(output.stderr.as_slice())
84-
);
81+
assert!(output.status.success());
82+
} else {
83+
// use pdl_compiler as a library
84+
let mut sources = pdl_compiler::ast::SourceDatabase::new();
85+
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");
86+
let analyzed_file = pdl_compiler::analyzer::analyze(&parsed_file).expect("PDL analysis failed");
87+
let rust_source = pdl_compiler::backends::rust::generate(&sources, &analyzed_file);
88+
out_file.write_all(rust_source.as_bytes()).expect("Could not write to output file");
89+
}
8590

86-
assert!(output.status.success());
91+
println!("cargo:rerun-if-changed={}", in_file.display());
8792
}

0 commit comments

Comments
 (0)