@@ -1,21 +1,35 @@
1−use anyhow::Result;
1+use anyhow::{anyhow, Context, Result};
2use std::fs;
2use std::fs;
3−use std::path::PathBuf;
4use std::time::SystemTime;
3use std::time::SystemTime;
5+ path::{Path, PathBuf},
6use std::sync::mpsc::channel;
9use std::sync::mpsc::channel;
8−fn collect_tree_sitter_dirs(ignore: &[String]) -> Vec<String> {
11+fn collect_tree_sitter_dirs(ignore: &[String]) -> Result<Vec<String>> {
9 let mut dirs = Vec::new();
12 let mut dirs = Vec::new();
10 let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("languages");
13 let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("languages");
11− for entry in fs::read_dir(path).unwrap().flatten() {
15+ for entry in fs::read_dir(path)? {
12 let path = entry.path();
17 let path = entry.path();
19+ if !entry.file_type()?.is_dir() {
13 let dir = path.file_name().unwrap().to_str().unwrap().to_string();
23 let dir = path.file_name().unwrap().to_str().unwrap().to_string();
14− if !ignore.contains(&dir) {
26+ if ignore.contains(&dir) {
15− dirs.push(dir);
27+ continue;
21#[cfg(unix)]
35#[cfg(unix)]
@@ -24,11 +38,6 @@ const DYLIB_EXTENSION: &str = "so";
24#[cfg(windows)]
38#[cfg(windows)]
25const DYLIB_EXTENSION: &str = "dll";
39const DYLIB_EXTENSION: &str = "dll";
27−// const BUILD_TARGET: &'static str = env!("BUILD_TARGET");
29−use anyhow::{anyhow, Context};
30−use std::{path::Path, process::Command};
32fn build_library(src_path: &Path, language: &str) -> Result<()> {
41fn build_library(src_path: &Path, language: &str) -> Result<()> {
33 let header_path = src_path;
42 let header_path = src_path;
34 // let grammar_path = src_path.join("grammar.json");
43 // let grammar_path = src_path.join("grammar.json");
@@ -57,8 +66,6 @@ fn build_library(src_path: &Path, language: &str) -> Result<()> {
58 let mut config = cc::Build::new();
67 let mut config = cc::Build::new();
59 config.cpp(true).opt_level(2).cargo_metadata(false);
68 config.cpp(true).opt_level(2).cargo_metadata(false);
60− // .target(BUILD_TARGET)
61− // .host(BUILD_TARGET);
62 let compiler = config.get_compiler();
69 let compiler = config.get_compiler();
63 let mut command = Command::new(compiler.path());
70 let mut command = Command::new(compiler.path());
64 command.current_dir(src_path);
71 command.current_dir(src_path);
@@ -137,39 +144,6 @@ fn mtime(path: &Path) -> Result<SystemTime> {
137 Ok(fs::metadata(path)?.modified()?)
144 Ok(fs::metadata(path)?.modified()?)
140−// fn build_c(files: Vec<String>, language: &str) {
141−// let mut build = cc::Build::new();
142−// for file in files {
145−// .include(PathBuf::from(file).parent().unwrap())
149−// build.compile(&format!("tree-sitter-{}-c", language));
152−// fn build_cpp(files: Vec<String>, language: &str) {
153−// let mut build = cc::Build::new();
155−// let flag = if build.get_compiler().is_like_msvc() {
161−// for file in files {
164−// .include(PathBuf::from(file).parent().unwrap())
168−// .flag_if_supported(flag);
170−// build.compile(&format!("tree-sitter-{}-cpp", language));
173fn build_dir(dir: &str, language: &str) {
147fn build_dir(dir: &str, language: &str) {
174 println!("Build language {}", language);
148 println!("Build language {}", language);
175 if PathBuf::from("languages")
149 if PathBuf::from("languages")
@@ -191,6 +165,7 @@ fn build_dir(dir: &str, language: &str) {
191 .join("languages")
165 .join("languages")
192 .join(dir)
166 .join(dir)
193 .join("src");
167 .join("src");
194 build_library(&path, language).unwrap();
169 build_library(&path, language).unwrap();
@@ -198,9 +173,8 @@ fn main() {
198 let ignore = vec![
173 let ignore = vec![
199 "tree-sitter-typescript".to_string(),
174 "tree-sitter-typescript".to_string(),
200 "tree-sitter-haskell".to_string(), // aarch64 failures: https://github.com/tree-sitter/tree-sitter-haskell/issues/34
175 "tree-sitter-haskell".to_string(), // aarch64 failures: https://github.com/tree-sitter/tree-sitter-haskell/issues/34
201− ".DS_Store".to_string(),
203− let dirs = collect_tree_sitter_dirs(&ignore);
177+ let dirs = collect_tree_sitter_dirs(&ignore).unwrap();
205 let mut n_jobs = 0;
179 let mut n_jobs = 0;
206 let pool = threadpool::Builder::new().build(); // by going through the builder, it'll use num_cpus
180 let pool = threadpool::Builder::new().build(); // by going through the builder, it'll use num_cpus
@@ -211,7 +185,7 @@ fn main() {
211 n_jobs += 1;
185 n_jobs += 1;
213 pool.execute(move || {
187 pool.execute(move || {
214− let language = &dir[12..]; // skip tree-sitter- prefix
188+ let language = &dir.strip_prefix("tree-sitter-").unwrap();
215 build_dir(&dir, language);
189 build_dir(&dir, language);
217 // report progress
191 // report progress
@@ -222,6 +196,6 @@ fn main() {
222 // drop(tx);
196 // drop(tx);
223 assert_eq!(rx.try_iter().sum::<usize>(), n_jobs);
197 assert_eq!(rx.try_iter().sum::<usize>(), n_jobs);
225− // build_dir("tree-sitter-typescript/tsx", "tsx");
199+ build_dir("tree-sitter-typescript/tsx", "tsx");
226− // build_dir("tree-sitter-typescript/typescript", "typescript");
200+ build_dir("tree-sitter-typescript/typescript", "typescript");