1+ use std:: path:: { Path , PathBuf } ;
2+
3+ use fs_err as fs;
14use miden_node_proto_build:: remote_prover_api_descriptor;
2- use miette:: IntoDiagnostic ;
5+ use miette:: { IntoDiagnostic , WrapErr } ;
36use tonic_prost_build:: FileDescriptorSet ;
47
5- /// Defines whether the build script should generate files in `/src`.
6- ///
7- /// The docs.rs build pipeline has a read-only filesystem, so we have to avoid writing to `src`,
8- /// otherwise the docs will fail to build there. Note that writing to `OUT_DIR` is fine.
9- const BUILD_GENERATED_FILES_IN_SRC : bool = option_env ! ( "BUILD_PROTO" ) . is_some ( ) ;
10-
11- const GENERATED_OUT_DIR : & str = "src/generated" ;
12-
138/// Generates Rust protobuf bindings.
149fn main ( ) -> miette:: Result < ( ) > {
1510 miden_node_rocksdb_cxx_linkage_fix:: configure ( ) ;
16- println ! ( "cargo:rerun-if-env-changed=BUILD_PROTO" ) ;
17- if !BUILD_GENERATED_FILES_IN_SRC {
18- return Ok ( ( ) ) ;
19- }
11+
12+ let dst_dir =
13+ PathBuf :: from ( std:: env:: var ( "OUT_DIR" ) . expect ( "OUT_DIR should be set" ) ) . join ( "generated" ) ;
14+
15+ // Remove all existing files.
16+ let _ = fs:: remove_dir_all ( & dst_dir) ;
17+ fs:: create_dir ( & dst_dir)
18+ . into_diagnostic ( )
19+ . wrap_err ( "creating destination folder" ) ?;
2020
2121 // Get the file descriptor set
2222 let remote_prover_descriptor = remote_prover_api_descriptor ( ) ;
2323
2424 // Build tonic code
25- build_tonic_from_descriptor ( remote_prover_descriptor) ?;
25+ build_tonic_from_descriptor ( remote_prover_descriptor, & dst_dir ) ?;
2626
2727 Ok ( ( ) )
2828}
@@ -31,9 +31,12 @@ fn main() -> miette::Result<()> {
3131// ================================================================================================
3232
3333/// Builds tonic code from a `FileDescriptorSet`
34- fn build_tonic_from_descriptor ( descriptor : FileDescriptorSet ) -> miette:: Result < ( ) > {
34+ fn build_tonic_from_descriptor (
35+ descriptor : FileDescriptorSet ,
36+ dst_dir : & Path ,
37+ ) -> miette:: Result < ( ) > {
3538 tonic_prost_build:: configure ( )
36- . out_dir ( GENERATED_OUT_DIR )
39+ . out_dir ( dst_dir )
3740 . build_server ( true )
3841 . build_transport ( true )
3942 . compile_fds_with_config ( descriptor, tonic_prost_build:: Config :: new ( ) )
0 commit comments