2014-06-27 11:33:05 +00:00
|
|
|
# This setup hook strips libraries and executables in the fixup phase.
|
|
|
|
|
2014-07-08 11:47:09 +00:00
|
|
|
fixupOutputHooks+=(_doStrip)
|
2014-06-27 11:33:05 +00:00
|
|
|
|
|
|
|
_doStrip() {
|
2017-07-07 19:57:01 +00:00
|
|
|
# We don't bother to strip build platform code because it shouldn't make it
|
|
|
|
# to $out anyways---if it does, that's a bigger problem that a lack of
|
|
|
|
# stripping will help catch.
|
|
|
|
local -ra flags=(dontStripHost dontStripTarget)
|
2022-07-22 20:22:50 +00:00
|
|
|
local -ra debugDirs=(stripDebugList stripDebugListTarget)
|
|
|
|
local -ra allDirs=(stripAllList stripAllListTarget)
|
2022-07-22 17:38:02 +00:00
|
|
|
local -ra stripCmds=(STRIP STRIP_FOR_TARGET)
|
2022-07-23 09:02:51 +00:00
|
|
|
local -ra ranlibCmds=(RANLIB RANLIB_FOR_TARGET)
|
2017-07-07 19:57:01 +00:00
|
|
|
|
2022-05-31 21:34:59 +00:00
|
|
|
# TODO(structured-attrs): This doesn't work correctly if one of
|
|
|
|
# the items in strip*List or strip*Flags contains a space,
|
|
|
|
# even with structured attrs enabled. This is OK for now
|
|
|
|
# because very few packages set any of these, and it doesn't
|
|
|
|
# affect any of them.
|
|
|
|
#
|
|
|
|
# After __structuredAttrs = true is universal, come back and
|
|
|
|
# push arrays all the way through this logic.
|
|
|
|
|
2022-07-22 20:22:50 +00:00
|
|
|
# Strip only host paths by default. Leave targets as is.
|
2022-05-31 21:34:59 +00:00
|
|
|
stripDebugList=${stripDebugList[*]:-lib lib32 lib64 libexec bin sbin}
|
|
|
|
stripDebugListTarget=${stripDebugListTarget[*]:-}
|
|
|
|
stripAllList=${stripAllList[*]:-}
|
|
|
|
stripAllListTarget=${stripAllListTarget[*]:-}
|
2017-07-07 19:57:01 +00:00
|
|
|
|
|
|
|
local i
|
|
|
|
for i in ${!stripCmds[@]}; do
|
|
|
|
local -n flag="${flags[$i]}"
|
2022-07-22 20:22:50 +00:00
|
|
|
local -n debugDirList="${debugDirs[$i]}"
|
|
|
|
local -n allDirList="${allDirs[$i]}"
|
2017-07-07 19:57:01 +00:00
|
|
|
local -n stripCmd="${stripCmds[$i]}"
|
2022-07-23 09:02:51 +00:00
|
|
|
local -n ranlibCmd="${ranlibCmds[$i]}"
|
2017-07-07 19:57:01 +00:00
|
|
|
|
|
|
|
# `dontStrip` disables them all
|
2023-01-06 16:57:11 +00:00
|
|
|
if [[ "${dontStrip-}" || "${flag-}" ]] || ! type -f "${stripCmd-}" 2>/dev/null 1>&2
|
2017-07-07 19:57:01 +00:00
|
|
|
then continue; fi
|
|
|
|
|
2023-04-04 15:01:32 +00:00
|
|
|
stripDirs "$stripCmd" "$ranlibCmd" "$debugDirList" "${stripDebugFlags[*]:--S -p}"
|
|
|
|
stripDirs "$stripCmd" "$ranlibCmd" "$allDirList" "${stripAllFlags[*]:--s -p}"
|
2017-07-07 19:57:01 +00:00
|
|
|
done
|
2014-06-27 11:33:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
stripDirs() {
|
2017-07-07 19:57:01 +00:00
|
|
|
local cmd="$1"
|
2022-07-23 09:02:51 +00:00
|
|
|
local ranlibCmd="$2"
|
2022-08-07 08:05:33 +00:00
|
|
|
local paths="$3"
|
2022-07-23 09:02:51 +00:00
|
|
|
local stripFlags="$4"
|
2023-02-28 10:28:41 +00:00
|
|
|
local excludeFlags=()
|
2022-08-07 08:05:33 +00:00
|
|
|
local pathsNew=
|
2014-06-27 11:33:05 +00:00
|
|
|
|
2023-07-21 18:56:38 +00:00
|
|
|
[ -z "$cmd" ] && echo "stripDirs: Strip command is empty" 1>&2 && exit 1
|
|
|
|
[ -z "$ranlibCmd" ] && echo "stripDirs: Ranlib command is empty" 1>&2 && exit 1
|
|
|
|
|
2023-02-28 10:28:41 +00:00
|
|
|
local pattern
|
|
|
|
if [ -n "${stripExclude:-}" ]; then
|
|
|
|
for pattern in "${stripExclude[@]}"; do
|
|
|
|
excludeFlags+=(-a '!' '(' -name "$pattern" -o -wholename "$prefix/$pattern" ')' )
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
|
2022-08-07 08:05:33 +00:00
|
|
|
local p
|
|
|
|
for p in ${paths}; do
|
|
|
|
if [ -e "$prefix/$p" ]; then
|
|
|
|
pathsNew="${pathsNew} $prefix/$p"
|
2014-06-27 11:33:05 +00:00
|
|
|
fi
|
|
|
|
done
|
2022-08-07 08:05:33 +00:00
|
|
|
paths=${pathsNew}
|
2014-06-27 11:33:05 +00:00
|
|
|
|
2022-08-07 08:05:33 +00:00
|
|
|
if [ -n "${paths}" ]; then
|
|
|
|
echo "stripping (with command $cmd and flags $stripFlags) in $paths"
|
2023-07-19 19:34:46 +00:00
|
|
|
local striperr
|
2023-08-07 15:20:56 +00:00
|
|
|
striperr="$(mktemp --tmpdir="$TMPDIR" 'striperr.XXXXXX')"
|
2024-05-24 04:11:50 +00:00
|
|
|
# Make sure we process files only once. `strip`ping the same file through different
|
|
|
|
# links in parallel can corrupt it:
|
|
|
|
# https://github.com/NixOS/nixpkgs/issues/246147#issuecomment-1657072039
|
2023-07-30 08:43:09 +00:00
|
|
|
|
2024-05-24 04:11:50 +00:00
|
|
|
# Do not strip lib/debug. This is a directory used by setup-hooks/separate-debug-info.sh.
|
|
|
|
# Print out each file's device and inode (which will be the same if two files are hardlinked
|
|
|
|
# or are the same file found through different symlinks), followed by its path...
|
|
|
|
find $paths -type f "${excludeFlags[@]}" -a '!' -path "$prefix/lib/debug/*" -printf '%D-%i,%p\0' |
|
|
|
|
# ... sort/uniq by device/inode, then cut them out and keep the path, ...
|
|
|
|
sort -t, -k1,1 -u -z | cut -d, -f2- -z |
|
|
|
|
# and finally strip each unique path in parallel.
|
2023-07-20 03:49:38 +00:00
|
|
|
xargs -r -0 -n1 -P "$NIX_BUILD_CORES" -- $cmd $stripFlags 2>"$striperr" || exit_code=$?
|
2022-12-15 22:13:39 +00:00
|
|
|
# xargs exits with status code 123 if some but not all of the
|
|
|
|
# processes fail. We don't care if some of the files couldn't
|
|
|
|
# be stripped, so ignore specifically this code.
|
2023-07-19 19:34:46 +00:00
|
|
|
[[ "$exit_code" = 123 || -z "$exit_code" ]] || (cat "$striperr" 1>&2 && exit 1)
|
|
|
|
|
|
|
|
rm "$striperr"
|
2022-07-23 09:02:51 +00:00
|
|
|
# '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'.
|
2022-08-07 08:05:33 +00:00
|
|
|
find $paths -name '*.a' -type f -exec $ranlibCmd '{}' \; 2>/dev/null
|
2014-06-27 11:33:05 +00:00
|
|
|
fi
|
|
|
|
}
|