提交 b523c977 编写于 作者: P Peter Hawkins 提交者: TensorFlower Gardener

Adds a Bazel BUILD for LLVM.

Change: 139938302
上级 e3093342
......@@ -193,6 +193,16 @@ def tf_workspace(path_prefix = "", tf_repo_name = ""):
build_file = str(Label("//:linenoise.BUILD")),
)
# TODO(phawkins): currently, this rule uses an unofficial LLVM mirror.
# Switch to an official source of snapshots if/when possible.
native.new_http_archive(
name = "llvm",
url = "http://github.com/llvm-mirror/llvm/archive/ad27fdae895df1b9ad11a93102de6622f63e1220.tar.gz",
sha256 = "ce7abf076586f2ef13dcd1c4e7ba13604a0826a0f44fe0a6faceeb9bdffc8544",
strip_prefix = "llvm-ad27fdae895df1b9ad11a93102de6622f63e1220",
build_file = str(Label("//third_party/llvm:llvm.BUILD")),
)
native.new_http_archive(
name = "jsoncpp_git",
url = "http://github.com/open-source-parsers/jsoncpp/archive/11086dd6a7eba04289944367ca82cea71299ed70.tar.gz",
......
licenses(["notice"])
py_binary(
name = "expand_cmake_vars",
srcs = ["expand_cmake_vars.py"],
visibility = ["@llvm//:__subpackages__"],
)
# Copyright 2016 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Expands CMake variables in a text file."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import re
import sys
_CMAKE_DEFINE_REGEX = re.compile(r"\s*#cmakedefine\s+([A-Za-z_0-9]*)(\s.*)?$")
_CMAKE_DEFINE01_REGEX = re.compile(r"\s*#cmakedefine01\s+([A-Za-z_0-9]*)")
_CMAKE_VAR_REGEX = re.compile(r"\${([A-Za-z_0-9]*)}")
def _parse_args(argv):
"""Parses arguments with the form KEY=VALUE into a dictionary."""
result = {}
for arg in argv:
k, v = arg.split("=")
result[k] = v
return result
def _expand_variables(input_str, cmake_vars):
"""Expands ${VARIABLE}s in 'input_str', using dictionary 'cmake_vars'.
Args:
input_str: the string containing ${VARIABLE} expressions to expand.
cmake_vars: a dictionary mapping variable names to their values.
Returns:
The expanded string.
"""
def replace(match):
if cmake_vars.has_key(match.group(1)):
return cmake_vars[match.group(1)]
return ""
return _CMAKE_VAR_REGEX.sub(replace, input_str)
def _expand_cmakedefines(line, cmake_vars):
"""Expands #cmakedefine declarations, using a dictionary 'cmake_vars'."""
# Handles #cmakedefine lines
match = _CMAKE_DEFINE_REGEX.match(line)
if match:
name = match.group(1)
suffix = match.group(2) or ""
if name in cmake_vars:
return "#define {}{}\n".format(name,
_expand_variables(suffix, cmake_vars))
else:
return "/* #undef {} */\n".format(name)
# Handles #cmakedefine01 lines
match = _CMAKE_DEFINE01_REGEX.match(line)
if match:
name = match.group(1)
value = cmake_vars.get(name, "0")
return "#define {} {}\n".format(name, value)
# Otherwise return the line unchanged.
return _expand_variables(line, cmake_vars)
def main():
cmake_vars = _parse_args(sys.argv[1:])
for line in sys.stdin:
sys.stdout.write(_expand_cmakedefines(line, cmake_vars))
if __name__ == "__main__":
main()
此差异已折叠。
"""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 +
"< $< > $@")
)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册