1 # This file is licensed under the Apache License v2.0 with LLVM Exceptions.
2 # See https://llvm.org/LICENSE.txt for license information.
3 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5 """A rule to expand LLVM target enumerations.
7 Replaces in a text file a single variable of the style `@LLVM_ENUM_FOOS@` with a
8 list of macro invocations, one for each target on its own line:
17 load(":enum_targets_gen.bzl", "enum_targets_gen")
20 name = "disassemblers_def_gen",
21 src = "include/llvm/Config/Disassemblers.def.in",
22 out = "include/llvm/Config/Disassemblers.def",
23 macro_name = "DISASSEMBLER",
24 targets = llvm_target_disassemblers,
27 This rule provides a slightly more semantic API than template_rule, but the main
28 reason it exists is to permit a list with selects to be passed for `targets` as
29 a select is not allowed to be passed to a rule within another data structure.
32 def enum_targets_gen_impl(ctx):
33 to_replace = "@LLVM_ENUM_{}S@".format(ctx.attr.macro_name)
34 replacement = "\n".join([
35 "LLVM_{}({})\n".format(ctx.attr.macro_name, t)
36 for t in ctx.attr.targets
39 ctx.actions.expand_template(
40 template = ctx.file.src,
41 output = ctx.outputs.out,
42 substitutions = {to_replace: replacement},
45 enum_targets_gen = rule(
49 allow_single_file = True,
51 "out": attr.output(mandatory = True),
52 "targets": attr.string_list(mandatory = True),
53 "macro_name": attr.string(
55 doc = "The name of the enumeration. This is the suffix of the" +
56 " placeholder being replaced `@LLVM_ENUM_{}S@` and of the" +
57 " macro invocations generated `LLVM_{}(TARGET)`. Should be" +
58 " all caps and singular, e.g. 'DISASSEMBLER'",
61 # output_to_genfiles is required for header files.
62 output_to_genfiles = True,
63 implementation = enum_targets_gen_impl,