Skip to content

Commit

Permalink
Remove unnecessary file from runfiles of cc_shared_library
Browse files Browse the repository at this point in the history
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
  • Loading branch information
oquenchil authored and copybara-github committed Jun 20, 2022
1 parent cc74b11 commit 26d882f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,11 @@ def _cc_shared_library_impl(ctx):
runfiles_files = []
if linking_outputs.library_to_link.resolved_symlink_dynamic_library != None:
runfiles_files.append(linking_outputs.library_to_link.resolved_symlink_dynamic_library)

# This is different to cc_binary(linkshared=1). Bazel never handles the
# linking implicitly for a cc_binary(linkshared=1) but it does so for a cc_shared_library,
# for which it will use the symlink in the solib directory. If we don't add it, a dependent
# linked against it would fail.
runfiles_files.append(linking_outputs.library_to_link.dynamic_library)
runfiles = ctx.runfiles(
files = runfiles_files,
Expand All @@ -505,8 +510,6 @@ def _cc_shared_library_impl(ctx):
# an interface library which is valid if the actual library is obtained from the system.
if precompiled_dynamic_library.dynamic_library != None:
precompiled_only_dynamic_libraries_runfiles.append(precompiled_dynamic_library.dynamic_library)
if precompiled_dynamic_library.resolved_symlink_dynamic_library != None:
precompiled_only_dynamic_libraries_runfiles.append(precompiled_dynamic_library.resolved_symlink_dynamic_library)

runfiles = runfiles.merge(ctx.runfiles(files = precompiled_only_dynamic_libraries_runfiles))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,24 @@ def _runfiles_test_impl(ctx):
return analysistest.end(env)

target_under_test = analysistest.target_under_test(env)
actual_files = []
for runfile in target_under_test[DefaultInfo].default_runfiles.files.to_list():
actual_files.append(runfile.basename)
expected = [
"renamed_so_file_copy.so",
"libdirect_so_file.so",
expected_suffixes = [
"libfoo_so.so",
"libbar_so.so",
"Smain_Sstarlark_Stests_Sbuiltins_Ubzl_Scc_Scc_Ushared_Ulibrary_Stest_Ucc_Ushared_Ulibrary_Slibfoo_Uso.so",
"Smain_Sstarlark_Stests_Sbuiltins_Ubzl_Scc_Scc_Ushared_Ulibrary_Stest_Ucc_Ushared_Ulibrary_Slibbar_Uso.so",
"Smain_Sstarlark_Stests_Sbuiltins_Ubzl_Scc_Scc_Ushared_Ulibrary_Stest_Ucc_Ushared_Ulibrary/renamed_so_file_copy.so",
"Smain_Sstarlark_Stests_Sbuiltins_Ubzl_Scc_Scc_Ushared_Ulibrary_Stest_Ucc_Ushared_Ulibrary/libdirect_so_file.so",
]
for expected_file in expected:
asserts.true(env, expected_file in actual_files, expected_file + " not found in actual files:\n" + "\n".join(actual_files))
for runfile in target_under_test[DefaultInfo].default_runfiles.files.to_list():
# Ignore Python runfiles
if "python" in runfile.path:
continue
found_suffix = False
for expected_suffix in expected_suffixes:
if runfile.path.endswith(expected_suffix):
found_suffix = True
break
asserts.true(env, found_suffix, runfile.path + " not found in expected suffixes:\n" + "\n".join(expected_suffixes))

return analysistest.end(env)

Expand Down

0 comments on commit 26d882f

Please sign in to comment.