Skip to content

Commit 07fed16

Browse files
krallinfacebook-github-bot
authored andcommitted
redo the way we do protobuf dependencies
Summary: Right now, if you add a dependency to a protobuf, then you have to go and update all the dependents and: - Make an env var exposing that dependency - Update the build script to pick it up The latter is a bit unavoidable (because we neeed to define the `extern_path` anyway), but the former is a bit overly verbose and pointless. This diff fixes that by introducing a UDR that represents a set of proto source files, and then you pass that as input when adding your rule. There was a previous attempt at doing this with `export_file_with`, but that only works if the files are next to each other (in which case we add the parent dir of one the files to the search path, and now we can find both, but it does not work if the files aren't next to each other). UX-wise, this would all work a lot better if instead a macro this was a UDR, but for now this gets a bit closer by forcing you to define a dependency tree of your .proto files. Note: I did keep the old way of passing individual .proto files because it turns out a bunch of things that are not Buck use this (oops): https://www.internalfb.com/code/search?q=-filepath%3Afbcode%2Fbuck2%20repo%3Aall%20fbcode%2F%2Fbuck2%3Aproto_defs.bzl Reviewed By: IanChilds Differential Revision: D73581679 fbshipit-source-id: ed1f97058a111a29cb9eba3eb3f690053d138fd4
1 parent 00c02e4 commit 07fed16

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

shim/buck2/proto_defs.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77

88
load(
99
"@shim//:shims.bzl",
10+
_proto_srcs = "proto_srcs",
1011
_rust_protobuf_library = "rust_protobuf_library",
1112
)
1213

1314
rust_protobuf_library = _rust_protobuf_library
15+
proto_srcs = _proto_srcs

shim/shims.bzl

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -310,11 +310,12 @@ def rust_protobuf_library(
310310
name,
311311
srcs,
312312
build_script,
313-
protos,
314-
build_env = None,
315-
deps = [],
313+
protos = None, # Pass a list of files. Thye'll be placed in the cwd. Prefer using proto_srcs.
314+
deps = None,
316315
test_deps = None,
317-
doctests = True):
316+
doctests = True,
317+
build_env = None,
318+
proto_srcs = None): # Use a proto_srcs() target, path is exposed as BUCK_PROTO_SRCS.
318319
build_name = name + "-build"
319320
proto_name = name + "-proto"
320321

@@ -335,10 +336,12 @@ def rust_protobuf_library(
335336
"PROTOC_INCLUDE": "$(location shim//third-party/proto:google_protobuf)",
336337
},
337338
)
339+
if proto_srcs:
340+
build_env["BUCK_PROTO_SRCS"] = "$(location {})".format(proto_srcs)
338341

339342
prelude.genrule(
340343
name = proto_name,
341-
srcs = protos + [
344+
srcs = (protos or []) + [
342345
"shim//third-party/proto:google_protobuf",
343346
],
344347
out = ".",
@@ -361,6 +364,26 @@ def rust_protobuf_library(
361364
] + (deps or []),
362365
)
363366

367+
ProtoSrcsInfo = provider(fields = ["srcs"])
368+
369+
def _proto_srcs_impl(ctx):
370+
srcs = {src.basename: src for src in ctx.attrs.srcs}
371+
for dep in ctx.attrs.deps:
372+
for src in dep[ProtoSrcsInfo].srcs:
373+
if src.basename in srcs:
374+
fail("Duplicate src:", src.basename)
375+
srcs[src.basename] = src
376+
out = ctx.actions.copied_dir(ctx.attrs.name, srcs)
377+
return [DefaultInfo(default_output = out), ProtoSrcsInfo(srcs = srcs.values())]
378+
379+
proto_srcs = rule(
380+
impl = _proto_srcs_impl,
381+
attrs = {
382+
"deps": attrs.list(attrs.dep(), default = []),
383+
"srcs": attrs.list(attrs.source(), default = []),
384+
},
385+
)
386+
364387
def ocaml_binary(
365388
deps = [],
366389
visibility = ["PUBLIC"],

0 commit comments

Comments
 (0)