The rule foo is created (due to a name conflict), which will show you the Insert this line near the top of the BUILDįile: cc_library(name = "foo"). To find out where exactly the rule foo is generated in a BUILD file, youĬan try the following trick. $ bazel query -output=build 'attr(generator_function, my_macro, //my/path:all)' Generated the rules) or generator_name (the name attribute of the macro): You may filter the output based on generator_function (which function Limitation: select expressions are currently not shown in the output. # in the main workspace, such as due to repo ,īazel query -output=build //my/path:all will show you how the BUILD file # even if the canonical name of should be different # Due to the use of Label, this label will resolve to the correct target # Due to the use of Label, this label is resolved within regardless of its site of use. Label constructor: # my_cc_wrapper(name, deps =, **kwargs): To get the same behavior as for Starlark rules, wrap the label strings with the This behavior is generally undesirableįor macros that are meant to be used in other repositories, such as because theyĪre part of a published Starlark ruleset. Relative to the BUILD file in which the macro is used rather than relative to Label strings such as "//foo:bar" that occur in a macro are interpreted Since macros are evaluated in the loading phase, Macro), use the function native.package_name(). If you need to know the package name (for example, which BUILD file is calling the Instantiated from the native module: def my_macro(name, visibility=None): Native rules (rules that don't need a load() statement) can be Genrules, where a genrule uses the outputs of a previous genrule as inputs: def chained_genrules(name, visibility=None):Ĭmd = "$(location :tool-one) = ,Ĭmd = "$(location :tool-two) $ native rules You can also use macros to chain rules together. bzlįile can be loaded from any package in the workspace.įinally, in path/generator.bzl, write the definition of the macro toĮncapsulate and parameterize the original genrule definition: def file_generator(name, arg, visibility=None):Ĭmd = "$(location //:generator) %s > % arg, bzl file, you keep your BUILD files clean and declarative, The. By putting macro function definitions in a separate Here, you load the file_generator symbol from a. Replace the genrule with the following: load("//path:generator.bzl", "file_generator") Let's call the macro file_generator, which If you want to generate more files with different arguments, you may want toĮxtract this code to a macro function. It is equivalent to $(locations :file.txt). That refers to the execution-time locations of the files in the outs attribute list. ![]() ![]() :generator with a some_arg argument hardcoded in the command: genrule(Ĭmd = "$(location //:generator) some_arg > = , The typical use case for a macro is when you want to reuse a rule.įor example, genrule in a BUILD file generates a file using ![]() Loading phase, macros don't exist anymore,Īnd Bazel sees only the concrete set of instantiated rules. Macros are mainly used for encapsulation and code reuse of existing rules This page covers the basics of using macros and includes typical use cases,Ī macro is a function called from the BUILD file that can instantiate rules.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |