message: Added support for linking against _pywrap_tensorflow_internal.so in tf_pybind_extension_opensource targets.
diff: @@ -419,6 +419,13 @@ def tf_binary_additional_data_deps():
  ],
  )
  +def tf_binary_pybind_deps():
  + return select({
  + clean_dep("//tensorflow:macos"): [clean_dep("//tensorflow/python:lib_pywrap_tensorflow_internal.dylib")],
  + clean_dep("//tensorflow:windows"): [clean_dep("//tensorflow/python:_pywrap_tensorflow_internal.dll")],
  + "//conditions:default": [clean_dep("//tensorflow/python:lib_pywrap_tensorflow_internal.so")],
  + })
  +
  # Helper function for the per-OS tensorflow libraries and their version symlinks
  def tf_shared_library_deps():
  return select({
  @@ -1895,7 +1902,11 @@ def tf_py_wrap_cc(
  # Convert a rule name such as foo/bar/baz to foo/bar/_baz.so
  # and use that as the name for the rule producing the .so file.
  - cc_library_name = "/".join(name.split("/")[:-1] + ["_" + module_name + ".so"])
  + cc_library_base = "/".join(name.split("/")[:-1] + ["_" + module_name])
  +
  + # TODO(b/137885063): tf_cc_shared_object needs to be cleaned up; we really
  + # shouldn't be passing a name qualified with .so here.
  + cc_library_name = cc_library_base + ".so"
  cc_library_pyd_name = "/".join(
  name.split("/")[:-1] + ["_" + module_name + ".pyd"],
  )
  @@ -1957,6 +1968,25 @@ def tf_py_wrap_cc(
  deps = deps + extra_deps,
  **kwargs
  )
  +
  + # When a non-versioned .so is added as a 'src' to a bazel target, it uses
  + # -l%(so_name) instead of -l:%(so_file) during linking. When -l%(so_name)
  + # is passed to ld, it will look for an associated file with the schema
  + # lib%(so_name).so. Since pywrap_tensorflow is not explicitly versioned
  + # and is not prefixed with lib_, we add a rule for the creation of an .so
  + # file with the canonical lib schema (e.g. libNAME.so), so that
  + # -l%(so_name) is resolved during linking.
  + #
  + # See: https://github.com/bazelbuild/bazel/blob/7a6808260a733d50983c1adf0cf5a7493472267f/src/main/java/com/google/devtools/build/lib/rules/cpp/LibrariesToLinkCollector.java#L319
  + for pattern in SHARED_LIBRARY_NAME_PATTERNS:
  + name_os = pattern % (cc_library_base, "")
  + native.genrule(
  + name = name_os + "_rule",
  + srcs = [":" + cc_library_name],
  + outs = [name_os],
  + cmd = "cp $< $@",
  + )
  +
  native.genrule(
  name = "gen_" + cc_library_pyd_name,
  srcs = [":" + cc_library_name],
  @@ -2401,11 +2431,11 @@ def tf_pybind_extension(
  )
  native.cc_binary(
  name = so_file,
  - srcs = srcs + hdrs,
  - data = data,
  + srcs = srcs + hdrs + tf_binary_additional_srcs() + tf_binary_pybind_deps(),
  + data = data + tf_binary_pybind_deps(),
  copts = copts,
  nocopts = nocopts,
  - linkopts = linkopts + select({
  + linkopts = linkopts + _rpath_linkopts(name) + select({
  "@local_config_cuda//cuda:darwin": [
  "-Wl,-exported_symbols_list,$(location %s)" % exported_symbols_file,
  ],
  