jjb: Add objtool to kernel headers for lttng-modules
[lttng-ci.git] / scripts / lttng-modules / param-build.sh
CommitLineData
f3d8604b
MJ
1#!/bin/sh -exu
2#
3# Copyright (C) 2016 - Michael Jeanson <mjeanson@efficios.com>
4#
5# This program is free software: you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation, either version 3 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program. If not, see <http://www.gnu.org/licenses/>.
17
39961776
MJ
18# Parameters
19arch=${arch:-}
20cross_arch=${cross_arch:-}
21ktag=${ktag:-}
22
23
339db64d
MJ
24## FUNCTIONS ##
25
f3d8604b
MJ
26# Kernel version compare functions
27verlte() {
51fa2ab3 28 [ "$1" = "$(printf '%s\n%s' "$1" "$2" | sort -V | head -n1)" ]
f3d8604b
MJ
29}
30
31verlt() {
51fa2ab3 32 [ "$1" = "$2" ] && return 1 || verlte "$1" "$2"
f3d8604b
MJ
33}
34
35vergte() {
51fa2ab3 36 [ "$1" = "$(printf '%s\n%s' "$1" "$2" | sort -V | tail -n1)" ]
f3d8604b
MJ
37}
38
39vergt() {
51fa2ab3 40 [ "$1" = "$2" ] && return 1 || vergte "$1" "$2"
f3d8604b
MJ
41}
42
339db64d 43
7e02032c
MJ
44prepare_lnx_sources() {
45
46 outdir=$1
47
48 if [ "$outdir" = "." ]; then
49 koutput=""
50 else
cbd45bc3 51 koutput="O=${outdir}"
7e02032c
MJ
52 fi
53
54 # Generate kernel configuration
a1ae361e 55 case "$ktag" in
7e02032c 56 Ubuntu*)
39961776
MJ
57 if [ "${cross_arch}" = "powerpc" ]; then
58 if vergte "$KVERSION" "4.10"; then
59 echo "Ubuntu removed big endian powerpc configuration from kernel >= 4.10. Don't try to build it."
60 exit 0
61 fi
62 fi
7e02032c
MJ
63 fakeroot debian/rules clean
64 fakeroot debian/rules genconfigs
51fa2ab3 65 cp CONFIGS/"${ubuntu_config}" "${outdir}"/.config
7e02032c
MJ
66 ;;
67 *)
a822250c 68 # Que sera sera
51fa2ab3 69 make "${vanilla_config}" CC="$CC" ${koutput}
7e02032c
MJ
70 ;;
71 esac
72
73 # GCC 4.8
74 sed -i "s/CONFIG_CC_STACKPROTECTOR_STRONG=y/# CONFIG_CC_STACKPROTECTOR_STRONG is not set/g" "${outdir}"/.config
75
76 # Don't try to sign modules
77 sed -i "s/CONFIG_MODULE_SIG=y/# CONFIG_MODULE_SIG is not set/g" "${outdir}"/.config
78
9ef18fa2
MJ
79 # Disable kernel stack frame correctness validation, introduced in 4.6.0 and currently fails
80 sed -i "s/CONFIG_STACK_VALIDATION=y/# CONFIG_STACK_VALIDATION is not set/g" "${outdir}"/.config
81
51fa2ab3
MJ
82 # Set required options
83 {
84 echo "CONFIG_KPROBES=y";
85 echo "CONFIG_FTRACE=y";
86 echo "CONFIG_BLK_DEV_IO_TRACE=y";
87 echo "CONFIG_TRACEPOINTS=y";
88 echo "CONFIG_KALLSYMS_ALL=y";
89 } >> "${outdir}"/.config
7e02032c
MJ
90
91
51fa2ab3
MJ
92 make "$oldconf_target" CC="$CC" ${koutput}
93 make modules_prepare CC="$CC" ${koutput}
94
95 # Debug
96 #cat "${outdir}"/.config
7e02032c 97
d815b018
MJ
98 # On powerpc this object is required to link modules
99 if [ "${karch}" = "powerpc" ]; then
100 make arch/powerpc/lib/crtsavres.o CC="$CC" ${koutput}
101 fi
102
a1ae361e
MJ
103 # On arm64 this object is required to build with ftrace support
104 if [ "${karch}" = "arm64" ]; then
105 if vergte "$KVERSION" "4.13-rc1"; then
106 make arch/arm64/kernel/ftrace-mod.o CC="$CC" ${koutput}
107 fi
108 fi
109
7e02032c 110 # Version specific tasks
a1ae361e 111 case "$ktag" in
7e02032c
MJ
112 Ubuntu*)
113 # Add Ubuntu ABI number to kernel headers, this is normally done by the packaging code
a1ae361e 114 ABINUM="$(echo "$ktag" | grep -P -o 'Ubuntu-(lts-)?.*-\K\d+(?=\..*)')"
51fa2ab3 115 echo "#define UTS_UBUNTU_RELEASE_ABI $ABINUM" >> "${outdir}"/include/generated/utsrelease.h
7e02032c
MJ
116 ;;
117 esac
7e02032c
MJ
118}
119
120
121
339db64d
MJ
122build_modules() {
123
124 kdir="$1"
125 bdir="$2"
126
339db64d
MJ
127 # Enter lttng-modules source dir
128 cd "${LTTSRCDIR}"
129
130 # kernels 3.10 to 3.10.13 and 3.11 to 3.11.2 introduce a deadlock in the
131 # timekeeping subsystem. We want those build to fail.
a1ae361e
MJ
132 if { vergte "$KVERSION" "3.10" && verlte "$KVERSION" "3.10.13"; } || \
133 { vergte "$KVERSION" "3.11" && verlte "$KVERSION" "3.11.2"; }; then
339db64d
MJ
134
135 set +e
136
137 # Build modules
51fa2ab3 138 KERNELDIR="${kdir}" make -j"${NPROC}" V=1 CC="$CC"
339db64d
MJ
139
140 # We expect this build to fail, if it doesn't, fail the job.
141 if [ "$?" -eq 0 ]; then
e9b44189 142 echo "This build should have failed."
339db64d
MJ
143 exit 1
144 fi
145
146 # We have to publish at least one file or the build will fail
7e02032c 147 echo "This kernel is broken, there is a deadlock in the timekeeping subsystem." > "${bdir}/BROKEN.txt.ko"
339db64d
MJ
148
149 set -e
150
51fa2ab3 151 KERNELDIR="${kdir}" make clean CC="$CC"
7e02032c 152
339db64d
MJ
153 else # Regular build
154
155 # Build modules against full kernel sources
51fa2ab3 156 KERNELDIR="${kdir}" make -j"${NPROC}" V=1 CC="$CC"
339db64d
MJ
157
158 # Install modules to build dir
51fa2ab3 159 KERNELDIR="${kdir}" make INSTALL_MOD_PATH="${bdir}" modules_install CC="$CC"
339db64d
MJ
160
161 # Clean build dir
51fa2ab3 162 KERNELDIR="${kdir}" make clean CC="$CC"
339db64d
MJ
163 fi
164}
165
166
167## MAIN ##
168
f3d8604b
MJ
169# Use all CPU cores
170NPROC=$(nproc)
171
339db64d
MJ
172LTTSRCDIR="${WORKSPACE}/src/lttng-modules"
173LNXSRCDIR="${WORKSPACE}/src/linux"
174
175LNXBUILDDIR="${WORKSPACE}/build/linux"
176LNXHDRDIR="${WORKSPACE}/build/linux-headers"
177
a822250c 178LTTBUILDKSRCDIR="${WORKSPACE}/build/lttng-modules-ksrc"
339db64d
MJ
179LTTBUILDKHDRDIR="${WORKSPACE}/build/lttng-modules-khdr"
180
181
7e02032c 182# Setup cross compile env if available
39961776 183if [ "x${cross_arch}" != "x" ]; then
7e02032c
MJ
184
185 case "$cross_arch" in
186 "armhf")
187 karch="arm"
188 cross_compile="arm-linux-gnueabihf-"
e9b44189 189 vanilla_config="allyesconfig"
7e02032c
MJ
190 ubuntu_config="armhf-config.flavour.generic"
191 ;;
192
193 "arm64")
194 karch="arm64"
195 cross_compile="aarch64-linux-gnu-"
e9b44189 196 vanilla_config="allyesconfig"
7e02032c
MJ
197 ubuntu_config="arm64-config.flavour.generic"
198 ;;
199
200 "powerpc")
201 karch="powerpc"
202 cross_compile="powerpc-linux-gnu-"
e9b44189 203 vanilla_config="ppc44x_defconfig"
7e02032c
MJ
204 ubuntu_config="powerpc-config.flavour.powerpc-smp"
205 ;;
206
207 "ppc64el")
208 karch="powerpc"
209 cross_compile="powerpc64le-linux-gnu-"
e9b44189 210 vanilla_config="pseries_le_defconfig"
7e02032c
MJ
211 ubuntu_config="ppc64el-config.flavour.generic"
212 ;;
213
214 *)
51fa2ab3 215 echo "Unsupported cross arch $cross_arch"
7e02032c
MJ
216 exit 1
217 ;;
218 esac
339db64d 219
fca23c5a
MJ
220 # Use gcc 4.9, older kernel don't build with gcc 5
221 CC="${cross_compile}gcc-4.9"
e9b44189 222
7e02032c
MJ
223 # Export variables used by Kbuild for cross compilation
224 export ARCH="${karch}"
225 export CROSS_COMPILE="${cross_compile}"
339db64d 226
e0552fd7
MJ
227 oldconf_target="olddefconfig"
228
7e02032c 229# Set arch specific values if we are not cross compiling
39961776 230elif [ "x${arch}" != "x" ]; then
e9b44189 231
7e02032c
MJ
232 case "$arch" in
233 "x86-32")
234 karch="x86"
e9b44189 235 vanilla_config="allyesconfig"
7e02032c
MJ
236 ubuntu_config="i386-config.flavour.generic"
237 ;;
238
239 "x86-64")
240 karch="x86"
e9b44189 241 vanilla_config="allyesconfig"
7e02032c
MJ
242 ubuntu_config="amd64-config.flavour.generic"
243 ;;
244
245 "armhf")
246 karch="arm"
e9b44189 247 vanilla_config="allyesconfig"
7e02032c
MJ
248 ubuntu_config="armhf-config.flavour.generic"
249 ;;
250
251 "arm64")
252 karch="arm64"
e9b44189 253 vanilla_config="allyesconfig"
7e02032c
MJ
254 ubuntu_config="arm64-config.flavour.generic"
255 ;;
256
257 "powerpc")
258 karch="powerpc"
e9b44189 259 vanilla_config="allyesconfig"
7e02032c
MJ
260 ubuntu_config="powerpc-config.flavour.powerpc-smp"
261 ;;
262
263 "ppc64el")
264 karch="powerpc"
e9b44189 265 vanilla_config="allyesconfig"
7e02032c
MJ
266 ubuntu_config="ppc64el-config.flavour.generic"
267 ;;
268
269 *)
270 echo "Unsupported arch $arch"
271 exit 1
272 ;;
273 esac
e9b44189
MJ
274
275 # Use gcc 4.9, older kernel don't build with gcc 5
276 CC=gcc-4.9
277
e0552fd7
MJ
278 oldconf_target="silentoldconfig"
279
7e02032c
MJ
280else
281 echo "Not arch or cross_arch specified"
282 exit 1
283fi
339db64d 284
339db64d 285
b214d997 286
339db64d
MJ
287
288# Create build directories
a822250c 289mkdir -p "${LNXBUILDDIR}" "${LNXHDRDIR}" "${LTTBUILDKSRCDIR}" "${LTTBUILDKHDRDIR}"
7e02032c 290
339db64d
MJ
291
292
293## PREPARE DISTRO STYLE KERNEL HEADERS / DEVEL
294
295# Enter linux source dir
296cd "${LNXSRCDIR}"
297
a1ae361e
MJ
298# Get kernel version from source tree
299KVERSION=$(make kernelversion)
300
7e02032c 301prepare_lnx_sources "."
339db64d 302
b214d997
MJ
303# For RT kernels, copy version file
304if [ -s localversion-rt ]; then
305 cp -a localversion-rt "${LNXHDRDIR}"
339db64d
MJ
306fi
307
b214d997
MJ
308# Copy all Makefile related stuff
309find . -path './include/*' -prune \
310 -o -path './scripts/*' -prune -o -type f \
311 \( -name 'Makefile*' -o -name 'Kconfig*' -o -name 'Kbuild*' -o \
312 -name '*.sh' -o -name '*.pl' -o -name '*.lds' \) \
313 -print | cpio -pd --preserve-modification-time "${LNXHDRDIR}"
339db64d 314
b214d997
MJ
315# Copy base scripts and include dirs
316cp -a scripts include "${LNXHDRDIR}"
339db64d 317
b214d997 318# Copy arch includes
51fa2ab3
MJ
319(find arch -name include -type d -print0 | \
320 xargs -0 -n1 -i: find : -type f) | \
b214d997 321 cpio -pd --preserve-modification-time "${LNXHDRDIR}"
339db64d 322
b214d997 323# Copy arch scripts
51fa2ab3
MJ
324(find arch -name scripts -type d -print0 | \
325 xargs -0 -n1 -i: find : -type f) | \
b214d997 326 cpio -pd --preserve-modification-time "${LNXHDRDIR}"
339db64d 327
b214d997 328# Cleanup scripts
339db64d
MJ
329rm -f "${LNXHDRDIR}/scripts/*.o"
330rm -f "${LNXHDRDIR}/scripts/*/*.o"
331
b214d997 332# On powerpc this object is required to link modules
339db64d
MJ
333if [ "${karch}" = "powerpc" ]; then
334 cp -a --parents arch/powerpc/lib/crtsavres.[So] "${LNXHDRDIR}/"
335fi
336
a1ae361e
MJ
337# On arm64 this object is required to build with ftrace support
338if [ "${karch}" = "arm64" ]; then
339 if vergte "$KVERSION" "4.13-rc1"; then
340 cp -a --parents arch/arm64/kernel/ftrace-mod.[So] "${LNXHDRDIR}/"
341 fi
342fi
343
d138fc44
MJ
344# Newer kernels need objtool to build modules when CONFIG_STACK_VALIDATION=y
345if [ -f tools/objtool/objtool ]; then
346 cp -a --parents tools/objtool/objtool "${LNXHDRDIR}/"
347fi
348
b214d997
MJ
349# Copy modules related stuff, if available
350if [ -s Module.symvers ]; then
351 cp Module.symvers "${LNXHDRDIR}"
352fi
353
354if [ -s System.map ]; then
355 cp System.map "${LNXHDRDIR}"
356fi
357
358if [ -s Module.markers ]; then
359 cp Module.markers "${LNXHDRDIR}"
339db64d
MJ
360fi
361
b214d997
MJ
362# Copy config file
363cp .config "${LNXHDRDIR}"
339db64d
MJ
364
365# Make sure the Makefile and version.h have a matching timestamp so that
366# external modules can be built
367if [ -s "${LNXHDRDIR}/include/generated/uapi/linux/version.h" ]; then
368 touch -r "${LNXHDRDIR}/Makefile" "${LNXHDRDIR}/include/generated/uapi/linux/version.h"
369elif [ -s "${LNXHDRDIR}/include/linux/version.h" ]; then
370 touch -r "${LNXHDRDIR}/Makefile" "${LNXHDRDIR}/include/linux/version.h"
371else
372 echo "Missing version.h"
373 exit 1
374fi
375touch -r "${LNXHDRDIR}/.config" "${LNXHDRDIR}/include/generated/autoconf.h"
376
377# Copy .config to include/config/auto.conf so "make prepare" is unnecessary.
378cp "${LNXHDRDIR}/.config" "${LNXHDRDIR}/include/config/auto.conf"
379
380
381
382
383## PREPARE FULL LINUX SOURCE TREE
f3d8604b
MJ
384
385# Enter linux source dir
386cd "${LNXSRCDIR}"
387
339db64d 388# Make sure linux source dir is clean
7e02032c 389git clean -xdf
f3d8604b 390
7e02032c 391prepare_lnx_sources "${LNXBUILDDIR}"
b214d997 392
f3d8604b 393
7e02032c 394## BUILD modules
f3d8604b 395
339db64d 396# Build modules against full kernel sources
a822250c 397build_modules "${LNXBUILDDIR}" "${LTTBUILDKSRCDIR}"
f3d8604b 398
339db64d
MJ
399# Build modules against kernel headers
400build_modules "${LNXHDRDIR}" "${LTTBUILDKHDRDIR}"
f3d8604b 401
b214d997 402# Make sure modules were built
a822250c
MJ
403tree "${LTTBUILDKSRCDIR}"
404if [ "x$(find "${LTTBUILDKSRCDIR}" -name '*.ko*' -printf yes -quit)" != "xyes" ]; then
7e02032c
MJ
405 echo "No modules built!"
406 exit 1
407fi
408
a822250c
MJ
409tree "${LTTBUILDKHDRDIR}"
410if [ "x$(find "${LTTBUILDKHDRDIR}" -name '*.ko*' -printf yes -quit)" != "xyes" ]; then
7e02032c
MJ
411 echo "No modules built!"
412 exit 1
413fi
b214d997 414
f3d8604b 415# EOF
This page took 0.0419079999999999 seconds and 4 git commands to generate.