rio: update to 0.2.0
[void-pkg.git] / common / hooks / post-install / 06-strip-and-debug-pkgs.sh
blobb6bd52320dc8d19e8b737e30b2e718986c13a0a9
1 # This hook executes the following tasks:
2 # - strips ELF binaries/libraries
3 # - generates -dbg pkgs
5 make_debug() {
6 local dname= fname= dbgfile=
8 [ -n "$nodebug" ] && return 0
10 dname=${1%/*}/ ; dname=${dname#$PKGDESTDIR}
11 fname="${1##*/}"
12 dbgfile="${dname}/${fname}"
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
21 chmod 644 "${PKGDESTDIR}/usr/lib/debug/${dbgfile}"
24 attach_debug() {
25 local dname= fname= dbgfile=
27 [ -n "$nodebug" ] && return 0
29 dname=${1%/*}/ ; dname=${dname#$PKGDESTDIR}
30 fname="${1##*/}"
31 dbgfile="${dname}/${fname}"
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
40 create_debug_pkg() {
41 local _pkgname= _destdir=
43 [ -n "$nodebug" ] && return 0
44 [ ! -d "${PKGDESTDIR}/usr/lib/debug" ] && return 0
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
54 printf "${pkgver} " >> ${_destdir}/rdeps
55 rmdir --ignore-fail-on-non-empty "${PKGDESTDIR}/usr/lib" 2>/dev/null
56 return 0
59 hook() {
60 local fname= x= f= _soname= STRIPCMD=
62 if [ -n "$nostrip" ]; then
63 return 0
66 STRIPCMD=/usr/bin/$STRIP
68 find ${PKGDESTDIR} -type f | while read -r f; do
69 if [[ $f =~ ^${PKGDESTDIR}/usr/lib/debug/ ]]; then
70 continue
73 fname=${f##*/}
74 for x in ${nostrip_files}; do
75 if [ "$x" = "$fname" -o "$x" = "${f#$PKGDESTDIR}" ]; then
76 found=1
77 break
79 done
80 if [ -n "$found" ]; then
81 unset found
82 continue
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 if ! $STRIPCMD "$f"; then
90 msg_red "$pkgver: failed to strip ${f#$PKGDESTDIR}\n"
91 return 1
93 echo " Stripped static executable: ${f#$PKGDESTDIR}"
94 else
95 make_debug "$f"
96 if ! $STRIPCMD "$f"; then
97 msg_red "$pkgver: failed to strip ${f#$PKGDESTDIR}\n"
98 return 1
100 echo " Stripped executable: ${f#$PKGDESTDIR}"
101 unset nopie_found
102 for x in ${nopie_files}; do
103 if [ "$x" = "${f#$PKGDESTDIR}" ]; then
104 nopie_found=1
105 break
107 done
108 if [ -z "$nopie" ] && [ -z "$nopie_found" ]; then
109 msg_red "$pkgver: non-PIE executable found in PIE build: ${f#$PKGDESTDIR}\n"
110 return 1
112 attach_debug "$f"
115 application/x-sharedlib*|application/x-pie-executable*)
116 local type="$(file -b "$f")"
117 if [[ $type =~ "no machine" ]]; then
118 # using ELF as a container format (e.g. guile)
119 echo " Ignoring ELF file without machine set: ${f#$PKGDESTDIR}"
120 continue
123 chmod +w "$f"
124 # shared library
125 make_debug "$f"
126 if ! $STRIPCMD --strip-unneeded "$f"; then
127 msg_red "$pkgver: failed to strip ${f#$PKGDESTDIR}\n"
128 return 1
130 if [[ $type =~ "interpreter " ]]; then
131 echo " Stripped position-independent executable: ${f#$PKGDESTDIR}"
132 else
133 echo " Stripped library: ${f#$PKGDESTDIR}"
135 attach_debug "$f"
137 application/x-archive*)
138 chmod +w "$f"
139 if ! $STRIPCMD --strip-debug "$f"; then
140 msg_red "$pkgver: failed to strip ${f#$PKGDESTDIR}\n"
141 return 1
143 echo " Stripped static library: ${f#$PKGDESTDIR}";;
144 esac
145 done
146 create_debug_pkg
147 return $?