| 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 |
"""This file contains BUILD extensions for generating source code from LLVM's |
| 6 |
table definition files using the TableGen tool. |
| 7 |
|
| 8 |
See http://llvm.org/cmds/tblgen.html for more information on the TableGen |
| 9 |
tool. |
| 10 |
TODO(chandlerc): Currently this expresses include-based dependencies as |
| 11 |
"sources", and has no transitive understanding due to these files not being |
| 12 |
correctly understood by the build system. |
| 13 |
""" |
| 14 |
|
| 15 |
def gentbl( |
| 16 |
name, |
| 17 |
tblgen, |
| 18 |
td_file, |
| 19 |
td_srcs, |
| 20 |
tbl_outs, |
| 21 |
library = True, |
| 22 |
tblgen_args = "", |
| 23 |
**kwargs): |
| 24 |
"""gentbl() generates tabular code from a table definition file. |
| 25 |
|
| 26 |
Args: |
| 27 |
name: The name of the build rule for use in dependencies. |
| 28 |
tblgen: The binary used to produce the output. |
| 29 |
td_file: The primary table definitions file. |
| 30 |
td_srcs: A list of table definition files included transitively. |
| 31 |
tbl_outs: A list of tuples (opts, out), where each opts is a string of |
| 32 |
options passed to tblgen, and the out is the corresponding output file |
| 33 |
produced. |
| 34 |
library: Whether to bundle the generated files into a library. |
| 35 |
tblgen_args: Extra arguments string to pass to the tblgen binary. |
| 36 |
**kwargs: Keyword arguments to pass to subsidiary cc_library() rule. |
| 37 |
""" |
| 38 |
llvm_project_execroot_path = Label("//llvm:tblgen.bzl").workspace_root |
| 39 |
|
| 40 |
if td_file not in td_srcs: |
| 41 |
td_srcs += [td_file] |
| 42 |
for (opts, out) in tbl_outs: |
| 43 |
rule_suffix = "_".join(opts.replace("-", "_").replace("=", "_").split(" ")) |
| 44 |
native.genrule( |
| 45 |
name = "%s_%s_genrule" % (name, rule_suffix), |
| 46 |
srcs = td_srcs, |
| 47 |
outs = [out], |
| 48 |
tools = [tblgen], |
| 49 |
message = "Generating code from table: %s" % td_file, |
| 50 |
cmd = (("$(location %s) -I %s/llvm/include " + |
| 51 |
"-I %s/clang/include " + |
| 52 |
"-I $$(dirname $(location %s)) " + |
| 53 |
"%s $(location %s) %s -o $@") % ( |
| 54 |
tblgen, |
| 55 |
llvm_project_execroot_path, |
| 56 |
llvm_project_execroot_path, |
| 57 |
td_file, |
| 58 |
opts, |
| 59 |
td_file, |
| 60 |
tblgen_args, |
| 61 |
)), |
| 62 |
) |
| 63 |
|
| 64 |
# For now, all generated files can be assumed to comprise public interfaces. |
| 65 |
# If this is not true, you should specify library = False |
| 66 |
# and list the generated '.inc' files in "srcs". |
| 67 |
if library: |
| 68 |
native.cc_library( |
| 69 |
name = name, |
| 70 |
# FIXME: This should be `textual_hdrs` instead of `hdrs`, but |
| 71 |
# unfortunately that doesn't work with `strip_include_prefix`: |
| 72 |
# https://github.com/bazelbuild/bazel/issues/12424 |
| 73 |
# |
| 74 |
# Once that issue is fixed and released, we can switch this to |
| 75 |
# `textual_hdrs` and remove the feature disabling the various Bazel |
| 76 |
# features (both current and under-development) that motivated the |
| 77 |
# distinction between these two. |
| 78 |
hdrs = [f for (_, f) in tbl_outs], |
| 79 |
features = ["-parse_headers", "-header_modules"], |
| 80 |
**kwargs |
| 81 |
) |