33//! Generates circuit binaries (prover, verifier, aggregator) at build time.
44//! This ensures the binaries are always consistent with the circuit crate version
55//! and eliminates the need to manually run `quantus developer build-circuits`.
6+ //!
7+ //! Outputs are written to `OUT_DIR` (required by cargo) and then copied to
8+ //! `generated-bins/` in the project root for runtime access — but only during
9+ //! normal builds, **not** during `cargo publish` verification where modifying the
10+ //! source directory is forbidden.
611
712use std:: { env, path:: Path , time:: Instant } ;
813
914fn main ( ) {
15+ let out_dir = env:: var ( "OUT_DIR" ) . expect ( "OUT_DIR not set" ) ;
1016 let manifest_dir = env:: var ( "CARGO_MANIFEST_DIR" ) . expect ( "CARGO_MANIFEST_DIR not set" ) ;
11- let output_dir = Path :: new ( & manifest_dir) . join ( "generated-bins" ) ;
17+
18+ let build_output_dir = Path :: new ( & out_dir) . join ( "generated-bins" ) ;
1219
1320 let num_leaf_proofs: usize = env:: var ( "QP_NUM_LEAF_PROOFS" )
1421 . unwrap_or_else ( |_| "16" . to_string ( ) )
1522 . parse ( )
1623 . expect ( "QP_NUM_LEAF_PROOFS must be a valid usize" ) ;
1724
18- // Rerun if the circuit builder crate changes
1925 println ! ( "cargo:rerun-if-changed=build.rs" ) ;
2026
2127 println ! (
@@ -25,15 +31,14 @@ fn main() {
2531
2632 let start = Instant :: now ( ) ;
2733
28- // Create the output directory if it doesn't exist
29- std :: fs :: create_dir_all ( & output_dir ) . expect ( "Failed to create generated-bins directory" ) ;
34+ std :: fs :: create_dir_all ( & build_output_dir )
35+ . expect ( "Failed to create generated-bins directory in OUT_DIR " ) ;
3036
31- // Generate all circuit binaries (leaf + aggregated, WITH prover)
3237 qp_wormhole_circuit_builder:: generate_all_circuit_binaries (
33- & output_dir ,
34- true , // include_prover = true (CLI needs prover for proof generation)
38+ & build_output_dir ,
39+ true ,
3540 num_leaf_proofs,
36- None , // num_layer0_proofs - no layer-1 aggregation
41+ None ,
3742 )
3843 . expect ( "Failed to generate circuit binaries" ) ;
3944
@@ -42,4 +47,17 @@ fn main() {
4247 "cargo:warning=[quantus-cli] ZK circuit binaries generated in {:.2}s" ,
4348 elapsed. as_secs_f64( )
4449 ) ;
50+
51+ // Copy bins to project root for runtime access, but NOT during `cargo publish`
52+ // verification (manifest_dir is inside target/package/ in that case).
53+ let project_bins = Path :: new ( & manifest_dir) . join ( "generated-bins" ) ;
54+ if !manifest_dir. contains ( "target/package/" ) {
55+ let _ = std:: fs:: create_dir_all ( & project_bins) ;
56+ if let Ok ( entries) = std:: fs:: read_dir ( & build_output_dir) {
57+ for entry in entries. flatten ( ) {
58+ let dest = project_bins. join ( entry. file_name ( ) ) ;
59+ let _ = std:: fs:: copy ( entry. path ( ) , dest) ;
60+ }
61+ }
62+ }
4563}
0 commit comments