2021-07-13 16:27:06 +02:00
|
|
|
use anyhow::{anyhow, Context, Result};
|
2021-06-05 02:27:59 +02:00
|
|
|
use std::fs;
|
2021-07-11 12:36:45 +02:00
|
|
|
use std::time::SystemTime;
|
2021-07-13 16:27:06 +02:00
|
|
|
use std::{
|
|
|
|
path::{Path, PathBuf},
|
|
|
|
process::Command,
|
|
|
|
};
|
2020-09-09 07:41:12 +02:00
|
|
|
|
2022-02-13 17:42:18 +01:00
|
|
|
use helix_core::syntax::DYLIB_EXTENSION;
|
2021-02-24 05:13:49 +01:00
|
|
|
|
2022-02-13 17:42:18 +01:00
|
|
|
const BUILD_TARGET: &str = env!("BUILD_TARGET");
|
|
|
|
|
|
|
|
pub fn collect_tree_sitter_dirs(ignore: &[String]) -> Result<Vec<String>> {
|
2020-09-09 07:41:12 +02:00
|
|
|
let mut dirs = Vec::new();
|
2022-02-13 17:42:18 +01:00
|
|
|
let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../helix-syntax/languages");
|
2021-07-13 16:27:06 +02:00
|
|
|
|
|
|
|
for entry in fs::read_dir(path)? {
|
|
|
|
let entry = entry?;
|
2021-03-01 09:37:31 +01:00
|
|
|
let path = entry.path();
|
2021-07-13 16:27:06 +02:00
|
|
|
|
|
|
|
if !entry.file_type()?.is_dir() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-03-01 09:37:31 +01:00
|
|
|
let dir = path.file_name().unwrap().to_str().unwrap().to_string();
|
2021-07-13 16:27:06 +02:00
|
|
|
|
|
|
|
// filter ignores
|
|
|
|
if ignore.contains(&dir) {
|
|
|
|
continue;
|
2020-09-09 07:41:12 +02:00
|
|
|
}
|
2021-07-13 16:27:06 +02:00
|
|
|
dirs.push(dir)
|
2020-09-09 07:41:12 +02:00
|
|
|
}
|
2021-07-13 16:27:06 +02:00
|
|
|
|
|
|
|
Ok(dirs)
|
2020-09-09 07:41:12 +02:00
|
|
|
}
|
|
|
|
|
2021-07-11 12:36:45 +02:00
|
|
|
fn build_library(src_path: &Path, language: &str) -> Result<()> {
|
|
|
|
let header_path = src_path;
|
|
|
|
// let grammar_path = src_path.join("grammar.json");
|
|
|
|
let parser_path = src_path.join("parser.c");
|
|
|
|
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();
|
2022-02-13 17:42:18 +01:00
|
|
|
config
|
|
|
|
.cpp(true)
|
|
|
|
.opt_level(2)
|
|
|
|
.cargo_metadata(false)
|
|
|
|
.host(BUILD_TARGET)
|
|
|
|
.target(BUILD_TARGET);
|
2021-07-11 12:36:45 +02:00
|
|
|
let compiler = config.get_compiler();
|
|
|
|
let mut command = Command::new(compiler.path());
|
2021-07-12 10:48:45 +02:00
|
|
|
command.current_dir(src_path);
|
2021-07-11 12:36:45 +02:00
|
|
|
for (key, value) in compiler.env() {
|
|
|
|
command.env(key, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg!(windows) {
|
|
|
|
command
|
|
|
|
.args(&["/nologo", "/LD", "/I"])
|
|
|
|
.arg(header_path)
|
2021-08-17 13:58:29 +02:00
|
|
|
.arg("/Od")
|
|
|
|
.arg("/utf-8");
|
2021-07-11 12:36:45 +02:00
|
|
|
if let Some(scanner_path) = scanner_path.as_ref() {
|
|
|
|
command.arg(scanner_path);
|
2021-03-01 09:37:31 +01:00
|
|
|
}
|
2021-07-11 18:01:56 +02:00
|
|
|
|
2021-07-11 12:36:45 +02:00
|
|
|
command
|
2021-07-11 18:01:56 +02:00
|
|
|
.arg(parser_path)
|
2021-07-11 12:36:45 +02:00
|
|
|
.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);
|
2020-09-09 07:41:12 +02:00
|
|
|
}
|
|
|
|
}
|
2021-07-11 12:36:45 +02:00
|
|
|
command.arg("-xc").arg(parser_path);
|
2021-08-17 02:52:25 +02:00
|
|
|
if cfg!(all(unix, not(target_os = "macos"))) {
|
|
|
|
command.arg("-Wl,-z,relro,-z,now");
|
|
|
|
}
|
2020-09-09 07:41:12 +02:00
|
|
|
}
|
|
|
|
|
2021-07-11 12:36:45 +02:00
|
|
|
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)
|
|
|
|
));
|
2020-09-09 07:41:12 +02:00
|
|
|
}
|
2021-07-11 12:36:45 +02:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
fn needs_recompile(
|
|
|
|
lib_path: &Path,
|
|
|
|
parser_c_path: &Path,
|
|
|
|
scanner_path: &Option<PathBuf>,
|
|
|
|
) -> Result<bool> {
|
|
|
|
if !lib_path.exists() {
|
|
|
|
return Ok(true);
|
|
|
|
}
|
|
|
|
let lib_mtime = mtime(lib_path)?;
|
|
|
|
if mtime(parser_c_path)? > lib_mtime {
|
|
|
|
return Ok(true);
|
|
|
|
}
|
|
|
|
if let Some(scanner_path) = scanner_path {
|
|
|
|
if mtime(scanner_path)? > lib_mtime {
|
|
|
|
return Ok(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(false)
|
2020-09-09 07:41:12 +02:00
|
|
|
}
|
|
|
|
|
2021-07-11 12:36:45 +02:00
|
|
|
fn mtime(path: &Path) -> Result<SystemTime> {
|
|
|
|
Ok(fs::metadata(path)?.modified()?)
|
|
|
|
}
|
2021-06-05 16:57:30 +02:00
|
|
|
|
2022-02-13 17:42:18 +01:00
|
|
|
pub fn build_dir(dir: &str, language: &str) {
|
2020-09-09 07:41:12 +02:00
|
|
|
println!("Build language {}", language);
|
2022-02-13 17:42:18 +01:00
|
|
|
if PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
|
|
|
.join("../helix-syntax/languages")
|
2020-09-09 07:41:12 +02:00
|
|
|
.join(dir)
|
|
|
|
.read_dir()
|
|
|
|
.unwrap()
|
|
|
|
.next()
|
|
|
|
.is_none()
|
|
|
|
{
|
|
|
|
eprintln!(
|
2022-02-13 17:42:18 +01:00
|
|
|
"The directory {} is empty, you probably need to use './scripts/grammars sync'?",
|
2020-09-09 07:41:12 +02:00
|
|
|
dir
|
|
|
|
);
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
2021-07-11 12:36:45 +02:00
|
|
|
|
2021-07-11 17:44:14 +02:00
|
|
|
let path = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
2022-02-13 17:42:18 +01:00
|
|
|
.join("../helix-syntax/languages")
|
2021-07-11 17:44:14 +02:00
|
|
|
.join(dir)
|
|
|
|
.join("src");
|
2021-07-13 16:27:06 +02:00
|
|
|
|
2021-07-11 12:36:45 +02:00
|
|
|
build_library(&path, language).unwrap();
|
2020-09-09 07:41:12 +02:00
|
|
|
}
|