Skip to content

Commit

Permalink
gen-protos: add BUCK files
Browse files Browse the repository at this point in the history
This is needed to emit the `.rs` files into the right build directory in a
follow up diff to add `BUCK` files.

Signed-off-by: Austin Seipp <[email protected]>
  • Loading branch information
thoughtpolice committed Jun 25, 2024
1 parent efe6f51 commit b02838a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
1 change: 1 addition & 0 deletions BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
alias(name = name, actual = actual)
for (name, actual) in [
# (top-level name, fully-qualified target name)
("gen-protos", "//lib/gen-protos:gen-protos"),
]
]
21 changes: 21 additions & 0 deletions lib/gen-protos/BUCK
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

load("//buck/shims/jj.bzl", "jj")

jj.rust_binary(
name = 'gen-protos',
srcs = ['src/main.rs'],
env = {
'CARGO_MANIFEST_DIR': '$(location //lib:protos.pb)',
},
deps = [
# CARGO-SYNC-START: dependencies
'third-party//rust:prost-build',
# CARGO-SYNC-END
]
)

genrule(
name = 'protos.rs',
cmd = "$(exe :gen-protos)",
out = ".",
)
24 changes: 21 additions & 3 deletions lib/gen-protos/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit b02838a

Please sign in to comment.