|
| 1 | +// Copyright 2023-Present Datadog, Inc. https://www.datadoghq.com/ |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +use std::error::Error; |
| 5 | + |
| 6 | +#[cfg(feature = "generate-protobuf")] |
| 7 | +use std::{ |
| 8 | + env, |
| 9 | + fs::File, |
| 10 | + io::{Read, Write}, |
| 11 | + path::Path, |
| 12 | +}; |
| 13 | + |
| 14 | +#[cfg(feature = "generate-protobuf")] |
| 15 | +const HEADER: &str = "// Copyright 2023-Present Datadog, Inc. https://www.datadoghq.com/ |
| 16 | +// SPDX-License-Identifier: Apache-2.0 |
| 17 | +
|
| 18 | +"; |
| 19 | + |
| 20 | +fn main() -> Result<(), Box<dyn Error>> { |
| 21 | + #[cfg(feature = "generate-protobuf")] |
| 22 | + { |
| 23 | + // protoc is required to compile proto files. This uses protobuf_src to compile protoc |
| 24 | + // from the source, setting the env var to tell prost_build where to find it. |
| 25 | + std::env::set_var("PROTOC", protoc_bin_vendored::protoc_bin_path().unwrap()); |
| 26 | + |
| 27 | + let mut config = prost_build::Config::new(); |
| 28 | + |
| 29 | + let cur_working_dir = env::var("CARGO_MANIFEST_DIR")?; |
| 30 | + let output_path = Path::new(&cur_working_dir).join("src"); |
| 31 | + |
| 32 | + config.out_dir(output_path.clone()); |
| 33 | + |
| 34 | + println!("cargo:rerun-if-changed=src/pb/DDSketch.proto"); |
| 35 | + config.compile_protos(&["src/pb/DDSketch.proto"], &["src/"])?; |
| 36 | + |
| 37 | + prepend_to_file(HEADER.as_bytes(), &output_path.join("pb.rs"))?; |
| 38 | + } |
| 39 | + |
| 40 | + Ok(()) |
| 41 | +} |
| 42 | + |
| 43 | +#[cfg(feature = "generate-protobuf")] |
| 44 | +fn prepend_to_file(data: &[u8], file_path: &Path) -> Result<(), Box<dyn Error>> { |
| 45 | + let mut f = File::open(file_path)?; |
| 46 | + let mut content = data.to_owned(); |
| 47 | + f.read_to_end(&mut content)?; |
| 48 | + |
| 49 | + let mut f = File::create(file_path)?; |
| 50 | + f.write_all(content.as_slice())?; |
| 51 | + Ok(()) |
| 52 | +} |
0 commit comments