Skip to content
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

Build script rework #59

Open
wants to merge 1 commit into
base: master
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
38 changes: 26 additions & 12 deletions .github/workflows/antlr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@ jobs:
uses: actions/setup-java@v1
with:
java-version: 1.8
- name: Install Rust
uses: actions-rs/toolchain@v1
with:
toolchain: stable
default: true
- name: Cache local Maven repository
uses: actions/cache@v2
with:
Expand All @@ -41,18 +36,24 @@ jobs:
${{ runner.os }}-maven-
- name: Build with Maven
run : mvn -DskipTests install -q
- uses: actions/upload-artifact@v2
with:
name: antlr-tool
path: tool/target/antlr4-4.8-2-SNAPSHOT-complete.jar
- name: Install Rust
uses: actions-rs/toolchain@v1
with:
toolchain: stable
default: true
- name : Maven tests
env:
ANTLR4_PATH: skip
run: |
mvn test -Dtest=rust.* -q
rc=$?;
cat target/surefire-reports/*.dumpstream || true;
exit $rc
working-directory: runtime-testsuite
- uses: actions/upload-artifact@v2
if: ${{ github.event_name == 'push' || github.event_name == 'workflow_dispatch' }}
with:
name: antlr-tool
path: tool/target/antlr4-4.8-2-SNAPSHOT-complete.jar

tests-rust:
name: cargo test
Expand All @@ -62,6 +63,8 @@ jobs:
strategy:
matrix:
rust_version: [ 1.52 , stable]
env:
ANTLR4_PATH: antlr4-4.8-2-SNAPSHOT-complete.jar
steps:
- uses: actions/checkout@v2
- name: Install Rust
Expand All @@ -70,12 +73,23 @@ jobs:
toolchain: ${{ matrix.rust_version }}
default: true
components: rustfmt
- name: Wait for antlr-tool being built
# todo find out better way to do that
run: sleep 60s
- uses: actions/download-artifact@v2
with:
name: antlr-tool
- name: Build
run: cargo build --verbose
run: cargo build -vv
- name: Run tests
run: cargo test --verbose
- name: Formatting
run: cargo fmt -- --check
run: |
cargo fmt
rm antlr4-4.8-2-SNAPSHOT-complete.jar
if [[ $(git status --porcelain | wc -l) != "0" ]]; then
exit 1
fi
- uses: actions/checkout@v2
if: ${{ github.event_name == 'push' }}
with:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/target
/tests/gen/*.tokens
/tests/gen/*.interp
/tests/gen/*
**/*.rs.bk
*.iml
Cargo.lock
72 changes: 52 additions & 20 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use std::convert::TryInto;
//! Used to generate tests.
//! Not uploaded to crates.io, so this build script should not affect users if this crate is used as a regular crates.io dependency
//!
//! set ANTLR4_PATH=download if you don't want/need a full rebuild of antlr tool,
//! and it will download a corresponding artifact right from github releases
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;

Expand All @@ -16,7 +18,6 @@ fn main() {
"XMLLexer",
"SimpleLR",
"Labels",
"FHIRPath",
];
let additional_args = vec![
Some("-visitor"),
Expand All @@ -27,30 +28,65 @@ fn main() {
None,
None,
];
let antlr_path = "/home/rrevenantt/dev/antlr4/tool/target/antlr4-4.8-2-SNAPSHOT-complete.jar";

println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-env-changed=ANTLR4_PATH");

let antlr_path = match env::var("ANTLR4_PATH").as_deref() {
Ok("download") => {
let url = format!(
"https://github.com/rrevenantt/antlr4rust/releases/download/antlr4-4.8-2-Rust{}/antlr4-4.8-2-SNAPSHOT-complete.jar",
env::var("CARGO_PKG_VERSION").unwrap()
);
let path = Path::new(&env::var("OUT_DIR").unwrap()).join("tool.jar");
Command::new("curl")
.arg(url)
.arg("-L")
.arg("-o")
.arg(&path)
.spawn()
.unwrap()
.wait_with_output()
.expect("error running curl");
path.to_str().unwrap().to_string()
}
Ok("skip") => return,
Ok(x) => Path::new(x)
.canonicalize()
.unwrap()
.to_str()
.unwrap()
.to_string(),
Err(VarError::NotUnicode(_)) => panic!("non unicode env variable"),
Err(_) => {
let default_path = env::current_dir()
.unwrap()
.join("../../tool/target/antlr4-4.8-2-SNAPSHOT-complete.jar")
.canonicalize();
match default_path {
Ok(x) if !x.exists() => return,
Err(_) => return,
Ok(x) => x.to_str().unwrap().to_string(),
}
}
};

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);
gen_for_grammar(grammar, &antlr_path, arg).expect("error running antlr tool");
}

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");
println!("cargo:rerun-if-changed={}", antlr_path);
}

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

) -> Result<(), Box<dyn Error>> {
let input = env::current_dir().unwrap().join("grammars");
let file_name = grammar_file_name.to_owned() + ".g4";

let c = Command::new("java")
Command::new("java")
.current_dir(input)
.arg("-cp")
.arg(antlr_path)
Expand All @@ -60,12 +96,8 @@ fn gen_for_grammar(
.arg("../tests/gen")
.arg(&file_name)
.args(additional_arg)
.spawn()
.expect("antlr tool failed to start")
.spawn()?
.wait_with_output()?;
// .unwrap()
// .stdout;
// eprintln!("xx{}",String::from_utf8(x).unwrap());

println!("cargo:rerun-if-changed=grammars/{}", file_name);
Ok(())
Expand Down
Loading