on master help sign in
feat: support grammar cross-compilation
commit6d16d2cbc910f2e3e4131fae41a8cd67c287e6a5 authoryvt <i@yvt.jp> dateWed 20 Jul 2022, 23:56 (4 years ago) parentf6f054a
3 files changed+19−9
helix-loader/src/grammar.rs+16−7 helix-term/build.rs+2−1 helix-term/src/main.rs+1−1
helix-loader/src/grammar.rs+16−7
@@ -92,8 +92,12 @@ pub fn fetch_grammars() -> Result<()> {
9292 run_parallel(grammars, fetch_grammar, "fetch")
9393}
9494
95−pub fn build_grammars() -> Result<()> {
96− run_parallel(get_grammar_configs()?, build_grammar, "build")
95+pub fn build_grammars(target: Option<String>) -> Result<()> {
96+ run_parallel(
97+ get_grammar_configs()?,
98+ move |grammar| build_grammar(grammar, target.as_deref()),
99+ "build",
100+ )
97101}
98102
99103// Returns the set of grammar configurations the user requests.
@@ -124,13 +128,14 @@ fn get_grammar_configs() -> Result<Vec<GrammarConfiguration>> {
124128
125129fn run_parallel<F>(grammars: Vec<GrammarConfiguration>, job: F, action: &'static str) -> Result<()>
126130where
127− F: Fn(GrammarConfiguration) -> Result<()> + std::marker::Send + 'static + Copy,
131+ F: Fn(GrammarConfiguration) -> Result<()> + std::marker::Send + 'static + Clone,
128132{
129133 let pool = threadpool::Builder::new().build();
130134 let (tx, rx) = channel();
131135
132136 for grammar in grammars {
133137 let tx = tx.clone();
138+ let job = job.clone();
134139
135140 pool.execute(move || {
136141 // Ignore any SendErrors, if any job in another thread has encountered an
@@ -240,7 +245,7 @@ where
240245 }
241246}
242247
243−fn build_grammar(grammar: GrammarConfiguration) -> Result<()> {
248+fn build_grammar(grammar: GrammarConfiguration, target: Option<&str>) -> Result<()> {
244249 let grammar_dir = if let GrammarSource::Local { path } = &grammar.source {
245250 PathBuf::from(&path)
246251 } else {
@@ -273,10 +278,14 @@ fn build_grammar(grammar: GrammarConfiguration) -> Result<()> {
273278 }
274279 .join("src");
275280
276− build_tree_sitter_library(&path, grammar)
281+ build_tree_sitter_library(&path, grammar, target)
277282}
278283
279−fn build_tree_sitter_library(src_path: &Path, grammar: GrammarConfiguration) -> Result<()> {
284+fn build_tree_sitter_library(
285+ src_path: &Path,
286+ grammar: GrammarConfiguration,
287+ target: Option<&str>,
288+) -> Result<()> {
280289 let header_path = src_path;
281290 let parser_path = src_path.join("parser.c");
282291 let mut scanner_path = src_path.join("scanner.c");
@@ -311,7 +320,7 @@ fn build_tree_sitter_library(src_path: &Path, grammar: GrammarConfiguration) ->
311320 .opt_level(3)
312321 .cargo_metadata(false)
313322 .host(BUILD_TARGET)
314− .target(BUILD_TARGET);
323+ .target(target.unwrap_or(BUILD_TARGET));
315324 let compiler = config.get_compiler();
316325 let mut command = Command::new(compiler.path());
317326 command.current_dir(src_path);
helix-term/build.rs+2−1
@@ -19,7 +19,8 @@ fn main() {
1919
2020 if std::env::var("HELIX_DISABLE_AUTO_GRAMMAR_BUILD").is_err() {
2121 fetch_grammars().expect("Failed to fetch tree-sitter grammars");
22− build_grammars().expect("Failed to compile tree-sitter grammars");
22+ build_grammars(Some(std::env::var("TARGET").unwrap()))
23+ .expect("Failed to compile tree-sitter grammars");
2324 }
2425
2526 println!("cargo:rerun-if-changed=../runtime/grammars/");
helix-term/src/main.rs+1−1
@@ -108,7 +108,7 @@ FLAGS:
108108 }
109109
110110 if args.build_grammars {
111− helix_loader::grammar::build_grammars()?;
111+ helix_loader::grammar::build_grammars(None)?;
112112 return Ok(0);
113113 }
114114