File: tblgen.bzl

package info (click to toggle)
llvm-toolchain-15 1%3A15.0.6-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,554,644 kB
  • sloc: cpp: 5,922,452; ansic: 1,012,136; asm: 674,362; python: 191,568; objc: 73,855; f90: 42,327; lisp: 31,913; pascal: 11,973; javascript: 10,144; sh: 9,421; perl: 7,447; ml: 5,527; awk: 3,523; makefile: 2,520; xml: 885; cs: 573; fortran: 567
file content (81 lines) | stat: -rw-r--r-- 3,324 bytes parent folder | download | duplicates (17)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# This file is licensed under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

"""This file contains BUILD extensions for generating source code from LLVM's
table definition files using the TableGen tool.

See http://llvm.org/cmds/tblgen.html for more information on the TableGen
tool.
TODO(chandlerc): Currently this expresses include-based dependencies as
"sources", and has no transitive understanding due to these files not being
correctly understood by the build system.
"""

def gentbl(
        name,
        tblgen,
        td_file,
        td_srcs,
        tbl_outs,
        library = True,
        tblgen_args = "",
        **kwargs):
    """gentbl() generates tabular code from a table definition file.

    Args:
      name: The name of the build rule for use in dependencies.
      tblgen: The binary used to produce the output.
      td_file: The primary table definitions file.
      td_srcs: A list of table definition files included transitively.
      tbl_outs: A list of tuples (opts, out), where each opts is a string of
        options passed to tblgen, and the out is the corresponding output file
        produced.
      library: Whether to bundle the generated files into a library.
      tblgen_args: Extra arguments string to pass to the tblgen binary.
      **kwargs: Keyword arguments to pass to subsidiary cc_library() rule.
    """
    llvm_project_execroot_path = Label("//llvm:tblgen.bzl").workspace_root

    if td_file not in td_srcs:
        td_srcs += [td_file]
    for (opts, out) in tbl_outs:
        rule_suffix = "_".join(opts.replace("-", "_").replace("=", "_").split(" "))
        native.genrule(
            name = "%s_%s_genrule" % (name, rule_suffix),
            srcs = td_srcs,
            outs = [out],
            tools = [tblgen],
            message = "Generating code from table: %s" % td_file,
            cmd = (("$(location %s) -I %s/llvm/include " +
                    "-I %s/clang/include " +
                    "-I $$(dirname $(location %s)) " +
                    "%s $(location %s) %s -o $@") % (
                tblgen,
                llvm_project_execroot_path,
                llvm_project_execroot_path,
                td_file,
                opts,
                td_file,
                tblgen_args,
            )),
        )

    # For now, all generated files can be assumed to comprise public interfaces.
    # If this is not true, you should specify library = False
    # and list the generated '.inc' files in "srcs".
    if library:
        native.cc_library(
            name = name,
            # FIXME: This should be `textual_hdrs` instead of `hdrs`, but
            # unfortunately that doesn't work with `strip_include_prefix`:
            # https://github.com/bazelbuild/bazel/issues/12424
            #
            # Once that issue is fixed and released, we can switch this to
            # `textual_hdrs` and remove the feature disabling the various Bazel
            # features (both current and under-development) that motivated the
            # distinction between these two.
            hdrs = [f for (_, f) in tbl_outs],
            features = ["-parse_headers", "-header_modules"],
            **kwargs
        )