Skip to content

Commit b06f495

Browse files
csmulherncopybara-github
authored andcommitted
Fixes incorrect install names on darwin platforms
#12304 added support to bazel for setting install names for dynamic libraries on darwin platforms. This would set LC_ID_DYLIB to @rpath/{library_name}, so that RPATH would be used to locate these libraries at runtime. However, the code was using a utility method that assumed the library name was mangled, which is often not the case. Given that the output path should already have been determined with the mangled or unmangled name, we should be able to just use the base name of the artifact. The test that was added in #12304 has been updated to actually use dynamic libaries, and passes with the changes made in this commit. Closes #13427. PiperOrigin-RevId: 377504015
1 parent cf8ec29 commit b06f495

2 files changed

Lines changed: 35 additions & 18 deletions

File tree

src/main/java/com/google/devtools/build/lib/rules/cpp/CppLinkActionBuilder.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -872,8 +872,7 @@ public CppLinkAction build() throws InterruptedException, RuleErrorException {
872872
getLinkType().linkerOrArchiver().equals(LinkerOrArchiver.LINKER),
873873
configuration.getBinDirectory(repositoryName).getExecPath(),
874874
output.getExecPathString(),
875-
SolibSymlinkAction.getDynamicLibrarySoname(
876-
output.getRootRelativePath(), /* preserveName= */ false),
875+
output.getRootRelativePath().getBaseName(),
877876
linkType.equals(LinkTargetType.DYNAMIC_LIBRARY),
878877
paramFile != null ? paramFile.getExecPathString() : null,
879878
thinltoParamFile != null ? thinltoParamFile.getExecPathString() : null,

src/test/shell/bazel/cpp_darwin_integration_test.sh

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -124,41 +124,59 @@ EOF
124124
}
125125

126126
function test_cc_test_with_explicit_install_name() {
127-
mkdir -p cpp
128-
cat > cpp/BUILD <<EOF
127+
mkdir -p cpp/install_name
128+
cat > cpp/install_name/BUILD <<EOF
129129
cc_library(
130130
name = "foo",
131131
srcs = ["foo.cc"],
132-
hdrs = ["foo.h"],
132+
)
133+
cc_binary(
134+
name = "libbar.so",
135+
srcs = ["bar.cc"],
136+
linkshared = 1,
137+
)
138+
cc_binary(
139+
name = "libbaz.dylib",
140+
srcs = ["baz.cc"],
141+
linkshared = 1,
133142
)
134143
cc_test(
135144
name = "test",
136-
srcs = ["test.cc"],
145+
srcs = ["test.cc", ":libbar.so", ":libbaz.dylib"],
137146
deps = [":foo"],
138147
)
139148
EOF
140-
cat > cpp/foo.h <<EOF
141-
int foo();
149+
cat > cpp/install_name/foo.cc <<EOF
150+
int foo() { return 2; }
142151
EOF
143-
cat > cpp/foo.cc <<EOF
144-
int foo() { return 0; }
152+
cat > cpp/install_name/bar.cc <<EOF
153+
int bar() { return 12; }
145154
EOF
146-
cat > cpp/test.cc <<EOF
147-
#include "cpp/foo.h"
155+
cat > cpp/install_name/baz.cc <<EOF
156+
int baz() { return 42; }
157+
EOF
158+
cat > cpp/install_name/test.cc <<EOF
159+
int foo();
160+
int bar();
161+
int baz();
148162
int main() {
149-
return foo();
163+
int result = foo() + bar() + baz();
164+
if (result == 56) {
165+
return 0;
166+
} else {
167+
return result;
168+
}
150169
}
151170
EOF
152171

153-
bazel test --incompatible_macos_set_install_name //cpp:test || \
154-
fail "bazel test //cpp:test failed"
172+
bazel test --incompatible_macos_set_install_name //cpp/install_name:test || \
173+
fail "bazel test //cpp/install_name:test failed"
155174
# Ensure @rpath is correctly set in the binary.
156-
./bazel-bin/cpp/test || \
175+
./bazel-bin/cpp/install_name/test || \
157176
fail "//cpp:test workspace execution failed, expected return 0, got $?"
158177
cd bazel-bin
159-
./cpp/test || \
178+
./cpp/install_name/test || \
160179
fail "//cpp:test execution failed, expected 0, but $?"
161180
}
162181

163182
run_suite "Tests for Bazel's C++ rules on Darwin"
164-

0 commit comments

Comments
 (0)