Bazel label function. Symbolic macros are available by default in Bazel 8.
Bazel label function They are designed to be amenable to lazy evaluation (which will be added in a future Bazel release). Oct 30, 2025 · Before the invocation of the executable (during bazel test), Bazel prepares the tree of runfiles alongside the test executable according to their source directory structure. By the end of the loading phase, legacy macros don't exist anymore, and Bazel sees only the concrete set of instantiated rules. Oct 30, 2025 · Visibility specifications All rule targets have a visibility attribute that takes a list of labels. cpp source files (inputs). With the exception of the last form, these are just syntactic placeholders that don't correspond to any actual target. A publicly visible target can be referenced from every package in the source tree. If both files exist, BUILD. bzl and related files. The BUILD file specifies what software outputs can be built from the source Jan 31, 2025 · You'll need to complete a few actions and gain 15 reputation points before being able to upvote. This helps to avoid inadvertent quoting problems, and makes it easier to construct tools and scripts that manipulate labels, such as the Bazel Query Language. To execute a query while ignoring errors such as missing targets, use the Rules A rule defines a series of actions that Bazel performs on inputs to produce a set of outputs. bazel takes precedence over BUILD. Bazel query loads BUILD files, but doesn't analyze targets. Package groups are primarily used for visibility control. Oct 30, 2025 · This page covers the options that are available with various Bazel commands, such as bazel build, bazel run, and bazel test. Jan 30, 2024 · Most targets are one of two principal kinds: rules or labels. //target is the expression fed to the function. This guide previews some basic query functions, but for more options see the query guide. Here’s a typical session: Oct 30, 2025 · The result of this function is the same Label value as would be produced by passing the given string to a label-valued attribute of a target declared in the BUILD file. Jul 13, 2023 · I see there are functions like Label() or native. Label syntax discourages use of metacharacters that have special meaning to the shell. Oct 14, 2024 · Bazel Python Rules. cquery supports most of query 's functions, plus a few new ones. For macros, a related function, native. Bazel query how-to This is a quick tutorial to get you started using Bazel's query language to trace dependencies in your code. Oct 30, 2025 · Bazel augments the core language with numerous build-related functions such as glob, genrule, java_binary, and so on. For a language details and --output flag details, please see the reference manuals, Bazel query reference and Bazel cquery reference. Macros are mainly used for encapsulation and code reuse of existing rules and other macros. Syntax Starlark's syntax is inspired by Python3 At analysis time (within the rule's implementation function), when retrieving the attribute value from ctx. It covers the label format, the internal representation in Gazelle, operations performed on labels, and utility functions for working with labels. Oct 30, 2025 · Symbolic macros offer typed arguments (string to label conversion, relative to where the macro was called) and the ability to restrict and specify the visibility of targets created. Declare and document provider fields. See the Bazel and Starlark documentation for more details, and the Rules SIG template as a starting point for new rulesets. You can browse the online help messages using bazel help. label( default = "@bazel_tools//tools/allowlists/function_transition_allowlist" ) to the attrs of my_rule=rule. Usage note: The difference between this function and Label () is that Label() uses the context of the package of the . Run g++ on the source files (action). By the end of the loading phase, macros don't exist anymore, and Bazel sees only the concrete set of instantiated rules Mar 19, 2021 · The Bazel genquery documentation says: In contrast to the command line and other places in BUILD files, labels here are resolved relative to the root directory of the workspace. As a rule writer, you must consider not only the user This is a top-level module for defining the attribute schemas of a rule or aspect. These rules forward the providers of the actual target to which the build setting is set. Lexical specification of a label Label syntax discourages use of metacharacters that have special meaning to the shell. Target syntax {:#target-syntax} Some commands, like build or test, can operate on a list of Oct 30, 2025 · Before evaluating the BUILD file, Bazel evaluates all the files it loads. See the Rules page for more on defining and using attributes. Bazel rules are highly structured, and learning this structure takes time. Oct 30, 2025 · A rule defines a series of actions that Bazel performs on inputs to produce a set of outputs, which are referenced in providers returned by the rule's implementation function. Oct 30, 2025 · This page covers how to get started using Bazel's query language to trace dependencies in your code. Providers can be instantiated by calling the resulting value as a function, or used directly as an index key for retrieving an instance of that provider from a target. The list of available functions differs depending on the file type (whether a BUILD file, or a . Oct 30, 2025 · This section lists the global functions available in Starlark. Unfrozen dictionaries are mutable, and may be updated by assigning to d[k] or by calling certain methods. Dictionaries are iterable; iteration yields the sequence of keys in insertion order. data is added only if LocationExpander is created with allowDataAttributeEntriesInLabel=true, which it isn't. It includes how to define build settings and provides examples. Oct 30, 2025 · The resulting value of this function must be stored in a global value to be usable in a rule or aspect implementation. Oct 30, 2025 · Configurable attributes, commonly known as select(), is a Bazel feature that lets users toggle the values of build rule attributes at the command line. To execute a query while ignoring errors such as Oct 30, 2025 · Avoid creating symlinks and directories. The most commonly used ones are bazel build and bazel test. A dictionary supports indexing using d[k] and key membership testing using k in d; both operations take constant time. Jul 27, 2022 · Ok I think I should add something like "_allowlist_function_transition": attr. . Upvoting indicates when questions and answers are useful. Note: The BUILD file can be named either BUILD or BUILD. In particular, it lets the implementation function access the current target's label, attributes, configuration, and the providers of its dependencies. The callback function _foo_binary_impl is not called. bzl file, etc). By definition, every package contains a BUILD file, which is a short program. Like label, this output format prints the labels of each target in the resulting graph, in topological order, but it additionally precedes the label by the kind of the target. Why you shouldn't use legacy macros (and should use Symbolic macros instead) Where possible you should use symbolic macros. The argument must refer to an absolute label. Jul 30, 2025 · A Label represents a label of a build target in Bazel. Use this function only when you want to give a default value for the label attributes. Members bool int int_list label label_keyed_string_dict label_list output Oct 30, 2025 · This page is an overview of Starlark, formerly known as Skylark, the language used in Bazel. 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 The bazel tool performs many functions, called commands. Oct 30, 2025 · A simple cquery call looks like: bazel cquery "function(//target)" The query expression "function(//target)" consists of the following: function() is the function to run on the target. This page is a companion to the list of Bazel's commands in Build with Bazel. Oct 30, 2025 · The previous sections described packages, targets and labels, and the build dependency graph abstractly. Source files in repositories 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. Options The Common useful functions for writing BUILD files and Starlark macros/rules - bazel-contrib/bazel-lib Oct 30, 2025 · This section defines various terms and concepts that are common to many functions or build rules. Building programs with Bazel The build command Type bazel build followed by the name of the target you wish to build. If the given value is already a Label, it is returned unchanged. label_flag and label_setting can be read/written by transitions and label_flag can be set by the user like other build_setting rules can. It has methods for declaring output files and the actions that produce Oct 30, 2025 · A rule defines a series of actions that Bazel performs on inputs to produce a set of outputs, which are referenced in providers returned by the rule's implementation function. Iteration May 15, 2017 · I looked at the source code and you're right: srcs, deps, and tools (if defined on the rule) are added to the set of labels that expand_locations understands. This function defines a set of packages and associates a label with the set. The precise details of allowed target names are below. For more information about the language, see Starlark's GitHub repo. This section describes the concrete syntax used to define a package. These objects are used as the values of the attrs dictionary argument of rule(), aspect(), repository_rule() and tag_class(). It provides access to the information and methods needed to analyze the current target. Bazel doesn't copy over the directory/symlink structure created by genrules and its dependency checking of directories is unsound. Instead, bazel has two built-in rules: label_flag and label_setting. What's reputation and how do I get it? Instead, you can save this post to reference later. Introduction Bazel builds software from source code organized in a directory called a workspace. The empty rule To create your first rule, create the file foo. For a complete list of functions and types, see the Bazel API reference. Each label has one of the following forms. Target syntax Some commands, like build or test, can operate on a list of targets. A defined set of repositories comprises the workspace. Oct 30, 2025 · Getting started What are Bazel queries? Queries help you to learn about a Bazel codebase by analyzing the relationships between BUILD files and examining the resulting output for useful information. A privately visible target can only be referenced within its own package (not Oct 31, 2025 · py_library(name, deps, srcs, data, aspect_hints, compatible_with, deprecation, distribs, exec_compatible_with, exec_group_compatible_with, exec_properties, experimental_venvs_site_packages, features, imports, licenses, package_metadata, precompile, precompile_invalidation_mode, precompile_optimize_level, precompile_source_retention, pyi_deps, pyi_srcs, restricted_to, srcs_version, tags, target Bazel augments the core language with numerous build-related functions such as glob, genrule, java_binary, and so on. package_relative_label() from a BUILD file. By the end of the loading phase, macros don't exist anymore, and Bazel sees only the concrete set of instantiated rules 2 days ago · A context object that is passed to the implementation function for a rule or aspect. attr, labels are replaced by the corresponding Target s. bzl file that called it, not the package of the BUILD file. Oct 30, 2025 · Label Label(input) Converts a label string into a Label object, in the context of the package where the calling . package_relative_label(), converts the input into a Label in the context of the package currently being constructed. Oct 30, 2025 · Label syntax discourages use of metacharacters that have special meaning to the shell. When referencing the genrule in other rules, you can use either the genrule's label or the labels of individual output files. cpp files (the inputs) Run g++ on them (the action) Return an executable file (the output). A macro is a function called from the BUILD file that can instantiate rules. package_group(name, packages, includes) This function defines a set of packages and associates a label with the set. bazel, WORKSPACE, *. Example Project: /_project. Members bool int int_list label label_keyed_string_dict label_list output Instead, bazel has two built-in rules: label_flag and label_setting. For example, the l Oct 30, 2025 · Rule implementation functions should almost always be private functions (named with a leading underscore). Aug 28, 2024 · Bazel labels are a fundamental concept in Bazel, and using them efficiently can greatly improve your build times and reduce code duplication. Each function returns an object representing the schema of a single attribute. This can be used, for example, for a multiplatform library that automatically chooses the appropriate implementation for the architecture, or for a feature-configurable binary that can be customized at build time. bzl: At analysis time (within the rule's implementation function), when retrieving the attribute value from ctx. yaml Commands and Options {% include “_buttons. In this example, the expression is a simple Nov 6, 2025 · Like label, this output format prints the labels of each target in the resulting graph, in topological order, but it additionally precedes the label by the kind of the target. Members bool int int_list label label_keyed Oct 30, 2025 · This page covers the basics of using macros and includes typical use cases, debugging, and conventions. "//visibility:public": Grants access to all packages. package_relative_label(), but I didn't manage to call them from the BUILD file directly. In this post, we'll dive into some best practices for writing efficient and readable Bazel labels. From Bazel’s perspective, g++ and the standard C++ libraries are also inputs to this rule. Oct 30, 2025 · dict is a built-in type representing an associative mapping or dictionary. For the authoritative specification of the Starlark syntax and behavior, see the Starlark Language Specification. Rule: A function that processes files as input and output, supporting chaining, where the output of one rule can serve as the input Jul 31, 2018 · Bazel lets you write rules in Starlark to support new languages. However, this structure helps you avoid introducing unnecessary complication in complex builds. This time, we'll cover writing a simple rule that compiles and links a Go binary from sources. Nov 6, 2025 · This is a top-level module for defining the attribute schemas of a rule or aspect. This makes it possible to: define custom flags for your project, obsoleting the need for --define write transitions to configure deps in different configurations than their parents (such as --compilation Use the Build Encyclopedia as the source of truth for all Bazel functions, as it autogenerates with each new build. Contribute to bazel-contrib/rules_python development by creating an account on GitHub. For simplicity's Oct 30, 2025 · Legacy macros are unstructured functions called from BUILD files that can create targets. bzl, you would see only one occurrence of "bzl file evaluation" because Bazel caches the result of the evaluation. You can get help by typing bazel help query or bazel help cquery on the command line. These objects are used as the values of the attrs dictionary argument of rule() and aspect(). bazel. I tried calling native. The label can be referenced in visibility attributes. A common style is to give the implementation function for myrule the name _myrule_impl. yaml Book: /_book. Symbolic macros are available by default in Bazel 8. If multiple BUILD files are loading foo. Example: Label("//tools:default") Parameters Concepts and terminology This document provides an overview of the source tree layout and the terminology used in Bazel. bzl source file lives. For example, a C++ binary rule might: Take a set of . html” %} This page covers the options that are available with various Bazel commands, such as bazel build, bazel run, and bazel test. Return the DefaultInfo provider with the executable output and other files to make available at Oct 30, 2025 · This page covers the benefits and basic usage of Starlark configurations, Bazel's API for customizing how your project builds. Pass information between your rules using a well-defined provider interface. Contents Bourne shell tokenization Label Expansion Typical attributes defined by most build rules Attributes common to all build rules Attributes common to all test rules (*_test) Attributes common to all binary rules (*_binary) Configurable attributes Implicit output targets Bourne shell Oct 30, 2025 · This page covers the basics of using macros and includes typical use cases, debugging, and conventions. Members bool int int_list label label_keyed_string_dict label_list output At analysis time (within the rule's implementation function), when retrieving the attribute value from ctx. This is a top-level module for defining the attribute schemas of a rule or aspect. Labels have three parts: a repository name, a package name, and a target name, formatted as @repo//pkg:target. They use a syntax more flexible than labels, which is documented in Specifying targets to build. Options The Oct 30, 2025 · For example, if //package:foo depends on //package:bar with a configuration transition, then the configuration of //package:bar (and its dependencies) will be //package:foo 's configuration plus the changes specified by the transition function. Oct 30, 2025 · Bazel builds software from source code organized in directory trees called repositories. Label Label Label(label_string, *, relative_to_caller_repository=False) Creates a Label referring to a BUILD target. Return the DefaultInfo provider with the executable output and other files to make available at The Starlark Language Server component provides intellisense features for BUILD, BUILD. Oct 30, 2025 · package_group package_group(name, packages, includes) This function defines a set of packages and associates a label with the set. Use that function to mimic the string Apr 29, 2025 · Bazel Label Handling Relevant source files Purpose and Scope This document describes how Gazelle processes, parses, and manipulates Bazel labels. Symbolic macros Prevent action at a distance Make it The result of this function is the same Label value as would be produced by passing the given string to a label-valued attribute of a target declared in the BUILD file. chtzo psymbcs pyw cmcu gvs kcmc ecubh iqi lmili fna dwfyjgra lit pomjp yuzss glminm