@@ -1,21 +1,35 @@
1−use anyhow::Result;
1+use anyhow::{anyhow, Context, Result};
22use std::fs;
3−use std::path::PathBuf;
43use std::time::SystemTime;
4+use std::{
5+ path::{Path, PathBuf},
6+ process::Command,
7+};
58
69use std::sync::mpsc::channel;
710
8−fn collect_tree_sitter_dirs(ignore: &[String]) -> Vec<String> {
11+fn collect_tree_sitter_dirs(ignore: &[String]) -> Result<Vec<String>> {
912 let mut dirs = Vec::new();
1013 let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("languages");
11− for entry in fs::read_dir(path).unwrap().flatten() {
14+
15+ for entry in fs::read_dir(path)? {
16+ let entry = entry?;
1217 let path = entry.path();
18+
19+ if !entry.file_type()?.is_dir() {
20+ continue;
21+ }
22+
1323 let dir = path.file_name().unwrap().to_str().unwrap().to_string();
14− if !ignore.contains(&dir) {
15− dirs.push(dir);
24+
25+ // filter ignores
26+ if ignore.contains(&dir) {
27+ continue;
1628 }
29+ dirs.push(dir)
1730 }
18− dirs
31+
32+ Ok(dirs)
1933}
2034
2135#[cfg(unix)]
@@ -24,11 +38,6 @@ const DYLIB_EXTENSION: &str = "so";
2438#[cfg(windows)]
2539const DYLIB_EXTENSION: &str = "dll";
2640
27−// const BUILD_TARGET: &'static str = env!("BUILD_TARGET");
28−
29−use anyhow::{anyhow, Context};
30−use std::{path::Path, process::Command};
31−
3241fn build_library(src_path: &Path, language: &str) -> Result<()> {
3342 let header_path = src_path;
3443 // let grammar_path = src_path.join("grammar.json");
@@ -57,8 +66,6 @@ fn build_library(src_path: &Path, language: &str) -> Result<()> {
5766 }
5867 let mut config = cc::Build::new();
5968 config.cpp(true).opt_level(2).cargo_metadata(false);
60− // .target(BUILD_TARGET)
61− // .host(BUILD_TARGET);
6269 let compiler = config.get_compiler();
6370 let mut command = Command::new(compiler.path());
6471 command.current_dir(src_path);
@@ -137,39 +144,6 @@ fn mtime(path: &Path) -> Result<SystemTime> {
137144 Ok(fs::metadata(path)?.modified()?)
138145}
139146
140−// fn build_c(files: Vec<String>, language: &str) {
141−// let mut build = cc::Build::new();
142−// for file in files {
143−// build
144−// .file(&file)
145−// .include(PathBuf::from(file).parent().unwrap())
146−// .pic(true)
147−// .warnings(false);
148−// }
149−// build.compile(&format!("tree-sitter-{}-c", language));
150−// }
151−
152−// fn build_cpp(files: Vec<String>, language: &str) {
153−// let mut build = cc::Build::new();
154−
155−// let flag = if build.get_compiler().is_like_msvc() {
156−// "/std:c++17"
157−// } else {
158−// "-std=c++14"
159−// };
160−
161−// for file in files {
162−// build
163−// .file(&file)
164−// .include(PathBuf::from(file).parent().unwrap())
165−// .pic(true)
166−// .warnings(false)
167−// .cpp(true)
168−// .flag_if_supported(flag);
169−// }
170−// build.compile(&format!("tree-sitter-{}-cpp", language));
171−// }
172−
173147fn build_dir(dir: &str, language: &str) {
174148 println!("Build language {}", language);
175149 if PathBuf::from("languages")
@@ -191,6 +165,7 @@ fn build_dir(dir: &str, language: &str) {
191165 .join("languages")
192166 .join(dir)
193167 .join("src");
168+
194169 build_library(&path, language).unwrap();
195170}
196171
@@ -198,9 +173,8 @@ fn main() {
198173 let ignore = vec![
199174 "tree-sitter-typescript".to_string(),
200175 "tree-sitter-haskell".to_string(), // aarch64 failures: https://github.com/tree-sitter/tree-sitter-haskell/issues/34
201− ".DS_Store".to_string(),
202176 ];
203− let dirs = collect_tree_sitter_dirs(&ignore);
177+ let dirs = collect_tree_sitter_dirs(&ignore).unwrap();
204178
205179 let mut n_jobs = 0;
206180 let pool = threadpool::Builder::new().build(); // by going through the builder, it'll use num_cpus
@@ -211,7 +185,7 @@ fn main() {
211185 n_jobs += 1;
212186
213187 pool.execute(move || {
214− let language = &dir[12..]; // skip tree-sitter- prefix
188+ let language = &dir.strip_prefix("tree-sitter-").unwrap();
215189 build_dir(&dir, language);
216190
217191 // report progress
@@ -222,6 +196,6 @@ fn main() {
222196 // drop(tx);
223197 assert_eq!(rx.try_iter().sum::<usize>(), n_jobs);
224198
225− // build_dir("tree-sitter-typescript/tsx", "tsx");
226− // build_dir("tree-sitter-typescript/typescript", "typescript");
199+ build_dir("tree-sitter-typescript/tsx", "tsx");
200+ build_dir("tree-sitter-typescript/typescript", "typescript");
227201}