Split headers from implementations in MLIR C API Bazel build.
authorPeter Hawkins <phawkins@google.com>
Thu, 11 Nov 2021 16:34:18 +0000 (08:34 -0800)
committerGeoffrey Martin-Noble <gcmn@google.com>
Thu, 11 Nov 2021 16:34:42 +0000 (08:34 -0800)
This allows clients to build, e.g., the Python bindings against the C API headers, without including the C API implementations. This is useful when distributing software as multiple shared libraries.

Reviewed By: GMNGeoffrey

Differential Revision: https://reviews.llvm.org/D113565

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

index 28ae1131718eebbaacc5f5b24a0c54403cd83e80..306c2698ec157a54c7bd0e957025f0b85e211292 100644 (file)
@@ -7,7 +7,12 @@
 
 load(":tblgen.bzl", "gentbl_cc_library", "td_library")
 load(":linalggen.bzl", "genlinalg")
-load(":build_defs.bzl", "cc_headers_only", "if_cuda_available")
+load(
+    ":build_defs.bzl",
+    "cc_headers_only",
+    "if_cuda_available",
+    "mlir_c_api_cc_library",
+)
 
 package(
     default_visibility = ["//visibility:public"],
@@ -308,7 +313,7 @@ cc_library(
     ],
 )
 
-cc_library(
+mlir_c_api_cc_library(
     name = "CAPIIR",
     srcs = [
         "lib/CAPI/Dialect/Standard.cpp",
@@ -348,20 +353,22 @@ cc_library(
         "include/mlir/CAPI/Utils.h",
         "include/mlir/CAPI/Wrap.h",
     ],
+    header_deps = [
+        ":IR",
+        ":Pass",
+        ":Support",
+        "//llvm:Support",
+    ],
     includes = ["include"],
     deps = [
         ":ConversionPassIncGen",
-        ":IR",
         ":InferTypeOpInterface",
         ":Parser",
-        ":Pass",
         ":StandardOps",
-        ":Support",
-        "//llvm:Support",
     ],
 )
 
-cc_library(
+mlir_c_api_cc_library(
     name = "CAPIInterfaces",
     srcs = [
         "lib/CAPI/Interfaces/Interfaces.cpp",
@@ -375,26 +382,27 @@ cc_library(
     ],
 )
 
-cc_library(
+mlir_c_api_cc_library(
     name = "CAPIAsync",
     srcs = [
         "lib/CAPI/Dialect/Async.cpp",
         "lib/CAPI/Dialect/AsyncPasses.cpp",
     ],
-    hdrs = [
-        "include/mlir-c/Dialect/Async.h",
+    hdrs = ["include/mlir-c/Dialect/Async.h"],
+    header_deps = [
+        ":AsyncPassIncGen",
+        ":CAPIIRHeaders",
     ],
     includes = ["include"],
     deps = [
         ":Async",
-        ":AsyncPassIncGen",
         ":AsyncTransforms",
         ":CAPIIR",
         ":Pass",
     ],
 )
 
-cc_library(
+mlir_c_api_cc_library(
     name = "CAPILinalg",
     srcs = [
         "lib/CAPI/Dialect/Linalg.cpp",
@@ -403,17 +411,20 @@ cc_library(
     hdrs = [
         "include/mlir-c/Dialect/Linalg.h",
     ],
+    header_deps = [
+        ":CAPIIRHeaders",
+        ":LinalgPassIncGen",
+    ],
     includes = ["include"],
     deps = [
         ":CAPIIR",
         ":LinalgOps",
-        ":LinalgPassIncGen",
         ":LinalgTransforms",
         ":Pass",
     ],
 )
 
-cc_library(
+mlir_c_api_cc_library(
     name = "CAPILLVM",
     srcs = [
         "lib/CAPI/Dialect/LLVM.cpp",
@@ -421,6 +432,9 @@ cc_library(
     hdrs = [
         "include/mlir-c/Dialect/LLVM.h",
     ],
+    header_deps = [
+        ":CAPIIRHeaders",
+    ],
     includes = ["include"],
     deps = [
         ":CAPIIR",
@@ -428,7 +442,7 @@ cc_library(
     ],
 )
 
-cc_library(
+mlir_c_api_cc_library(
     name = "CAPIGPU",
     srcs = [
         "lib/CAPI/Dialect/GPU.cpp",
@@ -437,17 +451,20 @@ cc_library(
     hdrs = [
         "include/mlir-c/Dialect/GPU.h",
     ],
+    header_deps = [
+        ":CAPIIRHeaders",
+        ":GPUPassIncGen",
+    ],
     includes = ["include"],
     deps = [
         ":CAPIIR",
         ":GPUDialect",
-        ":GPUPassIncGen",
         ":GPUTransforms",
         ":Pass",
     ],
 )
 
-cc_library(
+mlir_c_api_cc_library(
     name = "CAPISparseTensor",
     srcs = [
         "lib/CAPI/Dialect/SparseTensor.cpp",
@@ -456,34 +473,43 @@ cc_library(
     hdrs = [
         "include/mlir-c/Dialect/SparseTensor.h",
     ],
+    header_deps = [
+        ":CAPIIRHeaders",
+        ":SparseTensorPassIncGen",
+    ],
     includes = ["include"],
     deps = [
         ":CAPIIR",
         ":Pass",
         ":SparseTensor",
-        ":SparseTensorPassIncGen",
         ":SparseTensorTransforms",
         ":Support",
     ],
 )
 
-cc_library(
+mlir_c_api_cc_library(
     name = "CAPIConversion",
     srcs = ["lib/CAPI/Conversion/Passes.cpp"],
     hdrs = ["include/mlir-c/Conversion.h"],
+    header_deps = [
+        ":CAPIIRHeaders",
+        ":ConversionPassIncGen",
+    ],
     includes = ["include"],
     deps = [
         ":CAPIIR",
-        ":ConversionPassIncGen",
         ":ConversionPasses",
         ":Pass",
     ],
 )
 
-cc_library(
+mlir_c_api_cc_library(
     name = "CAPIDebug",
     srcs = ["lib/CAPI/Debug/Debug.cpp"],
     hdrs = ["include/mlir-c/Debug.h"],
+    header_deps = [
+        ":CAPIIRHeaders",
+    ],
     includes = ["include"],
     deps = [
         ":CAPIIR",
@@ -492,17 +518,20 @@ cc_library(
     ],
 )
 
-cc_library(
+mlir_c_api_cc_library(
     name = "CAPIExecutionEngine",
     srcs = ["lib/CAPI/ExecutionEngine/ExecutionEngine.cpp"],
     hdrs = [
         "include/mlir-c/ExecutionEngine.h",
         "include/mlir/CAPI/ExecutionEngine.h",
     ],
+    header_deps = [
+        ":CAPIIRHeaders",
+        ":ExecutionEngine",
+    ],
     includes = ["include"],
     deps = [
         ":CAPIIR",
-        ":ExecutionEngine",
         ":ExecutionEngineUtils",
         ":LLVMToLLVMIRTranslation",
         "//llvm:OrcJIT",
@@ -510,23 +539,29 @@ cc_library(
     ],
 )
 
-cc_library(
+mlir_c_api_cc_library(
     name = "CAPITransforms",
     srcs = ["lib/CAPI/Transforms/Passes.cpp"],
     hdrs = ["include/mlir-c/Transforms.h"],
+    header_deps = [
+        ":CAPIIRHeaders",
+        ":TransformsPassIncGen",
+    ],
     includes = ["include"],
     deps = [
         ":CAPIIR",
         ":Pass",
         ":Transforms",
-        ":TransformsPassIncGen",
     ],
 )
 
-cc_library(
+mlir_c_api_cc_library(
     name = "CAPIRegistration",
     srcs = ["lib/CAPI/Registration/Registration.cpp"],
     hdrs = ["include/mlir-c/Registration.h"],
+    header_deps = [
+        ":CAPIIRHeaders",
+    ],
     includes = ["include"],
     deps = [
         ":AllPassesAndDialects",
@@ -543,6 +578,30 @@ exports_files(
     glob(["lib/Bindings/Python/**/*.cpp"]),
 )
 
+MLIR_BINDINGS_PYTHON_HEADERS = [
+    "lib/Bindings/Python/*.h",
+    "include/mlir-c/Bindings/Python/*.h",
+    "include/mlir/Bindings/Python/*.h",
+]
+
+cc_library(
+    name = "MLIRBindingsPythonHeaders",
+    includes = [
+        "include",
+        "lib/Bindings/Python",
+    ],
+    tags = [
+        "manual",  # External dependency
+        "nobuildkite",  # TODO(gcmn): Add support for this target
+    ],
+    textual_hdrs = glob(MLIR_BINDINGS_PYTHON_HEADERS),
+    deps = [
+        ":CAPIIRHeaders",
+        "@pybind11",
+        "@python_runtime//:headers",
+    ],
+)
+
 cc_library(
     name = "MLIRBindingsPythonHeadersAndDeps",
     includes = [
@@ -553,11 +612,7 @@ cc_library(
         "manual",  # External dependency
         "nobuildkite",  # TODO(gcmn): Add support for this target
     ],
-    textual_hdrs = glob([
-        "lib/Bindings/Python/*.h",
-        "include/mlir-c/Bindings/Python/*.h",
-        "include/mlir/Bindings/Python/*.h",
-    ]),
+    textual_hdrs = glob(MLIR_BINDINGS_PYTHON_HEADERS),
     deps = [
         ":CAPIIR",
         "@pybind11",
@@ -566,7 +621,7 @@ cc_library(
 )
 
 cc_library(
-    name = "MLIRBindingsPythonCore",
+    name = "MLIRBindingsPythonCoreNoCAPI",
     srcs = [
         "lib/Bindings/Python/DialectLinalg.cpp",
         "lib/Bindings/Python/DialectSparseTensor.cpp",
@@ -592,6 +647,27 @@ cc_library(
         "manual",  # External dependency
         "nobuildkite",  # TODO(gcmn): Add support for this target
     ],
+    deps = [
+        ":CAPIAsyncHeaders",
+        ":CAPIDebugHeaders",
+        ":CAPIGPUHeaders",
+        ":CAPIIRHeaders",
+        ":CAPILinalgHeaders",
+        ":CAPIRegistrationHeaders",
+        ":CAPISparseTensorHeaders",
+        ":MLIRBindingsPythonHeaders",
+        "//llvm:Support",
+        "@pybind11",
+        "@python_runtime//:headers",
+    ],
+)
+
+cc_library(
+    name = "MLIRBindingsPythonCAPIDeps",
+    tags = [
+        "manual",  # External dependency
+        "nobuildkite",  # TODO(gcmn): Add support for this target
+    ],
     deps = [
         ":CAPIAsync",
         ":CAPIDebug",
@@ -601,10 +677,36 @@ cc_library(
         ":CAPILinalg",
         ":CAPIRegistration",
         ":CAPISparseTensor",
-        ":MLIRBindingsPythonHeadersAndDeps",
-        "//llvm:Support",
-        "@pybind11",
-        "@python_runtime//:headers",
+    ],
+)
+
+cc_library(
+    name = "MLIRBindingsPythonCAPIObjects",
+    tags = [
+        "manual",  # External dependency
+        "nobuildkite",  # TODO(gcmn): Add support for this target
+    ],
+    deps = [
+        ":CAPIAsyncObjects",
+        ":CAPIDebugObjects",
+        ":CAPIGPUObjects",
+        ":CAPIIRObjects",
+        ":CAPIInterfacesObjects",
+        ":CAPILinalgObjects",
+        ":CAPIRegistrationObjects",
+        ":CAPISparseTensorObjects",
+    ],
+)
+
+cc_library(
+    name = "MLIRBindingsPythonCore",
+    tags = [
+        "manual",  # External dependency
+        "nobuildkite",  # TODO(gcmn): Add support for this target
+    ],
+    deps = [
+        ":MLIRBindingsPythonCAPIDeps",
+        ":MLIRBindingsPythonCoreNoCAPI",
     ],
 )
 
index b9aabb789f5b1eebc966ad29188c041771689b3a..746ba17a88bb3010d91416d9c27d9a24744852ed 100644 (file)
@@ -24,3 +24,40 @@ cc_headers_only = rule(
     doc = "Provides the headers from 'src' without linking anything.",
     provides = [CcInfo],
 )
+
+def mlir_c_api_cc_library(
+        name,
+        srcs = [],
+        hdrs = [],
+        deps = [],
+        header_deps = [],
+        **kwargs):
+    """Macro that generates three targets for MLIR C API libraries.
+
+    * A standard cc_library target ("Name"),
+    * A header-only cc_library target ("NameHeaders")
+    * An implementation cc_library target tagged `alwayslink` suitable for
+      inclusion in a shared library built with cc_binary() ("NameObjects").
+    """
+
+    native.cc_library(
+        name = name,
+        srcs = srcs,
+        hdrs = hdrs,
+        deps = deps + header_deps,
+        **kwargs
+    )
+    native.cc_library(
+        name = name + "Headers",
+        hdrs = hdrs,
+        deps = header_deps,
+        **kwargs
+    )
+    native.cc_library(
+        name = name + "Objects",
+        srcs = srcs,
+        hdrs = hdrs,
+        deps = deps + header_deps,
+        alwayslink = True,
+        **kwargs
+    )