pub fn compile_protos<P>(&mut self, protos: &[P], includes: &[P]) -> Result<()>
    where
        P: AsRef<Path>,
    {
        let target: PathBuf = self.out_dir.clone().map(Ok).unwrap_or_else(|| {
            env::var_os("OUT_DIR")
                .ok_or_else(|| {
                    Error::new(ErrorKind::Other, "OUT_DIR environment variable is not set")
                })
                .map(Into::into)
        })?;

        // TODO: This should probably emit 'rerun-if-changed=PATH' directives for cargo, however
        // according to [1] if any are output then those paths replace the default crate root,
        // which is undesirable. Figure out how to do it in an additive way; perhaps gcc-rs has
        // this figured out.
        // [1]: http://doc.crates.io/build-script.html#outputs-of-the-build-script

        let tmp = tempfile::Builder::new().prefix("prost-build").tempdir()?;
        let descriptor_set = tmp.path().join("prost-descriptor-set");

        let mut cmd = Command::new(protoc());
        cmd.arg("--include_imports")
            .arg("--include_source_info")
            .arg("-o")
            .arg(&descriptor_set);

        for include in includes {
            cmd.arg("-I").arg(include.as_ref());
        }

        // Set the protoc include after the user includes in case the user wants to
        // override one of the built-in .protos.
        cmd.arg("-I").arg(protoc_include());

        for proto in protos {
            cmd.arg(proto.as_ref());
        }

        let output = cmd.output()?;
        if !output.status.success() {
            return Err(Error::new(
                ErrorKind::Other,
                format!("protoc failed: {}", String::from_utf8_lossy(&output.stderr)),
            ));
        }

        let buf = fs::read(descriptor_set)?;
        let descriptor_set = FileDescriptorSet::decode(&*buf)?;

        let modules = self.generate(descriptor_set.file)?;
        for (module, content) in modules {
            let mut filename = module.join(".");
            filename.push_str(".rs");

            let output_path = target.join(&filename);

            let previous_content = fs::read(&output_path);

            if previous_content
                .map(|previous_content| previous_content == content.as_bytes())
                .unwrap_or(false)
            {
                trace!("unchanged: {:?}", filename);
            } else {
                trace!("writing: {:?}", filename);
                fs::write(output_path, content)?;
            }
        }

        Ok(())
    }
/// Compile `.proto` files into Rust files during a Cargo build with additional code generator
/// configuration options.
///
/// This method is like the `prost_build::compile_protos` function, with the added ability to
/// specify non-default code generation options. See that function for more information about
/// the arguments and generated outputs.
///
/// # Example `build.rs`
///
/// ```rust,no_run
/// fn main() {
///     let mut prost_build = prost_build::Config::new();
///     prost_build.btree_map(&["."]);
///     prost_build.compile_protos(&["src/frontend.proto", "src/backend.proto"],
///                                &["src"]).unwrap();
/// }
/// ```
Compile `.proto` files into Rust files during a Cargo build with additional code generator
configuration options.

This method is like the `prost_build::compile_protos` function, with the added ability to
specify non-default code generation options. See that function for more information about
the arguments and generated outputs.
Compile ` . proto ` files into Rust files during a Cargo build with additional code generator configuration options . This method is like the ` prost_build :: compile_protos ` function with the added ability to specify non - default code generation options . See that function for more information about the arguments and generated outputs .
pub fn compile_protos<P>(&mut self, protos: &[P], includes: &[P]) -> Result<()>
    where
        P: AsRef<Path>,
    {
        let target: PathBuf = self.out_dir.clone().map(Ok).unwrap_or_else(|| {
            env::var_os("OUT_DIR")
                .ok_or_else(|| {
                    Error::new(ErrorKind::Other, "OUT_DIR environment variable is not set")
                })
                .map(Into::into)
        })?;
        // TODO: This should probably emit 'rerun-if-changed=PATH' directives for cargo, however
        // according to [1] if any are output then those paths replace the default crate root,
        // which is undesirable. Figure out how to do it in an additive way; perhaps gcc-rs has
        // this figured out.
        // [1]: http://doc.crates.io/build-script.html#outputs-of-the-build-script
        let tmp = tempfile::Builder::new().prefix("prost-build").tempdir()?;
        let descriptor_set = tmp.path().join("prost-descriptor-set");
        let mut cmd = Command::new(protoc());
        cmd.arg("--include_imports")
            .arg("--include_source_info")
            .arg("-o")
            .arg(&descriptor_set);
        for include in includes {
            cmd.arg("-I").arg(include.as_ref());
        }
        // Set the protoc include after the user includes in case the user wants to
        // override one of the built-in .protos.
        cmd.arg("-I").arg(protoc_include());
        for proto in protos {
            cmd.arg(proto.as_ref());
        }
        let output = cmd.output()?;
        if !output.status.success() {
            return Err(Error::new(
                ErrorKind::Other,
                format!("protoc failed: {}", String::from_utf8_lossy(&output.stderr)),
            ));
        }
        let buf = fs::read(descriptor_set)?;
        let descriptor_set = FileDescriptorSet::decode(&*buf)?;
        let modules = self.generate(descriptor_set.file)?;
        for (module, content) in modules {
            let mut filename = module.join(".");
            filename.push_str(".rs");
            let output_path = target.join(&filename);
            let previous_content = fs::read(&output_path);
            if previous_content
                .map(|previous_content| previous_content == content.as_bytes())
                .unwrap_or(false)
            {
                trace!("unchanged: {:?}", filename);
            } else {
                trace!("writing: {:?}", filename);
                fs::write(output_path, content)?;
            }
        }
        Ok(())
    }
pub fn compile_protos < P > ( & mut self , protos : & [ P ] , includes : & [ P ] ) -> Result < ( ) > where P : AsRef < Path > , { let target : PathBuf = self . out_dir . clone ( ) . map ( Ok ) . unwrap_or_else ( | | { env :: var_os ( " " ) . ok_or_else ( | | { Error :: new ( ErrorKind :: Other , " " ) } ) . map ( Into :: into ) } ) ? ; // TODO: This should probably emit 'rerun-if-changed=PATH' directives for cargo, however // according to [1] if any are output then those paths replace the default crate root, // which is undesirable. Figure out how to do it in an additive way; perhaps gcc-rs has // this figured out. // [1]: http://doc.crates.io/build-script.html#outputs-of-the-build-script let tmp = tempfile :: Builder :: new ( ) . prefix ( " " ) . tempdir ( ) ? ; let descriptor_set = tmp . path ( ) . join ( " " ) ; let mut cmd = Command :: new ( protoc ( ) ) ; cmd . arg ( " " ) . arg ( " " ) . arg ( " " ) . arg ( & descriptor_set ) ; for include in includes { cmd . arg ( " " ) . arg ( include . as_ref ( ) ) ; } // Set the protoc include after the user includes in case the user wants to // override one of the built-in .protos. cmd . arg ( " " ) . arg ( protoc_include ( ) ) ; for proto in protos { cmd . arg ( proto . as_ref ( ) ) ; } let output = cmd . output ( ) ? ; if ! output . status . success ( ) { return Err ( Error :: new ( ErrorKind :: Other , format ! ( " " String from_utf8_lossy ( output stderr ) ) , ) ) ; } let buf = fs :: read ( descriptor_set ) ? ; let descriptor_set = FileDescriptorSet :: decode ( & * buf ) ? ; let modules = self . generate ( descriptor_set . file ) ? ; for ( module , content ) in modules { let mut filename = module . join ( " " ) ; filename . push_str ( " " ) ; let output_path = target . join ( & filename ) ; let previous_content = fs :: read ( & output_path ) ; if previous_content . map ( | previous_content | previous_content == content . as_bytes ( ) ) . unwrap_or ( false ) { trace ! ( " " filename ) ; } else { trace ! ( " " filename ) ; fs :: write ( output_path , content ) ? ; } } Ok ( ( ) ) }
Compile `.proto` files into Rust files during a Cargo build with additional code generator
configuration options.
Compile ` . proto ` files into Rust files during a Cargo build with additional code generator configuration options .
&mut self
protos
&[P]
includes
&[P]
&mut self
protos
&[P]
includes
&[P]
example `build.rs`
```rust,no_run
fn main() {
let mut prost_build = prost_build::Config::new();
prost_build.btree_map(&["."]);
prost_build.compile_protos(&["src/frontend.proto", "src/backend.proto"],
&["src"]).unwrap();
}
```
` ` ` rust no_run fn main () { let mut prost_build = prost_build :: Config :: new () ; prost_build . btree_map ( & [ " . " ] ) ; prost_build . compile_protos ( & [ " src / frontend . proto " " src / backend . proto " ] & [ " src " ] ) . unwrap () ; } ` ` `