summaryrefslogtreecommitdiffstats
path: root/scripts/debrepro.sh
blob: d5935a6f325d95059c4d12b66bab22e302f57ef6 (plain)
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
240
241
242
243
244
#!/bin/sh

# debrepro: a reproducibility tester for Debian packages
#
# © 2016 Antonio Terceiro <terceiro@debian.org>
#
# 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, see <https://www.gnu.org/licenses/>.

set -eu

check_dependencies() {
    for optional in disorderfs diffoscope; do
        if ! which "$optional" > /dev/null; then
            echo "W: $optional not installed, there will be missing functionality" >&2
        fi
    done

    local failed=''
    for mandatory in faketime; do
        if ! which "$mandatory" > /dev/null; then
            echo "E: $mandatory not installed, cannot proceed." >&2
            failed=yes
        fi
    done
    if [ -n "$failed" ]; then
        exit 3
    fi
}

usage() {
    echo "usage: $0 [OPTIONS] [SOURCEDIR]"
    echo ""
    echo "Options:"
    echo ""
    echo " -b,--before-second-build COMMAND  Run COMMAND before second build"
    echo "                                   (e.g. apply a patch)"
    echo " -s,--skip VARIATION               Don't perform the named variation"
    echo " -h,--help                         Display this help message and exit"
}

first_banner=y
banner() {
    if [ "$first_banner" = n ]; then
        echo
    fi
    echo "$@" | sed -e 's/./=/g'
    echo "$@"
    echo "$@" | sed -e 's/./=/g'
    echo
    first_banner=n
}

variation() {
    echo
    echo "# Variation:" "$@"
}

vary() {
    local var="$1"

    for skipped in $skip_variations; do
        if [ "$skipped" = "$var" ]; then
            return
        fi
    done

    variation "$var"
    local first="$2"
    local second="$3"
    if [ "$which_build" = 'first' ]; then
        if [ -n "$first" ]; then
            echo "$first"
        fi
    else
        echo "$second"
    fi
}

create_build_script() {
    echo 'set -eu'

    echo
    echo "# this script must be run from inside an unpacked Debian source"
    echo "# package"
    echo

    vary path \
        '' \
        'export PATH="$PATH":/i/capture/the/path'

    vary user \
        'export USER=user1' \
        'export USER=user2'

    vary umask \
        'umask 0022' \
        'umask 0002'

    vary locale \
        'export LC_ALL=C.UTF-8 LANG=C.UTF-8' \
        'export LC_ALL=pt_BR.UTF-8 LANG=pt_BR.UTF-8'

    vary timezone \
        'export TZ=GMT+12' \
        'export TZ=GMT-14'

    if which disorderfs >/dev/null; then
        disorderfs_commands='cd .. &&
mv source orig &&
mkdir source &&
disorderfs --shuffle-dirents=yes orig source &&
trap "cd .. && fusermount -u source && rmdir source && mv orig source" INT TERM EXIT &&
cd source'
        vary filesystem-ordering \
            '' \
            "$disorderfs_commands"
    fi

    vary time \
        'build_prefix=""' \
        'build_prefix="faketime +213days+7hours+13minutes"; export NO_FAKE_STAT=1'

    echo '${build_prefix:-} dpkg-buildpackage -b -us -uc'
}


build() {
    export which_build="$1"
    mkdir "$tmpdir/build"

    if [ "$which_build" = second ] && [ -n "$before_second_build_command" ]; then
        banner "I: running before second build: $before_second_build_command"
        $before_second_build_command
    fi

    cp -r "$SOURCE" "$tmpdir/build/source"

    cd "$tmpdir/build/source"
    create_build_script > ../build.sh
    sh ../build.sh
    cd -

    mv "$tmpdir/build" "$tmpdir/$which_build"
}

binmatch() {
    cmp --silent "$1" "$2"
}

compare() {
    rc=0
    for first_deb in "$tmpdir"/first/*.deb; do
        deb="$(basename "$first_deb")"
        second_deb="$tmpdir"/second/"$deb"
        if binmatch "$first_deb" "$second_deb"; then
            echo "✓ $deb: binaries match"
        else
            echo "✗ $deb: binaries don't match"
            rc=1
        fi
    done
    if [ "$rc" -ne 0 ]; then
        if which diffoscope >/dev/null; then
            diffoscope "$tmpdir"/first/*.changes "$tmpdir"/second/*.changes || true
        else
            echo "I: install diffoscope for a deep comparison between artifacts"
        fi
        echo "E: package is not reproducible."
    fi
    return "$rc"
}

TEMP=$(getopt -n "debrepro" -o 'hs:b:' \
    -l 'help,skip:,before-second-build:' \
    -- "$@") || (rc=$?; usage >&2; exit $rc)
eval set -- "$TEMP"

skip_variations=""
before_second_build_command=''
while true; do
    case "$1" in
        -s|--skip)
            case "$2" in
                user|path|umask|locale|timezone|filesystem-ordering|time)
                    skip_variations="$skip_variations $2"
                    ;;
                *)
                    echo "E: invalid variation name $2"
                    exit 1
                    ;;
            esac
            shift
            ;;
        -b|--before-second-build)
            before_second_build_command="$2"
            shift
            ;;
        -h|--help)
            usage
            exit
            ;;
        --)
            shift
            break
            ;;
    esac
    shift
done

SOURCE="${1:-}"
if [ -z "$SOURCE" ]; then
    SOURCE="$(pwd)"
fi
if [ ! -f "$SOURCE/debian/changelog" ]; then
    echo "E: $SOURCE does not look like a Debian source package"
    exit 2
fi

tmpdir=$(mktemp --directory --tmpdir debrepro.XXXXXXXXXX)
trap "if [ \$? -eq 0 ]; then rm -rf $tmpdir; else echo; echo 'I: artifacts left in $tmpdir'; fi" INT TERM EXIT

check_dependencies

banner "First build"
build first

banner "Second build"
build second

banner "Comparing binaries"
compare first second

# vim:ts=4 sw=4 et