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 """This file contains BUILD extensions for generating source code from LLVM's
6 table definition files using the TableGen tool.
8 See http://llvm.org/cmds/tblgen.html for more information on the TableGen
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.
24 """gentbl() generates tabular code from a table definition file.
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
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.
38 llvm_project_execroot_path = Label("//llvm:tblgen.bzl").workspace_root
40 if td_file not in td_srcs:
42 for (opts, out) in tbl_outs:
43 rule_suffix = "_".join(opts.replace("-", "_").replace("=", "_").split(" "))
45 name = "%s_%s_genrule" % (name, rule_suffix),
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 $@") % (
55 llvm_project_execroot_path,
56 llvm_project_execroot_path,
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".
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
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"],