相约 2023 年 BazelCon 将于 10 月 24 日至 25 日在 Google 慕尼黑举办!报名现已开放! 了解详情

输出目录布局

报告问题 查看源代码

本页面将介绍输出目录的要求和布局。

要求

输出目录布局的要求:

  • 如果多个用户在同一框上进行构建,则不会冲突。
  • 支持同时在多个工作区中构建。
  • 支持在同一工作区中针对多个目标配置进行构建。
  • 不会与任何其他工具发生冲突。
  • 易于访问。
  • 易于清洁,即便是精心挑选也无妨。
  • 明确化,即使用户在切换到客户端目录时依赖符号链接也是如此。
  • 每位用户的所有构建状态都应在一个目录下(“我想从所有客户端中清除所有 .o 文件”)。

当前布局

目前实现的解决方案:

  • Bazel 必须从包含 WORKSPACE 文件的目录(“工作区目录”)或其子目录调用。否则,它会报告错误。
  • 在 Linux 上,outputRoot 目录默认为 ~/.cache/bazel;在 macOS 上,则默认为 /private/var/tmp;如果在 Windows 上,则默认为 ;如果已设置,则为 %USERPROFILE%;否则为在设置了 FOLDERID_Profile 标志的情况下调用 SHGetKnownFolderPath() 的结果。如果已设置环境变量 $TEST_TMPDIR(例如在 Bazel 本身测试中),则该值会替换默认值。
  • Bazel 用户的构建状态位于 outputRoot/_bazel_$USER 下。这称为 outputUserRoot 目录。
  • outputUserRoot 目录下面有一个 install 目录,它是一个 installBase 目录,其名称是 Bazel 安装清单的 MD5 哈希。
  • outputUserRoot 目录下,系统还会创建一个 outputBase 目录,其名称是工作区路径的 MD5 哈希。例如,如果 Bazel 在工作区目录 /home/user/src/my-project(或与该目录符号关联的目录)中运行,系统会创建一个名为 /home/user/.cache/bazel/_bazel_user/7ffd56a6e4cb724ea575aba15733d113 的输出基目录。您还可以在 Bazel 工作区中运行 echo -n $(pwd) | md5sum,以获取 MD5。
  • 您可以使用 Bazel 的 --output_base 启动选项来替换默认输出基目录。例如 bazel --output_base=/tmp/bazel/output build x/y:z
  • 您也可以使用 Bazel 的 --output_user_root 启动选项替换默认安装基目录和输出基目录。例如:bazel --output_user_root=/tmp/bazel build x/y:z

“bazel-<workspace-name>”、“bazel-out”、“bazel-testlogs”和“bazel-bin”的符号链接放在工作区目录中;这些符号链接指向输出目录内目标专用目录中的某些目录。这些符号链接只是为了方便用户,因为 Bazel 本身并不使用它们。此外,仅当工作区目录可写时,系统才会执行此操作。

布局示意图

目录布局如下:

<workspace-name>/                         <== The workspace directory
  bazel-my-project => <...my-project>     <== Symlink to execRoot
  bazel-out => <...bin>                   <== Convenience symlink to outputPath
  bazel-bin => <...bin>                   <== Convenience symlink to most recent written bin dir $(BINDIR)
  bazel-testlogs => <...testlogs>         <== Convenience symlink to the test logs directory

/home/user/.cache/bazel/                  <== Root for all Bazel output on a machine: outputRoot
  _bazel_$USER/                           <== Top level directory for a given user depends on the user name:
                                              outputUserRoot
    install/
      fba9a2c87ee9589d72889caf082f1029/   <== Hash of the Bazel install manifest: installBase
        _embedded_binaries/               <== Contains binaries and scripts unpacked from the data section of
                                              the bazel executable on first run (such as helper scripts and the
                                              main Java file BazelServer_deploy.jar)
    7ffd56a6e4cb724ea575aba15733d113/     <== Hash of the client's workspace directory (such as
                                              /home/user/src/my-project): outputBase
      action_cache/                       <== Action cache directory hierarchy
                                              This contains the persistent record of the file
                                              metadata (timestamps, and perhaps eventually also MD5
                                              sums) used by the FilesystemValueChecker.
      action_outs/                        <== Action output directory. This contains a file with the
                                              stdout/stderr for every action from the most recent
                                              bazel run that produced output.
      command.log                         <== A copy of the stdout/stderr output from the most
                                              recent bazel command.
      external/                           <== The directory that remote repositories are
                                              downloaded/symlinked into.
      server/                             <== The Bazel server puts all server-related files (such
                                              as socket file, logs, etc) here.
        jvm.out                           <== The debugging output for the server.
      execroot/                           <== The working directory for all actions. For special
                                              cases such as sandboxing and remote execution, the
                                              actions run in a directory that mimics execroot.
                                              Implementation details, such as where the directories
                                              are created, are intentionally hidden from the action.
                                              Every action can access its inputs and outputs relative
                                              to the execroot directory.
        <workspace-name>/                 <== Working tree for the Bazel build & root of symlink forest: execRoot
          _bin/                           <== Helper tools are linked from or copied to here.

          bazel-out/                      <== All actual output of the build is under here: outputPath
            local_linux-fastbuild/        <== one subdirectory per unique target BuildConfiguration instance;
                                              this is currently encoded
              bin/                        <== Bazel outputs binaries for target configuration here: $(BINDIR)
                foo/bar/_objs/baz/        <== Object files for a cc_* rule named //foo/bar:baz
                  foo/bar/baz1.o          <== Object files from source //foo/bar:baz1.cc
                  other_package/other.o   <== Object files from source //other_package:other.cc
                foo/bar/baz               <== foo/bar/baz might be the artifact generated by a cc_binary named
                                              //foo/bar:baz
                foo/bar/baz.runfiles/     <== The runfiles symlink farm for the //foo/bar:baz executable.
                  MANIFEST
                  <workspace-name>/
                    ...
              genfiles/                   <== Bazel puts generated source for the target configuration here:
                                              $(GENDIR)
                foo/bar.h                     such as foo/bar.h might be a headerfile generated by //foo:bargen
              testlogs/                   <== Bazel internal test runner puts test log files here
                foo/bartest.log               such as foo/bar.log might be an output of the //foo:bartest test with
                foo/bartest.status            foo/bartest.status containing exit status of the test (such as
                                              PASSED or FAILED (Exit 1), etc)
              include/                    <== a tree with include symlinks, generated as needed. The
                                              bazel-include symlinks point to here. This is used for
                                              linkstamp stuff, etc.
            host/                         <== BuildConfiguration for build host (user's workstation), for
                                              building prerequisite tools, that will be used in later stages
                                              of the build (ex: Protocol Compiler)
        <packages>/                       <== Packages referenced in the build appear as if under a regular workspace

*.runfiles 目录的布局在 RunfilesSupport 所指向的位置有更详细的说明。

bazel clean

bazel cleanoutputPathaction_cache 目录执行 rm -rf。还会移除工作区符号链接。--expunge 选项将清理整个 outputBase。