2
2
3
3
_thrift_filetype = FileType ([".thrift" ])
4
4
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
+
5
21
def _thrift_library_impl (ctx ):
6
22
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
+
7
27
jarcmd = "{jar} cMf {out} -C {out}_tmp ."
8
28
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 )
10
39
11
40
_valid_thrift_deps (ctx .attr .deps )
12
41
# We move the files and touch them so that the output file is a purely deterministic
@@ -23,14 +52,15 @@ rm -rf {out}_tmp"""
23
52
24
53
cmd = cmd .format (out = ctx .outputs .libarchive .path ,
25
54
jar = ctx .file ._jar .path )
55
+
26
56
ctx .action (
27
57
inputs = ctx .files .srcs +
28
58
ctx .files ._jar +
29
59
ctx .files ._jdk , # We need _jdk to even run _jar. Depending on _jar is not enough with sandbox
30
60
outputs = [ctx .outputs .libarchive ],
31
61
command = cmd ,
32
62
progress_message = "making thrift archive %s" % ctx .label ,
33
- arguments = [ f . path for f in ctx . files . srcs ] ,
63
+ arguments = src_paths ,
34
64
)
35
65
36
66
transitive_srcs = _collect_thrift_srcs (ctx .attr .deps )
0 commit comments