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