github.com/johnnyeven/libtools@v0.0.0-20191126065708-61829c1adf46/third_party/llvm/llvm.bzl (about) 1 """This file contains BUILD extensions for generating source code from LLVM's table definition files using the TableGen tool. 2 3 See http://llvm.org/cmds/tblgen.html for more information on the TableGen 4 tool. 5 TODO(chandlerc): Currently this expresses include-based dependencies as 6 "sources", and has no transitive understanding due to these files not being 7 correctly understood by the build system. 8 """ 9 10 def _dict_add(*dictionaries): 11 """Returns a new `dict` that has all the entries of the given dictionaries. 12 13 If the same key is present in more than one of the input dictionaries, the 14 last of them in the argument list overrides any earlier ones. 15 16 This function is designed to take zero or one arguments as well as multiple 17 dictionaries, so that it follows arithmetic identities and callers can avoid 18 special cases for their inputs: the sum of zero dictionaries is the empty 19 dictionary, and the sum of a single dictionary is a copy of itself. 20 21 Re-implemented here to avoid adding a dependency on skylib. 22 23 Args: 24 *dictionaries: Zero or more dictionaries to be added. 25 26 Returns: 27 A new `dict` that has all the entries of the given dictionaries. 28 """ 29 result = {} 30 for d in dictionaries: 31 result.update(d) 32 return result 33 34 def gentbl(name, tblgen, td_file, td_srcs, tbl_outs, library = True, **kwargs): 35 """gentbl() generates tabular code from a table definition file. 36 37 Args: 38 name: The name of the build rule for use in dependencies. 39 tblgen: The binary used to produce the output. 40 td_file: The primary table definitions file. 41 td_srcs: A list of table definition files included transitively. 42 tbl_outs: A list of tuples (opts, out), where each opts is a string of 43 options passed to tblgen, and the out is the corresponding output file 44 produced. 45 library: Whether to bundle the generated files into a library. 46 **kwargs: Keyword arguments to pass to subsidiary cc_library() rule. 47 """ 48 if td_file not in td_srcs: 49 td_srcs += [td_file] 50 includes = [] 51 for (opts, out) in tbl_outs: 52 outdir = out[:out.rindex("/")] 53 if outdir not in includes: 54 includes.append(outdir) 55 rule_suffix = "_".join(opts.replace("-", "_").replace("=", "_").split(" ")) 56 native.genrule( 57 name = "%s_%s_genrule" % (name, rule_suffix), 58 srcs = td_srcs, 59 outs = [out], 60 tools = [tblgen], 61 message = "Generating code from table: %s" % td_file, 62 cmd = (("$(location %s) " + "-I external/llvm/include " + 63 "-I external/llvm/tools/clang/include " + 64 "-I $$(dirname $(location %s)) " + "%s $(location %s) -o $@") % ( 65 tblgen, 66 td_file, 67 opts, 68 td_file, 69 )), 70 ) 71 72 # For now, all generated files can be assumed to comprise public interfaces. 73 # If this is not true, you should specify library = False 74 # and list the generated '.inc' files in "srcs". 75 if library: 76 native.cc_library( 77 name = name, 78 textual_hdrs = [f for (_, f) in tbl_outs], 79 includes = includes, 80 **kwargs 81 ) 82 83 def llvm_target_cmake_vars(native_arch, target_triple): 84 return { 85 "LLVM_HOST_TRIPLE": target_triple, 86 "LLVM_DEFAULT_TARGET_TRIPLE": target_triple, 87 "LLVM_NATIVE_ARCH": native_arch, 88 } 89 90 def _quote(s): 91 """Quotes the given string for use in a shell command. 92 93 This function double-quotes the given string (in case it contains spaces or 94 other special characters) and escapes any special characters (dollar signs, 95 double-quotes, and backslashes) that may be present. 96 97 Args: 98 s: The string to quote. 99 100 Returns: 101 An escaped and quoted version of the string that can be passed to a shell 102 command. 103 """ 104 return ('"' + 105 s.replace("\\", "\\\\").replace("$", "\\$").replace('"', '\\"') + 106 '"') 107 108 def cmake_var_string(cmake_vars): 109 """Converts a dictionary to an input suitable for expand_cmake_vars. 110 111 Ideally we would jist stringify in the expand_cmake_vars() rule, but select() 112 interacts badly with genrules. 113 114 TODO(phawkins): replace the genrule() with native rule and delete this rule. 115 116 Args: 117 cmake_vars: a dictionary with string keys and values that are convertable to 118 strings. 119 120 Returns: 121 cmake_vars in a form suitable for passing to expand_cmake_vars. 122 """ 123 return " ".join([ 124 _quote("{}={}".format(k, str(v))) 125 for (k, v) in cmake_vars.items() 126 ]) 127 128 def expand_cmake_vars(name, src, dst, cmake_vars): 129 """Expands #cmakedefine, #cmakedefine01, and CMake variables in a text file. 130 131 Args: 132 name: the name of the rule 133 src: the input of the rule 134 dst: the output of the rule 135 cmake_vars: a string containing the CMake variables, as generated by 136 cmake_var_string. 137 """ 138 expand_cmake_vars_tool = "@org_tensorflow//third_party/llvm:expand_cmake_vars" 139 native.genrule( 140 name = name, 141 srcs = [src], 142 tools = [expand_cmake_vars_tool], 143 outs = [dst], 144 cmd = ("$(location {}) ".format(expand_cmake_vars_tool) + cmake_vars + 145 "< $< > $@"), 146 ) 147 148 # TODO(phawkins): the set of CMake variables was hardcoded for expediency. 149 # However, we should really detect many of these via configure-time tests. 150 151 # The set of CMake variables common to all targets. 152 cmake_vars = { 153 # LLVM features 154 "ENABLE_BACKTRACES": 1, 155 "LLVM_BINDIR": "/dev/null", 156 "LLVM_DISABLE_ABI_BREAKING_CHECKS_ENFORCING": 0, 157 "LLVM_ENABLE_ABI_BREAKING_CHECKS": 0, 158 "LLVM_ENABLE_THREADS": 1, 159 "LLVM_ENABLE_ZLIB": 1, 160 "LLVM_HAS_ATOMICS": 1, 161 "LLVM_INCLUDEDIR": "/dev/null", 162 "LLVM_INFODIR": "/dev/null", 163 "LLVM_MANDIR": "/dev/null", 164 "LLVM_NATIVE_TARGET": 1, 165 "LLVM_NATIVE_TARGETINFO": 1, 166 "LLVM_NATIVE_TARGETMC": 1, 167 "LLVM_NATIVE_ASMPRINTER": 1, 168 "LLVM_NATIVE_ASMPARSER": 1, 169 "LLVM_NATIVE_DISASSEMBLER": 1, 170 "LLVM_PREFIX": "/dev/null", 171 "LLVM_VERSION_MAJOR": 0, 172 "LLVM_VERSION_MINOR": 0, 173 "LLVM_VERSION_PATCH": 0, 174 "PACKAGE_NAME": "llvm", 175 "PACKAGE_STRING": "llvm tensorflow-trunk", 176 "PACKAGE_VERSION": "tensorflow-trunk", 177 "RETSIGTYPE": "void", 178 } 179 180 # The set of CMake variables common to POSIX targets. 181 posix_cmake_vars = { 182 # Headers 183 "HAVE_DIRENT_H": 1, 184 "HAVE_DLFCN_H": 1, 185 "HAVE_ERRNO_H": 1, 186 "HAVE_EXECINFO_H": 1, 187 "HAVE_FCNTL_H": 1, 188 "HAVE_INTTYPES_H": 1, 189 "HAVE_PTHREAD_H": 1, 190 "HAVE_SIGNAL_H": 1, 191 "HAVE_STDINT_H": 1, 192 "HAVE_SYS_IOCTL_H": 1, 193 "HAVE_SYS_MMAN_H": 1, 194 "HAVE_SYS_PARAM_H": 1, 195 "HAVE_SYS_RESOURCE_H": 1, 196 "HAVE_SYS_STAT_H": 1, 197 "HAVE_SYS_TIME_H": 1, 198 "HAVE_SYS_TYPES_H": 1, 199 "HAVE_TERMIOS_H": 1, 200 "HAVE_UNISTD_H": 1, 201 "HAVE_ZLIB_H": 1, 202 203 # Features 204 "HAVE_BACKTRACE": 1, 205 "BACKTRACE_HEADER": "execinfo.h", 206 "HAVE_DLOPEN": 1, 207 "HAVE_FUTIMES": 1, 208 "HAVE_GETCWD": 1, 209 "HAVE_GETPAGESIZE": 1, 210 "HAVE_GETRLIMIT": 1, 211 "HAVE_GETRUSAGE": 1, 212 "HAVE_GETTIMEOFDAY": 1, 213 "HAVE_INT64_T": 1, 214 "HAVE_ISATTY": 1, 215 "HAVE_LIBEDIT": 1, 216 "HAVE_LIBPTHREAD": 1, 217 "HAVE_LIBZ": 1, 218 "HAVE_MKDTEMP": 1, 219 "HAVE_MKSTEMP": 1, 220 "HAVE_MKTEMP": 1, 221 "HAVE_PREAD": 1, 222 "HAVE_PTHREAD_GETSPECIFIC": 1, 223 "HAVE_PTHREAD_MUTEX_LOCK": 1, 224 "HAVE_PTHREAD_RWLOCK_INIT": 1, 225 "HAVE_REALPATH": 1, 226 "HAVE_SBRK": 1, 227 "HAVE_SETENV": 1, 228 "HAVE_SETRLIMIT": 1, 229 "HAVE_SIGALTSTACK": 1, 230 "HAVE_STRERROR": 1, 231 "HAVE_STRERROR_R": 1, 232 "HAVE_STRTOLL": 1, 233 "HAVE_SYSCONF": 1, 234 "HAVE_UINT64_T": 1, 235 "HAVE__UNWIND_BACKTRACE": 1, 236 237 # LLVM features 238 "LLVM_ON_UNIX": 1, 239 "LTDL_SHLIB_EXT": ".so", 240 } 241 242 # CMake variables specific to the Linux platform 243 linux_cmake_vars = { 244 "HAVE_MALLOC_H": 1, 245 "HAVE_LINK_H": 1, 246 "HAVE_MALLINFO": 1, 247 "HAVE_FUTIMENS": 1, 248 } 249 250 # CMake variables specific to the FreeBSD platform 251 freebsd_cmake_vars = { 252 "HAVE_MALLOC_H": 1, 253 "HAVE_LINK_H": 1, 254 } 255 256 # CMake variables specific to the Darwin (Mac OS X) platform. 257 darwin_cmake_vars = { 258 "HAVE_MALLOC_MALLOC_H": 1, 259 "HAVE_MALLOC_ZONE_STATISTICS": 1, 260 } 261 262 # CMake variables specific to the Windows platform. 263 win32_cmake_vars = { 264 # Headers 265 "HAVE_ERRNO_H": 1, 266 "HAVE_EXECINFO_H": 1, 267 "HAVE_FCNTL_H": 1, 268 "HAVE_FENV_H": 1, 269 "HAVE_INTTYPES_H": 1, 270 "HAVE_MALLOC_H": 1, 271 "HAVE_SIGNAL_H": 1, 272 "HAVE_STDINT_H": 1, 273 "HAVE_SYS_STAT_H": 1, 274 "HAVE_SYS_TYPES_H": 1, 275 "HAVE_ZLIB_H": 1, 276 277 # Features 278 "BACKTRACE_HEADER": "execinfo.h", 279 "HAVE_GETCWD": 1, 280 "HAVE_INT64_T": 1, 281 "HAVE_STRERROR": 1, 282 "HAVE_STRTOLL": 1, 283 "HAVE_SYSCONF": 1, 284 "HAVE_UINT64_T": 1, 285 "HAVE__CHSIZE_S": 1, 286 "HAVE___CHKSTK": 1, 287 288 # MSVC specific 289 "stricmp": "_stricmp", 290 "strdup": "_strdup", 291 292 # LLVM features 293 "LTDL_SHLIB_EXT": ".dll", 294 } 295 296 # Select a set of CMake variables based on the platform. 297 # TODO(phawkins): use a better method to select the right host triple, rather 298 # than hardcoding x86_64. 299 llvm_all_cmake_vars = select({ 300 "@org_tensorflow//tensorflow:macos": cmake_var_string( 301 _dict_add( 302 cmake_vars, 303 llvm_target_cmake_vars("X86", "x86_64-apple-darwin"), 304 posix_cmake_vars, 305 darwin_cmake_vars, 306 ), 307 ), 308 "@org_tensorflow//tensorflow:linux_ppc64le": cmake_var_string( 309 _dict_add( 310 cmake_vars, 311 llvm_target_cmake_vars("PowerPC", "powerpc64le-unknown-linux_gnu"), 312 posix_cmake_vars, 313 linux_cmake_vars, 314 ), 315 ), 316 "@org_tensorflow//tensorflow:windows": cmake_var_string( 317 _dict_add( 318 cmake_vars, 319 llvm_target_cmake_vars("X86", "x86_64-pc-win32"), 320 win32_cmake_vars, 321 ), 322 ), 323 "@org_tensorflow//tensorflow:freebsd": cmake_var_string( 324 _dict_add( 325 cmake_vars, 326 llvm_target_cmake_vars("X86", "x86_64-unknown-freebsd"), 327 posix_cmake_vars, 328 ), 329 ), 330 "//conditions:default": cmake_var_string( 331 _dict_add( 332 cmake_vars, 333 llvm_target_cmake_vars("X86", "x86_64-unknown-linux_gnu"), 334 posix_cmake_vars, 335 linux_cmake_vars, 336 ), 337 ), 338 }) 339 340 llvm_linkopts = select({ 341 "@org_tensorflow//tensorflow:windows": [], 342 "@org_tensorflow//tensorflow:freebsd": ["-ldl", "-lm", "-lpthread", "-lexecinfo"], 343 "//conditions:default": ["-ldl", "-lm", "-lpthread"], 344 }) 345 346 llvm_defines = select({ 347 "@org_tensorflow//tensorflow:windows": [ 348 "_CRT_SECURE_NO_DEPRECATE", 349 "_CRT_SECURE_NO_WARNINGS", 350 "_CRT_NONSTDC_NO_DEPRECATE", 351 "_CRT_NONSTDC_NO_WARNINGS", 352 "_SCL_SECURE_NO_DEPRECATE", 353 "_SCL_SECURE_NO_WARNINGS", 354 "UNICODE", 355 "_UNICODE", 356 ], 357 "//conditions:default": [], 358 }) + [ 359 "LLVM_ENABLE_STATS", 360 "__STDC_LIMIT_MACROS", 361 "__STDC_CONSTANT_MACROS", 362 "__STDC_FORMAT_MACROS", 363 "LLVM_BUILD_GLOBAL_ISEL", 364 ] 365 366 llvm_copts = select({ 367 "@org_tensorflow//tensorflow:windows": [ 368 "-Zc:inline", 369 "-Zc:strictStrings", 370 "-Zc:rvalueCast", 371 "-Oi", 372 "-wd4141", 373 "-wd4146", 374 "-wd4180", 375 "-wd4244", 376 "-wd4258", 377 "-wd4267", 378 "-wd4291", 379 "-wd4345", 380 "-wd4351", 381 "-wd4355", 382 "-wd4456", 383 "-wd4457", 384 "-wd4458", 385 "-wd4459", 386 "-wd4503", 387 "-wd4624", 388 "-wd4722", 389 "-wd4800", 390 "-wd4100", 391 "-wd4127", 392 "-wd4512", 393 "-wd4505", 394 "-wd4610", 395 "-wd4510", 396 "-wd4702", 397 "-wd4245", 398 "-wd4706", 399 "-wd4310", 400 "-wd4701", 401 "-wd4703", 402 "-wd4389", 403 "-wd4611", 404 "-wd4805", 405 "-wd4204", 406 "-wd4577", 407 "-wd4091", 408 "-wd4592", 409 "-wd4319", 410 "-wd4324", 411 "-w14062", 412 "-we4238", 413 ], 414 "//conditions:default": [], 415 }) 416 417 # Platform specific sources for libSupport. 418 419 def llvm_support_platform_specific_srcs_glob(): 420 return select({ 421 "@org_tensorflow//tensorflow:windows": native.glob([ 422 "lib/Support/Windows/*.inc", 423 "lib/Support/Windows/*.h", 424 ]), 425 "//conditions:default": native.glob([ 426 "lib/Support/Unix/*.inc", 427 "lib/Support/Unix/*.h", 428 ]), 429 })