我正在尝试使用Bazel为Thrift服务定义生成Python绑定。据我所知,现在没有.bzl
这样做,所以我在这里有点自己。我过去曾写过.bzl
规则,但在这种情况下我遇到的情况则不同。
一般问题是我在构建开始之前不知道thrift
命令中输出文件的名称,这意味着我无法使用{{生成py_library
规则1}}属性设置正确,因为我没有文件的名称。我试图通过生成srcs
文件来提前知道输出文件的示例,但.zip
规则只允许py_library
个文件作为srcs,所以这不会工作。
我唯一能想到的就是使用.py
来生成代码和repository_rule
文件,但我想要完成的事情看起来并不是很有意义。得到支持。
答案 0 :(得分:0)
之前有人试过这个。
在这里讨论: https://groups.google.com/forum/#!topic/bazel-dev/g3DVmhVhNZs
代码在这里: https://github.com/wt/bazel_thrift
我会从那里开始。
编辑: 我从那里开始。我没有达到我希望的程度。 Bazel正在扩展以支持通过一个输入生成多个输出,但它还不能很容易地实现,按:
groups.google.com/forum /#!话题/巴泽勒-讨论/ 3WQhHm194yU
无论如何,我确实为C ++ thrift绑定尝试了一些东西,它有同样的问题。 Java示例通过使用源jar作为构建源来解决这个问题,这对我们来说不起作用。为了使它工作,我传入了我关心的源文件列表,它将由thrift生成器创建。然后我将这些文件报告为将在impl中生成的输出。这似乎有效。这有点令人讨厌,因为你必须在构建之前知道你正在寻找什么文件,但它确实有效。也可以让一个小程序读取thift文件并确定它将生成的输出文件。那会更好,但我没有时间。另外,当前的方法很好,因为它明确定义了你要生成thrift的文件,这使得BUILD文件对于像我这样的新手来说更容易理解。
首先通过一些代码,也许我会清理它并将其作为补丁提交(可能不是):
###########
# CPP gen
###########
# Create Generated cpp source files from thrift idl files.
#
def _gen_thrift_cc_src_impl(ctx):
out = ctx.outputs.outs
if not out:
# empty set
# nothing to do, no inputs to build
return DefaultInfo(files=depset(out))
# Used dir(out[0]) to see what
# we had available in the object.
# dirname attribute tells us the directory
# we should be putting stuff in, works nicely.
# ctx.genfile_dir is not the output directory
# when called as an external repository
target_genfiles_root = out[0].dirname
thrift_includes_root = "/".join(
[ target_genfiles_root, "thrift_includes"])
gen_cpp_dir = "/".join([target_genfiles_root,"." ])
commands = []
commands.append(_mkdir_command_string(target_genfiles_root))
commands.append(_mkdir_command_string(thrift_includes_root))
thrift_lib_archive_files = ctx.attr.thrift_library._transitive_archive_files
for f in thrift_lib_archive_files:
commands.append(
_tar_extract_command_string(f.path, thrift_includes_root))
commands.append(_mkdir_command_string(gen_cpp_dir))
thrift_lib_srcs = ctx.attr.thrift_library.srcs
for src in thrift_lib_srcs:
commands.append(_thrift_cc_compile_command_string(
thrift_includes_root, gen_cpp_dir, src))
inputs = (
list(thrift_lib_archive_files) + thrift_lib_srcs )
ctx.action(
inputs = inputs,
outputs = out,
progress_message = "Generating CPP sources from thift archive %s" % target_genfiles_root,
command = " && ".join(commands),
)
return DefaultInfo(files=depset(out))
thrift_cc_gen_src= rule(
_gen_thrift_cc_src_impl,
attrs = {
"thrift_library": attr.label(
mandatory=True, providers=['srcs', '_transitive_archive_files']),
"outs" : attr.output_list(mandatory=True, non_empty=True),
},output_to_genfiles = True,
)
# wraps cc_library to generate a library from one or more .thrift files
# provided as a thrift_library bundle.
#
# Generates all src and hdr files needed, but you must specify the expected
# files. This is a bug in bazel: https://groups.google.com/forum/#!topic/bazel-discuss/3WQhHm194yU
#
# Instead of src and hdrs, requires: cpp_srcs and cpp_hdrs. These are required.
#
# Takes:
# name: The library name, like cc_library
#
# thrift_library: The library of source .thrift files from which our
# code will be built from.
#
# cpp_srcs: The expected source that will be generated and built. Passed to
# cc_library as src.
#
# cpp_hdrs: The expected header files that will be generated. Passed to
# cc_library as hdrs.
#
# Rest of options are documented in native.cc_library
#
def thrift_cc_library(name, thrift_library,
cpp_srcs=[],cpp_hdrs=[],
build_skeletons=False,
deps=[], alwayslink=0, copts=[],
defines=[], include_prefix=None,
includes=[], linkopts=[],
linkstatic=0, nocopts=None,
strip_include_prefix=None,
textual_hdrs=[],
visibility=None):
# from our thrift_library tarball source bundle,
# create a generated cpp source directory.
outs = []
for src in cpp_srcs:
outs.append("//:"+src)
for hdr in cpp_hdrs:
outs.append("//:"+hdr)
thrift_cc_gen_src(
name = name + 'cc_gen_src',
thrift_library = thrift_library,
outs = outs,
)
# Then make the library for the given name.
native.cc_library(
name = name,
deps = deps,
srcs = cpp_srcs,
hdrs = cpp_hdrs,
alwayslink = alwayslink,
copts = copts,
defines=defines,
include_prefix=include_prefix,
includes=includes,
linkopts=linkopts,
linkstatic=linkstatic,
nocopts=nocopts,
strip_include_prefix=strip_include_prefix,
textual_hdrs=textual_hdrs,
visibility=visibility,
)