on master help sign in
emit cargo metadata duiring build scripts to avoid outdated buildscript outputs (#6743)

* rebuild on revision change * rerun grammar build if grammars change

commit896404c7ead29c4c19564581ccb53b913440a583 authorPascal Kuthe <pascal.kuthe@semimod.de> dateFri 14 Apr 2023, 16:59 (4 years ago) parent78e8066
2 files changed+50−1
helix-loader/build.rs+38−1 helix-loader/src/grammar.rs+12
helix-loader/build.rs+38−1
@@ -1,4 +1,5 @@
11use std::borrow::Cow;
2+use std::path::Path;
23use std::process::Command;
34
45const VERSION: &str = include_str!("../VERSION");
@@ -11,7 +12,7 @@ fn main() {
1112 .filter(|output| output.status.success())
1213 .and_then(|x| String::from_utf8(x.stdout).ok());
1314
14− let version: Cow<_> = match git_hash {
15+ let version: Cow<_> = match &git_hash {
1516 Some(git_hash) => format!("{} ({})", VERSION, &git_hash[..8]).into(),
1617 None => VERSION.into(),
1718 };
@@ -23,4 +24,40 @@ fn main() {
2324
2425 println!("cargo:rerun-if-changed=../VERSION");
2526 println!("cargo:rustc-env=VERSION_AND_GIT_HASH={}", version);
27+
28+ if git_hash.is_none() {
29+ return;
30+ }
31+
32+ // we need to revparse because the git dir could be anywhere if you are
33+ // using detached worktrees but there is no good way to obtain an OsString
34+ // from command output so for now we can't accept non-utf8 paths here
35+ // probably rare enouch where it doesn't matter tough we could use gitoxide
36+ // here but that would be make it a hard dependency and slow compile times
37+ let Some(git_dir): Option<String> = Command::new("git")
38+ .args(["rev-parse", "--git-dir"])
39+ .output()
40+ .ok()
41+ .filter(|output| output.status.success())
42+ .and_then(|x| String::from_utf8(x.stdout).ok())
43+ else{ return; };
44+ // If heads starts pointing at something else (different branch)
45+ // we need to return
46+ let head = Path::new(&git_dir).join("HEAD");
47+ if head.exists() {
48+ println!("cargo:rerun-if-changed={}", head.display());
49+ }
50+ // if the thing head points to (branch) itself changes
51+ // we need to return
52+ let Some(head_ref): Option<String> = Command::new("git")
53+ .args(["symbolic-ref", "HEAD"])
54+ .output()
55+ .ok()
56+ .filter(|output| output.status.success())
57+ .and_then(|x| String::from_utf8(x.stdout).ok())
58+ else{ return; };
59+ let head_ref = Path::new(&git_dir).join(head_ref);
60+ if head_ref.exists() {
61+ println!("cargo:rerun-if-changed={}", head_ref.display());
62+ }
2663}
helix-loader/src/grammar.rs+12
@@ -413,6 +413,18 @@ fn build_tree_sitter_library(
413413 let mut library_path = parser_lib_path.join(&grammar.grammar_id);
414414 library_path.set_extension(DYLIB_EXTENSION);
415415
416+ // if we are running inside a buildscript emit cargo metadata
417+ // to detect if we are running from a buildscript check some env variables
418+ // that cargo only sets for build scripts
419+ if std::env::var("OUT_DIR").is_ok() && std::env::var("CARGO").is_ok() {
420+ if let Some(scanner_path) = scanner_path.as_ref().and_then(|path| path.to_str()) {
421+ println!("cargo:rerun-if-changed={scanner_path}");
422+ }
423+ if let Some(parser_path) = parser_path.to_str() {
424+ println!("cargo:rerun-if-changed={parser_path}");
425+ }
426+ }
427+
416428 let recompile = needs_recompile(&library_path, &parser_path, &scanner_path)
417429 .context("Failed to compare source and binary timestamps")?;
418430