Skip to content

Commit fa79f31

Browse files
authored
chore: offline builds with submodules (#486)
right now, we are cloning libpg_query on every build if its not available locally. this is not only unnecessary, it also makes reproducible builds even harder. this reverts that back to using submodules instead. also unified the directory structures across crates and removed the code that was intended to support multi-version builds. I think its unnecessary to even support that in our case since postgres syntax is backward compatible, and `EXPLAIN` exists since 6.4.
1 parent 0265881 commit fa79f31

File tree

9 files changed

+59
-107
lines changed

9 files changed

+59
-107
lines changed

.gitmodules

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,7 @@
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
8+
branch = 17-latest

Cargo.lock

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/pgt_lexer_codegen/build.rs

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,34 @@ use std::fs;
33
use std::io::Write;
44
use std::path::PathBuf;
55

6-
// TODO make this selectable via feature flags
7-
static LIBPG_QUERY_TAG: &str = "17-6.1.0";
6+
static LIBPG_QUERY_TAG: &str = "17-latest";
87

9-
/// Downloads the `kwlist.h` file from the specified version of `libpg_query`
108
fn main() -> Result<(), Box<dyn std::error::Error>> {
11-
let version = LIBPG_QUERY_TAG.to_string();
12-
13-
// Check for the postgres header file in the source tree first
149
let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR")?);
15-
let headers_dir = manifest_dir.join("postgres").join(&version);
16-
let kwlist_path = headers_dir.join("kwlist.h");
10+
let vendor_dir = manifest_dir.join("vendor").join(LIBPG_QUERY_TAG);
11+
let kwlist_path = vendor_dir.join("kwlist.h");
1712

18-
// Only download if the file doesn't exist
13+
// Download kwlist.h if not already present in source directory
1914
if !kwlist_path.exists() {
2015
println!(
21-
"cargo:warning=Downloading kwlist.h for libpg_query {}",
22-
version
16+
"cargo:warning=Downloading kwlist.h for libpg_query {} to source directory",
17+
LIBPG_QUERY_TAG
2318
);
2419

25-
fs::create_dir_all(&headers_dir)?;
20+
fs::create_dir_all(&vendor_dir)?;
2621

27-
let proto_url = format!(
22+
let kwlist_url = format!(
2823
"https://raw.githubusercontent.com/pganalyze/libpg_query/{}/src/postgres/include/parser/kwlist.h",
29-
version
24+
LIBPG_QUERY_TAG
3025
);
3126

32-
let response = ureq::get(&proto_url).call()?;
27+
let response = ureq::get(&kwlist_url).call()?;
3328
let content = response.into_string()?;
3429

3530
let mut file = fs::File::create(&kwlist_path)?;
3631
file.write_all(content.as_bytes())?;
3732

38-
println!("cargo:warning=Successfully downloaded kwlist.h");
33+
println!("cargo:warning=Successfully downloaded kwlist.h to source");
3934
}
4035

4136
println!(

crates/pgt_query/Cargo.toml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@ pgt_query_macros = { workspace = true }
1818

1919

2020
[features]
21-
default = ["postgres-17"]
22-
postgres-15 = []
23-
postgres-16 = []
24-
postgres-17 = []
21+
default = []
2522

2623
[build-dependencies]
2724
bindgen = "0.72.0"

crates/pgt_query/build.rs

Lines changed: 28 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,48 @@
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";
12-
fn get_libpg_query_tag() -> &'static str {
13-
#[cfg(feature = "postgres-15")]
14-
return "15-5.3.0";
15-
#[cfg(feature = "postgres-16")]
16-
return "16-6.1.0";
17-
#[cfg(feature = "postgres-17")]
18-
return "17-6.1.0";
19-
}
208

219
fn main() -> Result<(), Box<dyn std::error::Error>> {
22-
let libpg_query_tag = get_libpg_query_tag();
2310
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");
11+
let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR")?);
12+
let libpg_query_submodule = manifest_dir.join("vendor").join("libpg_query");
2713

28-
let src_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR")?).join("src");
14+
let src_dir = manifest_dir.join("src");
2915
let target = env::var("TARGET").unwrap();
3016
let is_emscripten = target.contains("emscripten");
3117

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

36-
// Clone libpg_query if not already present
37-
if !stamp_file.exists() {
38-
println!("cargo:warning=Cloning libpg_query {}", libpg_query_tag);
39-
40-
// Create vendor directory
41-
std::fs::create_dir_all(&vendor_dir)?;
42-
43-
// Clone the repository with partial clone for faster download
44-
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-
])
55-
.status()?;
56-
57-
if !status.success() {
58-
return Err("Failed to clone libpg_query".into());
59-
}
60-
61-
// Create stamp file
62-
std::fs::File::create(&stamp_file)?;
21+
// Check if submodule exists
22+
if !libpg_query_submodule.join(".git").exists() && !libpg_query_submodule.join("src").exists() {
23+
return Err(
24+
"libpg_query submodule not found. Please run: git submodule update --init --recursive"
25+
.into(),
26+
);
6327
}
6428

65-
// Tell cargo to rerun if the stamp file is deleted
66-
println!("cargo:rerun-if-changed={}", stamp_file.display());
29+
// Tell cargo to rerun if the submodule changes
30+
println!(
31+
"cargo:rerun-if-changed={}",
32+
libpg_query_submodule.join("src").display()
33+
);
6734

68-
// Copy necessary files to OUT_DIR for compilation
35+
// copy necessary files to out_dir for compilation
6936
let out_header_path = out_dir.join(LIBRARY_NAME).with_extension("h");
7037
let out_protobuf_path = out_dir.join("protobuf");
7138

7239
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"),
40+
libpg_query_submodule.join(LIBRARY_NAME).with_extension("h"),
41+
libpg_query_submodule.join("postgres_deparse.h"),
42+
libpg_query_submodule.join("Makefile"),
43+
libpg_query_submodule.join("src"),
44+
libpg_query_submodule.join("protobuf"),
45+
libpg_query_submodule.join("vendor"),
7846
];
7947

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

8553
fs_extra::copy_items(&source_paths, &out_dir, &copy_options)?;
8654

87-
// Compile the C library.
55+
// compile the c library.
8856
let mut build = cc::Build::new();
8957

90-
// Configure for Emscripten if needed
58+
// configure for emscripten if needed
9159
if is_emscripten {
92-
// Use emcc as the compiler instead of gcc/clang
60+
// use emcc as the compiler instead of gcc/clang
9361
build.compiler("emcc");
94-
// Use emar as the archiver instead of ar
62+
// use emar as the archiver instead of ar
9563
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
64+
// note: we don't add wasm-specific flags here as this creates a static library
65+
// the final linking flags should be added when building the final wasm module
9866
}
9967

10068
build
@@ -115,7 +83,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
11583
.include(out_dir.join("./vendor"))
11684
.include(out_dir.join("./src/postgres/include"))
11785
.include(out_dir.join("./src/include"))
118-
.warnings(false); // Avoid unnecessary warnings, as they are already considered as part of libpg_query development
86+
.warnings(false); // avoid unnecessary warnings, as they are already considered as part of libpg_query development
11987
if env::var("PROFILE").unwrap() == "debug" || env::var("DEBUG").unwrap() == "1" {
12088
build.define("USE_ASSERT_CHECKING", None);
12189
}

crates/pgt_query/vendor/libpg_query

Submodule libpg_query added at 9ac12d2

crates/pgt_query_macros/build.rs

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,47 +3,34 @@ use std::fs;
33
use std::io::Write;
44
use std::path::PathBuf;
55

6-
// This should match the version used by pgt_query crate
7-
// You can configure this via environment variable PG_QUERY_VERSION if needed
8-
static LIBPG_QUERY_TAG: &str = "17-6.1.0";
6+
static LIBPG_QUERY_TAG: &str = "17-latest";
97

108
fn main() -> Result<(), Box<dyn std::error::Error>> {
11-
// Allow version override via environment variable
12-
let version = env::var("PG_QUERY_VERSION").unwrap_or_else(|_| LIBPG_QUERY_TAG.to_string());
13-
14-
// Get the manifest directory (source directory)
159
let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR")?);
16-
let postgres_dir = manifest_dir.join("postgres");
17-
let proto_filename = format!("{}.proto", version);
18-
let proto_path = postgres_dir.join(&proto_filename);
10+
let vendor_dir = manifest_dir.join("vendor").join(LIBPG_QUERY_TAG);
11+
let proto_path = vendor_dir.join("pg_query.proto");
1912

2013
// Download proto file if not already present in source directory
2114
if !proto_path.exists() {
2215
println!(
2316
"cargo:warning=Downloading pg_query.proto for libpg_query {} to source directory",
24-
version
17+
LIBPG_QUERY_TAG
2518
);
2619

27-
// Create postgres directory if it doesn't exist
28-
fs::create_dir_all(&postgres_dir)?;
20+
fs::create_dir_all(&vendor_dir)?;
2921

30-
// Download the proto file
3122
let proto_url = format!(
3223
"https://raw.githubusercontent.com/pganalyze/libpg_query/{}/protobuf/pg_query.proto",
33-
version
24+
LIBPG_QUERY_TAG
3425
);
3526

3627
let response = ureq::get(&proto_url).call()?;
3728
let proto_content = response.into_string()?;
3829

39-
// Write proto file to source directory
4030
let mut file = fs::File::create(&proto_path)?;
4131
file.write_all(proto_content.as_bytes())?;
4232

43-
println!(
44-
"cargo:warning=Successfully downloaded pg_query.proto to {}",
45-
proto_path.display()
46-
);
33+
println!("cargo:warning=Successfully downloaded pg_query.proto to source");
4734
}
4835

4936
// Set environment variable for the proc macro

0 commit comments

Comments
 (0)