From 80ebd71a8af942b9de6c4598bdc3ab888d3e4a17 Mon Sep 17 00:00:00 2001 From: Austin Seipp Date: Sun, 12 May 2024 09:51:39 -0500 Subject: [PATCH] gen-protos: special case path for `cfg(buck_build)` This is needed to emit the `.rs` files into the right build directory in a follow up diff to add `BUCK` files. --- lib/gen-protos/src/main.rs | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/lib/gen-protos/src/main.rs b/lib/gen-protos/src/main.rs index c9b8d4f017..26e381765e 100644 --- a/lib/gen-protos/src/main.rs +++ b/lib/gen-protos/src/main.rs @@ -23,11 +23,29 @@ fn main() -> Result<()> { "working_copy.proto", ]; - let root = Path::new(env!("CARGO_MANIFEST_DIR")).parent().unwrap(); - let protos_dir = root.join("src").join("protos"); + let root = Path::new(env!("CARGO_MANIFEST_DIR")); + let protos_dir = if cfg!(buck_build) { + // NOTE (aseipp, buck_build): the cargo build allows `../src/protos` + // like below, but this is really awkward in the buck2 design since + // directory names are autogenerated. use `src/protos` instead. + root.join("src").join("protos") + } else { + root.parent().unwrap().join("src").join("protos") + }; + + let out_dir = if cfg!(buck_build) { + // NOTE (aseipp, buck_build): under `cfg(buck_build)` the genrule() that + // invokes this program will automatically set $OUT, so always use that + // and fail otherwise. + std::env::var("OUT") + .map(|s| Path::new(&s).to_path_buf()) + .expect("OUT environment variable not set during cfg(buck_build)") + } else { + protos_dir.clone() + }; prost_build::Config::new() - .out_dir(&protos_dir) + .out_dir(&out_dir) .include_file("mod.rs") // For old protoc versions. 3.12.4 needs this, but 3.21.12 doesn't. .protoc_arg("--experimental_allow_proto3_optional")