Skip to content

Commit

Permalink
feat: use lockfiles to dedup recompilation
Browse files Browse the repository at this point in the history
  • Loading branch information
amaanq committed Feb 12, 2024
1 parent e6d67ec commit 8abb33f
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 25 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,17 @@ anstyle = "1.0.6"
anyhow = "1.0.79"
cc = "1.0.83"
clap = { version = "4.4.18", features = [
"cargo",
"derive",
"env",
"help",
"unstable-styles",
"cargo",
"derive",
"env",
"help",
"unstable-styles",
] }
ctor = "0.2.6"
ctrlc = { version = "3.4.2", features = ["termination"] }
difference = "2.0.0"
dirs = "5.0.1"
fs4 = "0.7.0"
glob = "0.3.1"
html-escape = "0.2.13"
indexmap = "2.2.2"
Expand Down
1 change: 1 addition & 0 deletions cli/loader/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ wasm = ["tree-sitter/wasm"]
anyhow.workspace = true
cc.workspace = true
dirs.workspace = true
fs4.workspace = true
libloading.workspace = true
once_cell.workspace = true
regex.workspace = true
Expand Down
79 changes: 59 additions & 20 deletions cli/loader/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![doc = include_str!("../README.md")]

use anyhow::{anyhow, Context, Error, Result};
use fs4::FileExt;
use libloading::{Library, Symbol};
use once_cell::unsync::OnceCell;
use regex::{Regex, RegexBuilder};
Expand Down Expand Up @@ -371,7 +372,7 @@ impl Loader {
library_path.set_extension("wasm");
}

let recompile = needs_recompile(&library_path, &parser_path, scanner_path.as_deref())
let mut recompile = needs_recompile(&library_path, &parser_path, scanner_path.as_deref())
.with_context(|| "Failed to compare source and binary timestamps")?;

#[cfg(feature = "wasm")]
Expand All @@ -392,27 +393,58 @@ impl Loader {
return Ok(wasm_store.load_language(name, &wasm_bytes)?);
}

{
if recompile {
self.compile_parser_to_dylib(
header_paths,
&parser_path,
&scanner_path,
&library_path,
)?;
}
let lock_path = if env::var("CROSS_RUNNER").is_ok() {
PathBuf::from("/tmp")
.join("tree-sitter")
.join("lock")
.join(format!("{name}.lock"))
} else {
dirs::cache_dir()
.ok_or(anyhow!("Cannot determine cache directory"))?
.join("tree-sitter")
.join("lock")
.join(format!("{name}.lock"))
};

let library = unsafe { Library::new(&library_path) }
.with_context(|| format!("Error opening dynamic library {library_path:?}"))?;
let language = unsafe {
let language_fn: Symbol<unsafe extern "C" fn() -> Language> = library
.get(language_fn_name.as_bytes())
.with_context(|| format!("Failed to load symbol {language_fn_name}"))?;
language_fn()
};
mem::forget(library);
Ok(language)
if let Ok(lock_file) = fs::OpenOptions::new().write(true).open(&lock_path) {
recompile = false;
lock_file.lock_exclusive()?;
}

if recompile {
fs::create_dir_all(lock_path.parent().unwrap()).with_context(|| {
format!(
"Failed to create directory {:?}",
lock_path.parent().unwrap()
)
})?;
let lock_file = fs::OpenOptions::new()
.create(true)
.truncate(true)
.write(true)
.open(&lock_path)?;
lock_file.lock_exclusive()?;

self.compile_parser_to_dylib(
header_paths,
&parser_path,
&scanner_path,
&library_path,
&lock_file,
&lock_path,
)?;
}

let library = unsafe { Library::new(&library_path) }
.with_context(|| format!("Error opening dynamic library {library_path:?}"))?;
let language = unsafe {
let language_fn: Symbol<unsafe extern "C" fn() -> Language> = library
.get(language_fn_name.as_bytes())
.with_context(|| format!("Failed to load symbol {language_fn_name}"))?;
language_fn()
};
mem::forget(library);
Ok(language)
}

fn compile_parser_to_dylib(
Expand All @@ -421,6 +453,8 @@ impl Loader {
parser_path: &Path,
scanner_path: &Option<PathBuf>,
library_path: &PathBuf,
lock_file: &fs::File,
lock_path: &Path,
) -> Result<(), Error> {
let mut config = cc::Build::new();
config
Expand Down Expand Up @@ -494,6 +528,10 @@ impl Loader {
let output = command
.output()
.with_context(|| "Failed to execute C compiler")?;

lock_file.unlock()?;
fs::remove_file(lock_path)?;

if !output.status.success() {
return Err(anyhow!(
"Parser compilation failed.\nStdout: {}\nStderr: {}",
Expand Down Expand Up @@ -687,6 +725,7 @@ impl Loader {

fs::rename(src_path.join(output_name), output_path)
.context("failed to rename wasm output file")?;

Ok(())
}

Expand Down

0 comments on commit 8abb33f

Please sign in to comment.