diff options
author | Sanjoy Das <sanjoy@google.com> | 2018-07-30 14:52:10 -0700 |
---|---|---|
committer | TensorFlower Gardener <gardener@tensorflow.org> | 2018-07-30 14:55:13 -0700 |
commit | f017a9b9b400e65e7d616675beb46fea24f8bb2a (patch) | |
tree | 590d2300daaa5fd96508229d6fc9df699c315fcd /third_party/llvm | |
parent | cb07a15c1d85a0bf1b33e0e7a85c8401e3f65c5c (diff) |
Fix some whitespace and lint issues with llvm.bzl.
PiperOrigin-RevId: 206651482
Diffstat (limited to 'third_party/llvm')
-rw-r--r-- | third_party/llvm/llvm.bzl | 236 |
1 files changed, 142 insertions, 94 deletions
diff --git a/third_party/llvm/llvm.bzl b/third_party/llvm/llvm.bzl index dfdacafceb..c5bebc9f2f 100644 --- a/third_party/llvm/llvm.bzl +++ b/third_party/llvm/llvm.bzl @@ -7,103 +7,143 @@ TODO(chandlerc): Currently this expresses include-based dependencies as correctly understood by the build system. """ +def _dict_add(*dictionaries): + """Returns a new `dict` that has all the entries of the given dictionaries. + + If the same key is present in more than one of the input dictionaries, the + last of them in the argument list overrides any earlier ones. + + This function is designed to take zero or one arguments as well as multiple + dictionaries, so that it follows arithmetic identities and callers can avoid + special cases for their inputs: the sum of zero dictionaries is the empty + dictionary, and the sum of a single dictionary is a copy of itself. + + Re-implemented here to avoid adding a dependency on skylib. + + Args: + *dictionaries: Zero or more dictionaries to be added. + + Returns: + A new `dict` that has all the entries of the given dictionaries. + """ + result = {} + for d in dictionaries: + result.update(d) + return result + 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) + """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 + ) 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, - } + 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('"', '\\"') + - '"') + """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. + """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. + 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. + 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()]) + Args: + cmake_vars: a dictionary with string keys and values that are convertable to + strings. + + Returns: + cmake_vars in a form suitable for passing to expand_cmake_vars. + """ + 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 = Label("@org_tensorflow//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 + - "< $< > $@") - ) + """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 = Label("@org_tensorflow//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 + + "< $< > $@"), + ) # TODO(phawkins): the set of CMake variables was hardcoded for expediency. # However, we should really detect many of these via configure-time tests. @@ -212,18 +252,26 @@ darwin_cmake_vars = { # than hardcoding x86_64. llvm_all_cmake_vars = select({ "@org_tensorflow//tensorflow:darwin": cmake_var_string( - cmake_vars + llvm_target_cmake_vars("X86", "x86_64-apple-darwin") + - darwin_cmake_vars), + _dict_add( + cmake_vars, + llvm_target_cmake_vars("X86", "x86_64-apple-darwin"), + darwin_cmake_vars, + ), + ), "@org_tensorflow//tensorflow:linux_ppc64le": cmake_var_string( - cmake_vars + - llvm_target_cmake_vars("PowerPC", "powerpc64le-unknown-linux_gnu") + - linux_cmake_vars, + _dict_add( + cmake_vars, + llvm_target_cmake_vars("PowerPC", "powerpc64le-unknown-linux_gnu"), + linux_cmake_vars, + ), ), "//conditions:default": cmake_var_string( - cmake_vars + - llvm_target_cmake_vars("X86", "x86_64-unknown-linux_gnu") + - linux_cmake_vars), - + _dict_add( + cmake_vars, + llvm_target_cmake_vars("X86", "x86_64-unknown-linux_gnu"), + linux_cmake_vars, + ), + ), }) llvm_linkopts = ["-ldl", "-lm", "-lpthread"] |