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 7Replaces in a text file a single variable of the style `@LLVM_ENUM_FOOS@` with a 8list of macro invocations, one for each target on its own line: 9 10``` 11LLVM_FOO(TARGET1) 12LLVM_FOO(TARGET2) 13// ... 14``` 15 16Example: 17load(":enum_targets_gen.bzl", "enum_targets_gen") 18 19enum_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 27This rule provides a slightly more semantic API than template_rule, but the main 28reason it exists is to permit a list with selects to be passed for `targets` as 29a select is not allowed to be passed to a rule within another data structure. 30""" 31 32def 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 47enum_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 implementation = enum_targets_gen_impl, 68) 69