2017-06-14 13 views
0

Bazelを使用してThriftサービス定義用のPythonバインディングを生成しようとしています。私が言うことができた限り、これを行うための既存の.bzlは存在しませんので、私はここで私自身は多少です。私は過去に.bzlのルールを書いてきましたが、私がこのケースで実行している状況は異なります。BazelでThrift用Pythonバインディングを生成する方法

一般的な問題は、ビルドが開始される前にthriftコマンドの出力ファイルの名前がわからないことです。つまり、srcs属性を正しく設定したpy_libraryルールを生成できませんファイルの名前を持つ。私は出力ファイルが事前に.zipファイルを生成することで知られている例を追ってみようとしましたが、py_libraryルールは.pyファイルのみをsrcsとして許可します。

コードとBUILDファイルを生成するのにrepository_ruleを使用することが考えられますが、私が達成しようとしているのはストレッチのようなものではなく、サポートする必要があります。

答えて

0

これまでに誰かがこれを試みました。

ここで議論:ここ https://groups.google.com/forum/#!topic/bazel-dev/g3DVmhVhNZs

コード: https://github.com/wt/bazel_thrift

私はそこに開始します。

編集: 私はそこから始めました。私が望む限り私は得られなかった。 Bazelは、1つの入力によって生成された複数の出力を有するサポートするように拡張されているが、それはまだ非常に簡単にそれを許可しない、あたり:

groups.google.com/forum/#!topic/bazel-discuss/3WQhHm194yU

これとは関係なく、私は同じ問題を抱えているC++スティフトバインディングのために何かを試みました。 Javaの例では、ソースjarをビルドソースとして使用してこの問題を回避しましたが、これはうまくいきません。それを動作させるために、私は、リリーフジェネレータによって作成されるソースファイルのリストを渡しました。私はこれらのファイルをインプリで生成される出力として報告しました。それはうまくいくようです。ビルドする前に探しているファイルを知っておく必要があるという点で少し厄介ですが、うまくいきます。小さなプログラムにthiftファイルを読み込ませ、出力ファイルを決定させることも可能です。それはよかったですが、私は時間がありません。さらに、現在のアプローチは、あなたが生成するために探しているファイルを明示的に定義しているので、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, 
    ) 
+0

はい、私はちょうど明示的に生成されるべきファイルを記述について考えたのです。私はそれが動作することを知っているという事実にもかかわらず、私はその道を下っていませんでした。私はリポジトリルールを使うことについても考えていたが、それは恐ろしいようだ。最後に、私は生成されたファイルの '.zip'を生成し、ソースを含む' .zip'を解凍するスタブ '.py'を構築することを考えました。私はそれがうまくいくと確信していますが、これはハックのようです。 –

関連する問題