| 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 |
| 4 |
|
| 5 |
"""A rule to expand LLVM target enumerations. |
| 6 |
|
| 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: |
| 9 |
|
| 10 |
``` |
| 11 |
LLVM_FOO(TARGET1) |
| 12 |
LLVM_FOO(TARGET2) |
| 13 |
// ... |
| 14 |
``` |
| 15 |
|
| 16 |
Example: |
| 17 |
load(":enum_targets_gen.bzl", "enum_targets_gen") |
| 18 |
|
| 19 |
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, |
| 25 |
) |
| 26 |
|
| 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. |
| 30 |
""" |
| 31 |
|
| 32 |
def enum_targets_gen_impl(ctx): |
| 33 |
to_replace = ctx.attr.placeholder_name |
| 34 |
if not to_replace: |
| 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 |
| 39 |
]) |
| 40 |
|
| 41 |
ctx.actions.expand_template( |
| 42 |
template = ctx.file.src, |
| 43 |
output = ctx.outputs.out, |
| 44 |
substitutions = {to_replace: replacement}, |
| 45 |
) |
| 46 |
|
| 47 |
enum_targets_gen = rule( |
| 48 |
attrs = { |
| 49 |
"src": attr.label( |
| 50 |
mandatory = True, |
| 51 |
allow_single_file = True, |
| 52 |
), |
| 53 |
"out": attr.output(mandatory = True), |
| 54 |
"targets": attr.string_list(mandatory = True), |
| 55 |
"macro_name": attr.string( |
| 56 |
mandatory = True, |
| 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'", |
| 61 |
), |
| 62 |
"placeholder_name": attr.string( |
| 63 |
doc = "The name of the placeholder. If unset, this defaults to" + |
| 64 |
" `@LLVM_ENUM_{macro_name}S@`", |
| 65 |
), |
| 66 |
}, |
| 67 |
# output_to_genfiles is required for header files. |
| 68 |
output_to_genfiles = True, |
| 69 |
implementation = enum_targets_gen_impl, |
| 70 |
) |