pytrainer: unpin python 3.10
[NixPkgs.git] / pkgs / build-support / setup-hooks / separate-debug-info.sh
blobebc773a492186595e66357dcd31acd622309e2e4
1 export NIX_SET_BUILD_ID=1
2 export NIX_LDFLAGS+=" --compress-debug-sections=zlib"
3 export NIX_CFLAGS_COMPILE+=" -ggdb -Wa,--compress-debug-sections"
4 export NIX_RUSTFLAGS+=" -g -C strip=none"
6 fixupOutputHooks+=(_separateDebugInfo)
8 _separateDebugInfo() {
9 [ -e "$prefix" ] || return 0
11 local dst="${debug:-$out}"
12 if [ "$prefix" = "$dst" ]; then return 0; fi
14 # in case there is nothing to strip, don't fail the build
15 mkdir -p "$dst"
17 dst="$dst/lib/debug/.build-id"
19 # Find executables and dynamic libraries.
20 local i
21 while IFS= read -r -d $'\0' i; do
22 if ! isELF "$i"; then continue; fi
24 [ -z "${READELF:-}" ] && echo "_separateDebugInfo: '\$READELF' variable is empty, skipping." 1>&2 && break
25 [ -z "${OBJCOPY:-}" ] && echo "_separateDebugInfo: '\$OBJCOPY' variable is empty, skipping." 1>&2 && break
27 # Extract the Build ID. FIXME: there's probably a cleaner way.
28 local id="$($READELF -n "$i" | sed 's/.*Build ID: \([0-9a-f]*\).*/\1/; t; d')"
29 if [ "${#id}" != 40 ]; then
30 echo "could not find build ID of $i, skipping" >&2
31 continue
34 # Extract the debug info.
35 echo "separating debug info from $i (build ID $id)"
36 mkdir -p "$dst/${id:0:2}"
38 # This may fail, e.g. if the binary is for a different
39 # architecture than we're building for. (This happens with
40 # firmware blobs in QEMU.)
42 if [ -f "$dst/${id:0:2}/${id:2}.debug" ]
43 then
44 echo "separate-debug-info: warning: multiple files with build id $id found, overwriting"
46 $OBJCOPY --only-keep-debug "$i" "$dst/${id:0:2}/${id:2}.debug"
48 # Also a create a symlink <original-name>.debug.
49 ln -sfn ".build-id/${id:0:2}/${id:2}.debug" "$dst/../$(basename "$i")"
50 ) || rmdir -p "$dst/${id:0:2}"
51 done < <(find "$prefix" -type f -print0 | sort -z)