Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Up-level references ("..") in Bazel

Tags:

bazel

In my bazel BUILD file, I have a line:

srcs = glob([<pattern1>, <pattern2>, ...])

I tried to have one of my patterns be "../dir/*.cc" but I get an error that I'm not allowed to use the .. sequence here.

Checking the documentation, I have found that it's not permitted, but I'm not sure what the expected substitute is.

Similarly, up-level references (..) and current-directory references (./) are forbidden.

How can I include these other source files in my srcs list given my current file structure? If I can't reference the up-level directory, is there a way to use the package name of the other directory instead?

like image 564
Apollys supports Monica Avatar asked Jul 24 '19 22:07

Apollys supports Monica


People also ask

What is .bazelrc file?

Introduction. Bazel builds software from source code organized in a directory called a workspace. Source files in the workspace are organized in a nested hierarchy of packages, where each package is a directory that contains a set of related source files and one BUILD file.

What is build file in Bazel?

A BUILD file contains several different types of instructions for Bazel. The most important type is the build rule, which tells Bazel how to build the desired outputs, such as executable binaries or libraries.

Where does Bazel build output?

The Bazel user's build state is located beneath outputRoot/_bazel_$USER . This is called the outputUserRoot directory. Beneath the outputUserRoot directory there is an install directory, and in it is an installBase directory whose name is the MD5 hash of the Bazel installation manifest.

What up-level references are forbidden?

Similarly, up-level references ( ..) and current-directory references ( ./) are forbidden. The sole exception to this rule is that a target name may consist of exactly '.

What is a Bazel package?

A package is defined as a directory containing a file named BUILD or BUILD.bazel , residing beneath the top-level directory in the workspace. A package includes all files in its directory, plus all subdirectories beneath it, except those which themselves contain a BUILD file.

Does Bazel try to load external repositories from the bzlmod system?

If true, Bazel tries to load external repositories from the Bzlmod system before looking into the WORKSPACE file. Tags: loading_and_analysis --[no]experimental_repository_cache_hardlinksdefault: "false" If set, the repository cache will hardlink the file in case of a cache hit, rather than copying. This is intended to save disk space.

What are the different commands in Bazel?

Commands analyze-profile Analyzes build profile data. aquery Analyzes the given targets and queries the action graph. build Builds the specified targets. canonicalize-flags Canonicalizes a list of bazel options. clean Removes output files and optionally stops the server. coverage Generates code coverage report for specified test targets. cquery


1 Answers

Going "up" from your BUILD file would violate the package boundaries. If you really need that structure and cannot or don't want to change it, you have to make files from one package available to the other package by declaring the corresponding target(s) or at least export the files and making those visible. For instance assuming the following structure:

.
├── BUILD
├── WORKSPACE
├── hello.c
└── tgt
    └── BUILD

It the // (top-level) package BUILD I could say:

filegroup(
    name = "hello",
    srcs = ["hello.c"],
    visibility = ["//tgt:__pkg__"],
)                      

(Could also be: exports_files(["hello.c"], ["//tgt:__pkg__"]) instead in which case I would refer to the file by its name //:hello.c from tgt.)

And inside //tgt (tgt/BUILD) it can then read:

cc_binary(
    name="tgt",
    srcs=["//:hello"],
)                                                                                                                                              

Which would give me:

$ bazel run //tgt
WARNING: /tmp/bzl1/tgt/BUILD:3:10: in srcs attribute of cc_binary rule //tgt:tgt: please do not import '//:hello.c' directly. You should either move the file to this package or depend on an appropriate rule there
INFO: Analyzed target //tgt:tgt (11 packages loaded, 68 targets configured).
INFO: Found 1 target...
Target //tgt:tgt up-to-date:
  bazel-bin/tgt/tgt
INFO: Elapsed time: 0.247s, Critical Path: 0.09s
INFO: 2 processes: 2 linux-sandbox.
INFO: Build completed successfully, 6 total actions
INFO: Build completed successfully, 6 total actions
Hello World!

Note: bazel still flags this as something weird and noteworthy going on. I have to say I do not disagree with it. The tree structure does not seem to correspond to the content very well.

Perhaps in this example the tgt package boundary is artificial and not actually useful? Or hello.c is in the wrong place.

like image 184
Ondrej K. Avatar answered Oct 11 '22 16:10

Ondrej K.