75 lines · plain
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-exception4 5"""Rules and macros for MLIR"""6 7load("@rules_cc//cc:defs.bzl", "CcInfo", "cc_library")8 9def 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 16def _cc_headers_only_impl(ctx):17 return CcInfo(compilation_context = ctx.attr.src[CcInfo].compilation_context)18 19cc_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 31def 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 for44 inclusion in a shared library built with cc_binary() ("NameObjects").45 46 In order to avoid duplicate symbols, it is important that47 mlir_c_api_cc_library targets only depend on other mlir_c_api_cc_library48 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 **kwargs60 )61 cc_library(62 name = name + "Headers",63 hdrs = hdrs,64 deps = header_deps + capi_header_deps,65 **kwargs66 )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 **kwargs74 )75