~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
#!/bin/sh
# wrapper around dh_gencontrol that updates the binary version of corresponding
# ddebs
#
# Author: Martin Pitt <martin.pitt@ubuntu.com>
# (C) 2015 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
set -e

DEBUG=1

if [ -x /usr/bin/dh_gencontrol.pkg-create-dbgsym ]; then
    REAL_DHGENCONTROL=/usr/bin/dh_gencontrol.pkg-create-dbgsym
elif [ "$0" != /usr/bin/dh_gencontrol ]; then
    # called as "dh_gencontrol" somewhere before /usr/bin in the PATH, e.g.
    # from the testsuite during package build
    REAL_DHGENCONTROL=/usr/bin/dh_gencontrol
    if ! [ -x "$REAL_DHGENCONTROL" ]; then
	echo "FATAL: Can't run /usr/bin/dh_gencontrol; is debhelper installed?" >&2
	exit 1
    fi
else
    echo "FATAL: Can't find the diverted original dh_gencontrol and /usr/bin/dh_gencontrol loops back to myself!" >&2
    exit 1
fi

# call real dh_gencontrol first, so that it will set correct package versions
$REAL_DHGENCONTROL "$@"

HOSTARCH=`dpkg-architecture -qDEB_HOST_ARCH`

# print debug message
dbg() {
    [ -n "$DEBUG" ] || return 0
    echo "dh_gencontrol debug symbol wrapper: $@"
}

# return 0 if $1 is in the string list $2, otherwise return 1
in_list()
{
    echo "$2" | grep -q "\(^\|[[:space:]]\)$1\(\$\|[[:space:]]\)"
}

# get set of all architecture dependent packages from debian/control
packages=$(
    unset pkg
    sed -n 's/[[:space:]]*$//; s/^\(Package\|Architecture\):[[:space:]]*\(.\+\)/\1 \2/p' debian/control | while read key val; do
	if [ "$key" = Package ]; then
	    [ -z "$pkg" ] || {
		echo "Error: Package: and Architecture: do not alternate in debian/control" >&2
		exit 1
	    }
	    pkg="$val"
	fi
	if [ "$key" = Architecture ]; then
	    [ -n "$pkg" ] || {
		echo "Error: Package: and Architecture: do not alternate in debian/control" >&2
		exit 1
	    }
	    for arch in $val; do
		if dpkg-architecture -a"$HOSTARCH" -i"$arch"; then
		    printf "$pkg "
		    break
		fi
	    done
	    unset pkg
	fi
    done
)
dbg "all non-arch-all packages for this build platform $HOSTARCH: $packages"

# parse command line to determine set of packages to act on
for p in $DH_OPTIONS $@; do
    if [ -n "$next_arg_is_xopt" ]; then
	xopts="$xopts -X$p"
	unset next_arg_is_xopt
	continue
    fi

    p=${p#-O}
    case "$p" in
	-i|--indep)
	    unset actpkgs
	    optsel=1
	    ;;
	-a|--arch|-s|--same-arch)
	    actpkgs="$packages"
	    optsel=1
	    ;;
	-p*|--package=*)
	    pkg=${p#--package=}
	    pkg=${pkg#-p}
	    if ! in_list "$pkg" "$actpkgs" && in_list "$pkg" "$packages"; then
		actpkgs="$pkg $actpkgs"
	    fi
	    optsel=1
	    ;;
	-N*|--no-package=*)
	    pkg=${p#--no-package=}
	    pkg=${pkg#-N}
	    if ! in_list "$pkg" "$ignorepkgs"; then
		ignorepkgs="$pkg $ignorepkgs"
	    fi
	    ;;
	-P*|--tmpdir=*)
	    pkgdirarg=${p#--tmpdir=}
	    pkgdirarg=${pkgdirarg#-P}
	    dbg "-P/--tmpdir option: setting temporary package dir to $pkgdirarg"
	    ;;
	-X)
	    next_arg_is_xopt=1
	    ;;
	-X*)
	    xopts="$xopts $p"
	    ;;
    esac
done

[ -n "$optsel" ] || actpkgs="$packages"

dbg "packages to act on: $actpkgs"
dbg "ignored packages: $ignorepkgs"

# check for debhelper compat level 1
dhcompat1=1
if [ -e debian/compat ]; then
    if [ "`cat debian/compat`" != 1 ]; then
	unset dhcompat1
    fi
fi
if grep -q '^[[:space:]]*\(export[[:space:]]*\)\?DH_COMPAT[[:space:]]*:\?=[[:space:]]*[2-9]\>' debian/rules; then
    unset dhcompat1
elif grep -q '^[[:space:]]*\(export[[:space:]]*\)\?DH_COMPAT[[:space:]]*:\?=[[:space:]]*1\>' debian/rules; then
    dhcompat1=1
fi

if [ -n "$dhcompat1" ]; then
    first_control_pkg="`grep -m 1 ^Package debian/control | cut -f2- -d\ `"
    dbg "using obsolete debhelper compat mode 1"
fi

# process all packages
for p in $actpkgs; do
    if in_list "$p" "$ignorepkgs" ||
	echo $p | grep -q -- '-dbg$' ; then
	continue
    fi

    if [ -n "$pkgdirarg" ]; then
	pkgdir="$pkgdirarg"
    elif [ -n "$dhcompat1" -a $p = "$first_control_pkg" ]; then
	pkgdir=debian/tmp
    else
	pkgdir=debian/$p
    fi

    ddebpkgdir=debian/${p}-dbgsym
    if [ ! -d $ddebpkgdir ]; then
	dbg "no $ddebpkgdir, skipping package $p"
	continue
    fi

    auto_stamp=$ddebpkgdir/DEBIAN/auto.pkg-create-dbgsym
    if [ ! -e $auto_stamp ]; then
	dbg "$ddebpkgdir not produced by pkg_create_dbgsym dh_strip wrapper, skipping package $p"
	continue
    fi
    rm $auto_stamp

    dbg "processing package $p (pkgdir $pkgdir, ddeb package dir $ddebpkgdir)"

    # adjust ddeb version from deb, for dpkg-gencontrol's -v option
    srcversion=`dpkg-parsechangelog -SVersion`
    debversion=`grep '^Version:' $pkgdir/DEBIAN/control | cut -f2- -d\ `
    if [ -n "$debversion" ]; then
        sed -i "s/^Version: .*\$/Version: $debversion/; /^Depends:/ s/(= .*)/(= $debversion)/" $ddebpkgdir/DEBIAN/control
        # add Source: version too, if binary version is different
        if [ "$debversion" != "$srcversion" ]; then
            sed -i "/^Source:/ s/\$/ ($srcversion)/" $ddebpkgdir/DEBIAN/control
        fi
    else
        echo "WARNING! $pkgdir/DEBIAN/control does not contain Version, packaging error! Cannot adjust to binary package Version:" >&2
        debversion="$srcversion"
    fi

    # build .ddeb from its package directory
    ddebarch=`grep '^Architecture:' $ddebpkgdir/DEBIAN/control | cut -f2- -d\ `
    ddebsection=`grep '^Section:' $ddebpkgdir/DEBIAN/control | cut -f2- -d\ `
    ddebpriority=`grep '^Priority:' $ddebpkgdir/DEBIAN/control | cut -f2- -d\ `
    ddeb="${p}-dbgsym_${debversion#*:}_${ddebarch}.ddeb"
    dbg "building $ddeb"
    NO_PKG_MANGLE=1 dpkg-deb -Zxz --build $ddebpkgdir ../$ddeb
    add_to_files_stamp=$ddebpkgdir/DEBIAN/add_to_files.pkg-create-dbgsym
    if [ -e $add_to_files_stamp ]; then
	rm $add_to_files_stamp
	dbg "dpkg-distaddfile $ddeb $ddebsection $ddebpriority"
	dpkg-distaddfile "$ddeb" "$ddebsection" "$ddebpriority"
    fi

    # clean up ddeb pkg dir, since this package does not appear in debian/control
    rm -rf "$ddebpkgdir"
done