bazel - 如何创建使用另一个规则的运行文件路径的自定义 bazel 构建规则?

标签 bazel

我正在尝试创建自定义构建规则以从 py_binary 输出构建 pip 包。要创建我的 pip 包,我想调用一个 shell 脚本。 shell 脚本通过从 py_binary 输出的运行文件创建一个 zip 文件来构建一个 pip 包。

例如假设我有

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

建立这个规则产生

bazel-bin/some_binary.runfiles

我现在想创建一个自定义构建规则,该规则将使用位置 bazel-bin/some_binary.runfiles 调用我的 shell 脚本

我尝试创建一个宏

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 目录的位置来调用我的脚本。

最佳答案

如果您将 py_binary 放在 tools 属性而不是 srcs 中,bazel 将包含运行文件树。您可以通过 "$(location %s).runfiles"% py_binary

访问它

关于bazel - 如何创建使用另一个规则的运行文件路径的自定义 bazel 构建规则?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44171570/

相关文章:

Bazel- lark Action 可以读取命令行标志吗(strict_java_deps)

bazel - 使用 cc_common API 的自定义 C++ 规则

tensorflow - 错误 : Config value cuda is not defined in any . rc 文件信息:调用 ID:1faa4ce7-96be-42d3-80bc-10cac6a8f3a7

go - Bazel Golang 综合覆盖率报告

python - 为 GPU 编译 Tensor Flow 示例自定义操作

jenkins - 如何将Bazel工具与CI服务器Jenkins集成。这样我就可以自动化构建和一些构建后的 Action 。

c++ - 使用 bazel 链接到版本化的预构建库

go - 巴泽尔去嵌入数据 "could not embed"

git - 为什么公开可见的 Bazel ProtoBuf 目标是 'not declared'

c++ - 尝试使用 tensorflow 的 C++ API 时出错