Debian Codegraph @

utils/bazel/llvm-project-overlay/mlir/build_defs.bzl

raw
1 # This file is licensed under the Apache License v2.0 with LLVM Exceptions.
2 # See https://llvm.org/LICENSE.txt for license information.
3 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5 """Rules and macros for MLIR"""
6
7 def if_cuda_available(if_true, if_false = []):
8 return select({
9 # CUDA auto-detection is not yet supported.
10 "//mlir:enable_cuda_config": if_true,
11 "//conditions:default": if_false,
12 })
13
14 def _cc_headers_only_impl(ctx):
15 return CcInfo(compilation_context = ctx.attr.src[CcInfo].compilation_context)
16
17 cc_headers_only = rule(
18 implementation = _cc_headers_only_impl,
19 attrs = {
20 "src": attr.label(
21 mandatory = True,
22 providers = [CcInfo],
23 ),
24 },
25 doc = "Provides the headers from 'src' without linking anything.",
26 provides = [CcInfo],
27 )
28
29 def mlir_c_api_cc_library(
30 name,
31 srcs = [],
32 hdrs = [],
33 deps = [],
34 header_deps = [],
35 capi_deps = [],
36 **kwargs):
37 """Macro that generates three targets for MLIR C API libraries.
38
39 * A standard cc_library target ("Name"),
40 * A header-only cc_library target ("NameHeaders")
41 * An implementation cc_library target tagged `alwayslink` suitable for
42 inclusion in a shared library built with cc_binary() ("NameObjects").
43
44 In order to avoid duplicate symbols, it is important that
45 mlir_c_api_cc_library targets only depend on other mlir_c_api_cc_library
46 targets via the "capi_deps" parameter. This makes it so that "FooObjects"
47 depend on "BarObjects" targets and "Foo" targets depend on "Bar" targets.
48 Don't cross the streams.
49 """
50 capi_header_deps = ["%sHeaders" % d for d in capi_deps]
51 capi_object_deps = ["%sObjects" % d for d in capi_deps]
52 native.cc_library(
53 name = name,
54 srcs = srcs,
55 hdrs = hdrs,
56 deps = deps + capi_deps + header_deps,
57 **kwargs
58 )
59 native.cc_library(
60 name = name + "Headers",
61 hdrs = hdrs,
62 deps = header_deps + capi_header_deps,
63 **kwargs
64 )
65 native.cc_library(
66 name = name + "Objects",
67 srcs = srcs,
68 hdrs = hdrs,
69 deps = deps + capi_object_deps + capi_header_deps + header_deps,
70 alwayslink = True,
71 **kwargs
72 )