Skip to content

Commit

Permalink
feat: support grammar cross-compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
yvt authored and archseer committed Aug 2, 2022
1 parent f6f054a commit 6d16d2c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
23 changes: 16 additions & 7 deletions helix-loader/src/grammar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,12 @@ pub fn fetch_grammars() -> Result<()> {
run_parallel(grammars, fetch_grammar, "fetch")
}

pub fn build_grammars() -> Result<()> {
run_parallel(get_grammar_configs()?, build_grammar, "build")
pub fn build_grammars(target: Option<String>) -> Result<()> {
run_parallel(
get_grammar_configs()?,
move |grammar| build_grammar(grammar, target.as_deref()),
"build",
)
}

// Returns the set of grammar configurations the user requests.
Expand Down Expand Up @@ -124,13 +128,14 @@ fn get_grammar_configs() -> Result<Vec<GrammarConfiguration>> {

fn run_parallel<F>(grammars: Vec<GrammarConfiguration>, job: F, action: &'static str) -> Result<()>
where
F: Fn(GrammarConfiguration) -> Result<()> + std::marker::Send + 'static + Copy,
F: Fn(GrammarConfiguration) -> Result<()> + std::marker::Send + 'static + Clone,
{
let pool = threadpool::Builder::new().build();
let (tx, rx) = channel();

for grammar in grammars {
let tx = tx.clone();
let job = job.clone();

pool.execute(move || {
// Ignore any SendErrors, if any job in another thread has encountered an
Expand Down Expand Up @@ -240,7 +245,7 @@ where
}
}

fn build_grammar(grammar: GrammarConfiguration) -> Result<()> {
fn build_grammar(grammar: GrammarConfiguration, target: Option<&str>) -> Result<()> {
let grammar_dir = if let GrammarSource::Local { path } = &grammar.source {
PathBuf::from(&path)
} else {
Expand Down Expand Up @@ -273,10 +278,14 @@ fn build_grammar(grammar: GrammarConfiguration) -> Result<()> {
}
.join("src");

build_tree_sitter_library(&path, grammar)
build_tree_sitter_library(&path, grammar, target)
}

fn build_tree_sitter_library(src_path: &Path, grammar: GrammarConfiguration) -> Result<()> {
fn build_tree_sitter_library(
src_path: &Path,
grammar: GrammarConfiguration,
target: Option<&str>,
) -> Result<()> {
let header_path = src_path;
let parser_path = src_path.join("parser.c");
let mut scanner_path = src_path.join("scanner.c");
Expand Down Expand Up @@ -311,7 +320,7 @@ fn build_tree_sitter_library(src_path: &Path, grammar: GrammarConfiguration) ->
.opt_level(3)
.cargo_metadata(false)
.host(BUILD_TARGET)
.target(BUILD_TARGET);
.target(target.unwrap_or(BUILD_TARGET));
let compiler = config.get_compiler();
let mut command = Command::new(compiler.path());
command.current_dir(src_path);
Expand Down
3 changes: 2 additions & 1 deletion helix-term/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ fn main() {

if std::env::var("HELIX_DISABLE_AUTO_GRAMMAR_BUILD").is_err() {
fetch_grammars().expect("Failed to fetch tree-sitter grammars");
build_grammars().expect("Failed to compile tree-sitter grammars");
build_grammars(Some(std::env::var("TARGET").unwrap()))
.expect("Failed to compile tree-sitter grammars");
}

println!("cargo:rerun-if-changed=../runtime/grammars/");
Expand Down
2 changes: 1 addition & 1 deletion helix-term/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ FLAGS:
}

if args.build_grammars {
helix_loader::grammar::build_grammars()?;
helix_loader::grammar::build_grammars(None)?;
return Ok(0);
}

Expand Down

0 comments on commit 6d16d2c

Please sign in to comment.