11use glob:: glob;
22use std:: env;
33use std:: ffi:: OsString ;
4- use std:: fs:: { self , OpenOptions } ;
5- use std:: io:: { self , Write } ;
4+ use std:: fs;
5+ use std:: io;
66use std:: path:: { Path , PathBuf } ;
77use std:: process:: Command ;
88
@@ -29,7 +29,7 @@ fn main() {
2929
3030 if std:: env:: var ( "LIBSQL_DEV" ) . is_ok ( ) {
3131 make_amalgamation ( ) ;
32- build_multiple_ciphers ( & target , & out_path) ;
32+ build_multiple_ciphers ( & out_path) ;
3333 }
3434
3535 let bindgen_rs_path = if cfg ! ( feature = "session" ) {
@@ -50,7 +50,7 @@ fn main() {
5050 }
5151
5252 if cfg ! ( feature = "multiple-ciphers" ) {
53- copy_multiple_ciphers ( & target , & out_dir , & out_path) ;
53+ copy_multiple_ciphers ( & out_path) ;
5454 return ;
5555 }
5656
@@ -409,18 +409,22 @@ pub fn build_bundled(out_dir: &str, out_path: &Path) {
409409 println ! ( "cargo:lib_dir={out_dir}" ) ;
410410}
411411
412- fn copy_multiple_ciphers ( target : & str , out_dir : & str , out_path : & Path ) {
413- let dylib = format ! ( "{out_dir}/sqlite3mc/libsqlite3mc_static.a" ) ;
414- if !Path :: new ( & dylib) . exists ( ) {
415- build_multiple_ciphers ( target, out_path) ;
416- }
412+ fn copy_multiple_ciphers ( out_path : & Path ) {
413+ let dst = dbg ! ( build_multiple_ciphers( out_path) ) ;
417414
418- copy_with_cp ( dylib, format ! ( "{out_dir}/libsqlite3mc.a" ) ) . unwrap ( ) ;
419- println ! ( "cargo:rustc-link-lib=static=sqlite3mc" ) ;
420- println ! ( "cargo:rustc-link-search={out_dir}" ) ;
415+ println ! ( "cargo:rustc-link-search={}" , dst. join( "build" ) . display( ) ) ;
416+ println ! (
417+ "cargo:rustc-link-search={}" ,
418+ dst. join( "build" ) . join( "Release" ) . display( )
419+ ) ;
420+ println ! (
421+ "cargo:rustc-link-search={}" ,
422+ dst. join( "build" ) . join( "Debug" ) . display( )
423+ ) ;
424+ println ! ( "cargo:rustc-link-lib=static=sqlite3mc_static" ) ;
421425}
422426
423- fn build_multiple_ciphers ( target : & str , out_path : & Path ) {
427+ fn build_multiple_ciphers ( out_path : & Path ) -> PathBuf {
424428 let bindgen_rs_path = if cfg ! ( feature = "session" ) {
425429 "bundled/bindings/session_bindgen.rs"
426430 } else {
@@ -450,112 +454,33 @@ fn build_multiple_ciphers(target: &str, out_path: &Path) {
450454 . unwrap ( ) ;
451455
452456 let bundled_dir = format ! ( "{out_dir}/sqlite3mc" ) ;
453- let sqlite3mc_build_dir = env:: current_dir ( ) . unwrap ( ) . join ( out_dir) . join ( "sqlite3mc" ) ;
454-
455- let mut cmake_opts: Vec < & str > = vec ! [ ] ;
456457
457- let target_postfix = target. to_string ( ) . replace ( "-" , "_" ) ;
458- let cross_cc_var_name = format ! ( "CC_{}" , target_postfix) ;
459- println ! ( "cargo:warning=CC_var_name={}" , cross_cc_var_name) ;
460- let cross_cc = env:: var ( & cross_cc_var_name) . ok ( ) ;
458+ let mut config = cmake:: Config :: new ( & bundled_dir) ;
459+
460+ config
461+ . build_target ( "sqlite3mc_static" )
462+ . define ( "SQLITE3MC_STATIC" , "ON" )
463+ . define ( "CODEC_TYPE" , "AES256" )
464+ . define ( "SQLITE3MC_BUILD_SHELL" , "OFF" )
465+ . define ( "SQLITE_SHELL_IS_UTF8" , "OFF" )
466+ . define ( "SQLITE_USER_AUTHENTICATION" , "OFF" )
467+ . define ( "SQLITE_SECURE_DELETE" , "OFF" )
468+ . define ( "SQLITE_ENABLE_COLUMN_METADATA" , "ON" )
469+ . define ( "SQLITE_USE_URI" , "ON" )
470+ . define ( "CMAKE_POSITION_INDEPENDENT_CODE" , "ON" )
471+ . profile ( "Release" ) ;
461472
462- let cross_cxx_var_name = format ! ( "CXX_{}" , target_postfix) ;
463- let cross_cxx = env:: var ( & cross_cxx_var_name) . ok ( ) ;
464-
465- let toolchain_path = sqlite3mc_build_dir. join ( "toolchain.cmake" ) ;
466- let cmake_toolchain_opt = "-DCMAKE_TOOLCHAIN_FILE=toolchain.cmake" . to_string ( ) ;
467-
468- let mut toolchain_file = OpenOptions :: new ( )
469- . create ( true )
470- . write ( true )
471- . append ( true )
472- . open ( toolchain_path. clone ( ) )
473- . unwrap ( ) ;
474-
475- if let Some ( ref cc) = cross_cc {
476- let system_name = if cc. contains ( "linux" ) {
477- "Linux"
478- } else if cc. contains ( "darwin" ) {
479- "Darwin"
480- } else if cc. contains ( "w64" ) {
481- "Windows"
482- } else {
483- panic ! ( "Unsupported cross target {}" , cc)
484- } ;
485-
486- let system_processor = if cc. contains ( "x86_64" ) {
487- "x86_64"
488- } else if cc. contains ( "aarch64" ) {
489- "arm64"
490- } else if cc. contains ( "arm" ) {
491- "arm"
492- } else {
493- panic ! ( "Unsupported cross target {}" , cc)
494- } ;
495-
496- cmake_opts. push ( & cmake_toolchain_opt) ;
497- writeln ! ( toolchain_file, "set(CMAKE_SYSTEM_NAME \" {}\" )" , system_name) . unwrap ( ) ;
498- writeln ! (
499- toolchain_file,
500- "set(CMAKE_SYSTEM_PROCESSOR \" {}\" )" ,
501- system_processor
502- )
503- . unwrap ( ) ;
504- writeln ! ( toolchain_file, "set(CMAKE_C_COMPILER {})" , cc) . unwrap ( ) ;
505- }
506-
507- if let Some ( cxx) = cross_cxx {
508- writeln ! ( toolchain_file, "set(CMAKE_CXX_COMPILER {})" , cxx) . unwrap ( ) ;
509- }
510-
511- cmake_opts. push ( "-DCMAKE_BUILD_TYPE=Release" ) ;
512- cmake_opts. push ( "-DSQLITE3MC_STATIC=ON" ) ;
513- cmake_opts. push ( "-DCODEC_TYPE=AES256" ) ;
514- cmake_opts. push ( "-DSQLITE3MC_BUILD_SHELL=OFF" ) ;
515- cmake_opts. push ( "-DSQLITE_SHELL_IS_UTF8=OFF" ) ;
516- cmake_opts. push ( "-DSQLITE_USER_AUTHENTICATION=OFF" ) ;
517- cmake_opts. push ( "-DSQLITE_SECURE_DELETE=OFF" ) ;
518- cmake_opts. push ( "-DSQLITE_ENABLE_COLUMN_METADATA=ON" ) ;
519- cmake_opts. push ( "-DSQLITE_USE_URI=ON" ) ;
520- cmake_opts. push ( "-DCMAKE_POSITION_INDEPENDENT_CODE=ON" ) ;
521-
522- if target. contains ( "musl" ) {
523- cmake_opts. push ( "-DCMAKE_C_FLAGS=\" -U_FORTIFY_SOURCE\" -D_FILE_OFFSET_BITS=32" ) ;
524- cmake_opts. push ( "-DCMAKE_CXX_FLAGS=\" -U_FORTIFY_SOURCE\" -D_FILE_OFFSET_BITS=32" ) ;
525- }
526-
527- let mut cmake = Command :: new ( "cmake" ) ;
528- cmake. current_dir ( sqlite3mc_build_dir. clone ( ) ) ;
529- cmake. args ( cmake_opts. clone ( ) ) ;
530- cmake. arg ( bundled_dir. clone ( ) ) ;
531473 if cfg ! ( feature = "wasmtime-bindings" ) {
532- cmake . arg ( "-DLIBSQL_ENABLE_WASM_RUNTIME= 1") ;
474+ config . define ( "LIBSQL_ENABLE_WASM_RUNTIME" , " 1") ;
533475 }
476+
534477 if cfg ! ( feature = "session" ) {
535- cmake. arg ( "-DSQLITE_ENABLE_PREUPDATE_HOOK=ON" ) ;
536- cmake. arg ( "-DSQLITE_ENABLE_SESSION=ON" ) ;
537- }
538- println ! ( "Running `cmake` with options: {}" , cmake_opts. join( " " ) ) ;
539- let status = cmake. status ( ) . unwrap ( ) ;
540- if !status. success ( ) {
541- panic ! ( "Failed to run cmake with options: {}" , cmake_opts. join( " " ) ) ;
542- }
543-
544- let mut make = Command :: new ( "cmake" ) ;
545- make. current_dir ( sqlite3mc_build_dir. clone ( ) ) ;
546- make. args ( [ "--build" , "." ] ) ;
547- make. args ( [ "--config" , "Release" ] ) ;
548- if !make. status ( ) . unwrap ( ) . success ( ) {
549- panic ! ( "Failed to run make" ) ;
550- }
551- // The `msbuild` tool puts the output in a different place so let's move it.
552- if Path :: exists ( & sqlite3mc_build_dir. join ( "Release/sqlite3mc_static.lib" ) ) {
553- fs:: rename (
554- sqlite3mc_build_dir. join ( "Release/sqlite3mc_static.lib" ) ,
555- sqlite3mc_build_dir. join ( "libsqlite3mc_static.a" ) ,
556- )
557- . unwrap ( ) ;
478+ config
479+ . define ( "SQLITE_ENABLE_PREUPDATE_HOOK" , "ON" )
480+ . define ( "SQLITE_ENABLE_SESSION" , "ON" ) ;
558481 }
482+
483+ config. build ( )
559484}
560485
561486fn env ( name : & str ) -> Option < OsString > {
0 commit comments