如何从规则 Args map_each 函数中获取 Bazel 输出基数?

发布于 2025-01-18 08:04:21 字数 855 浏览 3 评论 0原文

我正在编写一个 Bazel 规则,它处理包含大量 jar 文件的输入 depsets。我的目标是获取用于启动应用程序的 jar 的完整(绝对)路径。

我使用 java_binary 作为初始应用程序。但是,应用程序本身随后会动态地将其他 jar 加载到 JVM 中。我需要为该应用程序提供这些 jar 的绝对路径。

由于 depset 的处理成本非常高,我想将其移至执行阶段。这需要将 Argsmap_each 函数结合使用,例如这:

args.add_joined("--jars", dependencies, join_with = ",", map_each = _expand_jars)

我现在陷入了如何将 jar 路径变成绝对路径的困境。在旧版本(在分析阶段运行)中,我使用规则属性传入路径。这是必需的,因为(由于某种原因)引导应用程序无法访问 external/ 目录中的 jar。在映射函数中,我将其硬编码为 "bazel-myworkspace/../../",我想将其删除。它确实遵循符号链接进入执行根目录,从那里我可以到达输出库。

def _expand_jars(file):
    if file.path.startswith("external/"):
        return "bazel-myworkspace/../../" + file.path
    else:
        return file.path

有什么想法吗?

I'm writing a Bazel rule which processes an input depsets with lots of jar files in it. My goal is to get the full (absolute) path of the jars for launching an application.

I use java_binary for the initial application. However, the application itself later then dynamically loads additional jars into the JVM. I need to give that application the absolute path to those jars.

As the depset is pretty expensive to process I want to move that into the execution phase. This requires the use of Args in combination with a map_each function like this:

args.add_joined("--jars", dependencies, join_with = ",", map_each = _expand_jars)

I'm now stuck with how to turn the jar path into an absolute path. In an older version (which was running in the analysis phase) I passed in the path using a rule attribute. This is required because (for some reason) the bootstrap application couldn't access the jars in the external/ directory. In the mapping function I have it hard coded to "bazel-myworkspace/../../", which I'd like to get rid of. It does follow the symlink into the exec root and from there I'm able to get to the output base.

def _expand_jars(file):
    if file.path.startswith("external/"):
        return "bazel-myworkspace/../../" + file.path
    else:
        return file.path

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文