Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions thrift/thrift.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,40 @@

_thrift_filetype = FileType([".thrift"])

def _common_prefix(strings):
pref = None
for s in strings:
if pref == None:
pref = s
elif s.startswith(pref):
pass
else:
tmp_pref = pref
for end in range(0, len(pref) + 1):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no while in skylark. :(

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(skylark may be a total language... No recursion, only for loops on collections, I think everything terminates).

If you consider any method error to be a type error (which a better compiler would have caught, I think it may be total....)

test = pref[0:end]
if s.startswith(test):
tmp_pref = test
pref = tmp_pref
return pref

def _thrift_library_impl(ctx):
prefix = ctx.attr.absolute_prefix
src_paths = [f.path for f in ctx.files.srcs]
if len(src_paths) <= 0:
fail("we require at least one thrift file in a target")

jarcmd = "{jar} cMf {out} -C {out}_tmp ."
if prefix != '':
jarcmd = "{{jar}} cMf {{out}} -C {{out}}_tmp/{prefix} .".format(prefix=prefix)
common_prefix = _common_prefix(src_paths)
pos = common_prefix.find(prefix)
if pos < 0:
fail("could not find prefix: {prefix} in the common prefix: {common_prefix}".format(
prefix = prefix,
common_prefix = common_prefix))
else:
endpos = pos + len(prefix)
actual_prefix = common_prefix[0:endpos]
jarcmd = "{{jar}} cMf {{out}} -C {{out}}_tmp/{pf} .".format(pf=actual_prefix)

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

cmd = cmd.format(out=ctx.outputs.libarchive.path,
jar=ctx.file._jar.path)

ctx.action(
inputs = ctx.files.srcs +
ctx.files._jar +
ctx.files._jdk, # We need _jdk to even run _jar. Depending on _jar is not enough with sandbox
outputs = [ctx.outputs.libarchive],
command = cmd,
progress_message = "making thrift archive %s" % ctx.label,
arguments = [f.path for f in ctx.files.srcs],
arguments = src_paths,
)

transitive_srcs = _collect_thrift_srcs(ctx.attr.deps)
Expand Down