2017-05-25 163 views
1

我试图创建一个自定义构建规则来从py_binary输出构建一个pip包。要创建我的pip包,我想调用一个shell脚本。 shell脚本通过从py_binary输出的运行文件创建zip文件来构建pip包。如何创建使用其他规则的runfiles路径的自定义bazel构建规则?

例如,假设我有

py_binary(
    name = "some_binary", 
    srcs = ["some_binary.py"], 
    srcs_version = "PY2AND3", 
) 

建立该规则会产生

bazel-bin/some_binary.runfiles 

我现在想创建一个自定义生成规则,将调用我的shell脚本的位置巴泽尔斌/ some_binary.runfiles

我试着创建一个宏

def build_pip_package(
    name, py_binary=None, setup_file=None): 
    """Create a pip package from a py_binary. 

    The source file should be a text file with python formatting i.e. 

    Args: 
    name: Name for the rule. 
    py_binary: Build rule producing the py_binary 
    setup_file: Build rule producing the setup.py file to use to produce 
     the package. 
    """ 
    output= "somefile" 
    native.genrule(
     name=name, 
     outs=[output], 
     cmd="echo $(location //:build_pip_package_script) " 
     + "--py_runfiles_path=$(locations %s)" % py_binary 
     + " --setup_file=$(location %s) " % setup_file, 
     tools=["//:build_pip_package_script"], 
     srcs=[setup_file, py_binary]) 

这最终调用我的shell脚本

bazel-out/local-fastbuild/bin/some_binary/model_train some_binary.py 

我如何调用我的脚本与对应some_binary目标runfiles目录的位置。

回答

5

如果将py_binary放入工具属性中而不是srcs中,bazel将包含runfiles树。您可以通过"$(location %s).runfiles" % py_binary