1- #![ cfg_attr( feature = "clippy" , feature( plugin) ) ]
2- #![ cfg_attr( feature = "clippy" , plugin( clippy) ) ]
3-
41use fs_extra:: dir:: CopyOptions ;
52use glob:: glob;
63use std:: env;
74use std:: path:: PathBuf ;
85use std:: process:: Command ;
96
107static 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
219fn 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 }
0 commit comments