Migrating to Bazel symbolic macros
4 days ago
- #Macros
- #Build Systems
- #Bazel
- Bazel 8 introduced two types of macros: legacy macros and symbolic macros.
- Symbolic macros are recommended for code clarity and include features like typed arguments and visibility control.
- Symbolic macros are expanded in the loading phase, unlike build rules which run in the analysis phase.
- Symbolic macros support lazy evaluation, a feature under consideration for future Bazel releases.
- Naming conventions for symbolic macros require targets to match or start with the macro's name followed by specific characters.
- Symbolic macros must follow Bazel's visibility rules and cannot access undeclared resources directly.
- Positional arguments are not supported in symbolic macros; all attributes must be declared in the `attrs` dictionary.
- Default values in symbolic macros are declared in the `attrs` dictionary, not in the function parameters.
- Symbolic macros can inherit attributes from rules or other macros using the `inherit_attrs` parameter.
- Mutation of arguments is not allowed in symbolic macros; a new object must be created for modifications.
- Symbolic macros support configurable attributes using `select()`, similar to legacy macros.
- Querying macros can be done using `bazel cquery` and `buildozer` for identifying and refactoring macros.
- Legacy macros can still be used and are supported, but symbolic macros offer better clarity and features.