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