···11-# This setup hook strips libraries and executables in the fixup phase.
22-33-fixupOutputHooks+=(_doStrip)
44-55-_doStrip() {
66- # We don't bother to strip build platform code because it shouldn't make it
77- # to $out anyways---if it does, that's a bigger problem that a lack of
88- # stripping will help catch.
99- local -ra flags=(dontStripHost dontStripTarget)
1010- local -ra debugDirs=(stripDebugList stripDebugListTarget)
1111- local -ra allDirs=(stripAllList stripAllListTarget)
1212- local -ra stripCmds=(STRIP STRIP_FOR_TARGET)
1313- local -ra ranlibCmds=(RANLIB RANLIB_FOR_TARGET)
1414-1515- # TODO(structured-attrs): This doesn't work correctly if one of
1616- # the items in strip*List or strip*Flags contains a space,
1717- # even with structured attrs enabled. This is OK for now
1818- # because very few packages set any of these, and it doesn't
1919- # affect any of them.
2020- #
2121- # After __structuredAttrs = true is universal, come back and
2222- # push arrays all the way through this logic.
2323-2424- # Strip only host paths by default. Leave targets as is.
2525- stripDebugList=${stripDebugList[*]:-lib lib32 lib64 libexec bin sbin}
2626- stripDebugListTarget=${stripDebugListTarget[*]:-}
2727- stripAllList=${stripAllList[*]:-}
2828- stripAllListTarget=${stripAllListTarget[*]:-}
2929-3030- local i
3131- for i in ${!stripCmds[@]}; do
3232- local -n flag="${flags[$i]}"
3333- local -n debugDirList="${debugDirs[$i]}"
3434- local -n allDirList="${allDirs[$i]}"
3535- local -n stripCmd="${stripCmds[$i]}"
3636- local -n ranlibCmd="${ranlibCmds[$i]}"
3737-3838- # `dontStrip` disables them all
3939- if [[ "${dontStrip-}" || "${flag-}" ]] || ! type -f "${stripCmd-}" 2>/dev/null 1>&2
4040- then continue; fi
4141-4242- stripDirs "$stripCmd" "$ranlibCmd" "$debugDirList" "${stripDebugFlags[*]:--S -p}"
4343- stripDirs "$stripCmd" "$ranlibCmd" "$allDirList" "${stripAllFlags[*]:--s -p}"
4444- done
4545-}
4646-4747-stripDirs() {
4848- local cmd="$1"
4949- local ranlibCmd="$2"
5050- local paths="$3"
5151- local stripFlags="$4"
5252- local pathsNew=
5353-5454- [ -z "$cmd" ] && echo "stripDirs: Strip command is empty" 1>&2 && exit 1
5555- [ -z "$ranlibCmd" ] && echo "stripDirs: Ranlib command is empty" 1>&2 && exit 1
5656-5757- local p
5858- for p in ${paths}; do
5959- if [ -e "$prefix/$p" ]; then
6060- pathsNew="${pathsNew} $prefix/$p"
6161- fi
6262- done
6363- paths=${pathsNew}
6464-6565- if [ -n "${paths}" ]; then
6666- echo "stripping (with command $cmd and flags $stripFlags) in $paths"
6767- local striperr
6868- striperr="$(mktemp 'striperr.XXXXXX')"
6969- # Do not strip lib/debug. This is a directory used by setup-hooks/separate-debug-info.sh.
7070- find $paths -type f -a '!' -path "$prefix/lib/debug/*" -print0 |
7171- # Make sure we process files under symlinks only once. Otherwise
7272- # 'strip` can corrupt files when writes to them in parallel:
7373- # https://github.com/NixOS/nixpkgs/issues/246147#issuecomment-1657072039
7474- xargs -r -0 -n1 -- realpath -z | sort -u -z |
7575-7676- xargs -r -0 -n1 -P "$NIX_BUILD_CORES" -- $cmd $stripFlags 2>"$striperr" || exit_code=$?
7777- # xargs exits with status code 123 if some but not all of the
7878- # processes fail. We don't care if some of the files couldn't
7979- # be stripped, so ignore specifically this code.
8080- [[ "$exit_code" = 123 || -z "$exit_code" ]] || (cat "$striperr" 1>&2 && exit 1)
8181-8282- rm "$striperr"
8383- # 'strip' does not normally preserve archive index in .a files.
8484- # This usually causes linking failures against static libs like:
8585- # ld: ...-i686-w64-mingw32-stage-final-gcc-13.0.0-lib/i686-w64-mingw32/lib/libstdc++.dll.a:
8686- # error adding symbols: archive has no index; run ranlib to add one
8787- # Restore the index by running 'ranlib'.
8888- find $paths -name '*.a' -type f -exec $ranlibCmd '{}' \; 2>/dev/null
8989- fi
9090-}