Skip to content

Commit

Permalink
C++: Fix versioned shared libraries for macOS (additional changes)
Browse files Browse the repository at this point in the history
The commit f0ade80 was missing some required
changes.

The toolchain was passing -l:name. This mechanism doesn' t exist on macOS,
instead the full path to the shared library should be passed.

To add a proper test like in bazelbuild#20847,
this change must first be checked in, the Apple toolchain needs to be
updated and then the test can be added.

RELNOTES:none
PiperOrigin-RevId: 597810345
Change-Id: Ic88feaabdde05143ab145e997de1ef9487af83fd
  • Loading branch information
oquenchil authored and keith committed Aug 19, 2024
1 parent 2c115a7 commit 7fd474d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -818,7 +818,7 @@ public static ImmutableList<CToolchain.Feature> getLegacyFeatures(
" variable: 'libraries_to_link.type'",
" value: 'versioned_dynamic_library'",
" }",
" flag: '-l:%{libraries_to_link.name}'",
" flag: '%{libraries_to_link.path}'",
" }"),
" flag_group {",
" expand_if_equal: {",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6473,11 +6473,11 @@ def _impl(ctx):
flag_group(
flag_groups = [
flag_group(
flags = ["-l:%{libraries_to_link.name}"],
flags = ["%{libraries_to_link.path}"],
expand_if_false = "libraries_to_link.is_whole_archive",
),
flag_group(
flags = ["-Wl,-force_load,-l:%{libraries_to_link.name}"],
flags = ["-Wl,-force_load,%{libraries_to_link.path}"],
expand_if_true = "libraries_to_link.is_whole_archive",
),
],
Expand Down
22 changes: 19 additions & 3 deletions tools/cpp/osx_cc_wrapper.sh.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
set -eu

LIBS=
LIB_PATHS=
LIB_DIRS=
RPATHS=
OUTPUT=
Expand All @@ -38,6 +39,10 @@ function parse_option() {
OUTPUT=$opt
elif [[ "$opt" =~ ^-l(.*)$ ]]; then
LIBS="${BASH_REMATCH[1]} $LIBS"
elif [[ "$opt" =~ ^(.*)\.so$ ]]; then
LIB_PATHS="${opt} $LIB_PATHS"
elif [[ "$opt" =~ ^(.*)\.dylib$ ]]; then
LIB_PATHS="${opt} $LIB_PATHS"
elif [[ "$opt" =~ ^-L(.*)$ ]]; then
LIB_DIRS="${BASH_REMATCH[1]} $LIB_DIRS"
elif [[ "$opt" =~ ^\@loader_path/(.*)$ ]]; then
Expand Down Expand Up @@ -99,6 +104,11 @@ function get_otool_path() {
get_realpath $1 | sed 's|^.*/bazel-out/|bazel-out/|'
}

function call_install_name() {
/usr/bin/xcrun install_name_tool -change $(get_otool_path "$1") \
"@loader_path/$2/$3" "${OUTPUT}"
}

# Do replacements in the output
for rpath in ${RPATHS}; do
for lib in ${LIBS}; do
Expand All @@ -113,10 +123,16 @@ for rpath in ${RPATHS}; do
if [[ -n "${libname-}" ]]; then
libpath=$(get_library_path ${lib})
if [ -n "${libpath}" ]; then
/usr/bin/xcrun install_name_tool -change $(get_otool_path "${libpath}") \
"@loader_path/${rpath}/${libname}" "${OUTPUT}"
call_install_name "${libpath}" "${rpath}" "${libname}"
fi
fi
done
for libpath in ${LIB_PATHS}; do
if [ -f "$libpath" ]; then
libname=$(basename "$libpath")
if [ -f "$(dirname ${OUTPUT})/${rpath}/${libname}" ]; then
call_install_name "${libpath}" "${rpath}" "${libname}"
fi
fi
done
done

0 comments on commit 7fd474d

Please sign in to comment.