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