1
- #![ cfg_attr( feature = "clippy" , feature( plugin) ) ]
2
- #![ cfg_attr( feature = "clippy" , plugin( clippy) ) ]
3
-
4
1
use fs_extra:: dir:: CopyOptions ;
5
2
use glob:: glob;
6
3
use std:: env;
7
4
use std:: path:: PathBuf ;
8
5
use std:: process:: Command ;
9
6
10
7
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
- }
20
8
21
9
fn main ( ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
22
- let libpg_query_tag = get_libpg_query_tag ( ) ;
23
10
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" ) ;
27
13
28
- let src_dir = PathBuf :: from ( env :: var ( "CARGO_MANIFEST_DIR" ) ? ) . join ( "src" ) ;
14
+ let src_dir = manifest_dir . join ( "src" ) ;
29
15
let target = env:: var ( "TARGET" ) . unwrap ( ) ;
30
16
let is_emscripten = target. contains ( "emscripten" ) ;
31
17
32
- // Configure cargo through stdout
33
18
println ! ( "cargo:rustc-link-search=native={}" , out_dir. display( ) ) ;
34
19
println ! ( "cargo:rustc-link-lib=static={LIBRARY_NAME}" ) ;
35
20
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
+ ) ;
63
27
}
64
28
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
+ ) ;
67
34
68
- // Copy necessary files to OUT_DIR for compilation
35
+ // copy necessary files to out_dir for compilation
69
36
let out_header_path = out_dir. join ( LIBRARY_NAME ) . with_extension ( "h" ) ;
70
37
let out_protobuf_path = out_dir. join ( "protobuf" ) ;
71
38
72
39
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" ) ,
78
46
] ;
79
47
80
48
let copy_options = CopyOptions {
@@ -84,17 +52,17 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
84
52
85
53
fs_extra:: copy_items ( & source_paths, & out_dir, & copy_options) ?;
86
54
87
- // Compile the C library.
55
+ // compile the c library.
88
56
let mut build = cc:: Build :: new ( ) ;
89
57
90
- // Configure for Emscripten if needed
58
+ // configure for emscripten if needed
91
59
if is_emscripten {
92
- // Use emcc as the compiler instead of gcc/clang
60
+ // use emcc as the compiler instead of gcc/clang
93
61
build. compiler ( "emcc" ) ;
94
- // Use emar as the archiver instead of ar
62
+ // use emar as the archiver instead of ar
95
63
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
98
66
}
99
67
100
68
build
@@ -115,7 +83,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
115
83
. include ( out_dir. join ( "./vendor" ) )
116
84
. include ( out_dir. join ( "./src/postgres/include" ) )
117
85
. 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
119
87
if env:: var ( "PROFILE" ) . unwrap ( ) == "debug" || env:: var ( "DEBUG" ) . unwrap ( ) == "1" {
120
88
build. define ( "USE_ASSERT_CHECKING" , None ) ;
121
89
}
0 commit comments