Skip to content

Commit

Permalink
Fix precompiled lib symlinks missing in runfiles of cc_shared_library
Browse files Browse the repository at this point in the history
26d882f was intended to remove unnecessary files from runfiles However it also removes (some?) required symlinks from runfiles. E.g. libtensorflow_cc.so.2.13.0 is present in the runfiles but `libtensorflow_cc.so.2` is not leading to failures as programs are linked to the latter. See tensorflow/tensorflow#60326

Use the same logic used in other places preferring `resolved_symlink_dynamic_library` over `dynamic_library`.

Closes #19378.

PiperOrigin-RevId: 567526683
Change-Id: Ie8768224a7adc3f3021bf00f68bd668d617b44e1
  • Loading branch information
Flamefire authored and copybara-github committed Sep 22, 2023
1 parent 70675b3 commit 04a0567
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 12 deletions.
11 changes: 7 additions & 4 deletions src/main/starlark/builtins_bzl/common/cc/cc_shared_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@

"""Implementation of cc_shared_library"""

load(":common/cc/cc_common.bzl", "cc_common")
load(":common/cc/cc_helper.bzl", "cc_helper")
load(":common/cc/semantics.bzl", "semantics")
load(":common/proto/proto_info.bzl", "ProtoInfo")
load(":common/cc/cc_info.bzl", "CcInfo")
load(":common/cc/cc_common.bzl", "cc_common")
load(":common/cc/cc_shared_library_hint_info.bzl", "CcSharedLibraryHintInfo")
load(":common/cc/semantics.bzl", "semantics")
load(":common/proto/proto_info.bzl", "ProtoInfo")

# TODO(#5200): Add export_define to library_to_link and cc_library

Expand Down Expand Up @@ -732,7 +732,10 @@ def _cc_shared_library_impl(ctx):
# precompiled_dynamic_library.dynamic_library could be None if the library to link just contains
# 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)
else:
precompiled_only_dynamic_libraries_runfiles.append(precompiled_dynamic_library.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 @@ -14,9 +14,9 @@

"""Starlark tests for cc_shared_library"""

load("@bazel_skylib//lib:paths.bzl", "paths")
load("@rules_testing//lib:truth.bzl", "matching")
load("@rules_testing//lib:analysis_test.bzl", "analysis_test")
load("@rules_testing//lib:truth.bzl", "matching")
load("@bazel_skylib//lib:paths.bzl", "paths")

def _same_package_or_above(label_a, label_b):
if label_a.workspace_name != label_b.workspace_name:
Expand Down Expand Up @@ -186,28 +186,52 @@ def _runfiles_test_impl(env, target):
"libfoo_so.so",
"libbar_so.so",
"libdiff_pkg_so.so",
"libdirect_so_file.so",
"libprivate_lib_so.so",
"renamed_so_file_copy.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_Ulibrary3_Slibdiff_Upkg_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 runfile in target[DefaultInfo].default_runfiles.files.to_list():
runfiles = [file.path for file in target[DefaultInfo].default_runfiles.files.to_list()]
for runfile in runfiles:
# Ignore Python runfiles
if "python" in runfile.path:
if "python" in runfile:
continue

found_basename = False
for expected_basename in expected_basenames:
if runfile.path.endswith(expected_basename):
if runfile.endswith(expected_basename):
found_basename = True
break

env.expect.where(
detail = runfile.path + " not found in expected basenames:\n" + "\n".join(expected_basenames),
detail = runfile + " not found in expected basenames:\n" + "\n".join(expected_basenames),
).that_bool(found_basename).equals(True)

# Match e.g. bazel-out/k8-fastbuild/bin/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/libdirect_so_file.so
path_suffix = "/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library"
expected_files = [
"libbar_so.so",
"libdirect_so_file.so",
"libfoo_so.so",
"libprivate_lib_so.so",
"python_test",
"renamed_so_file_copy.so",
]
for file in expected_files:
path = path_suffix + "/" + file

found = False
for runfile in runfiles:
if runfile.endswith(path):
found = True
break

env.expect.where(
detail = file + " not found in runfiles:\n" + "\n".join(runfiles),
).that_bool(found).equals(True)

def _runfiles_test_macro(name, target):
analysis_test(
name = name,
Expand Down

0 comments on commit 04a0567

Please sign in to comment.