[llvm] [bazel] Rework liblldb (PR #91549)
Keith Smiley via llvm-commits
llvm-commits at lists.llvm.org
Thu May 9 11:04:51 PDT 2024
https://github.com/keith updated https://github.com/llvm/llvm-project/pull/91549
>From ba5d58318d2fbeb903358bac93c9f972831db430 Mon Sep 17 00:00:00 2001
From: Keith Smiley <keithbsmiley at gmail.com>
Date: Wed, 8 May 2024 18:28:06 -0700
Subject: [PATCH 1/2] [bazel] Rework liblldb
Previously we were linking liblldb as a shared library, but also linking
the contents into the lldb binary. This is invalid and results in subtle
runtime issues because of duplicate constants, like the global plugin
registry. This now links the dylib to lldb directly. This requires we
switch to cc_binary instead because cc_shared_library expects your
library to export all symbols in your transitive dependency tree,
where we only want to export lldb symbols.
---
.../llvm-project-overlay/lldb/BUILD.bazel | 62 ++++++++++++-------
1 file changed, 41 insertions(+), 21 deletions(-)
diff --git a/utils/bazel/llvm-project-overlay/lldb/BUILD.bazel b/utils/bazel/llvm-project-overlay/lldb/BUILD.bazel
index 8fb90e850f000..3f3e49950e35d 100644
--- a/utils/bazel/llvm-project-overlay/lldb/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/lldb/BUILD.bazel
@@ -728,37 +728,57 @@ cc_library(
],
)
-cc_library(
- name = "liblldb.static",
- deps = [
- ":API",
- ":Interpreter",
- ],
+genrule(
+ name = "gen_exports_file_linux",
+ srcs = ["//lldb:source/API/liblldb-private.exports"],
+ outs = ["exports_linux.txt"],
+ cmd = """
+cat > $(OUTS) <<EOF
+{
+ global:
+ $$(sed 's/$$/;/g' $(SRCS))
+};
+EOF
+""",
)
-cc_shared_library(
- name = "liblldb",
- # TODO: Remove once fixed https://github.com/bazelbuild/bazel/issues/21893
+genrule(
+ name = "gen_exports_file_macos",
+ srcs = ["//lldb:source/API/liblldb-private.exports"],
+ outs = ["exports_macos.txt"],
+ cmd = "sed 's/^/_/g' $(SRCS) > $(OUTS)",
+)
+
+cc_binary(
+ name = "lldb{}".format(PACKAGE_VERSION),
additional_linker_inputs = select({
+ "@platforms//os:linux": [
+ ":gen_exports_file_linux",
+ ],
"@platforms//os:macos": [
- ":HostMacOSXObjCXX",
- "//lldb/source/Plugins:PluginPlatformMacOSXObjCXX",
+ ":gen_exports_file_macos",
],
"//conditions:default": [],
}),
- shared_lib_name = select({
- "@platforms//os:macos": "liblldb{}.dylib".format(PACKAGE_VERSION),
- "@platforms//os:linux": "liblldb{}.so".format(PACKAGE_VERSION),
- }),
- # TODO: Remove once fixed https://github.com/bazelbuild/bazel/issues/21893
- user_link_flags = select({
+ linkopts = select({
+ "@platforms//os:linux": [
+ "-Wl,--export-dynamic-symbol-list=$(location :gen_exports_file_linux)",
+ ],
"@platforms//os:macos": [
- "$(location :HostMacOSXObjCXX)",
- "$(location //lldb/source/Plugins:PluginPlatformMacOSXObjCXX)",
+ "-Wl,-exported_symbols_list,$(location :gen_exports_file_macos)",
],
"//conditions:default": [],
}),
- deps = [":liblldb.static"],
+ linkshared = True,
+ deps = [
+ ":API",
+ ":Interpreter",
+ ],
+)
+
+cc_import(
+ name = "liblldb_wrapper",
+ shared_library = "lldb{}".format(PACKAGE_VERSION),
)
gentbl_cc_library(
@@ -794,7 +814,7 @@ cc_binary(
deps = [
":APIHeaders",
":Host",
- ":liblldb.static",
+ ":liblldb_wrapper",
":lldb_options_inc_gen",
"//llvm:Option",
"//llvm:Support",
>From 7848357357a8f3a3f26694c96e21993e198286d4 Mon Sep 17 00:00:00 2001
From: Keith Smiley <keithbsmiley at gmail.com>
Date: Thu, 9 May 2024 11:04:38 -0700
Subject: [PATCH 2/2] comment
---
utils/bazel/llvm-project-overlay/lldb/BUILD.bazel | 11 +++++++++--
1 file changed, 9 insertions(+), 2 deletions(-)
diff --git a/utils/bazel/llvm-project-overlay/lldb/BUILD.bazel b/utils/bazel/llvm-project-overlay/lldb/BUILD.bazel
index 3f3e49950e35d..b3a413c401cdd 100644
--- a/utils/bazel/llvm-project-overlay/lldb/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/lldb/BUILD.bazel
@@ -749,6 +749,10 @@ genrule(
cmd = "sed 's/^/_/g' $(SRCS) > $(OUTS)",
)
+# Create a shared library using linkshared=True for liblldb. This uses
+# cc_binary instead of cc_shared_library since the latter expects you to
+# re-export all transitive dependencies vs them being relinked into other
+# binaries.
cc_binary(
name = "lldb{}".format(PACKAGE_VERSION),
additional_linker_inputs = select({
@@ -776,8 +780,11 @@ cc_binary(
],
)
+# cc_binary targets using linkshared=True to build a shared library cannot be
+# imported directly and instead need to be referenced indirectly through
+# cc_import
cc_import(
- name = "liblldb_wrapper",
+ name = "liblldb.wrapper",
shared_library = "lldb{}".format(PACKAGE_VERSION),
)
@@ -814,7 +821,7 @@ cc_binary(
deps = [
":APIHeaders",
":Host",
- ":liblldb_wrapper",
+ ":liblldb.wrapper",
":lldb_options_inc_gen",
"//llvm:Option",
"//llvm:Support",
More information about the llvm-commits
mailing list