简体   繁体   English

Bamp select在ctx.file中失败

[英]Bazel select fails inside ctx.file

I am trying to specify build conditions based on the os I'm running bazel from, so in my .bzl script I have a rule that makes all the simlinks from external sources and writes a BUILD file (with ctx.file), in which I'm declaring all the imports and libraries and in those I would like to add the select function. 我试图根据我运行bazel的os来指定构建条件,所以在我的.bzl脚本中我有一个规则,它从外部源创建所有simlink并写入一个BUILD文件(带有ctx.file),其中我正在声明所有的导入和库,在那些我想添加select函数。 However, when I build I get this error message: 但是,当我构建时,我收到此错误消息:

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

so here's an example of my code and what I'm trying to achieve: 所以这是我的代码和我想要实现的目标的一个例子:

_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"],
)

does anyone have any idea why this is happening? 有没有人知道为什么会这样? I looked at select and configurable attributes docs and seems like that's the way to use it; 我查看了选择和可配置属性文档,看起来就像是使用它的方式; I wonder if it's me doing something wrong or if there's a bug somewhere. 我想知道是不是我做错了什么,或者某个地方是否有错误。

Thanks for any help! 谢谢你的帮助!

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. 看起来像Bazel真的不喜欢在ctx.file中使用select,我会留下问题以防万一有人能够对它有所了解。 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; 在此期间,我通过制作所有cc_imports解决了它,并在链接的repo中包含public,同时将所有cc_libraries select为我的插件的BUILD文件; from there I'm able to use the condition and everything builds. 从那里我能够使用条件和一切建立。

It looks like the error is coming from this line, specifically the call to string.format . 看起来错误来自这一行,特别是对string.format的调用。

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

string.format searches the template string for curly braces like {} or {key} and replaces them with positional or keyword arguments. string.format在模板字符串中搜索大括号,如{}{key} ,并用位置或关键字参数替换它们。

You're seeing this error because string.format is mistaking the dict argument to select within the template as something to replace because it starts with a curly brace. 你看到这个错误是因为string.format错误地将dict参数误认为是在模板中select要替换的东西,因为它以大括号开头。 Escaping the braces within the template string by doubling them should fix the problem: 通过加倍模板字符串中的大括号来解决问题:

_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"],
)
...

FYI, you might find repository_ctx.template easier to work with. 仅供参考,您可能会发现repository_ctx.template更易于使用。 It has slightly different semantics: it replaces strings literally, without looking for special characters like { , so escaping is not needed. 它的语义略有不同:它可以直接替换字符串,而不需要查找像{这样的特殊字符,因此不需要转义。

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

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