setup-hooks/strip.sh: run RANLIB on static archives after stripping

'strip' does not normally preserve archive index in .a files.
This usually causes linking failures against static libs like:

    $ nix build --no-link -f. pkgsCross.mingw32.re2c
    > ...-i686-w64-mingw32-binutils-2.38/bin/i686-w64-mingw32-ld:
      /nix/store/...-i686-w64-mingw32-stage-final-gcc-13.0.0-lib/i686-w64-mingw32/lib/libstdc++.dll.a:
        error adding symbols: archive has no index; run ranlib to add one

We restore the index by running ranlib explicitly.
This commit is contained in:
Sergei Trofimovich 2022-07-23 10:02:51 +01:00
parent 0f45ce6e77
commit 0507725061

View File

@ -10,6 +10,7 @@ _doStrip() {
local -ra debugDirs=(stripDebugList stripDebugListTarget)
local -ra allDirs=(stripAllList stripAllListTarget)
local -ra stripCmds=(STRIP STRIP_FOR_TARGET)
local -ra ranlibCmds=(RANLIB RANLIB_FOR_TARGET)
# Strip only host paths by default. Leave targets as is.
stripDebugList=${stripDebugList:-lib lib32 lib64 libexec bin sbin}
@ -23,20 +24,22 @@ _doStrip() {
local -n debugDirList="${debugDirs[$i]}"
local -n allDirList="${allDirs[$i]}"
local -n stripCmd="${stripCmds[$i]}"
local -n ranlibCmd="${ranlibCmds[$i]}"
# `dontStrip` disables them all
if [[ "${dontStrip-}" || "${flag-}" ]] || ! type -f "${stripCmd-}" 2>/dev/null
then continue; fi
stripDirs "$stripCmd" "$debugDirList" "${stripDebugFlags:--S}"
stripDirs "$stripCmd" "$allDirList" "${stripAllFlags:--s}"
stripDirs "$stripCmd" "$ranlibCmd" "$debugDirList" "${stripDebugFlags:--S}"
stripDirs "$stripCmd" "$ranlibCmd" "$allDirList" "${stripAllFlags:--s}"
done
}
stripDirs() {
local cmd="$1"
local dirs="$2"
local stripFlags="$3"
local ranlibCmd="$2"
local dirs="$3"
local stripFlags="$4"
local dirsNew=
local d
@ -50,5 +53,11 @@ stripDirs() {
if [ -n "${dirs}" ]; then
echo "stripping (with command $cmd and flags $stripFlags) in$dirs"
find $dirs -type f -exec $cmd $stripFlags '{}' \; 2>/dev/null
# 'strip' does not normally preserve archive index in .a files.
# This usually causes linking failures against static libs like:
# ld: ...-i686-w64-mingw32-stage-final-gcc-13.0.0-lib/i686-w64-mingw32/lib/libstdc++.dll.a:
# error adding symbols: archive has no index; run ranlib to add one
# Restore the index by running 'ranlib'.
find $dirs -name '*.a' -type f -exec $ranlibCmd '{}' \; 2>/dev/null
fi
}