Skip to content

Commit f34ca28

Browse files
authored
Fix for thrift rule issue that manifests with remote repos (bazel-contrib#83)
* add some debug * improve prefix operation
1 parent 5a6f332 commit f34ca28

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

thrift/thrift.bzl

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,40 @@
22

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

5+
def _common_prefix(strings):
6+
pref = None
7+
for s in strings:
8+
if pref == None:
9+
pref = s
10+
elif s.startswith(pref):
11+
pass
12+
else:
13+
tmp_pref = pref
14+
for end in range(0, len(pref) + 1):
15+
test = pref[0:end]
16+
if s.startswith(test):
17+
tmp_pref = test
18+
pref = tmp_pref
19+
return pref
20+
521
def _thrift_library_impl(ctx):
622
prefix = ctx.attr.absolute_prefix
23+
src_paths = [f.path for f in ctx.files.srcs]
24+
if len(src_paths) <= 0:
25+
fail("we require at least one thrift file in a target")
26+
727
jarcmd = "{jar} cMf {out} -C {out}_tmp ."
828
if prefix != '':
9-
jarcmd = "{{jar}} cMf {{out}} -C {{out}}_tmp/{prefix} .".format(prefix=prefix)
29+
common_prefix = _common_prefix(src_paths)
30+
pos = common_prefix.find(prefix)
31+
if pos < 0:
32+
fail("could not find prefix: {prefix} in the common prefix: {common_prefix}".format(
33+
prefix = prefix,
34+
common_prefix = common_prefix))
35+
else:
36+
endpos = pos + len(prefix)
37+
actual_prefix = common_prefix[0:endpos]
38+
jarcmd = "{{jar}} cMf {{out}} -C {{out}}_tmp/{pf} .".format(pf=actual_prefix)
1039

1140
_valid_thrift_deps(ctx.attr.deps)
1241
# We move the files and touch them so that the output file is a purely deterministic
@@ -23,14 +52,15 @@ rm -rf {out}_tmp"""
2352

2453
cmd = cmd.format(out=ctx.outputs.libarchive.path,
2554
jar=ctx.file._jar.path)
55+
2656
ctx.action(
2757
inputs = ctx.files.srcs +
2858
ctx.files._jar +
2959
ctx.files._jdk, # We need _jdk to even run _jar. Depending on _jar is not enough with sandbox
3060
outputs = [ctx.outputs.libarchive],
3161
command = cmd,
3262
progress_message = "making thrift archive %s" % ctx.label,
33-
arguments = [f.path for f in ctx.files.srcs],
63+
arguments = src_paths,
3464
)
3565

3666
transitive_srcs = _collect_thrift_srcs(ctx.attr.deps)

0 commit comments

Comments
 (0)