06-strip-and-debug-pkgs.sh (3695B)
1# This hook executes the following tasks: 2# - strips ELF binaries/libraries 3# - generates -dbg pkgs 4 5make_debug() { 6 local dname= fname= dbgfile= 7 8 [ -n "$nodebug" ] && return 0 9 10 dname=${1%/*}/ ; dname=${dname#$PKGDESTDIR} 11 fname="${1##*/}" 12 dbgfile="${dname}/${fname}" 13 14 mkdir -p "${PKGDESTDIR}/usr/lib/debug/${dname}" 15 $OBJCOPY --only-keep-debug --compress-debug-sections \ 16 "$1" "${PKGDESTDIR}/usr/lib/debug/${dbgfile}" 17 if [ $? -ne 0 ]; then 18 msg_red "${pkgver}: failed to create dbg file: ${dbgfile}\n" 19 return 1 20 fi 21 chmod 644 "${PKGDESTDIR}/usr/lib/debug/${dbgfile}" 22} 23 24attach_debug() { 25 local dname= fname= dbgfile= 26 27 [ -n "$nodebug" ] && return 0 28 29 dname=${1%/*}/ ; dname=${dname#$PKGDESTDIR} 30 fname="${1##*/}" 31 dbgfile="${dname}/${fname}" 32 33 $OBJCOPY --add-gnu-debuglink="${PKGDESTDIR}/usr/lib/debug/${dbgfile}" "$1" 34 if [ $? -ne 0 ]; then 35 msg_red "${pkgver}: failed to attach dbg to ${dbgfile}\n" 36 return 1 37 fi 38} 39 40create_debug_pkg() { 41 local _pkgname= _destdir= 42 43 [ -n "$nodebug" ] && return 0 44 [ ! -d "${PKGDESTDIR}/usr/lib/debug" ] && return 0 45 46 _pkgname="${pkgname}-dbg-${version}" 47 _destdir="${XBPS_DESTDIR}/${XBPS_CROSS_TRIPLET}/${_pkgname}" 48 mkdir -p "${_destdir}/usr/lib" 49 mv ${PKGDESTDIR}/usr/lib/debug ${_destdir}/usr/lib 50 if [ $? -ne 0 ]; then 51 msg_red "$pkgver: failed to create debug pkg\n" 52 return 1 53 fi 54 printf "${pkgver} " >> ${_destdir}/rdeps 55 rmdir --ignore-fail-on-non-empty "${PKGDESTDIR}/usr/lib" 2>/dev/null 56 return 0 57} 58 59hook() { 60 local fname= x= f= _soname= STRIPCMD= 61 62 if [ -n "$nostrip" ]; then 63 return 0 64 fi 65 66 STRIPCMD=/usr/bin/$STRIP 67 68 find ${PKGDESTDIR} -type f | while read f; do 69 if [[ $f =~ ^${PKGDESTDIR}/usr/lib/debug/ ]]; then 70 continue 71 fi 72 73 fname=${f##*/} 74 for x in ${nostrip_files}; do 75 if [ "$x" = "$fname" ]; then 76 found=1 77 break 78 fi 79 done 80 if [ -n "$found" ]; then 81 unset found 82 continue 83 fi 84 case "$(file -bi "$f")" in 85 application/x-executable*) 86 chmod +w "$f" 87 if [[ $(file $f) =~ "statically linked" ]]; then 88 # static binary 89 $STRIPCMD "$f" 90 if [ $? -ne 0 ]; then 91 msg_red "$pkgver: failed to strip ${f#$PKGDESTDIR}\n" 92 return 1 93 fi 94 echo " Stripped static executable: ${f#$PKGDESTDIR}" 95 else 96 make_debug "$f" 97 $STRIPCMD "$f" 98 if [ $? -ne 0 ]; then 99 msg_red "$pkgver: failed to strip ${f#$PKGDESTDIR}\n" 100 return 1 101 fi 102 echo " Stripped executable: ${f#$PKGDESTDIR}" 103 unset nopie_found 104 for x in ${nopie_files}; do 105 if [ "$x" = "${f#$PKGDESTDIR}" ]; then 106 nopie_found=1 107 break 108 fi 109 done 110 if [ -z "$nopie" ] && [ -z "$nopie_found" ]; then 111 msg_red "$pkgver: non-PIE executable found in PIE build: ${f#$PKGDESTDIR}\n" 112 return 1 113 fi 114 attach_debug "$f" 115 fi 116 ;; 117 application/x-sharedlib*|application/x-pie-executable*) 118 local type="$(file -b "$f")" 119 if [[ $type =~ "no machine" ]]; then 120 # using ELF as a container format (e.g. guile) 121 echo " Ignoring ELF file without machine set: ${f#$PKGDESTDIR}" 122 continue 123 fi 124 125 chmod +w "$f" 126 # shared library 127 make_debug "$f" 128 $STRIPCMD --strip-unneeded "$f" 129 if [ $? -ne 0 ]; then 130 msg_red "$pkgver: failed to strip ${f#$PKGDESTDIR}\n" 131 return 1 132 fi 133 if [[ $type =~ "interpreter " ]]; then 134 echo " Stripped position-independent executable: ${f#$PKGDESTDIR}" 135 else 136 echo " Stripped library: ${f#$PKGDESTDIR}" 137 fi 138 attach_debug "$f" 139 ;; 140 application/x-archive*) 141 chmod +w "$f" 142 $STRIPCMD --strip-debug "$f" 143 if [ $? -ne 0 ]; then 144 msg_red "$pkgver: failed to strip ${f#$PKGDESTDIR}\n" 145 return 1 146 fi 147 echo " Stripped static library: ${f#$PKGDESTDIR}";; 148 esac 149 done 150 create_debug_pkg 151 return $? 152}