简体   繁体   English

如何在 bazel 中查询存储库规则?

[英]How to query repository rule in bazel?

I'm trying to translate my company's project from legacy build tool to bazel.我正在尝试将我公司的项目从遗留构建工具转换为 bazel。 Now I'm facing this problem and searched a lot, but unfortunately, I haven't had a clue so far.现在我正面临这个问题并进行了很多搜索,但不幸的是,到目前为止我还没有线索。

Here's the thing:事情是这样的:

For compliance with open source audit, we must provide a list of open-source software which are built into our binary.为了遵守开源审计,我们必须提供一份内置在我们的二进制文件中的开源软件列表。 As external dependencies are introduced by repository rules, my intuitive thought is to query these rules and get the URLs.由于存储库规则引入了外部依赖,我的直觉是查询这些规则并获取 URL。 However, subcommand query/cquery hasn't provided such functionality yet AFAIK, it can print rule/target/buildfiles but no repository rules nor their attributes.但是,据我所知,子命令 query/cquery 还没有提供这样的功能,它可以打印 rule/target/buildfiles 但没有存储库规则或它们的属性。

Is there a way that I can gather such information from repository rules in WORKSPACE?有没有办法可以从 WORKSPACE 的存储库规则中收集此类信息? It's not viable to do it manually as there are thousands of projects in my company and the dependencies also change frequently.手动完成是不可行的,因为我公司有成千上万的项目,而且依赖关系也经常变化。

For example, a workspace rule:例如,工作区规则:

http_archive(
    name = "testrunner",
    urls = ["https://github.com/testrunner/v2.zip"],
    sha256 = "..."
)

This dependency is used by a rule named "my_target", so what i expected is that the dependency could be queried like this:此依赖项由名为“my_target”的规则使用,所以我期望可以像这样查询依赖项:

> bazel queryExtDep my_target
External Dependency of my_target: name->testrunner, urls = "https://github.com/testrunner/v2.zip"

--experimental_repository_resolved_file will give you all that information in a single Starlark file, which you can easily process with Starlark or Python etc to extract the information you're looking for. --experimental_repository_resolved_file将在单个 Starlark 文件中为您提供所有信息,您可以使用 Starlark 或 Python 等轻松处理该文件以提取您要查找的信息。

The resolved file looks something like this:解析后的文件看起来像这样:

resolved = [
    ...,
    {
        "original_rule_class": "@bazel_tools//tools/build_defs/repo:git.bzl%git_repository",
        "original_attributes": {
            "name": "com_google_protobuf",
            "remote": "https://github.com/google/protobuf",
            "branch": "master"
        },
        "repositories": [
            {
                "rule_class": "@bazel_tools//tools/build_defs/repo:git.bzl%git_repository",
                "attributes": {
                    "remote": "https://github.com/google/protobuf",
                    "commit": "78ba021b846e060d5b8f3424259d30a1f3ae4eef",
                    "shallow_since": "2018-02-07",
                    "init_submodules": False,
                    "verbose": False,
                    "strip_prefix": "",
                    "patches": [],
                    "patch_tool": "patch",
                    "patch_args": [
                        "-p0"
                    ],
                    "patch_cmds": [],
                    "name": "com_google_protobuf"
                }
            }
        ]
    }
]

This includes the original attributes, which is where that URL you're looking for is.这包括原始属性,这就是您要查找的 URL 所在的位置。 It also includes any additional information returned by the repository rule (ie for git_repository, the actual commit a given ref refers to).它还包括存储库规则返回的任何附加信息(即对于 git_repository,给定 ref 引用的实际提交)。

I got that example from blog post introducing that flag , which also has some more background.我从介绍那个标志的博客文章中得到了那个例子,它也有更多的背景。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM