on master help sign in
feat: support grammar cross-compilation
6d16d2cyvt
helix-loader/src/grammar.rs+16−7
@@ -92,8 +92,12 @@ pub fn fetch_grammars() -> Result<()> {
92 run_parallel(grammars, fetch_grammar, "fetch")
92 run_parallel(grammars, fetch_grammar, "fetch")
93}
93}
94
94
95−pub fn build_grammars() -> Result<()> {
95+pub fn build_grammars(target: Option<String>) -> Result<()> {
96+ run_parallel(
96− run_parallel(get_grammar_configs()?, build_grammar, "build")
97+ get_grammar_configs()?,
98+ move |grammar| build_grammar(grammar, target.as_deref()),
99+ "build",
100+ )
97}
101}
98
102
99// Returns the set of grammar configurations the user requests.
103// Returns the set of grammar configurations the user requests.
@@ -124,13 +128,14 @@ fn get_grammar_configs() -> Result<Vec<GrammarConfiguration>> {
124
128
125fn run_parallel<F>(grammars: Vec<GrammarConfiguration>, job: F, action: &'static str) -> Result<()>
129fn run_parallel<F>(grammars: Vec<GrammarConfiguration>, job: F, action: &'static str) -> Result<()>
126where
130where
127− F: Fn(GrammarConfiguration) -> Result<()> + std::marker::Send + 'static + Copy,
131+ F: Fn(GrammarConfiguration) -> Result<()> + std::marker::Send + 'static + Clone,
128{
132{
129 let pool = threadpool::Builder::new().build();
133 let pool = threadpool::Builder::new().build();
130 let (tx, rx) = channel();
134 let (tx, rx) = channel();
131
135
132 for grammar in grammars {
136 for grammar in grammars {
133 let tx = tx.clone();
137 let tx = tx.clone();
138+ let job = job.clone();
134
139
135 pool.execute(move || {
140 pool.execute(move || {
136 // Ignore any SendErrors, if any job in another thread has encountered an
141 // Ignore any SendErrors, if any job in another thread has encountered an
@@ -240,7 +245,7 @@ where
240 }
245 }
241}
246}
242
247
243−fn build_grammar(grammar: GrammarConfiguration) -> Result<()> {
248+fn build_grammar(grammar: GrammarConfiguration, target: Option<&str>) -> Result<()> {
244 let grammar_dir = if let GrammarSource::Local { path } = &grammar.source {
249 let grammar_dir = if let GrammarSource::Local { path } = &grammar.source {
245 PathBuf::from(&path)
250 PathBuf::from(&path)
246 } else {
251 } else {
@@ -273,10 +278,14 @@ fn build_grammar(grammar: GrammarConfiguration) -> Result<()> {
273 }
278 }
274 .join("src");
279 .join("src");
275
280
276− build_tree_sitter_library(&path, grammar)
281+ build_tree_sitter_library(&path, grammar, target)
277}
282}
278
283
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<()> {
280 let header_path = src_path;
289 let header_path = src_path;
281 let parser_path = src_path.join("parser.c");
290 let parser_path = src_path.join("parser.c");
282 let mut scanner_path = src_path.join("scanner.c");
291 let mut scanner_path = src_path.join("scanner.c");
@@ -311,7 +320,7 @@ fn build_tree_sitter_library(src_path: &Path, grammar: GrammarConfiguration) ->
311 .opt_level(3)
320 .opt_level(3)
312 .cargo_metadata(false)
321 .cargo_metadata(false)
313 .host(BUILD_TARGET)
322 .host(BUILD_TARGET)
314− .target(BUILD_TARGET);
323+ .target(target.unwrap_or(BUILD_TARGET));
315 let compiler = config.get_compiler();
324 let compiler = config.get_compiler();
316 let mut command = Command::new(compiler.path());
325 let mut command = Command::new(compiler.path());
317 command.current_dir(src_path);
326 command.current_dir(src_path);
helix-term/build.rs+2−1
@@ -19,7 +19,8 @@ fn main() {
19
19
20 if std::env::var("HELIX_DISABLE_AUTO_GRAMMAR_BUILD").is_err() {
20 if std::env::var("HELIX_DISABLE_AUTO_GRAMMAR_BUILD").is_err() {
21 fetch_grammars().expect("Failed to fetch tree-sitter grammars");
21 fetch_grammars().expect("Failed to fetch tree-sitter grammars");
22+ build_grammars(Some(std::env::var("TARGET").unwrap()))
22− build_grammars().expect("Failed to compile tree-sitter grammars");
23+ .expect("Failed to compile tree-sitter grammars");
23 }
24 }
24
25
25 println!("cargo:rerun-if-changed=../runtime/grammars/");
26 println!("cargo:rerun-if-changed=../runtime/grammars/");
helix-term/src/main.rs+1−1
@@ -108,7 +108,7 @@ FLAGS:
108 }
108 }
109
109
110 if args.build_grammars {
110 if args.build_grammars {
111− helix_loader::grammar::build_grammars()?;
111+ helix_loader::grammar::build_grammars(None)?;
112 return Ok(0);
112 return Ok(0);
113 }
113 }
114
114