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 = ctx.attr.placeholder_name
35 to_replace = "@LLVM_ENUM_{}S@".format(ctx.attr.macro_name)
36 replacement = "\n".join([
37 "LLVM_{}({})\n".format(ctx.attr.macro_name, t)
38 for t in ctx.attr.targets
41 ctx.actions.expand_template(
42 template = ctx.file.src,
43 output = ctx.outputs.out,
44 substitutions = {to_replace: replacement},
47 enum_targets_gen = rule(
51 allow_single_file = True,
53 "out": attr.output(mandatory = True),
54 "targets": attr.string_list(mandatory = True),
55 "macro_name": attr.string(
57 doc = "The name of the enumeration. This is the suffix of the" +
58 " placeholder being replaced `@LLVM_ENUM_{}S@` and of the" +
59 " macro invocations generated `LLVM_{}(TARGET)`. Should be" +
60 " all caps and singular, e.g. 'DISASSEMBLER'",
62 "placeholder_name": attr.string(
63 doc = "The name of the placeholder. If unset, this defaults to" +
64 " `@LLVM_ENUM_{macro_name}S@`",
67 # output_to_genfiles is required for header files.
68 output_to_genfiles = True,
69 implementation = enum_targets_gen_impl,