kbuild: deb-pkg: split image and debug objects staging out into functions
[platform/kernel/linux-starfive.git] / scripts / package / builddeb
1 #!/bin/sh
2 #
3 # builddeb 1.3
4 # Copyright 2003 Wichert Akkerman <wichert@wiggy.net>
5 #
6 # Simple script to generate a deb package for a Linux kernel. All the
7 # complexity of what to do with a kernel after it is installed or removed
8 # is left to other scripts and packages: they can install scripts in the
9 # /etc/kernel/{pre,post}{inst,rm}.d/ directories (or an alternative location
10 # specified in KDEB_HOOKDIR) that will be called on package install and
11 # removal.
12
13 set -e
14
15 is_enabled() {
16         grep -q "^$1=y" include/config/auto.conf
17 }
18
19 if_enabled_echo() {
20         if is_enabled "$1"; then
21                 echo -n "$2"
22         elif [ $# -ge 3 ]; then
23                 echo -n "$3"
24         fi
25 }
26
27 create_package() {
28         local pname="$1" pdir="$2"
29         local dpkg_deb_opts
30
31         mkdir -m 755 -p "$pdir/DEBIAN"
32         mkdir -p "$pdir/usr/share/doc/$pname"
33         cp debian/copyright "$pdir/usr/share/doc/$pname/"
34         cp debian/changelog "$pdir/usr/share/doc/$pname/changelog.Debian"
35         gzip -n -9 "$pdir/usr/share/doc/$pname/changelog.Debian"
36         sh -c "cd '$pdir'; find . -type f ! -path './DEBIAN/*' -printf '%P\0' \
37                 | xargs -r0 md5sum > DEBIAN/md5sums"
38
39         # Fix ownership and permissions
40         if [ "$DEB_RULES_REQUIRES_ROOT" = "no" ]; then
41                 dpkg_deb_opts="--root-owner-group"
42         else
43                 chown -R root:root "$pdir"
44         fi
45         # a+rX in case we are in a restrictive umask environment like 0077
46         # ug-s in case we build in a setuid/setgid directory
47         chmod -R go-w,a+rX,ug-s "$pdir"
48
49         # Create the package
50         dpkg-gencontrol -p$pname -P"$pdir"
51         dpkg-deb $dpkg_deb_opts ${KDEB_COMPRESS:+-Z$KDEB_COMPRESS} --build "$pdir" ..
52 }
53
54 install_linux_image () {
55         pdir=$1
56         pname=$2
57
58         rm -rf ${pdir}
59
60         # Only some architectures with OF support have this target
61         if is_enabled CONFIG_OF_EARLY_FLATTREE && [ -d "${srctree}/arch/${SRCARCH}/boot/dts" ]; then
62                 ${MAKE} -f ${srctree}/Makefile INSTALL_DTBS_PATH="${pdir}/usr/lib/linux-image-${KERNELRELEASE}" dtbs_install
63         fi
64
65         if is_enabled CONFIG_MODULES; then
66                 ${MAKE} -f ${srctree}/Makefile INSTALL_MOD_PATH="${pdir}" modules_install
67                 rm -f "${pdir}/lib/modules/${KERNELRELEASE}/build"
68                 rm -f "${pdir}/lib/modules/${KERNELRELEASE}/source"
69                 if [ "${SRCARCH}" = um ] ; then
70                         mkdir -p "${pdir}/usr/lib/uml/modules"
71                         mv "${pdir}/lib/modules/${KERNELRELEASE}" "${pdir}/usr/lib/uml/modules/${KERNELRELEASE}"
72                 fi
73         fi
74
75         # Install the kernel
76         if [ "${ARCH}" = um ] ; then
77                 mkdir -p "${pdir}/usr/bin" "${pdir}/usr/share/doc/${pname}"
78                 cp System.map "${pdir}/usr/lib/uml/modules/${KERNELRELEASE}/System.map"
79                 cp ${KCONFIG_CONFIG} "${pdir}/usr/share/doc/${pname}/config"
80                 gzip "${pdir}/usr/share/doc/${pname}/config"
81         else
82                 mkdir -p "${pdir}/boot"
83                 cp System.map "${pdir}/boot/System.map-${KERNELRELEASE}"
84                 cp ${KCONFIG_CONFIG} "${pdir}/boot/config-${KERNELRELEASE}"
85         fi
86
87         # Not all arches have the same installed path in debian
88         # XXX: have each arch Makefile export a variable of the canonical image install
89         # path instead
90         case "${SRCARCH}" in
91         um)
92                 installed_image_path="usr/bin/linux-${KERNELRELEASE}";;
93         parisc|mips|powerpc)
94                 installed_image_path="boot/vmlinux-${KERNELRELEASE}";;
95         *)
96                 installed_image_path="boot/vmlinuz-${KERNELRELEASE}";;
97         esac
98         cp "$(${MAKE} -s -f ${srctree}/Makefile image_name)" "${pdir}/${installed_image_path}"
99
100         # Install the maintainer scripts
101         # Note: hook scripts under /etc/kernel are also executed by official Debian
102         # kernel packages, as well as kernel packages built using make-kpkg.
103         # make-kpkg sets $INITRD to indicate whether an initramfs is wanted, and
104         # so do we; recent versions of dracut and initramfs-tools will obey this.
105         debhookdir=${KDEB_HOOKDIR:-/etc/kernel}
106         for script in postinst postrm preinst prerm; do
107                 mkdir -p "${pdir}${debhookdir}/${script}.d"
108
109                 mkdir -p "${pdir}/DEBIAN"
110                 cat <<-EOF > "${pdir}/DEBIAN/${script}"
111
112                 #!/bin/sh
113
114                 set -e
115
116                 # Pass maintainer script parameters to hook scripts
117                 export DEB_MAINT_PARAMS="\$*"
118
119                 # Tell initramfs builder whether it's wanted
120                 export INITRD=$(if_enabled_echo CONFIG_BLK_DEV_INITRD Yes No)
121
122                 test -d ${debhookdir}/${script}.d && run-parts --arg="${KERNELRELEASE}" --arg="/${installed_image_path}" ${debhookdir}/${script}.d
123                 exit 0
124                 EOF
125                 chmod 755 "${pdir}/DEBIAN/${script}"
126         done
127 }
128
129 install_linux_image_dbg () {
130         pdir=$1
131         image_pdir=$2
132
133         rm -rf ${pdir}
134
135         for module in $(find ${image_pdir}/lib/modules/ -name *.ko -printf '%P\n'); do
136                 module=lib/modules/${module}
137                 mkdir -p $(dirname ${pdir}/usr/lib/debug/${module})
138                 # only keep debug symbols in the debug file
139                 ${OBJCOPY} --only-keep-debug ${image_pdir}/${module} ${pdir}/usr/lib/debug/${module}
140                 # strip original module from debug symbols
141                 ${OBJCOPY} --strip-debug ${image_pdir}/${module}
142                 # then add a link to those
143                 ${OBJCOPY} --add-gnu-debuglink=${pdir}/usr/lib/debug/${module} ${image_pdir}/${module}
144         done
145
146         # re-sign stripped modules
147         if is_enabled CONFIG_MODULE_SIG_ALL; then
148                 ${MAKE} -f ${srctree}/Makefile INSTALL_MOD_PATH="${image_pdir}" modules_sign
149         fi
150
151         # Build debug package
152         # Different tools want the image in different locations
153         # perf
154         mkdir -p ${pdir}/usr/lib/debug/lib/modules/${KERNELRELEASE}/
155         cp vmlinux ${pdir}/usr/lib/debug/lib/modules/${KERNELRELEASE}/
156         # systemtap
157         mkdir -p ${pdir}/usr/lib/debug/boot/
158         ln -s ../lib/modules/${KERNELRELEASE}/vmlinux ${pdir}/usr/lib/debug/boot/vmlinux-${KERNELRELEASE}
159         # kdump-tools
160         ln -s lib/modules/${KERNELRELEASE}/vmlinux ${pdir}/usr/lib/debug/vmlinux-${KERNELRELEASE}
161 }
162
163 deploy_kernel_headers () {
164         pdir=$1
165
166         rm -rf $pdir
167
168         (
169                 cd $srctree
170                 find . arch/$SRCARCH -maxdepth 1 -name Makefile\*
171                 find include scripts -type f -o -type l
172                 find arch/$SRCARCH -name Kbuild.platforms -o -name Platform
173                 find $(find arch/$SRCARCH -name include -o -name scripts -type d) -type f
174         ) > debian/hdrsrcfiles
175
176         {
177                 if is_enabled CONFIG_OBJTOOL; then
178                         echo tools/objtool/objtool
179                 fi
180
181                 find arch/$SRCARCH/include Module.symvers include scripts -type f
182
183                 if is_enabled CONFIG_GCC_PLUGINS; then
184                         find scripts/gcc-plugins -name \*.so
185                 fi
186         } > debian/hdrobjfiles
187
188         destdir=$pdir/usr/src/linux-headers-$version
189         mkdir -p $destdir
190         tar -c -f - -C $srctree -T debian/hdrsrcfiles | tar -xf - -C $destdir
191         tar -c -f - -T debian/hdrobjfiles | tar -xf - -C $destdir
192         rm -f debian/hdrsrcfiles debian/hdrobjfiles
193
194         # copy .config manually to be where it's expected to be
195         cp $KCONFIG_CONFIG $destdir/.config
196
197         mkdir -p $pdir/lib/modules/$version/
198         ln -s /usr/src/linux-headers-$version $pdir/lib/modules/$version/build
199 }
200
201 deploy_libc_headers () {
202         pdir=$1
203
204         rm -rf $pdir
205
206         $MAKE -f $srctree/Makefile headers
207         $MAKE -f $srctree/Makefile headers_install INSTALL_HDR_PATH=$pdir/usr
208
209         # move asm headers to /usr/include/<libc-machine>/asm to match the structure
210         # used by Debian-based distros (to support multi-arch)
211         host_arch=$(dpkg-architecture -a$(cat debian/arch) -qDEB_HOST_MULTIARCH)
212         mkdir $pdir/usr/include/$host_arch
213         mv $pdir/usr/include/asm $pdir/usr/include/$host_arch/
214 }
215
216 version=$KERNELRELEASE
217 packagename=linux-image-$version
218 dbg_packagename=$packagename-dbg
219
220 if [ "$ARCH" = "um" ] ; then
221         packagename=user-mode-linux-$version
222 fi
223
224 rm -f debian/files
225
226 if [ "$ARCH" != "um" ]; then
227         if is_enabled CONFIG_MODULES; then
228                 deploy_kernel_headers debian/linux-headers
229                 create_package linux-headers-$version debian/linux-headers
230         fi
231
232         deploy_libc_headers debian/linux-libc-dev
233         create_package linux-libc-dev debian/linux-libc-dev
234 fi
235
236 install_linux_image debian/linux-image "$packagename"
237
238 if is_enabled CONFIG_DEBUG_INFO; then
239         install_linux_image_dbg debian/linux-image-dbg debian/linux-image
240         create_package "$dbg_packagename" debian/linux-image-dbg
241 fi
242
243 create_package "$packagename" debian/linux-image
244
245 exit 0