1

In its C++ unit testing tutorial, Bazel suggests adding a root level gtest.BUILD file to the workspace root in order to properly integrate Google Test into the test project.

https://docs.bazel.build/versions/master/cpp-use-cases.html

Why would one create a new BUILD file and add gtest prefix to it rather than adding a new build rule to an existing BUILD file in the workspace? Is it just a minor style preference?

Luke Gehorsam
  • 340
  • 4
  • 16

1 Answers1

4

Because if you added a BUILD file somewhere in the workspace (e.g. under //third_party/gtest/BUILD) then that file would create a package there.

Then, if you had targets declared in that BUILD file, would their files exist under //third_party/gtest, or would they exist in the zip file that the http_archive downloads? If the former, then there's no need for a http_archive because the files are already in the source tree; if the latter, then the BUILD file references non-existent files in its own package. Both scenarios are flawed.

Better to call gtest's BUILD-file-to-be something that doesn't create a package, but that's descriptive of its purpose.

The build_file attribute of http_archive can reference any file, there's no requirement of the name. The name gtest.BUILD is mostly stylistic, yes, but it also avoids creating a package where it shouldn't. You could say it's an "inactive" BUILD file that will be "active" when Bazel downloads the http_archive, extracts it somewhere, and creates in that directory a symlink called BUILD which points to gtest.BUILD.

Another advantage of having such "inactive" BUILD files is that you can have multiple of them within one package, for multiple http_archives.

László
  • 3,973
  • 1
  • 13
  • 26
  • If my answer helps you, then you'll be in the best position to rephrase the documentation to help others following your path and who ask themselves the same question. If you agree, this is the edit link: https://github.com/bazelbuild/bazel/edit/master/site/docs/cpp-use-cases.md – László Oct 29 '19 at 08:42
  • Thanks. I think the documentation could use a note that the file is named arbitrarily and that there is nothing special about BUILD if it's prefixed with something else. – Luke Gehorsam Oct 29 '19 at 15:01