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