bazel: Use linkopts instead of srcs for consuming archives

This should fix the build on Debian 10 with LLVM 11.

- Keep order of files to look up in `ls` invocation
- Use `--start-group`/`--end-group` linker options
- Drive-by: use `splitlines()` instead of manual `split("\n")[:-1]`

PiperOrigin-RevId: 439248079
Change-Id: I919bb292ac8a5f514431aa004345f1c6478b1cc9
This commit is contained in:
Christian Blichmann 2022-04-04 01:00:12 -07:00 committed by Copybara-Service
parent e46b7fc50a
commit e98133c7b4

View File

@ -16,7 +16,7 @@
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe") load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
SYSTEM_LLVM_BAZEL = """package(default_visibility = ["//visibility:public"]) SYSTEM_LLVM_BAZEL_TEMPLATE = """package(default_visibility = ["//visibility:public"])
# Create one hidden library with all LLVM headers that depends on all its # Create one hidden library with all LLVM headers that depends on all its
# static library archives. This will be used to provide individual library # static library archives. This will be used to provide individual library
# targets named the same as the upstream Bazel files. # targets named the same as the upstream Bazel files.
@ -32,12 +32,15 @@ cc_library(
"llvm-project-include/llvm/**/*.h", "llvm-project-include/llvm/**/*.h",
"llvm-project-include/llvm/**/*.inc", "llvm-project-include/llvm/**/*.inc",
]), ]),
linkopts = ["-lncurses"],
includes = ["llvm-project-include"], includes = ["llvm-project-include"],
srcs = glob([ linkopts = [
"llvm-project-lib/libLLVM*.a", "-lncurses",
"llvm-project-lib/libclang*.a", "-lz",
]), "-L%{llvm_lib_dir}",
"-Wl,--start-group",
%{llvm_libs}
"-Wl,--end-group",
],
visibility = ["@llvm-project//clang:__pkg__"], visibility = ["@llvm-project//clang:__pkg__"],
) )
# Fake support library # Fake support library
@ -60,14 +63,14 @@ def _use_system_llvm(ctx):
# Look for LLVM in known places # Look for LLVM in known places
llvm_dirs = ctx.execute( llvm_dirs = ctx.execute(
["ls", "-1"] + ["ls", "-1f"] +
[ [
"/usr/lib/llvm-{}/include/llvm/Support/InitLLVM.h".format(ver) "/usr/lib/llvm-{}/include/llvm/Support/InitLLVM.h".format(ver)
for ver in [16, 15, 14, 13, 12, 11] # Debian for ver in [16, 15, 14, 13, 12, 11] # Debian
] + [ ] + [
"/usr/include/llvm/Support/InitLLVM.h", # Fedora and others "/usr/include/llvm/Support/InitLLVM.h", # Fedora and others
], ],
).stdout.split("\n")[:-1] ).stdout.splitlines()
if llvm_dirs: if llvm_dirs:
llvm_dir = llvm_dirs[0].split("/include/llvm/")[0] llvm_dir = llvm_dirs[0].split("/include/llvm/")[0]
for suffix in ["llvm", "llvm-c", "clang", "clang-c"]: for suffix in ["llvm", "llvm-c", "clang", "clang-c"]:
@ -78,16 +81,28 @@ def _use_system_llvm(ctx):
# Try to find the lib directory # Try to find the lib directory
lib_dirs = ctx.execute( lib_dirs = ctx.execute(
["ls", "-d", "-1"] + ["ls", "-d1f"] +
[llvm_dir + "/lib64", llvm_dir + "/lib"], [llvm_dir + "/lib64", llvm_dir + "/lib"],
).stdout.split("\n")[:-1] ).stdout.splitlines()
if lib_dirs: if lib_dirs:
ctx.symlink(lib_dirs[0], "llvm/llvm-project-lib")
found = True found = True
if found: if found:
# Create stub targets in sub-packages # Create stub targets in sub-packages
ctx.file("llvm/BUILD.bazel", SYSTEM_LLVM_BAZEL) lib_dir = lib_dirs[0] # buildifier: disable=uninitialized
archives = ctx.execute(
["find", ".", "-maxdepth", "1"] +
["(", "-name", "libLLVM*.a", "-o", "-name", "libclang*.a", ")"],
working_directory = lib_dir,
).stdout.splitlines()
lib_strs = sorted(["\"-l{}\",".format(a[5:-2]) for a in archives])
ctx.file(
"llvm/BUILD.bazel",
SYSTEM_LLVM_BAZEL_TEMPLATE
.replace("%{llvm_lib_dir}", lib_dir)
.replace("%{llvm_libs}", "\n".join(lib_strs)),
)
ctx.file("clang/BUILD.bazel", SYSTEM_CLANG_BAZEL) ctx.file("clang/BUILD.bazel", SYSTEM_CLANG_BAZEL)
return found return found