forked from rrevenantt/antlr4rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
72 lines (64 loc) · 1.92 KB
/
build.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
use std::convert::TryInto;
use std::env;
use std::env::VarError;
use std::error::Error;
use std::fs::{read_dir, DirEntry, File};
use std::io::Write;
use std::path::Path;
use std::process::Command;
fn main() {
let grammars = vec![
"VisitorBasic",
"VisitorCalc",
"CSV",
"ReferenceToATN",
"XMLLexer",
"SimpleLR",
"Labels",
"FHIRPath",
];
let additional_args = vec![
Some("-visitor"),
Some("-visitor"),
Some("-visitor"),
None,
None,
None,
None,
];
let antlr_path = "/home/rrevenantt/dev/antlr4/tool/target/antlr4-4.8-2-SNAPSHOT-complete.jar";
for (grammar, arg) in grammars.into_iter().zip(additional_args) {
//ignoring error because we do not need to run anything when deploying to crates.io
let _ = gen_for_grammar(grammar, antlr_path, arg);
}
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=/home/rrevenantt/dev/antlr4/tool/target/antlr4-4.8-2-SNAPSHOT-complete.jar");
}
fn gen_for_grammar(
grammar_file_name: &str,
antlr_path: &str,
additional_arg: Option<&str>,
) -> Result<(), Box<Error>> {
// let out_dir = env::var("OUT_DIR").unwrap();
// let dest_path = Path::new(&out_dir);
let input = env::current_dir().unwrap().join("grammars");
let file_name = grammar_file_name.to_owned() + ".g4";
let c = Command::new("java")
.current_dir(input)
.arg("-cp")
.arg(antlr_path)
.arg("org.antlr.v4.Tool")
.arg("-Dlanguage=Rust")
.arg("-o")
.arg("../tests/gen")
.arg(&file_name)
.args(additional_arg)
.spawn()
.expect("antlr tool failed to start")
.wait_with_output()?;
// .unwrap()
// .stdout;
// eprintln!("xx{}",String::from_utf8(x).unwrap());
println!("cargo:rerun-if-changed=grammars/{}", file_name);
Ok(())
}