1 // Copyright 2023, Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 use std::env;
16 use std::fs::File;
17 use std::io::Write;
18 use std::path::{Path, PathBuf};
19
main()20 fn main() {
21 generate_module(&Path::new("src/uci_packets.pdl").canonicalize().unwrap());
22 }
23
generate_module(in_file: &Path)24 fn generate_module(in_file: &Path) {
25 let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
26 let mut out_file = File::create(
27 out_dir
28 .join(in_file.file_name().unwrap())
29 .with_extension("rs"),
30 )
31 .unwrap();
32
33 println!("cargo:rerun-if-changed={}", in_file.display());
34
35 let mut sources = pdl_compiler::ast::SourceDatabase::new();
36 let parsed_file = pdl_compiler::parser::parse_file(
37 &mut sources,
38 in_file.to_str().expect("Filename is not UTF-8"),
39 )
40 .expect("PDL parse failed");
41 let analyzed_file = pdl_compiler::analyzer::analyze(&parsed_file).expect("PDL analysis failed");
42 let rust_source = pdl_compiler::backends::rust_legacy::generate(&sources, &analyzed_file);
43 out_file
44 .write_all(rust_source.as_bytes())
45 .expect("Could not write to output file");
46 }
47