From ef4ca6a66f40b8807b1b48fc6a7292696cd7e1af Mon Sep 17 00:00:00 2001 From: Oscar Boykin Date: Wed, 27 Jul 2016 15:38:00 -1000 Subject: [PATCH 1/2] add some debug --- thrift/thrift.bzl | 3 +++ 1 file changed, 3 insertions(+) diff --git a/thrift/thrift.bzl b/thrift/thrift.bzl index ae15e3c79..8518b1d30 100644 --- a/thrift/thrift.bzl +++ b/thrift/thrift.bzl @@ -23,6 +23,9 @@ rm -rf {out}_tmp""" cmd = cmd.format(out=ctx.outputs.libarchive.path, jar=ctx.file._jar.path) + print(cmd) + print([f.path for f in ctx.files.srcs]) + ctx.action( inputs = ctx.files.srcs + ctx.files._jar + From 3f786044d31dc701fd4f0fbf67ee278e9fda4ded Mon Sep 17 00:00:00 2001 From: Oscar Boykin Date: Thu, 28 Jul 2016 11:13:37 -1000 Subject: [PATCH 2/2] improve prefix operation --- thrift/thrift.bzl | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/thrift/thrift.bzl b/thrift/thrift.bzl index 8518b1d30..15ae8494f 100644 --- a/thrift/thrift.bzl +++ b/thrift/thrift.bzl @@ -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): + 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 @@ -23,8 +52,6 @@ rm -rf {out}_tmp""" cmd = cmd.format(out=ctx.outputs.libarchive.path, jar=ctx.file._jar.path) - print(cmd) - print([f.path for f in ctx.files.srcs]) ctx.action( inputs = ctx.files.srcs + @@ -33,7 +60,7 @@ rm -rf {out}_tmp""" 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)