在Bazel中使用绝对路径安全吗?

问题描述

我正在尝试将Bazel与旧的基于make / shell的构建系统一起添加。我可以很容易地使Shell命令返回到旧构建系统所提供的某些工具或库构建的绝对路径作为早期先决条件。我可以在genrule()中使用这些命令,该命令将所需的文件(如标头和库)复制到Bazel中,并以cc_library()的形式公开。

我发现,如果该命令使用具有绝对路径的文件,则genrule()不会检测到依赖项-它不会被沙箱捕获。我在某种程度上正在滥用这种行为。

安全吗?将来的Bazel更新是否会在genrule命令中以这种方式拒绝基于绝对路径的文件访问?

解决方法

默认情况下,大多数Bazel的沙箱都允许访问源树之外的大多数路径。详细信息取决于您使用的沙盒实现。例如,码头工人沙箱允许访问码头工人映像内的所有那些路径。对未来的Bazel版本做出承诺很难,但是我认为沙箱不太可能阻止访问/bin/bash(例如),这意味着其他绝对路径也可能继续起作用。

--sandbox_block_path可用于显式阻止路径。

如果在每台构建机器上始终都有可用的文件,则您的设置应该可以使用。请记住,当这些文件的内容更改时,Bazel将不会识别,因此您可以轻松地在各种缓存中获得陈旧的结果。您可以通过确保外部路径随时随其内容更改而避免这种情况。

如果您提前知道路径,

new_local_repository可能更适合避免这些问题。

如果您不提前知道路径,则可以编写一个custom repository rule,它通过repository_ctx.execute运行任意命令以检索路径并将它们与repository_ctx.symlink符号链接。

Tensorflow的third_party/sycl/sycl_configure.bzl给出了一个类似的示例(除了查看find_computecpp_root这样的环境变量之外,您还可以做其他事情,并且可以符号链接整个目录而不是其中的所有文件):

def _symlink_dir(repository_ctx,src_dir,dest_dir):
    """Symlinks all the files in a directory.
    Args:
      repository_ctx: The repository context.
      src_dir: The source directory.
      dest_dir: The destination directory to create the symlinks in.
    """
    files = repository_ctx.path(src_dir).readdir()
    for src_file in files:
        repository_ctx.symlink(src_file,dest_dir + "/" + src_file.basename)

def find_computecpp_root(repository_ctx):
    """Find ComputeCpp compiler."""
    sycl_name = ""
    if _COMPUTECPP_TOOLKIT_PATH in repository_ctx.os.environ:
        sycl_name = repository_ctx.os.environ[_COMPUTECPP_TOOLKIT_PATH].strip()
    if sycl_name.startswith("/"):
        return sycl_name
    fail("Cannot find SYCL compiler,please correct your path")

def _sycl_autoconf_imp(repository_ctx):
<snip>
            computecpp_root = find_computecpp_root(repository_ctx)
<snip>
            _symlink_dir(repository_ctx,computecpp_root + "/lib","sycl/lib")
            _symlink_dir(repository_ctx,computecpp_root + "/include","sycl/include")
            _symlink_dir(repository_ctx,computecpp_root + "/bin","sycl/bin")

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...