General Rules


filegroup(name, srcs, data, compatible_with, deprecation, distribs, features, licenses, output_group, output_licenses, path, restricted_to, tags, testonly, visibility)

Use filegroup to give a convenient name to a collection of targets. These can then be referenced from other rules.

Using filegroup is encouraged instead of referencing directories directly. The latter is unsound since the build system does not have full knowledge of all files below the directory, so it may not rebuild when these files change. When combined with glob, filegroup can ensure that all files are explicitly known to the build system.


To create a filegroup consisting of two source files, do

    name = "mygroup",
    srcs = [

Or, use a glob to grovel a testdata directory:

    name = "exported_testdata",
    srcs = glob([

To make use of these definitions, reference the filegroup with a label from any rule:

    name = "my_library",
    srcs = [""],
    data = [



Name; required

A unique name for this rule.


List of labels; optional

The list of targets that are members of the file group.

It is common to use the result of a glob expression for the value of the srcs attribute. If a rule and a source file with the same name both exist in the package, the glob will return the outputs of the rule instead of the source file.


List of labels; optional

The list of files needed by this rule at runtime.

Targets named in the data attribute will be added to the runfiles of this filegroup rule. When the filegroup is referenced in the data attribute of another rule its runfiles will be added to the runfiles of the depending rule. See the data dependencies section and general documentation of data for more information about how to depend on and use data files.


String; optional

The output group from which to gather artifacts from sources. If this attribute is specified, artifacts from the specified output group of the dependencies will be exported instead of the default output group.

An "output group" is a category of output artifacts of a target, specified in that rule's implementation.


String; optional

An optional string to set a path to the files in the group, relative to the package path.

This attribute can be used internally by other rules depending on this filegroup to find the name of the directory holding the files.


genquery(name, deps, data, compatible_with, deprecation, distribs, expression, features, licenses, opts, restricted_to, scope, strict, tags, testonly, visibility)

genquery() runs a query specified in the Bazel query language and dumps the result into a file.

In order to keep the build consistent, the query is allowed only to visit the transitive closure of the targets specified in the scope attribute. Queries violating this rule will fail during execution if strict is unspecified or true (if strict is false, the out of scope targets will simply be skipped with a warning). The easiest way to make sure this does not happen is to mention the same labels in the scope as in the query expression.

The only difference between the queries allowed here and on the command line is that queries containing wildcard target specifications (e.g. //pkg:* or //pkg:all) are not allowed here. The reasons for this are two-fold: first, because genquery has to specify a scope to prevent targets outside the transitive closure of the query to influence its output; and, second, because BUILD files do not support wildcard dependencies (e.g. deps=["//a/..."] is not allowed).

The genquery's output is ordered using --order_output=full in order to enforce deterministic output.

The name of the output file is the name of the rule.


This example writes the list of the labels in the transitive closure of the specified target to a file.

    name = "kiwi-deps",
    expression = "deps(//kiwi:kiwi_lib)",
    scope = ["//kiwi:kiwi_lib"],



Name; required

A unique name for this rule.


String; required

The query to be executed.

List of strings; optional

The options that are passed to the query engine. These correspond to the command line options that can be passed to bazel query. The only query options that are not allowed here are --keep_going and --order_output.

List of labels; required

The scope of the query. The query is not allowed to touch targets outside the transitive closure of these targets.

Boolean; optional; default is 1

If true, targets whose queries escape the transitive closure of their scopes will fail to build. If false, Bazel will print a warning and skip whatever query path led it outside of the scope, while completing the rest of the query.


test_suite(name, compatible_with, deprecation, distribs, features, licenses, restricted_to, tags, testonly, tests, visibility)

A test_suite defines a set of tests that are considered "useful" to humans. This allows projects to define sets of tests, such as "tests you must run before checkin", "our project's stress tests" or "all small tests."


A test suite to run all of the small tests in the current package.

    name = "small_tests",
    tags = ["small"],

A test suite that runs a specified set of tests:

    name = "smoke_tests",
    tests = [

A test suite to run all tests in the current package which are not flaky.

    name = "non_flaky_test",
    tags = ["-flaky"],



Name; required

A unique name for this rule.


List of strings; optional; nonconfigurable

List of text tags such as "small" or "database" or "-flaky". Tags may be any valid string.

Tags which begin with a "-" character are considered negative tags. The preceding "-" character is not considered part of the tag, so a suite tag of "-small" matches a test's "small" size. All other tags are considered positive tags.

Optionally, to make positive tags more explicit, tags may also begin with the "+" character, which will not be evaluated as part of the text of the tag. It merely makes the positive and negative distinction easier to read.

Only test rules that match all of the positive tags and none of the negative tags will be included in the test suite. Note that this does not mean that error checking for dependencies on tests that are filtered out is skipped; the dependencies on skipped tests still need to be legal (e.g. not blocked by visibility constraints).

The manual tag keyword is treated specially. It marks the test_suite target as "manual" so that it will be ignored by the wildcard expansion and automated testing facilities. It does not work as a filter on the set of tests in the suite. So when the manual tag is used on a test_suite, test rules do not have to be tagged as manual to be included in the test suite.

Note that a test's size is considered a tag for the purpose of filtering.

If you need a test_suite that contains tests with mutually exclusive tags (e.g. all small and medium tests), you'll have to create three test_suite rules: one for all small tests, one for all medium tests, and one that includes the previous two.


List of labels; optional; nonconfigurable

A list of test suites and test targets of any language.

Any *_test is accepted here, independent of the language. No *_binary targets are accepted however, even if they happen to run a test. Filtering by the specified tags is only done for tests listed directly in this attribute. If this attribute contains test_suites, the tests inside those will not be filtered by this test_suite (they are considered to be filtered already).

If the tests attribute is unspecified or empty, the rule will default to including all test rules in the current BUILD file that are not tagged as manual. These rules are still subject to tag filtering.


alias(name, actual, compatible_with, deprecation, features, restricted_to, tags, testonly, visibility)

The alias rule creates another name a rule can be referred to as.

Aliasing only works for "regular" targets. In particular, package_group, config_setting and test_suite rules cannot be aliased.

The alias rule has its own visibility declaration. In all other respects, it behaves like the rule it references with some minor exceptions:

  • Tests are not run if their alias is mentioned on the command line
  • When defining environment groups, the aliases to environment rules are not supported. They are not supported in the --target_environment command line option, either.


    name = "data",
    srcs = ["data.txt"],

    name = "other",
    actual = ":data",



Name; required

A unique name for this rule.


Label; required

The target this alias refers to. It does not need to be a rule, it can also be an input file.


config_setting(name, deprecation, distribs, features, licenses, tags, testonly, values, visibility)

Matches an expected configuration state (expressed as Bazel flags) for the purpose of triggering configurable attributes. See select for how to consume this rule and Configurable attributes for an overview of the general feature.


The following matches any Bazel invocation that specifies --compilation_mode=opt or -c opt (either explicitly at the command line or implicitly from .bazelrc files, etc.), when applied to a target configuration rule:

    name = "simple",
    values = {"compilation_mode": "opt"}

The following matches any Bazel invocation that builds for ARM and applies a custom define (e.g. bazel build --cpu=armeabi --define FOO=bar ...), when applied to a target configuration rule:

    name = "two_conditions",
    values = {
        "cpu": "armeabi",
        "define": "FOO=bar"


See select for policies on what happens depending on how many rules match an invocation.

For flags that support shorthand forms (e.g. --compilation_mode vs. -c), values definitions must use the full form. These automatically match invocations using either form.

The currently endorsed method for creating custom conditions that can't be expressed through dedicated build flags is through the --define flag. Use this flag with caution: it's not ideal and only endorsed for lack of a currently better workaround. See the Configurable attributes section for more discussion.

Try to consolidate config_setting definitions as much as possible. In other words, define //common/conditions:foo in one common package instead of repeating separate instances in //project1:foo, //project2:foo, etc. that all mean the same thing.



Name; required

A unique name for this rule.


null; required; nonconfigurable

The set of configuration values that match this rule (expressed as Bazel flags) (Dictionary mapping flags to expected values, both expressed as strings; mandatory)

This rule inherits the configuration of the configured target that references it in a select statement. It is considered to "match" a Bazel invocation if, for every entry in the dictionary, its configuration matches the entry's expected value. For example values = {"compilation_mode": "opt"} matches the invocations bazel build --compilation_mode=opt ... and bazel build -c opt ... on target-configured rules.

For convenience's sake, configuration values are specified as Bazel flags (without the preceding "--"). But keep in mind that the two are not the same. This is because targets can be built in multiple configurations within the same build. For example, a host configuration's "cpu" matches the value of --host_cpu, not --cpu. So different instances of the same config_setting may match the same invocation differently depending on the configuration of the rule using them.

If a flag is not explicitly set at the command line, its default value is used. If a key appears multiple times in the dictionary, only the last instance is used. If a key references a flag that can be set multiple times on the command line (e.g. bazel build --copt=foo --copt=bar --copt=baz ...), a match occurs if any of those settings match.

This attribute cannot be empty.


genrule(name, srcs, outs, cmd, compatible_with, deprecation, distribs, executable, features, licenses, local, message, output_licenses, output_to_bindir, restricted_to, tags, testonly, tools, visibility)

A genrule generates one or more files using a user-defined Bash command.

Genrules are generic build rules that you can use if there's no specific rule for the task. If for example you want to minify JavaScript files then you can use a genrule to do so. If however you need to compile C++ files, stick to the existing cc_* rules, because all the heavy lifting has already been done for you.

Do not use a genrule for running tests. There are special dispensations for tests and test results, including caching policies and environment variables. Tests generally need to be run after the build is complete and on the target architecture, whereas genrules are executed during the build and on the host architecture (the two may be different). If you need a general purpose testing rule, use sh_test.

Cross-compilation Considerations

See the user manual for more info about cross-compilation.

While genrules run during a build, their outputs are often used after the build, for deployment or testing. Consider the example of compiling C code for a microcontroller: the compiler accepts C source files and generates code that runs on a microcontroller. The generated code obviously cannot run on the CPU that was used for building it, but the C compiler (if compiled from source) itself has to.

The build system uses the host configuration to describe the machine(s) on which the build runs and the target configuration to describe the machine(s) on which the output of the build is supposed to run. It provides options to configure each of these and it segregates the corresponding files into separate directories to avoid conflicts.

For genrules, the build system ensures that dependencies are built appropriately: srcs are built (if necessary) for the target configuration, tools are built for the host configuration, and the output is considered to be for the target configuration. It also provides "Make" variables that genrule commands can pass to the corresponding tools.

It is intentional that genrule defines no deps attribute: other built-in rules use language-dependent meta information passed between the rules to automatically determine how to handle dependent rules, but this level of automation is not possible for genrules. Genrules work purely at the file and runfiles level.

Special Cases

Host-host compilation: in some cases, the build system needs to run genrules such that the output can also be executed during the build. If for example a genrule builds some custom compiler which is subsequently used by another genrule, the first one has to produce its output for the host configuration, because that's where the compiler will run in the other genrule. In this case, the build system does the right thing automatically: it builds the srcs and outs of the first genrule for the host configuration instead of the target configuration. See the user manual for more info.

JDK & C++ Tooling: to use a tool from the JDK or the C++ compiler suite, the build system provides a set of variables to use. See "Make" variable for details.

Genrule Environment

The genrule command is executed in a Bash shell, configured to fail when a command or a pipeline fails (set -e -o pipefail). Genrules should not access the network (except to create connections between processes running within the same genrule on the same machine), though this is not currently enforced.

The build system automatically deletes any existing output files, but creates any necessary parent directories before it runs a genrule. It also removes any output files in case of a failure.

General Advice

  • Do ensure that tools run by a genrule are deterministic and hermetic. They should not write timestamps to their output, and they should use stable ordering for sets and maps, as well as write only relative file paths to the output, no absolute paths. Not following this rule will lead to unexpected build behavior (Bazel not rebuilding a genrule you thought it would) and degrade cache performance.
  • Do use $(location) extensively, for outputs, tools and sources. Due to the segregation of output files for different configurations, genrules cannot rely on hard-coded and/or absolute paths.
  • Do write a common Skylark macro in case the same or very similar genrules are used in multiple places. If the genrule is complex, consider implementing it in a script or as a Skylark rule. This improves readability as well as testability.
  • Do make sure that the exit code correctly indicates success or failure of the genrule.
  • Do not write informational messages to stdout or stderr. While useful for debugging, this can easily become noise; a successful genrule should be silent. On the other hand, a failing genrule should emit good error messages.
  • $$ evaluates to a $, a literal dollar-sign, so in order to invoke a shell command containing dollar-signs such as ls $(dirname $x), one must escape it thus: ls $$(dirname $$x).
  • Avoid creating symlinks and directories. Bazel doesn't copy over the directory/symlink structure created by genrules and its dependency checking of directories is unsound.
  • When referencing the genrule in other rules, you can use either the genrule's label or the labels of individual output files. Sometimes the one approach is more readable, sometimes the other: referencing outputs by name in a consuming rule's srcs will avoid unintentionally picking up other outputs of the genrule, but can be tedious if the genrule produces many outputs.


This example generates foo.h. There are no sources, because the command doesn't take any input. The "binary" run by the command is a perl script in the same package as the genrule.

    name = "foo",
    srcs = [],
    outs = ["foo.h"],
    cmd = "./$(location > \"$@\"",
    tools = [""],

The following example shows how to use a filegroup and the outputs of another genrule. Note that using $(SRCS) instead of explicit $(location) directives would also work; this example uses the latter for sake of demonstration.

    name = "concat_all_files",
    srcs = [
        "//some:files",  # a filegroup with multiple files in it ==> $(locations)
        "//other:gen",   # a genrule with a single output ==> $(location)
    outs = ["concatenated.txt"],
    cmd = "cat $(locations //some:files) $(location //other:gen) > $@",



Name; required

A unique name for this rule.

You may refer to this rule by name in the srcs or deps section of other BUILD rules. If the rule generates source files, you should use the srcs attribute.

List of labels; optional

A list of inputs for this rule, such as source files to process.

This attributes is not suitable to list tools executed by the cmd; use the tools attribute for them instead.

The build system ensures these prerequisites are built before running the genrule command; they are built using the same configuration as the original build request. The names of the files of these prerequisites are available to the command as a space-separated list in $(SRCS); alternatively the path of an individual srcs target //x:y can be obtained using $(location //x:y), or using $< provided it's the only entry in //srcs.


List of filenames; required; nonconfigurable

A list of files generated by this rule.

Output files must not cross package boundaries. Output filenames are interpreted as relative to the package.

If the executable flag is set, outs must contain exactly one label.


String; required

The command to run. Subject to $(location) and "Make" variable substitution.
  1. First $(location) substitution is applied, replacing all occurrences of $(location label) and of $(locations label).
  2. Note that outs are not included in this substitution. Output files are always generated into a predictable location (available via $(@D), $@, $(OUTS) or $(location output_name); see below).

  3. Next, "Make" variables are expanded. Note that predefined variables $(JAVA), $(JAVAC) and $(JAVABASE) expand under the host configuration, so Java invocations that run as part of a build step can correctly load shared libraries and other dependencies.
  4. Finally, the resulting command is executed using the Bash shell. If its exit code is non-zero the command is considered to have failed.

The command may refer to *_binarytargets; it should use a label for this. The following variables are available within the cmd sublanguage:


Boolean; optional; nonconfigurable; default is 0

Declare output to be executable.

Setting this flag to 1 means the output is an executable file and can be run using the run command. The genrule must produce exactly one output in this case. If this attribute is set, run will try executing the file regardless of its content.

Declaring data dependencies for the generated executable is not supported.


Boolean; optional; default is 0

If set to 1, this option force this genrule to run with the standalone strategy, without sandboxing.

This is equivalent to providing 'local' as a tag (tags=["local"]). The local strategy is applied if either one is specified.

The --genrule_strategy option value local overrides this attribute.


String; optional

A progress message.

A progress message that will be printed as this build step is executed. By default, the message is "Generating output" (or something equally bland) but you may provide a more specific one. Use this attribute instead of echo or other print statements in your cmd command, as this allows the build tool to control whether such progress messages are printed or not.


null; optional

See common attributes

Boolean; optional; nonconfigurable; default is 0

If set to 1, this option causes output files to be written into the bin directory instead of the genfiles directory.


List of labels; optional

A list of tool dependencies for this rule. See the definition of dependencies for more information.

The build system ensures these prerequisites are built before running the genrule command; they are built using the host configuration, since these tools are executed as part of the build. The path of an individual tools target //x:y can be obtained using $(location //x:y).

Any *_binary or tool to be executed by cmd must appear in this list, not in srcs, to ensure they are built in the correct configuration.