Skip to content

Commit e4bc370

Browse files
ckolli5oquenchil
andauthored
Ck/cherry pick cc shared library (#15754)
* Add regular library in interface_library group of cc_shared_library The regular library was already accessible via the main_shared_library_output group but making it also available in interface_library when there is no actual interface library available is inline with the current behavior of cc_binary. RELNOTES:none PiperOrigin-RevId: 455497044 Change-Id: Ia80fed253e0c79584f23f08eb25b930362e3ff02 * Remove unnecessary file from runfiles of cc_shared_library When a precompilied library appeared in the transitive closure of a cc_shared_library, we would be adding the library itself to runfiles as well as the symlink created in the solib directory. Although harmless, it is unnecessary to add the original library since the symlink in the solib directory will be followed anyway by the Bazel runfiles logic. The behavior now matches that of cc_binary(linkshared=1) The same reasoning can be applied for the main output of a cc_shared_library or a cc_binary(linkshared=1). However, for the latter the logic is written the opposite way, it only adds the library and not the symlink in solib. Unlike cc_binary(linkshared=1), for cc_shared_library we must add the symlink in cc_shared_library because this is what its dependents are linked against. We could just add the symlink in solib and be done since it will in turn pull the original library. However, to keep it more similar to cc_binary(linkshared=1) we add also add the original library, this is documented in this CL with a comment. RELNOTES:none PiperOrigin-RevId: 456074285 Change-Id: Ic383c04569b3d780ca09fc09713bce95c3304636 Co-authored-by: Googler <[email protected]>
1 parent fbc5cb6 commit e4bc370

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

src/main/starlark/builtins_bzl/common/cc/experimental_cc_shared_library.bzl

+21
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,11 @@ def _cc_shared_library_impl(ctx):
480480
runfiles_files = []
481481
if linking_outputs.library_to_link.resolved_symlink_dynamic_library != None:
482482
runfiles_files.append(linking_outputs.library_to_link.resolved_symlink_dynamic_library)
483+
484+
# This is different to cc_binary(linkshared=1). Bazel never handles the
485+
# linking implicitly for a cc_binary(linkshared=1) but it does so for a cc_shared_library,
486+
# for which it will use the symlink in the solib directory. If we don't add it, a dependent
487+
# linked against it would fail.
483488
runfiles_files.append(linking_outputs.library_to_link.dynamic_library)
484489
runfiles = ctx.runfiles(
485490
files = runfiles_files,
@@ -489,6 +494,15 @@ def _cc_shared_library_impl(ctx):
489494
runfiles = runfiles.merge(dep[DefaultInfo].data_runfiles)
490495
transitive_debug_files.append(dep[OutputGroupInfo].rule_impl_debug_files)
491496

497+
precompiled_only_dynamic_libraries_runfiles = []
498+
for precompiled_dynamic_library in precompiled_only_dynamic_libraries:
499+
# precompiled_dynamic_library.dynamic_library could be None if the library to link just contains
500+
# an interface library which is valid if the actual library is obtained from the system.
501+
if precompiled_dynamic_library.dynamic_library != None:
502+
precompiled_only_dynamic_libraries_runfiles.append(precompiled_dynamic_library.dynamic_library)
503+
504+
runfiles = runfiles.merge(ctx.runfiles(files = precompiled_only_dynamic_libraries_runfiles))
505+
492506
for export in ctx.attr.roots:
493507
exports[str(export.label)] = True
494508

@@ -511,6 +525,13 @@ def _cc_shared_library_impl(ctx):
511525
precompiled_only_dynamic_libraries_runfiles.append(precompiled_dynamic_library.resolved_symlink_dynamic_library)
512526

513527
runfiles = runfiles.merge(ctx.runfiles(files = precompiled_only_dynamic_libraries_runfiles))
528+
interface_library = []
529+
if linking_outputs.library_to_link.resolved_symlink_interface_library != None:
530+
interface_library.append(linking_outputs.library_to_link.resolved_symlink_interface_library)
531+
elif linking_outputs.library_to_link.interface_library != None:
532+
interface_library.append(linking_outputs.library_to_link.interface_library)
533+
else:
534+
interface_library = library
514535

515536
return [
516537
DefaultInfo(

src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/starlark_tests.bzl

+55
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,58 @@ def _debug_files_test_impl(ctx):
146146
return analysistest.end(env)
147147

148148
debug_files_test = analysistest.make(_debug_files_test_impl)
149+
150+
def _runfiles_test_impl(ctx):
151+
env = analysistest.begin(ctx)
152+
if not ctx.attr.is_linux:
153+
return analysistest.end(env)
154+
155+
target_under_test = analysistest.target_under_test(env)
156+
expected_suffixes = [
157+
"libfoo_so.so",
158+
"libbar_so.so",
159+
"Smain_Sstarlark_Stests_Sbuiltins_Ubzl_Scc_Scc_Ushared_Ulibrary_Stest_Ucc_Ushared_Ulibrary_Slibfoo_Uso.so",
160+
"Smain_Sstarlark_Stests_Sbuiltins_Ubzl_Scc_Scc_Ushared_Ulibrary_Stest_Ucc_Ushared_Ulibrary_Slibbar_Uso.so",
161+
"Smain_Sstarlark_Stests_Sbuiltins_Ubzl_Scc_Scc_Ushared_Ulibrary_Stest_Ucc_Ushared_Ulibrary/renamed_so_file_copy.so",
162+
"Smain_Sstarlark_Stests_Sbuiltins_Ubzl_Scc_Scc_Ushared_Ulibrary_Stest_Ucc_Ushared_Ulibrary/libdirect_so_file.so",
163+
]
164+
for runfile in target_under_test[DefaultInfo].default_runfiles.files.to_list():
165+
# Ignore Python runfiles
166+
if "python" in runfile.path:
167+
continue
168+
found_suffix = False
169+
for expected_suffix in expected_suffixes:
170+
if runfile.path.endswith(expected_suffix):
171+
found_suffix = True
172+
break
173+
asserts.true(env, found_suffix, runfile.path + " not found in expected suffixes:\n" + "\n".join(expected_suffixes))
174+
175+
return analysistest.end(env)
176+
177+
runfiles_test = analysistest.make(
178+
_runfiles_test_impl,
179+
attrs = {
180+
"is_linux": attr.bool(),
181+
},
182+
)
183+
184+
def _interface_library_output_group_test_impl(ctx):
185+
env = analysistest.begin(ctx)
186+
if not ctx.attr.is_windows:
187+
return analysistest.end(env)
188+
189+
target_under_test = analysistest.target_under_test(env)
190+
actual_files = []
191+
for interface_library in target_under_test[OutputGroupInfo].interface_library.to_list():
192+
actual_files.append(interface_library.basename)
193+
expected_files = ["foo_so.if.lib"]
194+
asserts.equals(env, expected_files, actual_files)
195+
196+
return analysistest.end(env)
197+
198+
interface_library_output_group_test = analysistest.make(
199+
_interface_library_output_group_test_impl,
200+
attrs = {
201+
"is_windows": attr.bool(),
202+
},
203+
)

0 commit comments

Comments
 (0)