aboutsummaryrefslogtreecommitdiffhomepage
path: root/third_party/llvm/llvm.bzl
blob: 1d6bf1c7065a7c4734c9723b9c9e8b1c1439a737 (plain)
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
"""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, **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.
    **kwargs: Keyword arguments to pass to subsidiary cc_library() rule.
  """
  if td_file not in td_srcs:
    td_srcs += [td_file]
  includes = []
  for (opts, out) in tbl_outs:
    outdir = out[:out.rindex("/")]
    if outdir not in includes:
      includes.append(outdir)
    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 external/llvm/include " +
              "-I external/llvm/tools/clang/include " +
              "-I $$(dirname $(location %s)) " + "%s $(location %s) -o $@") % (
                  tblgen, td_file, opts, td_file)))
  # 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, textual_hdrs=[f for (_, f) in tbl_outs],
                      includes=includes,  **kwargs)


# Rule for simple expansion of template files. This performs a simple
# search over the template file for the keys in substitutions,
# and replaces them with the corresponding values.
#
# Typical usage:
#   load("/tools/build_rules/expand_header_template", "expand_header_template")
#   expand_header_template(
#       name = "ExpandMyTemplate",
#       template = "my.template",
#       out = "my.txt",
#       substitutions = {
#         "$VAR1": "foo",
#         "$VAR2": "bar",
#       }
#   )
#
# Args:
#   name: The name of the rule.
#   template: The template file to expand
#   out: The destination of the expanded file
#   substitutions: A dictionary mapping strings to their substitutions

def expand_header_template_impl(ctx):
  ctx.template_action(
      template = ctx.file.template,
      output = ctx.outputs.out,
      substitutions = ctx.attr.substitutions,
  )

expand_header_template = rule(
    implementation = expand_header_template_impl,
    attrs = {
        "template": attr.label(mandatory=True, allow_files=True, single_file=True),
        "substitutions": attr.string_dict(mandatory=True),
        "out": attr.output(mandatory=True),
    },
    # output_to_genfiles is required for header files.
    output_to_genfiles = True,
)


def llvm_target_cmake_vars(native_arch, target_triple):
  return {
      "LLVM_HOST_TRIPLE": target_triple,
      "LLVM_DEFAULT_TARGET_TRIPLE": target_triple,
      "LLVM_NATIVE_ARCH": native_arch,
  }

def _quote(s):
  """Quotes the given string for use in a shell command.

  This function double-quotes the given string (in case it contains spaces or
  other special characters) and escapes any special characters (dollar signs,
  double-quotes, and backslashes) that may be present.

  Args:
    s: The string to quote.
  Returns:
    An escaped and quoted version of the string that can be passed to a shell
    command.
  """
  return ('"' +
          s.replace("\\", "\\\\").replace("$", "\\$").replace('"', '\\"') +
          '"')

def cmake_var_string(cmake_vars):
  """Converts a dictionary to an input suitable for expand_cmake_vars.

  Ideally we would jist stringify in the expand_cmake_vars() rule, but select()
  interacts badly with genrules.

  TODO(phawkins): replace the genrule() with native rule and delete this rule.

  Args:
    cmake_vars: a dictionary with string keys and values that are convertable to
      strings.
  """
  return " ".join([_quote("{}={}".format(k, str(v)))
                   for (k, v) in cmake_vars.items()])

def expand_cmake_vars(name, src, dst, cmake_vars):
  """Expands #cmakedefine, #cmakedefine01, and CMake variables in a text file.

  Args:
    name: the name of the rule
    src: the input of the rule
    dst: the output of the rule
    cmake_vars: a string containing the CMake variables, as generated by
      cmake_var_string.
  """
  expand_cmake_vars_tool = "@//third_party/llvm:expand_cmake_vars"
  native.genrule(
      name = name,
      srcs = [src],
      tools = [expand_cmake_vars_tool],
      outs = [dst],
      cmd = ("$(location {}) ".format(expand_cmake_vars_tool) + cmake_vars +
             "< $< > $@")
  )