Skip to content

Commit bcbb074

Browse files
committed
chore: offline builds with submodules
1 parent 7cca8f4 commit bcbb074

File tree

3 files changed

+73
-46
lines changed

3 files changed

+73
-46
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@
22
path = lib/tree_sitter_sql/tree-sitter-sql
33
url = https://github.com/DerekStride/tree-sitter-sql
44
branch = gh-pages
5+
[submodule "crates/pgt_query/vendor/libpg_query"]
6+
path = crates/pgt_query/vendor/libpg_query
7+
url = https://github.com/pganalyze/libpg_query.git

crates/pgt_query/build.rs

Lines changed: 69 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,103 @@
1-
#![cfg_attr(feature = "clippy", feature(plugin))]
2-
#![cfg_attr(feature = "clippy", plugin(clippy))]
3-
41
use fs_extra::dir::CopyOptions;
52
use glob::glob;
63
use std::env;
74
use std::path::PathBuf;
85
use std::process::Command;
96

107
static LIBRARY_NAME: &str = "pg_query";
11-
static LIBPG_QUERY_REPO: &str = "https://github.com/pganalyze/libpg_query.git";
8+
129
fn get_libpg_query_tag() -> &'static str {
1310
#[cfg(feature = "postgres-15")]
14-
return "15-5.3.0";
11+
return "15-4.2.4";
1512
#[cfg(feature = "postgres-16")]
16-
return "16-6.1.0";
13+
return "16-5.2.0";
1714
#[cfg(feature = "postgres-17")]
1815
return "17-6.1.0";
1916
}
2017

2118
fn main() -> Result<(), Box<dyn std::error::Error>> {
2219
let libpg_query_tag = get_libpg_query_tag();
2320
let out_dir = PathBuf::from(env::var("OUT_DIR")?);
24-
let vendor_dir = out_dir.join("vendor");
25-
let libpg_query_dir = vendor_dir.join("libpg_query").join(libpg_query_tag);
26-
let stamp_file = libpg_query_dir.join(".stamp");
21+
let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR")?);
22+
let libpg_query_submodule = manifest_dir.join("vendor").join("libpg_query");
2723

28-
let src_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR")?).join("src");
24+
let src_dir = manifest_dir.join("src");
2925
let target = env::var("TARGET").unwrap();
3026
let is_emscripten = target.contains("emscripten");
3127

32-
// Configure cargo through stdout
3328
println!("cargo:rustc-link-search=native={}", out_dir.display());
3429
println!("cargo:rustc-link-lib=static={LIBRARY_NAME}");
3530

36-
// Clone libpg_query if not already present
37-
if !stamp_file.exists() {
38-
println!("cargo:warning=Cloning libpg_query {}", libpg_query_tag);
31+
if !libpg_query_submodule.join(".git").exists() && !libpg_query_submodule.join("src").exists() {
32+
return Err(
33+
"libpg_query submodule not found. Please run: git submodule update --init --recursive"
34+
.into(),
35+
);
36+
}
3937

40-
// Create vendor directory
41-
std::fs::create_dir_all(&vendor_dir)?;
38+
// check if we need to checkout a different tag
39+
let current_head = Command::new("git")
40+
.args(["rev-parse", "HEAD"])
41+
.current_dir(&libpg_query_submodule)
42+
.output()?;
43+
44+
let tag_commit = Command::new("git")
45+
.args(["rev-list", "-n", "1", libpg_query_tag])
46+
.current_dir(&libpg_query_submodule)
47+
.output();
48+
49+
let needs_checkout = match tag_commit {
50+
Ok(output) => {
51+
let current = String::from_utf8_lossy(&current_head.stdout);
52+
let target = String::from_utf8_lossy(&output.stdout);
53+
current.trim() != target.trim()
54+
}
55+
Err(_) => {
56+
// tag not found locally, try fetching
57+
let status = Command::new("git")
58+
.args(["fetch", "--tags"])
59+
.current_dir(&libpg_query_submodule)
60+
.status()?;
61+
62+
if !status.success() {
63+
return Err("Failed to fetch tags from libpg_query".into());
64+
}
65+
true
66+
}
67+
};
4268

43-
// Clone the repository with partial clone for faster download
69+
if needs_checkout {
70+
// checkout the correct tag for the selected postgresql version
71+
println!(
72+
"cargo:warning=Checking out libpg_query tag: {}",
73+
libpg_query_tag
74+
);
4475
let status = Command::new("git")
45-
.args([
46-
"clone",
47-
"--filter=blob:none",
48-
"--depth",
49-
"1",
50-
"--branch",
51-
libpg_query_tag,
52-
LIBPG_QUERY_REPO,
53-
libpg_query_dir.to_str().unwrap(),
54-
])
76+
.args(["checkout", libpg_query_tag])
77+
.current_dir(&libpg_query_submodule)
5578
.status()?;
5679

5780
if !status.success() {
58-
return Err("Failed to clone libpg_query".into());
81+
return Err(format!("Failed to checkout libpg_query tag: {}", libpg_query_tag).into());
5982
}
60-
61-
// Create stamp file
62-
std::fs::File::create(&stamp_file)?;
6383
}
6484

65-
// Tell cargo to rerun if the stamp file is deleted
66-
println!("cargo:rerun-if-changed={}", stamp_file.display());
85+
// tell cargo to rerun if the submodule changes
86+
println!(
87+
"cargo:rerun-if-changed={}",
88+
libpg_query_submodule.join("src").display()
89+
);
6790

68-
// Copy necessary files to OUT_DIR for compilation
91+
// copy necessary files to out_dir for compilation
6992
let out_header_path = out_dir.join(LIBRARY_NAME).with_extension("h");
7093
let out_protobuf_path = out_dir.join("protobuf");
7194

7295
let source_paths = vec![
73-
libpg_query_dir.join(LIBRARY_NAME).with_extension("h"),
74-
libpg_query_dir.join("Makefile"),
75-
libpg_query_dir.join("src"),
76-
libpg_query_dir.join("protobuf"),
77-
libpg_query_dir.join("vendor"),
96+
libpg_query_submodule.join(LIBRARY_NAME).with_extension("h"),
97+
libpg_query_submodule.join("Makefile"),
98+
libpg_query_submodule.join("src"),
99+
libpg_query_submodule.join("protobuf"),
100+
libpg_query_submodule.join("vendor"),
78101
];
79102

80103
let copy_options = CopyOptions {
@@ -84,17 +107,17 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
84107

85108
fs_extra::copy_items(&source_paths, &out_dir, &copy_options)?;
86109

87-
// Compile the C library.
110+
// compile the c library.
88111
let mut build = cc::Build::new();
89112

90-
// Configure for Emscripten if needed
113+
// configure for emscripten if needed
91114
if is_emscripten {
92-
// Use emcc as the compiler instead of gcc/clang
115+
// use emcc as the compiler instead of gcc/clang
93116
build.compiler("emcc");
94-
// Use emar as the archiver instead of ar
117+
// use emar as the archiver instead of ar
95118
build.archiver("emar");
96-
// Note: We don't add WASM-specific flags here as this creates a static library
97-
// The final linking flags should be added when building the final WASM module
119+
// note: we don't add wasm-specific flags here as this creates a static library
120+
// the final linking flags should be added when building the final wasm module
98121
}
99122

100123
build
@@ -115,7 +138,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
115138
.include(out_dir.join("./vendor"))
116139
.include(out_dir.join("./src/postgres/include"))
117140
.include(out_dir.join("./src/include"))
118-
.warnings(false); // Avoid unnecessary warnings, as they are already considered as part of libpg_query development
141+
.warnings(false); // avoid unnecessary warnings, as they are already considered as part of libpg_query development
119142
if env::var("PROFILE").unwrap() == "debug" || env::var("DEBUG").unwrap() == "1" {
120143
build.define("USE_ASSERT_CHECKING", None);
121144
}
Submodule libpg_query added at 1c1a32e

0 commit comments

Comments
 (0)