|
| 1 | +# Copyright 2026 The Bazel Authors. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +"""Helper functions for creating solib and dynamic library symbolic links.""" |
| 15 | + |
| 16 | +load("@bazel_skylib//lib:paths.bzl", "paths") |
| 17 | +load("//cc/common:cc_helper_internal.bzl", "extensions", "is_versioned_shared_library_extension_valid", "root_relative_path") |
| 18 | +load("//cc/private:cc_internal.bzl", _cc_internal = "cc_internal") |
| 19 | + |
| 20 | +# TODO(bgorshenev): Remove this variable once we're sure these functions won't |
| 21 | +# won't interfere with path mapping. |
| 22 | +USE_STARLARK_SOLIB_SYMLINK = True |
| 23 | + |
| 24 | +def _escaped_path(path): |
| 25 | + return path.replace("_", "_U").replace("/", "_S").replace("\\", "_B").replace(":", "_C").replace("@", "_A") |
| 26 | + |
| 27 | +def _label_to_string(label): |
| 28 | + """Java and Starlark convert label to string slightly differently. Match Java's behavior.""" |
| 29 | + s = str(label) |
| 30 | + if s.startswith("@@//"): # buildifier: disable=canonical-repository |
| 31 | + return s[2:] |
| 32 | + if s.startswith("@//"): |
| 33 | + return s[1:] |
| 34 | + return s |
| 35 | + |
| 36 | +def _is_shared_library_filetype(basename): |
| 37 | + if "." not in basename: |
| 38 | + return True |
| 39 | + if is_versioned_shared_library_extension_valid(basename): |
| 40 | + return True |
| 41 | + ext = basename[basename.rfind("."):] |
| 42 | + if ext in extensions.SHARED_LIBRARY: |
| 43 | + return True |
| 44 | + if ext in extensions.INTERFACE_SHARED_LIBRARY: |
| 45 | + return True |
| 46 | + return False |
| 47 | + |
| 48 | +def _dynamic_library_soname(library_path, preserve_name, mnemonic): |
| 49 | + if preserve_name: |
| 50 | + return paths.basename(library_path) |
| 51 | + else: |
| 52 | + mnemonic_mangling = "" |
| 53 | + if "ST-" in mnemonic: |
| 54 | + st_index = mnemonic.find("ST-") |
| 55 | + mnemonic_mangling = mnemonic[st_index:] + "_" |
| 56 | + return "lib" + mnemonic_mangling + _escaped_path(library_path) |
| 57 | + |
| 58 | +def dynamic_library_soname(actions, library_path, preserve_name): |
| 59 | + """Compute the SONAME to use for a dynamic library. |
| 60 | +
|
| 61 | + This name is basically the name of the shared |
| 62 | + library in its final symlinked location. |
| 63 | +
|
| 64 | + Args: |
| 65 | + actions: action construction context of rule requesting symlink |
| 66 | + library_path: name of the shared library that needs to be mangled |
| 67 | + preserve_name: whether to preserve the name of the library |
| 68 | +
|
| 69 | + Returns: |
| 70 | + soname to embed in the dynamic library |
| 71 | + """ |
| 72 | + if USE_STARLARK_SOLIB_SYMLINK and hasattr(_cc_internal, "maybe_hash_preserve_extension"): |
| 73 | + ctx = _cc_internal.actions2ctx_cheat(actions) |
| 74 | + mnemonic = ctx.bin_dir.path.split("/")[1] |
| 75 | + return _dynamic_library_soname(library_path, preserve_name, mnemonic) |
| 76 | + else: |
| 77 | + return _cc_internal.dynamic_library_soname(actions, library_path, preserve_name) |
| 78 | + |
| 79 | +def _get_mangled_name(label, solib_dir, mnemonic, library_path, preserve_name, prefix_consumer): |
| 80 | + escaped_rule_path = _escaped_path("_" + _label_to_string(label)) |
| 81 | + soname = _dynamic_library_soname(library_path, preserve_name, mnemonic) |
| 82 | + |
| 83 | + if preserve_name: |
| 84 | + parent_dir = paths.dirname(library_path) |
| 85 | + escaped_library_path = _escaped_path("_" + parent_dir) |
| 86 | + escaped_full_path = escaped_rule_path + "__" + escaped_library_path if prefix_consumer else escaped_library_path |
| 87 | + mangled_dir = solib_dir + "/" + _cc_internal.maybe_hash_preserve_extension(escaped_full_path) |
| 88 | + return mangled_dir + "/" + soname |
| 89 | + else: |
| 90 | + filename = escaped_rule_path + "__" + soname if prefix_consumer else soname |
| 91 | + return solib_dir + "/" + _cc_internal.maybe_hash_preserve_extension(filename) |
| 92 | + |
| 93 | +def dynamic_library_symlink(actions, library, solib_directory, preserve_name, prefix_consumer): |
| 94 | + """ Create dynamic library symlink. |
| 95 | +
|
| 96 | + Replaces shared library artifact with mangled symlink and creates related symlink action. For |
| 97 | + artifacts that should retain filename (e.g. libraries with SONAME tag), link is created to the |
| 98 | + parent directory instead. |
| 99 | +
|
| 100 | + This action is performed to minimize number of -rpath entries used during linking process |
| 101 | + (by essentially "collecting" as many shared libraries as possible in the single directory), |
| 102 | + since we will be paying quadratic price for each additional entry on the -rpath. |
| 103 | +
|
| 104 | + Args: |
| 105 | + actions: action construction context of rule requesting symlink |
| 106 | + library: Shared library artifact that needs to be mangled. |
| 107 | + solib_directory: String giving the solib directory |
| 108 | + preserve_name: whether to preserve the name of the library |
| 109 | + prefix_consumer: whether to prefix the output artifact name with the label of the consumer |
| 110 | + Returns: |
| 111 | + mangled symlink artifact. |
| 112 | + """ |
| 113 | + if USE_STARLARK_SOLIB_SYMLINK and hasattr(_cc_internal, "maybe_hash_preserve_extension"): |
| 114 | + if not _is_shared_library_filetype(library.basename): |
| 115 | + fail("Library '%s' does not match expected filetype" % library.basename) |
| 116 | + if root_relative_path(library).startswith("_solib_"): |
| 117 | + fail("Library '%s' is already in _solib_" % library.path) |
| 118 | + ctx = _cc_internal.actions2ctx_cheat(actions) |
| 119 | + label = ctx.label |
| 120 | + mnemonic = ctx.bin_dir.path.split("/")[1] |
| 121 | + mangled_name = _get_mangled_name( |
| 122 | + label, |
| 123 | + solib_directory, |
| 124 | + mnemonic, |
| 125 | + root_relative_path(library), |
| 126 | + preserve_name, |
| 127 | + prefix_consumer, |
| 128 | + ) |
| 129 | + symlink = ctx.actions.declare_shareable_artifact(mangled_name) |
| 130 | + ctx.actions.symlink( |
| 131 | + output = symlink, |
| 132 | + target_file = library, |
| 133 | + ) |
| 134 | + return symlink |
| 135 | + else: |
| 136 | + return _cc_internal.dynamic_library_symlink(actions, library, solib_directory, preserve_name, prefix_consumer) |
| 137 | + |
| 138 | +def dynamic_library_symlink2(actions, library, solib_directory, path): |
| 139 | + """Creates a symlink for a dynamic library with a rule-specified path. |
| 140 | +
|
| 141 | + Args: |
| 142 | + actions: action construction context of rule requesting symlink |
| 143 | + library: Shared library artifact |
| 144 | + solib_directory: String giving the solib directory |
| 145 | + path: Symlink path underneath the solib directory. |
| 146 | + Returns: |
| 147 | + symlink artifact. |
| 148 | + """ |
| 149 | + if USE_STARLARK_SOLIB_SYMLINK and hasattr(_cc_internal, "maybe_hash_preserve_extension"): |
| 150 | + if not _is_shared_library_filetype(library.basename): |
| 151 | + fail("Library '%s' does not match expected filetype" % library.basename) |
| 152 | + if not _is_shared_library_filetype(path.split("/")[-1]): |
| 153 | + fail("Path '%s' does not match expected filetype" % path) |
| 154 | + if root_relative_path(library).startswith("_solib_"): |
| 155 | + fail("Library '%s' is already in _solib_" % library.path) |
| 156 | + ctx = _cc_internal.actions2ctx_cheat(actions) |
| 157 | + symlink_name = solib_directory + "/" + path |
| 158 | + symlink = ctx.actions.declare_shareable_artifact(symlink_name) |
| 159 | + ctx.actions.symlink( |
| 160 | + output = symlink, |
| 161 | + target_file = library, |
| 162 | + ) |
| 163 | + return symlink |
| 164 | + else: |
| 165 | + return _cc_internal.dynamic_library_symlink2(actions, library, solib_directory, path) |
| 166 | + |
| 167 | +def solib_symlink_action(ctx, artifact, solib_directory, runtime_solib_dir_base): |
| 168 | + """Create a symlink for C++ runtime libraries without name or directory mangling. |
| 169 | +
|
| 170 | + Args: |
| 171 | + ctx: rule context of rule requesting symlink |
| 172 | + artifact: Shared library artifact |
| 173 | + solib_directory: String giving the solib directory, as defined by the toolchain. |
| 174 | + runtime_solib_dir_base: Base directory for runtime symlinks, if the toolchain's needs to be overridden. |
| 175 | + Returns: |
| 176 | + symlink artifact. |
| 177 | + """ |
| 178 | + if USE_STARLARK_SOLIB_SYMLINK: |
| 179 | + if not _is_shared_library_filetype(artifact.basename): |
| 180 | + fail("Library '%s' does not match expected filetype" % artifact.basename) |
| 181 | + if root_relative_path(artifact).startswith("_solib_"): |
| 182 | + fail("Library '%s' is already in _solib_" % artifact.path) |
| 183 | + solib_dir = runtime_solib_dir_base if runtime_solib_dir_base != None else solib_directory |
| 184 | + symlink_name = solib_dir + "/" + artifact.basename |
| 185 | + symlink = ctx.actions.declare_shareable_artifact(symlink_name) |
| 186 | + ctx.actions.symlink( |
| 187 | + output = symlink, |
| 188 | + target_file = artifact, |
| 189 | + ) |
| 190 | + return symlink |
| 191 | + else: |
| 192 | + return _cc_internal.solib_symlink_action(ctx, artifact, solib_directory, runtime_solib_dir_base) |
0 commit comments