Skip to content

Commit d36b4ab

Browse files
authored
[bazel] Rework liblldb (#91549)
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.
1 parent dcf92a2 commit d36b4ab

File tree

1 file changed

+48
-21
lines changed

1 file changed

+48
-21
lines changed

utils/bazel/llvm-project-overlay/lldb/BUILD.bazel

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -728,37 +728,64 @@ cc_library(
728728
],
729729
)
730730

731-
cc_library(
732-
name = "liblldb.static",
733-
deps = [
734-
":API",
735-
":Interpreter",
736-
],
731+
genrule(
732+
name = "gen_exports_file_linux",
733+
srcs = ["//lldb:source/API/liblldb-private.exports"],
734+
outs = ["exports_linux.txt"],
735+
cmd = """
736+
cat > $(OUTS) <<EOF
737+
{
738+
global:
739+
$$(sed 's/$$/;/g' $(SRCS))
740+
};
741+
EOF
742+
""",
737743
)
738744

739-
cc_shared_library(
740-
name = "liblldb",
741-
# TODO: Remove once fixed https://github.com/bazelbuild/bazel/issues/21893
745+
genrule(
746+
name = "gen_exports_file_macos",
747+
srcs = ["//lldb:source/API/liblldb-private.exports"],
748+
outs = ["exports_macos.txt"],
749+
cmd = "sed 's/^/_/g' $(SRCS) > $(OUTS)",
750+
)
751+
752+
# Create a shared library using linkshared=True for liblldb. This uses
753+
# cc_binary instead of cc_shared_library since the latter expects you to
754+
# re-export all transitive dependencies vs them being relinked into other
755+
# binaries.
756+
cc_binary(
757+
name = "lldb{}".format(PACKAGE_VERSION),
742758
additional_linker_inputs = select({
759+
"@platforms//os:linux": [
760+
":gen_exports_file_linux",
761+
],
743762
"@platforms//os:macos": [
744-
":HostMacOSXObjCXX",
745-
"//lldb/source/Plugins:PluginPlatformMacOSXObjCXX",
763+
":gen_exports_file_macos",
746764
],
747765
"//conditions:default": [],
748766
}),
749-
shared_lib_name = select({
750-
"@platforms//os:macos": "liblldb{}.dylib".format(PACKAGE_VERSION),
751-
"@platforms//os:linux": "liblldb{}.so".format(PACKAGE_VERSION),
752-
}),
753-
# TODO: Remove once fixed https://github.com/bazelbuild/bazel/issues/21893
754-
user_link_flags = select({
767+
linkopts = select({
768+
"@platforms//os:linux": [
769+
"-Wl,--export-dynamic-symbol-list=$(location :gen_exports_file_linux)",
770+
],
755771
"@platforms//os:macos": [
756-
"$(location :HostMacOSXObjCXX)",
757-
"$(location //lldb/source/Plugins:PluginPlatformMacOSXObjCXX)",
772+
"-Wl,-exported_symbols_list,$(location :gen_exports_file_macos)",
758773
],
759774
"//conditions:default": [],
760775
}),
761-
deps = [":liblldb.static"],
776+
linkshared = True,
777+
deps = [
778+
":API",
779+
":Interpreter",
780+
],
781+
)
782+
783+
# cc_binary targets using linkshared=True to build a shared library cannot be
784+
# imported directly and instead need to be referenced indirectly through
785+
# cc_import
786+
cc_import(
787+
name = "liblldb.wrapper",
788+
shared_library = "lldb{}".format(PACKAGE_VERSION),
762789
)
763790

764791
gentbl_cc_library(
@@ -794,7 +821,7 @@ cc_binary(
794821
deps = [
795822
":APIHeaders",
796823
":Host",
797-
":liblldb.static",
824+
":liblldb.wrapper",
798825
":lldb_options_inc_gen",
799826
"//llvm:Option",
800827
"//llvm:Support",

0 commit comments

Comments
 (0)