2017-06-14 75 views
0

我試圖生成使用巴澤勒舊貨服務定義Python綁定。據我所知,沒有現成的.bzl這樣做,所以我有點在我自己這裏。我在過去寫過.bzl規則,但在這種情況下我遇到的情況是不同的。如何生成Python綁定節儉在巴澤爾

一般的問題是,我不知道從thrift命令的輸出文件的名稱的構建開始,這意味着,因爲我不,我不能生成py_library規則與srcs屬性設置正確之前有文件的名稱。我試圖按照例子,其中輸出文件由生成.zip文件的方式提前知道的,但py_library規則只允許.py文件作爲索馬里紅新月所以這是行不通的。

我能想到的唯一的事情就是使用repository_rule來生成代碼和BUILD文件,但是我試圖完成的東西看起來沒有什麼特別之處,應該得到支持。

回答

0

有人嘗試過這一點。

這裏討論: https://groups.google.com/forum/#!topic/bazel-dev/g3DVmhVhNZs

代碼這裏: https://github.com/wt/bazel_thrift

我就從那裏開始。

編輯: 我在那裏開始。我沒有達到我所希望的程度。巴澤勒被擴展,以支持具有由一個輸入而產生多個輸出,但它不允許很容易只是還沒有,每:

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

無論如何,我的確嘗試了一些C++ thrift綁定,它們有相同的問題。 Java示例通過使用源代碼庫作爲構建源解決了這個問題,這對我們來說不起作用。爲了使它工作,我通過了我關心的源文件列表,這些文件將由節儉生成器創建。然後我將這些文件報告爲將在impl中生成的輸出。這似乎工作。這有點令人討厭,因爲在構建之前,你必須知道你正在尋找哪些文件,但它確實有效。也可以讓一個小程序讀取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',並構建一個存根'.py'來解壓包含源文件的'.zip'。我敢肯定它會起作用,但再次,這似乎是一個破解。 –