|
|
@ -1,5 +1,7 @@
|
|
|
|
|
|
|
|
use anyhow::Result;
|
|
|
|
use std::fs;
|
|
|
|
use std::fs;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
|
|
use std::time::SystemTime;
|
|
|
|
|
|
|
|
|
|
|
|
use std::sync::mpsc::channel;
|
|
|
|
use std::sync::mpsc::channel;
|
|
|
|
|
|
|
|
|
|
|
@ -15,66 +17,156 @@ fn collect_tree_sitter_dirs(ignore: &[String]) -> Vec<String> {
|
|
|
|
dirs
|
|
|
|
dirs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn collect_src_files(dir: &str) -> (Vec<String>, Vec<String>) {
|
|
|
|
#[cfg(unix)]
|
|
|
|
eprintln!("Collect files for {}", dir);
|
|
|
|
const DYLIB_EXTENSION: &str = "so";
|
|
|
|
|
|
|
|
|
|
|
|
let mut c_files = Vec::new();
|
|
|
|
#[cfg(windows)]
|
|
|
|
let mut cpp_files = Vec::new();
|
|
|
|
const DYLIB_EXTENSION: &str = "dll";
|
|
|
|
let path = PathBuf::from("languages").join(&dir).join("src");
|
|
|
|
|
|
|
|
for entry in fs::read_dir(path).unwrap().flatten() {
|
|
|
|
// const BUILD_TARGET: &'static str = env!("BUILD_TARGET");
|
|
|
|
let path = entry.path();
|
|
|
|
|
|
|
|
if path
|
|
|
|
use anyhow::{anyhow, Context};
|
|
|
|
.file_stem()
|
|
|
|
use std::{path::Path, process::Command};
|
|
|
|
.unwrap()
|
|
|
|
|
|
|
|
.to_str()
|
|
|
|
fn build_library(src_path: &Path, language: &str) -> Result<()> {
|
|
|
|
.unwrap()
|
|
|
|
let header_path = src_path;
|
|
|
|
.starts_with("binding")
|
|
|
|
// let grammar_path = src_path.join("grammar.json");
|
|
|
|
{
|
|
|
|
let parser_path = src_path.join("parser.c");
|
|
|
|
continue;
|
|
|
|
let mut scanner_path = src_path.join("scanner.c");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let scanner_path = if scanner_path.exists() {
|
|
|
|
|
|
|
|
Some(scanner_path)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
scanner_path.set_extension("cc");
|
|
|
|
|
|
|
|
if scanner_path.exists() {
|
|
|
|
|
|
|
|
Some(scanner_path)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
None
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
let parser_lib_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../runtime/grammars");
|
|
|
|
|
|
|
|
let mut library_path = parser_lib_path.join(language);
|
|
|
|
|
|
|
|
library_path.set_extension(DYLIB_EXTENSION);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let recompile = needs_recompile(&library_path, &parser_path, &scanner_path)
|
|
|
|
|
|
|
|
.with_context(|| "Failed to compare source and binary timestamps")?;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if !recompile {
|
|
|
|
|
|
|
|
return Ok(());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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());
|
|
|
|
|
|
|
|
for (key, value) in compiler.env() {
|
|
|
|
|
|
|
|
command.env(key, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Some(ext) = path.extension() {
|
|
|
|
|
|
|
|
if ext == "c" {
|
|
|
|
if cfg!(windows) {
|
|
|
|
c_files.push(path.to_str().unwrap().to_string());
|
|
|
|
command
|
|
|
|
} else if ext == "cc" || ext == "cpp" || ext == "cxx" {
|
|
|
|
.args(&["/nologo", "/LD", "/I"])
|
|
|
|
cpp_files.push(path.to_str().unwrap().to_string());
|
|
|
|
.arg(header_path)
|
|
|
|
|
|
|
|
.arg("/Od")
|
|
|
|
|
|
|
|
.arg(parser_path);
|
|
|
|
|
|
|
|
if let Some(scanner_path) = scanner_path.as_ref() {
|
|
|
|
|
|
|
|
command.arg(scanner_path);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
command
|
|
|
|
|
|
|
|
.arg("/link")
|
|
|
|
|
|
|
|
.arg(format!("/out:{}", library_path.to_str().unwrap()));
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
command
|
|
|
|
|
|
|
|
.arg("-shared")
|
|
|
|
|
|
|
|
.arg("-fPIC")
|
|
|
|
|
|
|
|
.arg("-fno-exceptions")
|
|
|
|
|
|
|
|
.arg("-g")
|
|
|
|
|
|
|
|
.arg("-I")
|
|
|
|
|
|
|
|
.arg(header_path)
|
|
|
|
|
|
|
|
.arg("-o")
|
|
|
|
|
|
|
|
.arg(&library_path)
|
|
|
|
|
|
|
|
.arg("-O2");
|
|
|
|
|
|
|
|
if let Some(scanner_path) = scanner_path.as_ref() {
|
|
|
|
|
|
|
|
if scanner_path.extension() == Some("c".as_ref()) {
|
|
|
|
|
|
|
|
command.arg("-xc").arg("-std=c99").arg(scanner_path);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
command.arg(scanner_path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
command.arg("-xc").arg(parser_path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(c_files, cpp_files)
|
|
|
|
|
|
|
|
|
|
|
|
let output = command
|
|
|
|
|
|
|
|
.output()
|
|
|
|
|
|
|
|
.with_context(|| "Failed to execute C compiler")?;
|
|
|
|
|
|
|
|
if !output.status.success() {
|
|
|
|
|
|
|
|
return Err(anyhow!(
|
|
|
|
|
|
|
|
"Parser compilation failed.\nStdout: {}\nStderr: {}",
|
|
|
|
|
|
|
|
String::from_utf8_lossy(&output.stdout),
|
|
|
|
|
|
|
|
String::from_utf8_lossy(&output.stderr)
|
|
|
|
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn build_c(files: Vec<String>, language: &str) {
|
|
|
|
Ok(())
|
|
|
|
let mut build = cc::Build::new();
|
|
|
|
}
|
|
|
|
for file in files {
|
|
|
|
fn needs_recompile(
|
|
|
|
build
|
|
|
|
lib_path: &Path,
|
|
|
|
.file(&file)
|
|
|
|
parser_c_path: &Path,
|
|
|
|
.include(PathBuf::from(file).parent().unwrap())
|
|
|
|
scanner_path: &Option<PathBuf>,
|
|
|
|
.pic(true)
|
|
|
|
) -> Result<bool> {
|
|
|
|
.warnings(false);
|
|
|
|
if !lib_path.exists() {
|
|
|
|
|
|
|
|
return Ok(true);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
let lib_mtime = mtime(lib_path)?;
|
|
|
|
|
|
|
|
if mtime(parser_c_path)? > lib_mtime {
|
|
|
|
|
|
|
|
return Ok(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
build.compile(&format!("tree-sitter-{}-c", language));
|
|
|
|
if let Some(scanner_path) = scanner_path {
|
|
|
|
|
|
|
|
if mtime(scanner_path)? > lib_mtime {
|
|
|
|
|
|
|
|
return Ok(true);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn build_cpp(files: Vec<String>, language: &str) {
|
|
|
|
fn mtime(path: &Path) -> Result<SystemTime> {
|
|
|
|
let mut build = cc::Build::new();
|
|
|
|
Ok(fs::metadata(path)?.modified()?)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let flag = if build.get_compiler().is_like_msvc() {
|
|
|
|
// fn build_c(files: Vec<String>, language: &str) {
|
|
|
|
"/std:c++17"
|
|
|
|
// let mut build = cc::Build::new();
|
|
|
|
} else {
|
|
|
|
// for file in files {
|
|
|
|
"-std=c++14"
|
|
|
|
// build
|
|
|
|
};
|
|
|
|
// .file(&file)
|
|
|
|
|
|
|
|
// .include(PathBuf::from(file).parent().unwrap())
|
|
|
|
|
|
|
|
// .pic(true)
|
|
|
|
|
|
|
|
// .warnings(false);
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// build.compile(&format!("tree-sitter-{}-c", language));
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
for file in files {
|
|
|
|
// fn build_cpp(files: Vec<String>, language: &str) {
|
|
|
|
build
|
|
|
|
// let mut build = cc::Build::new();
|
|
|
|
.file(&file)
|
|
|
|
|
|
|
|
.include(PathBuf::from(file).parent().unwrap())
|
|
|
|
// let flag = if build.get_compiler().is_like_msvc() {
|
|
|
|
.pic(true)
|
|
|
|
// "/std:c++17"
|
|
|
|
.warnings(false)
|
|
|
|
// } else {
|
|
|
|
.cpp(true)
|
|
|
|
// "-std=c++14"
|
|
|
|
.flag_if_supported(flag);
|
|
|
|
// };
|
|
|
|
}
|
|
|
|
|
|
|
|
build.compile(&format!("tree-sitter-{}-cpp", language));
|
|
|
|
// 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) {
|
|
|
|
fn build_dir(dir: &str, language: &str) {
|
|
|
|
println!("Build language {}", language);
|
|
|
|
println!("Build language {}", language);
|
|
|
@ -92,13 +184,9 @@ fn build_dir(dir: &str, language: &str) {
|
|
|
|
eprintln!("You can fix in using 'git submodule init && git submodule update --recursive'.");
|
|
|
|
eprintln!("You can fix in using 'git submodule init && git submodule update --recursive'.");
|
|
|
|
std::process::exit(1);
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let (c, cpp) = collect_src_files(dir);
|
|
|
|
|
|
|
|
if !c.is_empty() {
|
|
|
|
let path = Path::new("languages").join(dir).join("src");
|
|
|
|
build_c(c, language);
|
|
|
|
build_library(&path, language).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
if !cpp.is_empty() {
|
|
|
|
|
|
|
|
build_cpp(cpp, language);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
fn main() {
|
|
|
@ -129,6 +217,6 @@ fn main() {
|
|
|
|
// drop(tx);
|
|
|
|
// drop(tx);
|
|
|
|
assert_eq!(rx.try_iter().sum::<usize>(), n_jobs);
|
|
|
|
assert_eq!(rx.try_iter().sum::<usize>(), n_jobs);
|
|
|
|
|
|
|
|
|
|
|
|
build_dir("tree-sitter-typescript/tsx", "tsx");
|
|
|
|
// build_dir("tree-sitter-typescript/tsx", "tsx");
|
|
|
|
build_dir("tree-sitter-typescript/typescript", "typescript");
|
|
|
|
// build_dir("tree-sitter-typescript/typescript", "typescript");
|
|
|
|
}
|
|
|
|
}
|
|
|
|