1+
12use std:: { env, path:: PathBuf , process:: Command } ;
23
34const GO_SRC : & str = "./gnark/verifier.go" ;
@@ -8,35 +9,84 @@ const CIRCOM_SRC: &str = "./circom/verifier.go";
89const CIRCOM_OUT : & str = "libcircomverifier.a" ;
910const CIRCOM_LIB : & str = "circomverifier" ;
1011
11- fn main ( ) {
12- let out_dir = PathBuf :: from ( env:: var ( "OUT_DIR" ) . unwrap ( ) ) ;
12+ fn ensure_go_dependencies ( ) {
13+ // Check if there's a go.mod in the current directory or parent directories
14+ let current_dir = env:: current_dir ( ) . expect ( "Failed to get current directory" ) ;
15+ println ! ( "Build script running in: {:?}" , current_dir) ;
16+
17+ // Try to find go.mod files
18+ let possible_dirs = vec ! [
19+ current_dir. clone( ) ,
20+ current_dir. parent( ) . unwrap_or( & current_dir) . to_path_buf( ) ,
21+ current_dir. parent( ) . unwrap_or( & current_dir) . parent( ) . unwrap_or( & current_dir) . to_path_buf( ) ,
22+ ] ;
23+
24+ for dir in possible_dirs {
25+ eprintln ! ( "mod tidy dir: {:?}" , dir) ;
26+ let go_mod_path = dir. join ( "go.mod" ) ;
27+ if go_mod_path. exists ( ) {
28+ println ! ( "Found go.mod in: {:?}" , dir) ;
29+
30+ // Run go mod tidy to ensure all dependencies are properly resolved
31+ let mut tidy_cmd = Command :: new ( "go" ) ;
32+ tidy_cmd. arg ( "mod" ) . arg ( "tidy" ) . current_dir ( & dir) ;
33+
34+ let tidy_output = tidy_cmd. output ( ) . expect ( "Failed to run go mod tidy" ) ;
35+ if !tidy_output. status . success ( ) {
36+ eprintln ! ( "go mod tidy failed in {:?}: {}" , dir, String :: from_utf8_lossy( & tidy_output. stderr) ) ;
37+ } else {
38+ println ! ( "Successfully ran go mod tidy in {:?}" , dir) ;
39+ }
40+
41+ // Run go mod download to ensure all modules are cached
42+ let mut cmd = Command :: new ( "go" ) ;
43+ cmd. arg ( "mod" ) . arg ( "download" ) . current_dir ( & dir) ;
44+
45+ let output = cmd. output ( ) . expect ( "Failed to run go mod download" ) ;
46+ if !output. status . success ( ) {
47+ eprintln ! ( "go mod download failed in {:?}: {}" , dir, String :: from_utf8_lossy( & output. stderr) ) ;
48+ } else {
49+ println ! ( "Successfully ran go mod download in {:?}" , dir) ;
50+ }
51+
52+ break ;
53+ }
54+ }
55+ }
56+
57+ fn build_go_library ( src : & str , out : & str , out_dir : & PathBuf ) {
58+ println ! ( "Building Go library: {} -> {}" , src, out) ;
59+
1360 let mut go_build = Command :: new ( "go" ) ;
1461 go_build
1562 . arg ( "build" )
1663 . arg ( "-buildmode=c-archive" )
1764 . arg ( "-o" )
18- . arg ( out_dir. join ( GO_OUT ) )
19- . arg ( GO_SRC ) ;
65+ . arg ( out_dir. join ( out ) )
66+ . arg ( src ) ;
2067
21- go_build. status ( ) . expect ( "Go build failed" ) ;
22-
23- let mut circom_build = Command :: new ( "go" ) ;
24- circom_build
25- . arg ( "build" )
26- . arg ( "-buildmode=c-archive" )
27- . arg ( "-o" )
28- . arg ( out_dir. join ( CIRCOM_OUT ) )
29- . arg ( CIRCOM_SRC ) ;
30-
31- circom_build. status ( ) . expect ( "Circom build failed" ) ;
32- let output = circom_build. output ( ) . expect ( "Failed to execute Circom build command" ) ;
68+ let output = go_build. output ( ) . expect ( "Failed to execute Go build command" ) ;
3369 if !output. status . success ( ) {
34- eprintln ! ( "Circom build failed: {}" , String :: from_utf8_lossy( & output. stderr) ) ;
35- panic ! ( "Circom build failed" ) ;
70+ eprintln ! ( "Go build failed for {} : {}" , src , String :: from_utf8_lossy( & output. stderr) ) ;
71+ panic ! ( "Go build failed for {}" , src ) ;
3672 }
73+ println ! ( "Successfully built {}" , src) ;
74+ }
75+
76+ fn main ( ) {
77+ let out_dir = PathBuf :: from ( env:: var ( "OUT_DIR" ) . unwrap ( ) ) ;
78+
79+ // Ensure Go dependencies are available
80+ ensure_go_dependencies ( ) ;
81+
82+ // Build both Go libraries
83+ build_go_library ( GO_SRC , GO_OUT , & out_dir) ;
84+ build_go_library ( CIRCOM_SRC , CIRCOM_OUT , & out_dir) ;
3785
3886 println ! ( "cargo:rerun-if-changed={}" , GO_SRC ) ;
3987 println ! ( "cargo:rerun-if-changed={}" , CIRCOM_SRC ) ;
88+ println ! ( "cargo:rerun-if-changed=go.mod" ) ;
89+ println ! ( "cargo:rerun-if-changed=go.sum" ) ;
4090 println ! (
4191 "cargo:rustc-link-search=native={}" ,
4292 out_dir. to_str( ) . unwrap( )
0 commit comments