~pitti/ubuntu/xenial/pkg-create-dbgsym/trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#!/bin/sh -e

# generate debug symbol package for a given binary package within a built
# debian source tree.
#
# Author: Martin Pitt <martin.pitt@ubuntu.com>
# (C) 2006 Canonical Ltd.
#
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA

# evaluate options
blacklist=""
add_to_files=0
build_ddeb=1

DEB_HOST_GNU_TYPE=$(dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE=$(dpkg-architecture -qDEB_BUILD_GNU_TYPE)
if [ "$DEB_HOST_GNU_TYPE" = "$DEB_BUILD_GNU_TYPE" ]; then
	READELF=readelf
	OBJCOPY=objcopy
else
	READELF=${DEB_HOST_GNU_TYPE}-readelf
	OBJCOPY=${DEB_HOST_GNU_TYPE}-objcopy
fi

dbg() {
    echo "$0: $*"
}

for p; do
    case $p in
        -X*)
            blacklist=`/bin/echo -e "${p#-X}\n$blacklist"`
            shift
            ;;
        -a)
            add_to_files=1
            shift
            ;;
        -B)
            build_ddeb=0
            shift
            ;;
        -*)
            echo "ERROR: Unknown option $p" >&2
            exit 1
            ;;
        *)
            break
            ;;
    esac
done

pkgname="$1"
pkgpath="$2"
nodebuglink="$3"

[ -n "$pkgpath" ] || pkgpath="debian/$pkgname"

[ -d "$pkgpath" ] || {
    if [ -z "$pkgname" ]; then
	echo "Usage: $0 [-Xitem] [-n] <binary package name> [<package path>] [nodebuglink]"
    else
	echo "Directory $pkgpath does not exist, aborting" >&2
    fi
    exit 1
}

# don't create -dbgsym packages for python* packages; they have their own -dbg
# packages usually
if [ "$pkgname" != "${pkgname#python}" ]; then
    dbg "ignoring python* package: $pkgname"
    exit 0
fi

# determine set of -dbg packages; if there are any, we just build dummy dbgsyms
# that depend on -dbg, otherwise we depend on the actual package; but ignore
# python-*-dbg, these are quite different
dbgdepends=
for p in `grep '^Package:.*-dbg' debian/control | cut -f 2 -d\ `; do
    if [ "$p" != "${p#python}" ]; then
        dbg "ignoring python* package: $p"
        continue
    fi
    # heuristic to avoid considering transitional -dbg packages which are going
    # to be empty
    if [ -n "`perl -n000 -e 'print if m/Package: $p/ && m/transitional/' debian/control`" ]; then
        dbg "ignoring transitional package $p"
        continue
    fi
    [ -z "$dbgdepends" ] || dbgdepends="$dbgdepends, "
    dbgdepends="$dbgdepends$p (= \1)"
done
if [ -n "$dbgdepends" ]; then
    depends="$dbgdepends"
else
    depends="$pkgname (= \1)"
fi

p=`readlink -f "$pkgpath"` # original package path
dp="debian/${pkgname}-dbgsym" # debug package path
dp=`readlink -f "$dp"` # debug package path

if [ -d "$dp" ]; then
    echo "pkg_create_dbgsym: $dp already exists, ignoring"
    exit 0
fi

if [ -e "debian/compat" ] && [ "`cat debian/compat`" -ge 9 ]; then
    use_buildid="1"
    echo "Using buildid for compat level >= 9"
fi

# find all ELF files and extract their debug symbols; also add debug links
# to original files; if we do not find any unstripped files, then a likely
# cause is that we already ran this program on the current package, in which
# case we must not run it again to not destroy debug link CRC sums and produce
# empty ddebs
unset any_unstripped
origdir=`pwd`
cd $p
find -type f | ( while read f; do
    # ignore static libraries
    if echo "$f" | grep -q '\.[ao]$'; then continue; fi
    # ignore non-ELF files
    $READELF -h "$f" > /dev/null 2>&1 || continue
    # check blacklist
    if [ -n "$blacklist" ]; then
        if echo "$f" | fgrep -q "$blacklist"; then continue; fi
    fi
    if ! $READELF -S "$f" | grep -q ' .debug_'; then
	# no debug information
	continue
    fi
    any_unstripped=1

    if [ -n "$dbgdepends" ]; then
        echo "we have -dbg package(s), not installing anything into ddeb" >&2
        exit 0
    fi

    # use buildid for compat >= 9, and when present
    dest=`LANGUAGE= LC_ALL=C readelf -n "$f" | awk '/Build.ID:/ { print ".build-id/" substr($3, 1,2) "/" substr($3,3) ".debug" }' | head -n1`
    if [ -z "$use_buildid" -o -z "$dest" ]; then
        #echo "using debuglink for $f: $dest"
        dest="$f"
    fi

    mkdir -p $dp/usr/lib/debug/`dirname "$dest"`
    $OBJCOPY --only-keep-debug "$f" "$dp/usr/lib/debug/$dest" || {
	echo "objcopy --only-keep-debug failed on `readlink -f $f`" >&2
	exit 1
    }	
    [ -n "$nodebuglink" ] || $OBJCOPY -R .gnu_debuglink --add-gnu-debuglink="$dp/usr/lib/debug/$dest" "$f" || {
	echo "objcopy --add-gnu-debuglink failed on `readlink -f $f`" >&2
	exit 1
    }
done
if [ -z "$any_unstripped" ]; then
    echo "$pkgname has no unstripped objects, ignoring" >&2
    rm -rf $dp
    exit 0
fi
)
cd $origdir

if [ ! -d "$dp" -o -z "`find $dp -type f`" ] && [ -z "$dbgdepends" ]; then
    dbg "nothing in $dp and no dbgdepends, ignoring"
    rm -rf $dp
    exit 0
fi

mkdir -p $dp/DEBIAN

# generate a control file for current package (since it is usually not yet
# existing) and adapt it to be suitable for the dbgsym package
perl -p000 -e 's/^#.*?\n//msg; s/^(?:Pre-Depends|Depends|Recommends|Suggests|Conflicts|Provides|Replaces|Breaks|Enhances|Essential):.*?\n(?![ \t])//msg' debian/control |
    dpkg-gencontrol -c- -fdhstrip-dummy-debian-files -p$pkgname -is -ip -Pdebian/$pkgname-dbgsym -Tdebian/$pkgname.substvars -O | 
    sed "s/^Priority:.*$/Priority: extra/;
    s/^Version: \(.*\)$/&\nDepends: $depends/;
    s/^Package:.*$/&-dbgsym/;
    s/^Description:.*$/Description: debug symbols for package $pkgname/" \
    > $dp/DEBIAN/control
rm -f dhstrip-dummy-debian-files

# binaries named the same as the source will lack a Source field.
# since we are renaming binaries, we might need to add one.
if [ -z "`grep ^Source: $dp/DEBIAN/control`" ]; then
    sed -i "s/^Package:.*$/&\nSource: $pkgname/" $dp/DEBIAN/control
fi

# extract package name, version, architecture
ddebname=${pkgname}-dbgsym
ddebversion=`grep '^Version:' $dp/DEBIAN/control | cut -f2- -d\ `
ddebversion=${ddebversion#*:} # strip off epoch
ddebarch=`grep '^Architecture:' $dp/DEBIAN/control | cut -f2- -d\ `
ddebsection=`grep '^Section:' $dp/DEBIAN/control | cut -f2- -d\ `
ddebpriority=`grep '^Priority:' $dp/DEBIAN/control | cut -f2- -d\ `

[ -n "$ddebsection" -a -n "$ddebpriority" ] || {
    echo "Error: parsed ddeb section or priority is empty" >&2
    exit 1
}

if [ "$build_ddeb" != "1" ]; then
    dbg "deferring build of .ddeb to dh_gencontrol"
    # leave marker to tell dh_gencontrol to process this
    touch $dp/DEBIAN/auto.pkg-create-dbgsym
    # leave marker for $add_to_files for dh_gencontrol to pick up
    if [ "$add_to_files" = "1" ]; then
        touch $dp/DEBIAN/add_to_files.pkg-create-dbgsym
    fi
    exit 0
fi

# build .ddeb and add it to debian/files
ddeb="${ddebname}_${ddebversion}_${ddebarch}.ddeb"
dbg "building ddeb package"
NO_PKG_MANGLE=1 dpkg-deb -Zxz --build $dp ../$ddeb
if [ "$add_to_files" = "1" ]; then
    dbg "dpkg-distaddfile $ddeb $ddebsection $ddebpriority"
    dpkg-distaddfile "$ddeb" "$ddebsection" "$ddebpriority"
fi

# clean up, since this package does not appear in debian/control
rm -rf $dp