on master help sign in
Build ts/tsx again, refactor collect_tree_sitter_dirs
a4b077eBlaž Hrastnik
helix-syntax/build.rs+26−52
@@ -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;
4+use std::{
5+ path::{Path, PathBuf},
6+ process::Command,
7+};
5
8
6use std::sync::mpsc::channel;
9use std::sync::mpsc::channel;
7
10
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");
14+
11− for entry in fs::read_dir(path).unwrap().flatten() {
15+ for entry in fs::read_dir(path)? {
16+ let entry = entry?;
12 let path = entry.path();
17 let path = entry.path();
18+
19+ if !entry.file_type()?.is_dir() {
20+ continue;
21+ }
22+
13 let dir = path.file_name().unwrap().to_str().unwrap().to_string();
23 let dir = path.file_name().unwrap().to_str().unwrap().to_string();
24+
25+ // filter ignores
14− if !ignore.contains(&dir) {
26+ if ignore.contains(&dir) {
15− dirs.push(dir);
27+ continue;
16 }
28 }
29+ dirs.push(dir)
17 }
30 }
31+
18− dirs
32+ Ok(dirs)
19}
33}
20
34
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";
26
40
27−// const BUILD_TARGET: &'static str = env!("BUILD_TARGET");
28−
29−use anyhow::{anyhow, Context};
30−use std::{path::Path, process::Command};
31−
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<()> {
57 }
66 }
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()?)
138}
145}
139
146
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−
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");
168+
194 build_library(&path, language).unwrap();
169 build_library(&path, language).unwrap();
195}
170}
196
171
@@ -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(),
202 ];
176 ];
203− let dirs = collect_tree_sitter_dirs(&ignore);
177+ let dirs = collect_tree_sitter_dirs(&ignore).unwrap();
204
178
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;
212
186
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);
216
190
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);
224
198
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");
227}
201}