summaryrefslogtreecommitdiff
path: root/build-tor-sources
blob: adf5d2fa48ac1b835f0373b9d1a8e0d65f65281c (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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!/bin/bash

# helper script to build tor debian releases

# Copyright 2007,2008,2009,2010,2011,2012,2013 Peter Palfrader
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

assert_files_dont_exist () {
	local pkg="$1"; shift
	local debian_version="$1";
	if [ -z "$debian_version" ]; then
		echo "assert_files_dont_exist called without debian_version" >&2
		exit 1;
	fi

	if [ -e "${PKG}_$debian_version.diff.gz" ] ; then
		echo "${PKG}_$debian_version.diff.gz already exists" >&2
		exit 1;
	fi
	if [ -e "${PKG}_$debian_version.dsc" ] ; then
		echo "${PKG}_$debian_version.dsc already exists" >&2
		exit 1;
	fi
	if [ -e "${PKG}_$debian_version""_amd64.deb" ] ; then
		echo "${PKG}_$debian_version""_amd64.deb already exists" >&2
		exit 1;
	fi
	if [ -e "${PKG}_$debian_version""_amd64.changes" ] ; then
		echo "${PKG}_$debian_version""_amd64.changes already exists" >&2
		exit 1;
	fi
}

get_debian_version() {
	local dir="$1"; shift
	local which="${1:-}"; shift

	if [ -z "$which" ]; then
		( cd $dir && dpkg-parsechangelog | grep-dctrl -n -s Version '' )
	else
		local v=$(get_debian_version $dir)
		case "$which" in
			upstream) echo "${v%-*}" ;;
			debrev) echo "${v##*-}" ;;
			*)
				echo >&2 "Unknown key '$which' in get_debian_version"
				exit 1
		esac
	fi
}

# remove_completely ... 0 replace hardening-includes with hardening-wrapper
#                       1 get rid entirely
hardening_backport() {
	local remove_completely="$1"

	sed -i -e '/^Build-Depends/ s/, *hardening-includes//' debian/control
	if [ "$remove_completely" = 0 ]; then
		sed -i -e '/^Build-Depends/ s/$/, hardening-wrapper/' debian/control
	fi

	if [ "$remove_completely" = 0 ]; then
		sed -i -e 's#include /usr/share/hardening-includes/hardening.make#export DEB_BUILD_HARDENING=1#' debian/rules
		sed -i -e '/export DEB_BUILD_HARDENING=1/ a export DEB_BUILD_HARDENING_DEBUG=1' debian/rules
	else
		sed -i -e 's#include /usr/share/hardening-includes/hardening.make##' debian/rules
	fi

	if [ "$remove_completely" = 0 ]; then
		dch --append "Replace hardening-includes use with hardening-wrapper."
	else
		dch --append "Completely remove hardening-includes use."
	fi
}

remove_apparmor() {
	local builddep_only="${1:-}"

	if grep -q dh-apparmor debian/control; then
		sed -i -e '/^Build-Depends/ s/, *dh-apparmor//' debian/control
		if [ "$builddep_only" = 1 ]; then
			dch --append "Remove dh-apparmor build dependency for backport."
		else
			sed -i -e 's/dh_apparmor/# &/' debian/rules
			sed -i -e 's;install.*etc/apparmor.d;# &;' debian/rules
			dch --append "Remove apparmor support for backport."
		fi
	fi
}

bp1() {
	local pkg="$1"; shift
	local dir="$1"; shift
	local sid_debian_version="$1"; shift
	local dist="$1"; shift

	dpkg-source -x ${pkg}_$sid_debian_version.dsc
	(cd $dir; backport $dist)
}
bp2() {
	local pkg="$1"; shift
	local dir="$1"; shift
	local origtar="$1"; shift

	local debian_version=$(get_debian_version $dir)
	assert_files_dont_exist $pkg $debian_version
	dpkg-source -b $dir $origtar
	rm -r $dir
}

backport_all() {
	local pkg="$1"; shift
	local dir="$1"; shift
	local origtar="$1"; shift
	local sid_debian_version="$1"; shift

	# SID
	#################################################
	# null

	# SQUEEZE
	#################################################
	bp1 $pkg $dir $sid_debian_version squeeze
	(cd $dir; remove_apparmor)
	bp2 $pkg $dir $origtar

	# WHEEZY
	#################################################
	bp1 $pkg $dir $sid_debian_version wheezy
	bp2 $pkg $dir $origtar


	# LUCID  (EOL: April 2015)
	#################################################
	bp1 $pkg $dir $sid_debian_version lucid
	(cd $dir; remove_apparmor)
	bp2 $pkg $dir $origtar

	# NATTY  (EOL: October 2012)
	#################################################
	bp1 $pkg $dir $sid_debian_version natty
	(cd $dir; remove_apparmor)
	bp2 $pkg $dir $origtar

	# ONEIRIC  (EOL: April 2013)
	#################################################
	bp1 $pkg $dir $sid_debian_version oneiric
	(cd $dir; remove_apparmor 1) # dh_apparmor is in their patched debhelper
	bp2 $pkg $dir $origtar

	# PRECISE  (EOL: April 2017)
	#################################################
	bp1 $pkg $dir $sid_debian_version precise
	bp2 $pkg $dir $origtar

	# QUANTAL  (EOL: April 2014)
	#################################################
	bp1 $pkg $dir $sid_debian_version quantal
	bp2 $pkg $dir $origtar


	#################################################
	## BPO
	#################################################

	if [ "${DO_BPO:-}" = 1 ]; then
		# SQUEEZE-BPO
		#################################################
		bp1 $pkg $dir $sid_debian_version squeeze-bpo
		bp2 $pkg $dir $origtar

		mkdir bpo
		mv *'~bpo'* bpo/
	fi
}

main() {
	local origtar="$1"; shift
	local deb_revision="$1"; shift
	local gitdir="$1"; shift
	local pkg="$1"; shift

	[ -d local-build ] || mkdir local-build

	if [ -z "$origtar" ] ; then
		echo "Usage: $0 <orig.tar.gz> [version]" >&2
		exit 1;
	fi


	if [ ! -e "$origtar" ] ; then
		echo "$origtar does not exist." >&2
		exit 1;
	fi

	if [ "${origtar#${pkg}-}" != $origtar ]; then
		ver="$origtar"
		ver=${ver#${pkg}-}
		ver=${ver%.tar.gz}
		neworig="${pkg}_$ver.orig.tar.gz"
		if ! [ -e "$neworig" ]; then
			ln -v "$origtar" "$neworig"
		fi
		echo "Using $neworig instead of $origtar"
		origtar="$neworig"
	fi

	local dir
	local dir_version
	dir=`tar tzf $origtar 2>/dev/null | head -n1`
	dir="${dir%%/}"
	dir_version="${dir##${pkg}-}"
	if [ -e "$dir" ] ; then
		echo "$dir already exists." >&2
		exit 1;
	fi
	tar xzf $origtar
	git clone -n -s "$gitdir" git-"$dir"
	local tag="debian-${pkg}-$dir_version-$deb_revision"
	(cd "git-$dir" && git checkout $tag)
	if diff -qr "git-$dir" "$dir" --exclude .git  | grep -v '^Only in ' | grep --color .; then
		echo "Differenced detected."
		exit 1
	fi
	(cd "git-$dir" && echo "\"`git rev-parse --short=16 "$tag"`\"" > "debian/micro-revision.i")
	cp -av "git-$dir/debian" "$dir"
	rm -rf "git-$dir"


	debian_upstream_version=$(get_debian_version $dir upstream)
	if [ "$origtar" != "${pkg}_$debian_upstream_version.orig.tar.gz" ] ; then
		echo "possible mismatch: $origtar but $debian_upstream_version in debian/changelog" >&2
		exit 1;
	fi

	debian_version=$(get_debian_version $dir)
	sid_debian_version="$debian_version"
	assert_files_dont_exist $pkg $debian_version
	dpkg-source -b $dir $origtar
	rm -r $dir



	# local
	#################################################
	cd local-build
	dpkg-source -x ../${pkg}_$debian_version.dsc
	cd ${pkg}-$debian_upstream_version
	remove_apparmor
	debuild -j8 -rfakeroot -uc -us
	cd ../..


	backport_all "$pkg" "$dir" "$origtar" "$sid_debian_version"

	echo
	echo "All done"
}

# this is hardcoded to weasel's directory layout. sorry.
case "$(basename $0)" in
	build-tor-sources)
		set -e
		set -x
		GITDIR="$HOME/projects/tor/tor"
		PKG="tor"
		DO_BPO=1
		main "${1:-}" ${2:-1} $GITDIR $PKG
		;;
esac