gpt4 book ai didi

Bazel 选择在 ctx.file 中失败

转载 作者:行者123 更新时间:2023-12-01 09:38:31 27 4
gpt4 key购买 nike

我正在尝试根据运行 bazel 的操作系统指定构建条件,因此在我的 .bzl 脚本中我有一个规则,该规则从外部源创建所有模拟链接并编写一个构建文件(使用 ctx.file) ,我在其中声明了所有导入和库,并且在其中我想添加选择功能。但是,当我构建时收到此错误消息:

ERROR: no such package '@maya_repo//': Traceback (most recent call last):
File "/var/tmp/doNotRemove/mdilena_plugins/MayaMathNodes/src/maya.bzl", line 149
ctx.file("BUILD", _BUILD_STRUC.format(maya_...))
File "/var/tmp/doNotRemove/mdilena_plugins/MayaMathNodes/src/maya.bzl", line 149, in ctx.file
_BUILD_STRUC.format(maya_dir = maya_dir)
Invalid character '[' inside replacement field

下面是我的代码示例以及我要实现的目标:

_BUILD_STRUC = \
"""
# Windows imports
cc_import(
name = "Foundation-win",
interface_library = "{maya_dir}/lib/Foundation.lib",
shared_library = "{maya_dir}/bin/Foundation.dll",
)

cc_import(
name = "OpenMaya-win",
interface_library = "{maya_dir}/lib/OpenMaya.lib",
shared_library = "{maya_dir}/bin/OpenMaya.dll",
)

# Linux imports
cc_import(
name = "Foundation-lnx",
shared_library = "{maya_dir}/bin/Foundation.so",
)

cc_import(
name = "OpenMaya-lnx",
shared_library = "{maya_dir}/bin/OpenMaya.so",
)

cc_library(
name = "Foundation",
deps = select({
"@bazel_tools//src/conditions:windows": [":Foundation-win"],
"//conditions:default": [":Foundation-lnx"],
}),
includes = ["{maya_dir}/include"],
visibility = ["//visibility:public"],
)

cc_library(
name = "OpenMaya",
deps = select({
"@bazel_tools//src/conditions:windows": [":OpenMaya-win"],
"//conditions:default": [":OpenMaya-lnx"],
}),
includes = ["{maya_dir}/include"],
visibility = ["//visibility:public"],
)
"""

def _impl(ctx):
maya_src = ctx.os.environ["MAYA_LOCATION"]
maya_ver = ctx.os.environ["MAYA_VERSION"]
maya_dir = "maya{}".format(maya_ver)
ctx.symlink(maya_src, maya_dir)
ctx.file("BUILD", _BUILD_STRUC.format(maya_dir=maya_dir))


link_maya = repository_rule(
implementation = _impl,
local = True,
environ = ["MAYA_LOCATION"],
)

有人知道为什么会这样吗?我看了selectconfigurable attributes文档,似乎这就是使用它的方式;我想知道是我做错了什么还是某处有错误。

感谢您的帮助!

EDIT:

looks like Bazel really doesn't like using select inside a ctx.file, I'll leave the question open in case someone will be able to shed some light on it. In the meantime I solved it by making all the cc_imports and includes public from the linked repo, while leaving all the cc_libraries with select to my plugin's BUILD file; from there I'm able to use the condition and everything builds.

最佳答案

看起来错误来自这一行,特别是对 string.format 的调用.

ctx.file("BUILD", _BUILD_STRUC.format(maya_dir=maya_dir))

string.format 在模板字符串中搜索大括号,例如 {}{key},并用位置参数或关键字参数替换它们。

您看到此错误是因为 string.format 将模板中 select 的 dict 参数误认为是要替换的内容,因为它以花括号开头。通过加倍转义模板字符串中的大括号应该可以解决问题:

_BUILD_STRUC = \
"""
...
cc_library(
name = "Foundation",
deps = select({{
"@bazel_tools//src/conditions:windows": [":Foundation-win"],
"//conditions:default": [":Foundation-lnx"],
}}),
includes = ["{maya_dir}/include"],
visibility = ["//visibility:public"],
)
...

仅供引用,您可能会找到 repository_ctx.template更容易使用。它的语义略有不同:它按字面替换字符串,而不寻找像 { 这样的特殊字符,因此不需要转义。

关于Bazel 选择在 ctx.file 中失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52257038/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com