Bazel build part of third party library as static library with headers

197 Views Asked by At

I'm trying to build a subset of a third-party C++ library. I want as output a static library (.lo probably), and the headers of the specified targets. I'm not adding any of my own source files at this stage. How do I do this?

I will use this to compile a single dynamic (.so) library with some wrapper code.

The library I'm building is XLA but a generic solution is welcome. I've tried various iterations on this

cc_library(
    name = "my_xla",
    srcs = [],
    deps = [
        "@xla//xla/client:xla_builder,
        "@com_google_absl//absl/strings:str_format",
        "@tsl//tsl/platform:logging",
        "@tsl//tsl/platform:platform_port",
    ],
    hdrs = [
        "@xla//xla/client:xla_builder",
    ],
    alwayslink = True,
)

and it builds "successfully", but I'm not getting a .a or .lo in my bazel-bin dir, or a list of headers. Here's my WORKSPACE

local_repository(
  name = "xla",
  path = "xla",
)

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

load("@xla//:workspace4.bzl", "xla_workspace4")
xla_workspace4()

load("@xla//:workspace3.bzl", "xla_workspace3")
xla_workspace3()

load("@xla//:workspace2.bzl", "xla_workspace2")
xla_workspace2()

load("@xla//:workspace1.bzl", "xla_workspace1")
xla_workspace1()

load("@xla//:workspace0.bzl", "xla_workspace0")
xla_workspace0()
1

There are 1 best solutions below

1
MD NAEEM On

To build a subset of a third-party C++ library like XLA as a static library with headers using Bazel, you need to properly define your BUILD file and ensure that the necessary targets are included. also, Regarding the absence of the .a or .lo file in your bazel-bin directory, it's possible that Bazel is not actually building the library because it's empty due to having no source files. Since you mentioned you want to compile a dynamic library with some wrapper code, you'll need to add some source files to your my_xla target, even if they're just empty placeholder files.

cc_library(
    name = "my_xla",
    hdrs = glob(["path/to/xla/client/*.h"]),  ### Adjust the path
    srcs = ["path/to/emptyfile.cc"],  ### Replace Placeholder source file
    deps = [
        "@xla//xla/client:xla_builder",
        "@com_google_absl//absl/strings:str_format",
        "@tsl//tsl/platform:logging",
        "@tsl//tsl/platform:platform_port",
    ],
)