Bazel:在py_library中从python文件创建py_binary

时间:2016-04-14 13:20:39

标签: python bazel

Bazel构建系统的新功能。

我想从py_library中的文件创建一个py_binary,它是从http_archive创建的。

目前我有:

WORKSPACE

new_http_archive(
    name = "cpplint_archive",
    url = "https://pypi.python.org/packages/source/c/cpplint/cpplint-1.2.2.tar.gz",
    sha256 = "b2979ff630299293f23c52096e408f2b359e2e26cb5cdf24aed4ce53e4293468",
    build_file = "cpplint.BUILD",
    strip_prefix = "cpplint-1.2.2"
)

cpplint.BUILD

py_library(
    name = "cpplint",
    srcs = glob(["*.py"]),
    visibility = ['//visibility:public']
)

src/BUILD

py_binary(
    name = "lint",
    main = ":cpplint/cpplint.py",
    srcs = [":cpplint/cpplint.py"],
    deps = [
        "@cpplint_archive//:cpplint"
    ]
)

srcs中的路径是一个主要的错误,给出了#34;没有这样的包&c; cpplint / cpplint.py'"当我跑bazel run src/lint时。我无法弄清楚如何引用库中包含的文件。

1 个答案:

答案 0 :(得分:2)

您可以将py_binary规则直接放入cpplint.BUILD

py_binary(
    name = "cpplint",
    srcs = ["cpplint.py"],
)

然后像这样构建它:

$ bazel build @cpplint_archive//:cpplint
INFO: Found 1 target...
Target @cpplint_archive//:cpplint up-to-date:
  bazel-bin/external/cpplint_archive/cpplint
INFO: Elapsed time: 2.327s, Critical Path: 0.01s

如果您确实希望py_binary规则位于主存储库中,您可以执行以下操作:

cpplint.BUILD

exports_files(["cpplint.py"])

BUILD

py_binary(
    name = "cpplint",
    srcs = ["@cpplint_archive//:cpplint.py"],
)

但从其他软件包中提取文件通常不太好。