Skip to content

Commit

Permalink
Build ts/tsx again, refactor collect_tree_sitter_dirs
Browse files Browse the repository at this point in the history
  • Loading branch information
archseer committed Jul 13, 2021
1 parent 65d591d commit 605ce98
Showing 1 changed file with 26 additions and 52 deletions.
78 changes: 26 additions & 52 deletions helix-syntax/build.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
use anyhow::Result;
use anyhow::{anyhow, Context, Result};
use std::fs;
use std::path::PathBuf;
use std::time::SystemTime;
use std::{
path::{Path, PathBuf},
process::Command,
};

use std::sync::mpsc::channel;

fn collect_tree_sitter_dirs(ignore: &[String]) -> Vec<String> {
fn collect_tree_sitter_dirs(ignore: &[String]) -> Result<Vec<String>> {
let mut dirs = Vec::new();
let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("languages");
for entry in fs::read_dir(path).unwrap().flatten() {

for entry in fs::read_dir(path)? {
let entry = entry?;
let path = entry.path();

if entry.file_type()?.is_dir() {
continue;
}

let dir = path.file_name().unwrap().to_str().unwrap().to_string();
if !ignore.contains(&dir) {
dirs.push(dir);

// filter ignores
if ignore.contains(&dir) {
continue;
}
dirs.push(dir)
}
dirs

Ok(dirs)
}

#[cfg(unix)]
Expand All @@ -24,11 +38,6 @@ const DYLIB_EXTENSION: &str = "so";
#[cfg(windows)]
const DYLIB_EXTENSION: &str = "dll";

// const BUILD_TARGET: &'static str = env!("BUILD_TARGET");

use anyhow::{anyhow, Context};
use std::{path::Path, process::Command};

fn build_library(src_path: &Path, language: &str) -> Result<()> {
let header_path = src_path;
// let grammar_path = src_path.join("grammar.json");
Expand Down Expand Up @@ -57,8 +66,6 @@ fn build_library(src_path: &Path, language: &str) -> Result<()> {
}
let mut config = cc::Build::new();
config.cpp(true).opt_level(2).cargo_metadata(false);
// .target(BUILD_TARGET)
// .host(BUILD_TARGET);
let compiler = config.get_compiler();
let mut command = Command::new(compiler.path());
command.current_dir(src_path);
Expand Down Expand Up @@ -137,39 +144,6 @@ fn mtime(path: &Path) -> Result<SystemTime> {
Ok(fs::metadata(path)?.modified()?)
}

// fn build_c(files: Vec<String>, language: &str) {
// let mut build = cc::Build::new();
// for file in files {
// build
// .file(&file)
// .include(PathBuf::from(file).parent().unwrap())
// .pic(true)
// .warnings(false);
// }
// build.compile(&format!("tree-sitter-{}-c", language));
// }

// fn build_cpp(files: Vec<String>, language: &str) {
// let mut build = cc::Build::new();

// let flag = if build.get_compiler().is_like_msvc() {
// "/std:c++17"
// } else {
// "-std=c++14"
// };

// for file in files {
// build
// .file(&file)
// .include(PathBuf::from(file).parent().unwrap())
// .pic(true)
// .warnings(false)
// .cpp(true)
// .flag_if_supported(flag);
// }
// build.compile(&format!("tree-sitter-{}-cpp", language));
// }

fn build_dir(dir: &str, language: &str) {
println!("Build language {}", language);
if PathBuf::from("languages")
Expand All @@ -191,16 +165,16 @@ fn build_dir(dir: &str, language: &str) {
.join("languages")
.join(dir)
.join("src");

build_library(&path, language).unwrap();
}

fn main() {
let ignore = vec![
"tree-sitter-typescript".to_string(),
"tree-sitter-haskell".to_string(), // aarch64 failures: https://github.com/tree-sitter/tree-sitter-haskell/issues/34
".DS_Store".to_string(),
];
let dirs = collect_tree_sitter_dirs(&ignore);
let dirs = collect_tree_sitter_dirs(&ignore).unwrap();

let mut n_jobs = 0;
let pool = threadpool::Builder::new().build(); // by going through the builder, it'll use num_cpus
Expand All @@ -211,7 +185,7 @@ fn main() {
n_jobs += 1;

pool.execute(move || {
let language = &dir[12..]; // skip tree-sitter- prefix
let language = &dir.strip_prefix("tree-sitter-").unwrap();
build_dir(&dir, language);

// report progress
Expand All @@ -222,6 +196,6 @@ fn main() {
// drop(tx);
assert_eq!(rx.try_iter().sum::<usize>(), n_jobs);

// build_dir("tree-sitter-typescript/tsx", "tsx");
// build_dir("tree-sitter-typescript/typescript", "typescript");
build_dir("tree-sitter-typescript/tsx", "tsx");
build_dir("tree-sitter-typescript/typescript", "typescript");
}

0 comments on commit 605ce98

Please sign in to comment.