Skip to content

Commit 06e9fc1

Browse files
johnynekittaiz
authored andcommitted
Add a ThriftInfo provider (#464)
* Add a ThriftInfo provider * minor cleanup * address review comments
1 parent 1058cce commit 06e9fc1

File tree

2 files changed

+36
-28
lines changed

2 files changed

+36
-28
lines changed

thrift/thrift.bzl

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
_thrift_filetype = FileType([".thrift"])
44

5+
ThriftInfo = provider(
6+
fields=[
7+
"srcs", # The source files in this rule
8+
"transitive_srcs", # the transitive version of the above
9+
"external_jars", # external jars of thrift files
10+
"transitive_external_jars" # transitive version of the above
11+
])
12+
513
def _common_prefix(strings):
614
pref = None
715
for s in strings:
@@ -87,30 +95,29 @@ rm {out}.contents
8795
jarfiles.append(depset(jar.files))
8896
transitive_external_jars = depset(transitive = jarfiles)
8997

90-
return struct(
91-
thrift = struct(
92-
srcs = ctx.outputs.libarchive,
93-
transitive_srcs = transitive_srcs,
94-
external_jars = ctx.attr.external_jars,
95-
transitive_external_jars = transitive_external_jars,
96-
),
97-
)
98+
return [
99+
ThriftInfo(
100+
srcs = ctx.outputs.libarchive,
101+
transitive_srcs = transitive_srcs,
102+
external_jars = ctx.attr.external_jars,
103+
transitive_external_jars = transitive_external_jars,
104+
)]
98105

99-
def _collect_thrift_attr_depsets(targets, attr):
106+
def _collect_thrift_srcs(targets):
100107
ds = []
101108
for target in targets:
102-
ds.append(getattr(target.thrift, attr))
109+
ds.append(target[ThriftInfo].transitive_srcs)
103110
return ds
104111

105-
def _collect_thrift_srcs(targets):
106-
return _collect_thrift_attr_depsets(targets, "transitive_srcs")
107-
108112
def _collect_thrift_external_jars(targets):
109-
return _collect_thrift_attr_depsets(targets, "transitive_external_jars")
113+
ds = []
114+
for target in targets:
115+
ds.append(target[ThriftInfo].transitive_external_jars)
116+
return ds
110117

111118
def _valid_thrift_deps(targets):
112119
for target in targets:
113-
if not hasattr(target, "thrift"):
120+
if not ThriftInfo in target:
114121
fail("thrift_library can only depend on thrift_library", target)
115122

116123
# Some notes on the raison d'etre of thrift_library vs. code gen specific

twitter_scrooge/twitter_scrooge.bzl

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ load("//scala:scala.bzl",
55
"collect_srcjars",
66
"collect_jars")
77

8+
load("//thrift:thrift.bzl", "ThriftInfo")
9+
810
_jar_filetype = FileType([".jar"])
911

1012
def twitter_scrooge():
@@ -57,8 +59,8 @@ def twitter_scrooge():
5759
def _collect_transitive_srcs(targets):
5860
r = []
5961
for target in targets:
60-
if hasattr(target, "thrift"):
61-
r.append(target.thrift.transitive_srcs)
62+
if ThriftInfo in target:
63+
r.append(target[ThriftInfo].transitive_srcs)
6264
return depset(transitive = r)
6365

6466
def _collect_owned_srcs(targets):
@@ -73,13 +75,12 @@ def _collect_owned_srcs(targets):
7375
def _collect_external_jars(targets):
7476
r = []
7577
for target in targets:
76-
if hasattr(target, "thrift"):
77-
thrift = target.thrift
78-
if hasattr(thrift, "external_jars"):
79-
for jar in thrift.external_jars:
80-
r.append(depset(_jar_filetype.filter(jar.files)))
81-
r.append(depset(_jar_filetype.filter(thrift.transitive_external_jars)))
82-
return depset(transitive = r)
78+
if ThriftInfo in target:
79+
thrift = target[ThriftInfo]
80+
for jar in thrift.external_jars:
81+
r.extend(_jar_filetype.filter(jar.files))
82+
r.extend(_jar_filetype.filter(thrift.transitive_external_jars))
83+
return depset(r)
8384

8485
def collect_extra_srcjars(targets):
8586
srcjar = []
@@ -92,11 +93,11 @@ def collect_extra_srcjars(targets):
9293
return depset(srcjar, transitive = srcjars)
9394

9495
def _collect_immediate_srcs(targets):
95-
r = []
96+
srcs = []
9697
for target in targets:
97-
if hasattr(target, "thrift"):
98-
r.append(depset([target.thrift.srcs]))
99-
return depset(transitive = r)
98+
if ThriftInfo in target:
99+
srcs.append(target[ThriftInfo].srcs)
100+
return depset(srcs)
100101

101102
def _assert_set_is_subset(want, have):
102103
missing = []

0 commit comments

Comments
 (0)