summaryrefslogtreecommitdiffstats
path: root/scripts/package/builddeb
blob: c5ae57167d7ceb8c8a3633b77829057f45e278ff (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
#!/bin/sh
#
# builddeb 1.3
# Copyright 2003 Wichert Akkerman <wichert@wiggy.net>
#
# Simple script to generate a deb package for a Linux kernel. All the
# complexity of what to do with a kernel after it is installed or removed
# is left to other scripts and packages: they can install scripts in the
# /etc/kernel/{pre,post}{inst,rm}.d/ directories (or an alternative location
# specified in KDEB_HOOKDIR) that will be called on package install and
# removal.

set -e

is_enabled() {
	grep -q "^$1=y" include/config/auto.conf
}

if_enabled_echo() {
	if is_enabled "$1"; then
		echo -n "$2"
	elif [ $# -ge 3 ]; then
		echo -n "$3"
	fi
}

create_package() {
	local pname="$1" pdir="$2"
	local dpkg_deb_opts

	mkdir -m 755 -p "$pdir/DEBIAN"
	mkdir -p "$pdir/usr/share/doc/$pname"
	cp debian/copyright "$pdir/usr/share/doc/$pname/"
	cp debian/changelog "$pdir/usr/share/doc/$pname/changelog.Debian"
	gzip -n -9 "$pdir/usr/share/doc/$pname/changelog.Debian"
	sh -c "cd '$pdir'; find . -type f ! -path './DEBIAN/*' -printf '%P\0' \
		| xargs -r0 md5sum > DEBIAN/md5sums"

	# Fix ownership and permissions
	if [ "$DEB_RULES_REQUIRES_ROOT" = "no" ]; then
		dpkg_deb_opts="--root-owner-group"
	else
		chown -R root:root "$pdir"
	fi
	# a+rX in case we are in a restrictive umask environment like 0077
	# ug-s in case we build in a setuid/setgid directory
	chmod -R go-w,a+rX,ug-s "$pdir"

	# Create the package
	dpkg-gencontrol -p$pname -P"$pdir"
	dpkg-deb $dpkg_deb_opts ${KDEB_COMPRESS:+-Z$KDEB_COMPRESS} --build "$pdir" ..
}

install_linux_image () {
	pdir=$1
	pname=$2

	rm -rf ${pdir}

	# Only some architectures with OF support have this target
	if is_enabled CONFIG_OF_EARLY_FLATTREE && [ -d "${srctree}/arch/${SRCARCH}/boot/dts" ]; then
		${MAKE} -f ${srctree}/Makefile INSTALL_DTBS_PATH="${pdir}/usr/lib/linux-image-${KERNELRELEASE}" dtbs_install
	fi

	if is_enabled CONFIG_MODULES; then
		${MAKE} -f ${srctree}/Makefile INSTALL_MOD_PATH="${pdir}" modules_install
		rm -f "${pdir}/lib/modules/${KERNELRELEASE}/build"
		rm -f "${pdir}/lib/modules/${KERNELRELEASE}/source"
		if [ "${SRCARCH}" = um ] ; then
			mkdir -p "${pdir}/usr/lib/uml/modules"
			mv "${pdir}/lib/modules/${KERNELRELEASE}" "${pdir}/usr/lib/uml/modules/${KERNELRELEASE}"
		fi
	fi

	# Install the kernel
	if [ "${ARCH}" = um ] ; then
		mkdir -p "${pdir}/usr/bin" "${pdir}/usr/share/doc/${pname}"
		cp System.map "${pdir}/usr/lib/uml/modules/${KERNELRELEASE}/System.map"
		cp ${KCONFIG_CONFIG} "${pdir}/usr/share/doc/${pname}/config"
		gzip "${pdir}/usr/share/doc/${pname}/config"
	else
		mkdir -p "${pdir}/boot"
		cp System.map "${pdir}/boot/System.map-${KERNELRELEASE}"
		cp ${KCONFIG_CONFIG} "${pdir}/boot/config-${KERNELRELEASE}"
	fi

	# Not all arches have the same installed path in debian
	# XXX: have each arch Makefile export a variable of the canonical image install
	# path instead
	case "${SRCARCH}" in
	um)
		installed_image_path="usr/bin/linux-${KERNELRELEASE}";;
	parisc|mips|powerpc)
		installed_image_path="boot/vmlinux-${KERNELRELEASE}";;
	*)
		installed_image_path="boot/vmlinuz-${KERNELRELEASE}";;
	esac
	cp "$(${MAKE} -s -f ${srctree}/Makefile image_name)" "${pdir}/${installed_image_path}"

	# Install the maintainer scripts
	# Note: hook scripts under /etc/kernel are also executed by official Debian
	# kernel packages, as well as kernel packages built using make-kpkg.
	# make-kpkg sets $INITRD to indicate whether an initramfs is wanted, and
	# so do we; recent versions of dracut and initramfs-tools will obey this.
	debhookdir=${KDEB_HOOKDIR:-/etc/kernel}
	for script in postinst postrm preinst prerm; do
		mkdir -p "${pdir}${debhookdir}/${script}.d"

		mkdir -p "${pdir}/DEBIAN"
		cat <<-EOF > "${pdir}/DEBIAN/${script}"

		#!/bin/sh

		set -e

		# Pass maintainer script parameters to hook scripts
		export DEB_MAINT_PARAMS="\$*"

		# Tell initramfs builder whether it's wanted
		export INITRD=$(if_enabled_echo CONFIG_BLK_DEV_INITRD Yes No)

		test -d ${debhookdir}/${script}.d && run-parts --arg="${KERNELRELEASE}" --arg="/${installed_image_path}" ${debhookdir}/${script}.d
		exit 0
		EOF
		chmod 755 "${pdir}/DEBIAN/${script}"
	done
}

install_linux_image_dbg () {
	pdir=$1
	image_pdir=$2

	rm -rf ${pdir}

	for module in $(find ${image_pdir}/lib/modules/ -name *.ko -printf '%P\n'); do
		module=lib/modules/${module}
		mkdir -p $(dirname ${pdir}/usr/lib/debug/${module})
		# only keep debug symbols in the debug file
		${OBJCOPY} --only-keep-debug ${image_pdir}/${module} ${pdir}/usr/lib/debug/${module}
		# strip original module from debug symbols
		${OBJCOPY} --strip-debug ${image_pdir}/${module}
		# then add a link to those
		${OBJCOPY} --add-gnu-debuglink=${pdir}/usr/lib/debug/${module} ${image_pdir}/${module}
	done

	# re-sign stripped modules
	if is_enabled CONFIG_MODULE_SIG_ALL; then
		${MAKE} -f ${srctree}/Makefile INSTALL_MOD_PATH="${image_pdir}" modules_sign
	fi

	# Build debug package
	# Different tools want the image in different locations
	# perf
	mkdir -p ${pdir}/usr/lib/debug/lib/modules/${KERNELRELEASE}/
	cp vmlinux ${pdir}/usr/lib/debug/lib/modules/${KERNELRELEASE}/
	# systemtap
	mkdir -p ${pdir}/usr/lib/debug/boot/
	ln -s ../lib/modules/${KERNELRELEASE}/vmlinux ${pdir}/usr/lib/debug/boot/vmlinux-${KERNELRELEASE}
	# kdump-tools
	ln -s lib/modules/${KERNELRELEASE}/vmlinux ${pdir}/usr/lib/debug/vmlinux-${KERNELRELEASE}
}

install_kernel_headers () {
	pdir=$1

	rm -rf $pdir

	(
		cd $srctree
		find . arch/$SRCARCH -maxdepth 1 -name Makefile\*
		find include scripts -type f -o -type l
		find arch/$SRCARCH -name Kbuild.platforms -o -name Platform
		find $(find arch/$SRCARCH -name include -o -name scripts -type d) -type f
	) > debian/hdrsrcfiles

	{
		if is_enabled CONFIG_OBJTOOL; then
			echo tools/objtool/objtool
		fi

		find arch/$SRCARCH/include Module.symvers include scripts -type f

		if is_enabled CONFIG_GCC_PLUGINS; then
			find scripts/gcc-plugins -name \*.so
		fi
	} > debian/hdrobjfiles

	destdir=$pdir/usr/src/linux-headers-$version
	mkdir -p $destdir
	tar -c -f - -C $srctree -T debian/hdrsrcfiles | tar -xf - -C $destdir
	tar -c -f - -T debian/hdrobjfiles | tar -xf - -C $destdir
	rm -f debian/hdrsrcfiles debian/hdrobjfiles

	# copy .config manually to be where it's expected to be
	cp $KCONFIG_CONFIG $destdir/.config

	mkdir -p $pdir/lib/modules/$version/
	ln -s /usr/src/linux-headers-$version $pdir/lib/modules/$version/build
}

install_libc_headers () {
	pdir=$1

	rm -rf $pdir

	$MAKE -f $srctree/Makefile headers
	$MAKE -f $srctree/Makefile headers_install INSTALL_HDR_PATH=$pdir/usr

	# move asm headers to /usr/include/<libc-machine>/asm to match the structure
	# used by Debian-based distros (to support multi-arch)
	host_arch=$(dpkg-architecture -a$(cat debian/arch) -qDEB_HOST_MULTIARCH)
	mkdir $pdir/usr/include/$host_arch
	mv $pdir/usr/include/asm $pdir/usr/include/$host_arch/
}

rm -f debian/files

packages_enabled=$(dh_listpackages)

for package in ${packages_enabled}
do
	case ${package} in
	*-dbg)
		# This must be done after linux-image, that is, we expect the
		# debug package appears after linux-image in debian/control.
		install_linux_image_dbg debian/linux-image-dbg debian/linux-image;;
	linux-image-*|user-mode-linux-*)
		install_linux_image debian/linux-image ${package};;
	linux-libc-dev)
		install_libc_headers debian/linux-libc-dev;;
	linux-headers-*)
		install_kernel_headers debian/linux-headers;;
	esac
done

for package in ${packages_enabled}
do
	case ${package} in
	*-dbg)
		create_package ${package} debian/linux-image-dbg;;
	linux-image-*|user-mode-linux-*)
		create_package ${package} debian/linux-image;;
	linux-libc-dev)
		create_package ${package} debian/linux-libc-dev;;
	linux-headers-*)
		create_package ${package} debian/linux-headers;;
	esac
done

exit 0