jjb: Add no-pie to kernel builds for modules
[lttng-ci.git] / scripts / lttng-modules / param-build.sh
1 #!/bin/bash -exu
2 #
3 # Copyright (C) 2016-2018 - 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 # Parameters
19 arch=${arch:-}
20 cross_arch=${cross_arch:-}
21 ktag=${ktag:-}
22 kgitrepo=${kgitrepo:-}
23 mversion=${mversion:-}
24 mgitrepo=${mgitrepo:-}
25
26
27 ## FUNCTIONS ##
28
29 # Kernel version compare functions
30 verlte() {
31 [ "$1" = "$(printf '%s\n%s' "$1" "$2" | sort -V | head -n1)" ]
32 }
33
34 verlt() {
35 [ "$1" = "$2" ] && return 1 || verlte "$1" "$2"
36 }
37
38 vergte() {
39 [ "$1" = "$(printf '%s\n%s' "$1" "$2" | sort -V | tail -n1)" ]
40 }
41
42 vergt() {
43 [ "$1" = "$2" ] && return 1 || vergte "$1" "$2"
44 }
45
46
47 git_clone_modules_sources() {
48 mkdir -p "$MODULES_GIT_DIR"
49 git clone --depth=1 -b "${mversion}" "${mgitrepo}" "$MODULES_GIT_DIR"
50 }
51
52 # Checkout a shallow kernel tree of the specified tag
53 git_clone_linux_sources() {
54 mkdir -p "$LINUX_GIT_DIR"
55 git clone --depth=1 -b "${ktag}" --reference "$LINUX_GIT_REF_REPO_DIR" "${kgitrepo}" "$LINUX_GIT_DIR"
56 }
57
58
59 # Export the kernel sources from the git repo
60 git_export_linux_sources() {
61 cd "$LINUX_GIT_DIR"
62 git archive "${ktag}" | tar -x -C "$LINUX_SRCOBJ_DIR"
63 }
64
65
66 # Upload the tar archive to the object store
67 upload_archive_obj() {
68 s3cmd -c "$WORKSPACE/.s3cfg" put "$WORKSPACE/$obj_name" "$obj_url_prefix/"
69 rm -f "$WORKSPACE/$obj_name"
70 }
71
72
73 extract_archive_obj() {
74 tar -xf "$WORKSPACE/$obj_name" -C "$LINUX_OBJ_DIR"
75 rm -f "$WORKSPACE/$obj_name"
76 }
77
78
79 tar_archive_obj() {
80 cd "$LINUX_OBJ_DIR"
81 tar -cf "$WORKSPACE/$obj_name" -I pbzip2 .
82 cd -
83 }
84
85 # Find the most recent GCC version supported by the kernel sources
86 select_compiler() {
87 local selected_cc
88
89 cd "$LINUX_SRCOBJ_DIR"
90
91 kversion=$(make -s kernelversion)
92
93 set +e
94
95 for cc in gcc-7 gcc-5 gcc-4.8; do
96 if "${CROSS_COMPILE:-}${cc}" -I include/ -D__LINUX_COMPILER_H -D__LINUX_COMPILER_TYPES_H -E include/linux/compiler-gcc.h; then
97 selected_cc="$cc"
98 break
99 fi
100 done
101
102 set -e
103
104 if [ "x$selected_cc" = "x" ]; then
105 echo "Found no suitable compiler."
106 exit 1
107 fi
108
109 # Force gcc-4 for buggy kernel branches
110 if { vergte "$kversion" "3.2" && verlt "$kversion" "3.3"; } || \
111 { vergte "$kversion" "3.4" && verlt "$kversion" "3.5"; } || \
112 { vergte "$kversion" "3.17" && verlt "$kversion" "3.18"; }; then
113 selected_cc=gcc-4.8
114 fi
115
116 case "$ktag" in
117 Ubuntu*)
118 if { vergte "$kversion" "3.13" && verlt "$kversion" "3.14"; }; then
119 selected_cc=gcc-4.8
120 fi
121 ;;
122 esac
123
124 export CC="${CROSS_COMPILE:-}${selected_cc}"
125
126 cd -
127 }
128
129
130 build_linux_kernel() {
131 cd "$LINUX_SRCOBJ_DIR"
132
133 kversion=$(make -s kernelversion)
134
135 # Generate kernel configuration
136 case "$ktag" in
137 Ubuntu*)
138 if [ "${cross_arch}" = "powerpc" ]; then
139 if vergte "${kversion}" "4.10"; then
140 echo "Ubuntu removed big endian powerpc configuration from kernel >= 4.10. Don't try to build it."
141 exit 0
142 fi
143 fi
144 fakeroot debian/rules clean
145 fakeroot debian/rules genconfigs
146 cp CONFIGS/"${ubuntu_config}" .config
147 ;;
148 *)
149 # Force 32bit build on i386, default is 64bit
150 if [ "$arch" = "i386" ]; then
151 export ARCH="i386"
152 fi
153
154 # allyesconfig is mostly broken for kernels of the 2.6 series
155 if verlt "$kversion" "3.0"; then
156 vanilla_config="defconfig"
157 fi
158
159 make "${vanilla_config}"
160 ;;
161 esac
162
163 # silentoldconfig was renamed in 4.19
164 if vergte "$kversion" "4.19"; then
165 update_conf_target="syncconfig"
166 else
167 update_conf_target="silentoldconfig"
168 fi
169
170 # Fix 'defined(@array)' was removed from recent perl
171 if [ -f "kernel/timeconst.pl" ]; then
172 sed -i 's/defined(\@\(.*\))/@\1/' kernel/timeconst.pl
173 fi
174
175 # Fix syntax of inline assembly which is confused with C++11 raw strings on gcc >= 5
176 if [ "$CC" != "gcc-4.8" ]; then
177 if [ -f "arch/x86/kvm/svm.c" ]; then
178 sed -i 's/ R"/ R "/g; s/"R"/" R "/g' arch/x86/kvm/svm.c
179 fi
180
181 if [ -f "arch/x86/kvm/vmx.c" ]; then
182 sed -i 's/ R"/ R "/g; s/"R"/" R "/g' arch/x86/kvm/vmx.c
183 fi
184 fi
185
186 # Fix a typo in v2.6.36.x
187 if [ -f "arch/x86/kernel/entry_64.S" ]; then
188 sed -i 's/END(do_hypervisor_callback)/END(xen_do_hypervisor_callback)/' arch/x86/kernel/entry_64.S
189 fi
190
191 # Fix kernel < 3.0 with gcc >= 4.7
192 if verlt "$kversion" "3.0"; then
193 sed -i '/linux\/compiler.h/a #include <linux\/linkage.h> \/* For asmregparm *\/' arch/x86/include/asm/ptrace.h
194 sed -i 's/extern long syscall_trace_enter/extern asmregparm long syscall_trace_enter/' arch/x86/include/asm/ptrace.h
195 sed -i 's/extern void syscall_trace_leave/extern asmregparm void syscall_trace_leave/' arch/x86/include/asm/ptrace.h
196 echo "header-y += linkage.h" >> include/linux/Kbuild
197 fi
198
199 # GCC 4.8
200 sed -i "s/CONFIG_CC_STACKPROTECTOR_STRONG=y/# CONFIG_CC_STACKPROTECTOR_STRONG is not set/g" .config
201
202 # Don't try to sign modules
203 sed -i "s/CONFIG_MODULE_SIG=y/# CONFIG_MODULE_SIG is not set/g" .config
204
205 # Disable kernel stack frame correctness validation, introduced in 4.6.0 and currently fails
206 sed -i "s/CONFIG_STACK_VALIDATION=y/# CONFIG_STACK_VALIDATION is not set/g" .config
207
208 # Cause problems with inline assembly on i386
209 sed -i "s/CONFIG_DEBUG_SECTION_MISMATCH=y/# CONFIG_DEBUG_SECTION_MISMATCH is not set/g" .config
210
211 # IGBVF won't build with recent gcc on 2.6.38.x
212 if { vergte "$kversion" "2.6.37" && verlt "$kversion" "2.6.38"; }; then
213 sed -i "s/CONFIG_IGBVF=y/# CONFIG_IGBVF is not set/g" .config
214 fi
215
216 # Set required options
217 {
218 echo "CONFIG_KPROBES=y";
219 echo "CONFIG_FTRACE=y";
220 echo "CONFIG_BLK_DEV_IO_TRACE=y";
221 echo "CONFIG_TRACEPOINTS=y";
222 echo "CONFIG_KALLSYMS_ALL=y";
223 } >> .config
224
225 # Debug
226 #cat .config
227
228 make "$update_conf_target" CC="$CC"
229 make -j"$NPROC" CC="$CC"
230
231 krelease=$(make -s kernelrelease)
232
233 # Save the kernel and modules
234 mkdir -p "$LINUX_INSTOBJ_DIR/boot"
235 make INSTALL_MOD_PATH="$LINUX_INSTOBJ_DIR" INSTALL_MOD_STRIP=1 modules_install
236 make INSTALL_PATH="$LINUX_INSTOBJ_DIR/boot" install
237 rm -f "$LINUX_INSTOBJ_DIR/lib/modules/${krelease}/source" "$LINUX_INSTOBJ_DIR/lib/modules/${krelease}/build"
238 ln -s ../../../../sources "$LINUX_INSTOBJ_DIR/lib/modules/${krelease}/source"
239 ln -s ../../../../sources "$LINUX_INSTOBJ_DIR/lib/modules/${krelease}/source"
240 }
241
242
243 extract_distro_headers() {
244
245 # Enter linux source dir
246 cd "${LINUX_SRCOBJ_DIR}"
247
248
249 # For RT kernels, copy version file
250 if [ -s localversion-rt ]; then
251 cp -a localversion-rt "${LINUX_HDROBJ_DIR}"
252 fi
253
254 # Copy all Makefile related stuff
255 find . -path './include/*' -prune \
256 -o -path './scripts/*' -prune -o -type f \
257 \( -name 'Makefile*' -o -name 'Kconfig*' -o -name 'Kbuild*' -o \
258 -name '*.sh' -o -name '*.pl' -o -name '*.lds' \) \
259 -print | cpio -pd --preserve-modification-time "${LINUX_HDROBJ_DIR}"
260
261 # Copy base scripts and include dirs
262 cp -a scripts include "${LINUX_HDROBJ_DIR}"
263
264 # Copy arch includes
265 (find arch -name include -type d -print0 | \
266 xargs -0 -n1 -i: find : -type f) | \
267 cpio -pd --preserve-modification-time "${LINUX_HDROBJ_DIR}"
268
269 # Copy arch scripts
270 (find arch -name scripts -type d -print0 | \
271 xargs -0 -n1 -i: find : -type f) | \
272 cpio -pd --preserve-modification-time "${LINUX_HDROBJ_DIR}"
273
274 # Cleanup scripts
275 rm -f "${LINUX_HDROBJ_DIR}/scripts/*.o"
276 rm -f "${LINUX_HDROBJ_DIR}/scripts/*/*.o"
277
278 # On powerpc this object is required to link modules
279 if [ "${karch}" = "powerpc" ]; then
280 cp -a --parents arch/powerpc/lib/crtsavres.[So] "${LINUX_HDROBJ_DIR}/"
281 fi
282
283 # On arm64 between 4.13 and 1.15 this object is required to build with ftrace support
284 if [ "${karch}" = "arm64" ]; then
285 if [ -f "arch/arm64/kernel/ftrace-mod.S" ]; then
286 cp -a --parents arch/arm64/kernel/ftrace-mod.[So] "${LINUX_HDROBJ_DIR}/"
287 fi
288 fi
289
290 # Newer kernels need objtool to build modules when CONFIG_STACK_VALIDATION=y
291 if [ -f tools/objtool/objtool ]; then
292 cp -a --parents tools/objtool/objtool "${LINUX_HDROBJ_DIR}/"
293 fi
294
295 if [ -f "arch/x86/kernel/macros.s" ]; then
296 cp -a --parents arch/x86/kernel/macros.s "${LINUX_HDROBJ_DIR}/"
297 fi
298
299 # Copy modules related stuff, if available
300 if [ -s Module.symvers ]; then
301 cp Module.symvers "${LINUX_HDROBJ_DIR}"
302 fi
303
304 if [ -s System.map ]; then
305 cp System.map "${LINUX_HDROBJ_DIR}"
306 fi
307
308 if [ -s Module.markers ]; then
309 cp Module.markers "${LINUX_HDROBJ_DIR}"
310 fi
311
312 # Copy config file
313 cp .config "${LINUX_HDROBJ_DIR}"
314
315 # Make sure the Makefile and version.h have a matching timestamp so that
316 # external modules can be built
317 if [ -s "${LINUX_HDROBJ_DIR}/include/generated/uapi/linux/version.h" ]; then
318 touch -r "${LINUX_HDROBJ_DIR}/Makefile" "${LINUX_HDROBJ_DIR}/include/generated/uapi/linux/version.h"
319 elif [ -s "${LINUX_HDROBJ_DIR}/include/linux/version.h" ]; then
320 touch -r "${LINUX_HDROBJ_DIR}/Makefile" "${LINUX_HDROBJ_DIR}/include/linux/version.h"
321 else
322 echo "Missing version.h"
323 exit 1
324 fi
325 touch -r "${LINUX_HDROBJ_DIR}/.config" "${LINUX_HDROBJ_DIR}/include/generated/autoconf.h"
326
327 # Copy .config to include/config/auto.conf so "make prepare" is unnecessary.
328 cp "${LINUX_HDROBJ_DIR}/.config" "${LINUX_HDROBJ_DIR}/include/config/auto.conf"
329
330 # Finally clean the object files from the full source tree
331 make clean
332
333 # And regen the modules support files
334 make modules_prepare CC="$CC"
335
336 # On powerpc this object is required to link modules
337 if [ "${karch}" = "powerpc" ]; then
338 make arch/powerpc/lib/crtsavres.o CC="$CC"
339 fi
340
341 # On arm64 between 4.13 and 4.15 this object is required to build with ftrace support
342 if [ "${karch}" = "arm64" ]; then
343 if [ -f "arch/arm64/kernel/ftrace-mod.S" ]; then
344 make arch/arm64/kernel/ftrace-mod.o CC="$CC"
345 fi
346 fi
347
348 # Version specific tasks
349 case "$ktag" in
350 Ubuntu*)
351 # Add Ubuntu ABI number to kernel headers, this is normally done by the packaging code
352 ABINUM="$(echo "$ktag" | grep -P -o 'Ubuntu-(lts-)?.*-\K\d+(?=\..*)')"
353 echo "#define UTS_UBUNTU_RELEASE_ABI $ABINUM" >> include/generated/utsrelease.h
354 echo "#define UTS_UBUNTU_RELEASE_ABI $ABINUM" >> "${LINUX_HDROBJ_DIR}/include/generated/utsrelease.h"
355 ;;
356 esac
357 }
358
359
360 build_modules() {
361
362 local kdir="$1"
363 local outdir="$2"
364 local kversion
365
366 kversion=$(make -C "$LINUX_HDROBJ_DIR" -s kernelversion)
367
368 # Enter lttng-modules source dir
369 cd "${MODULES_GIT_DIR}"
370
371 # kernels 3.10 to 3.10.13 and 3.11 to 3.11.2 introduce a deadlock in the
372 # timekeeping subsystem. We want those build to fail.
373 if { vergte "$kversion" "3.10" && verlte "$kversion" "3.10.13"; } || \
374 { vergte "$kversion" "3.11" && verlte "$kversion" "3.11.2"; }; then
375
376 set +e
377
378 # Build modules
379 KERNELDIR="${kdir}" make -j"${NPROC}" V=1 CC="$CC"
380
381 set -e
382
383 # We expect this build to fail, if it doesn't, fail the job.
384 if [ "$?" -eq 0 ]; then
385 echo "This build should have failed."
386 exit 1
387 fi
388
389 # We have to publish at least one file or the build will fail
390 echo "This kernel is broken, there is a deadlock in the timekeeping subsystem." > "${outdir}/BROKEN.txt.ko"
391
392 set -e
393
394 KERNELDIR="${kdir}" make clean
395
396 else # Regular build
397
398 # Build modules against full kernel sources
399 KERNELDIR="${kdir}" make -j"${NPROC}" V=1 CC="$CC"
400
401 # Install modules to build dir
402 KERNELDIR="${kdir}" make INSTALL_MOD_PATH="${outdir}" modules_install
403
404 # Clean build dir
405 KERNELDIR="${kdir}" make clean
406 fi
407 }
408
409
410 ## MAIN ##
411
412 # Use all CPU cores
413 NPROC=$(nproc)
414
415 MODULES_GIT_DIR="${WORKSPACE}/src/lttng-modules"
416 LINUX_GIT_DIR="${WORKSPACE}/src/linux"
417
418 LINUX_OBJ_DIR="${WORKSPACE}/linux"
419 LINUX_SRCOBJ_DIR="${LINUX_OBJ_DIR}/sources"
420 LINUX_HDROBJ_DIR="${LINUX_OBJ_DIR}/headers"
421 LINUX_INSTOBJ_DIR="${LINUX_OBJ_DIR}/install"
422
423 MODULES_OUTPUT_KSRC_DIR="${WORKSPACE}/build/lttng-modules-ksrc"
424 MODULES_OUTPUT_KHDR_DIR="${WORKSPACE}/build/lttng-modules-khdr"
425
426 LINUX_GIT_REF_REPO_DIR="$HOME/gitcache/linux-stable.git/"
427
428 OBJ_STORE_URL="s3://jenkins"
429
430 # Older kernel Makefiles do not expect the compiler to default to PIE
431 KAFLAGS="-fno-pie"
432 KCFLAGS="-fno-pie -no-pie"
433 KCPPFLAGS="-fno-pie"
434 export KAFLAGS KCFLAGS KCPPFLAGS
435
436 cd "$WORKSPACE"
437
438 # Create build directories
439 mkdir -p "${LINUX_SRCOBJ_DIR}" "${LINUX_HDROBJ_DIR}" "${LINUX_INSTOBJ_DIR}" "${MODULES_OUTPUT_KSRC_DIR}" "${MODULES_OUTPUT_KHDR_DIR}"
440
441 git_clone_modules_sources
442
443 # Setup cross compile env if available
444 if [ "x${cross_arch}" != "x" ]; then
445
446 case "$cross_arch" in
447 "armhf")
448 karch="arm"
449 cross_compile="arm-linux-gnueabihf-"
450 vanilla_config="allyesconfig"
451 ubuntu_config="armhf-config.flavour.generic"
452 ;;
453
454 "arm64")
455 karch="arm64"
456 cross_compile="aarch64-linux-gnu-"
457 vanilla_config="allyesconfig"
458 ubuntu_config="arm64-config.flavour.generic"
459 ;;
460
461 "powerpc")
462 karch="powerpc"
463 cross_compile="powerpc-linux-gnu-"
464 vanilla_config="ppc44x_defconfig"
465 ubuntu_config="powerpc-config.flavour.powerpc-smp"
466 ;;
467
468 "ppc64el")
469 karch="powerpc"
470 cross_compile="powerpc64le-linux-gnu-"
471 vanilla_config="pseries_le_defconfig"
472 ubuntu_config="ppc64el-config.flavour.generic"
473 ;;
474
475 *)
476 echo "Unsupported cross arch $cross_arch"
477 exit 1
478 ;;
479 esac
480
481 # Export variables used by Kbuild for cross compilation
482 export ARCH="${karch}"
483 export CROSS_COMPILE="${cross_compile}"
484
485 # Set arch specific values if we are not cross compiling
486 elif [ "x${arch}" != "x" ]; then
487
488 case "$arch" in
489 "i386")
490 karch="x86"
491 vanilla_config="allyesconfig"
492 ubuntu_config="i386-config.flavour.generic"
493 ;;
494
495 "amd64")
496 karch="x86"
497 vanilla_config="allyesconfig"
498 ubuntu_config="amd64-config.flavour.generic"
499 ;;
500
501 "armhf")
502 karch="arm"
503 vanilla_config="allyesconfig"
504 ubuntu_config="armhf-config.flavour.generic"
505 ;;
506
507 "arm64")
508 karch="arm64"
509 vanilla_config="allyesconfig"
510 ubuntu_config="arm64-config.flavour.generic"
511 ;;
512
513 "powerpc")
514 karch="powerpc"
515 vanilla_config="allyesconfig"
516 ubuntu_config="powerpc-config.flavour.powerpc-smp"
517 ;;
518
519 "ppc64el")
520 karch="powerpc"
521 vanilla_config="allyesconfig"
522 ubuntu_config="ppc64el-config.flavour.generic"
523 ;;
524
525 *)
526 echo "Unsupported arch $arch"
527 exit 1
528 ;;
529 esac
530 else
531 echo "Not arch or cross_arch specified"
532 exit 1
533 fi
534
535
536
537 # First get the kernel build from the object store, or build it, if it's
538 # not available.
539
540 echo "# Setup endpoint
541 host_base = obj.internal.efficios.com
542 host_bucket = obj.internal.efficios.com
543 bucket_location = us-east-1
544 use_https = True
545
546 # Setup access keys
547 access_key = jenkins
548 secret_key = echo123456
549
550 # Enable S3 v4 signature APIs
551 signature_v2 = False" > "$WORKSPACE/.s3cfg"
552
553 url_hash="$(echo -n "$kgitrepo/$ktag/$arch/$cross_arch" | md5sum | awk '{ print $1 }')"
554 obj_name="linux.tar.bz2"
555 obj_url_prefix="$OBJ_STORE_URL/linux-build/$url_hash"
556 obj_url="$obj_url_prefix/$obj_name"
557
558 set +e
559 s3cmd -c "$WORKSPACE/.s3cfg" get "$obj_url"
560 ret=$?
561 set -e
562
563 case "$ret" in
564 "0")
565 extract_archive_obj
566 ;;
567
568 "12")
569 echo "File not found"
570
571 # Build all the things and upload
572 # then finish the module build...
573
574 git_clone_linux_sources
575 git_export_linux_sources
576
577 select_compiler
578
579 ## PREPARE FULL LINUX SOURCE TREE
580 build_linux_kernel
581
582 ## EXTRACT DISTRO STYLE KERNEL HEADERS / DEVEL
583 extract_distro_headers
584
585 tar_archive_obj
586
587 upload_archive_obj
588
589 ;;
590
591 *)
592 echo "Unknown error? Abort"
593 exit 1
594 ;;
595 esac
596
597 select_compiler
598
599 ## BUILD modules
600 # Either we downloaded a pre-build kernel or we built it and uploaded
601 # the archive for future builds.
602
603 cd "$WORKSPACE"
604
605 # Build modules against full kernel sources
606 build_modules "${LINUX_SRCOBJ_DIR}" "${MODULES_OUTPUT_KSRC_DIR}"
607
608 # Build modules against kernel headers
609 build_modules "${LINUX_HDROBJ_DIR}" "${MODULES_OUTPUT_KHDR_DIR}"
610
611 # Make sure some modules were actually built
612 tree "${MODULES_OUTPUT_KSRC_DIR}"
613 if [ "x$(find "${MODULES_OUTPUT_KSRC_DIR}" -name '*.ko*' -printf yes -quit)" != "xyes" ]; then
614 echo "No modules built!"
615 exit 1
616 fi
617
618 tree "${MODULES_OUTPUT_KHDR_DIR}"
619 if [ "x$(find "${MODULES_OUTPUT_KHDR_DIR}" -name '*.ko*' -printf yes -quit)" != "xyes" ]; then
620 echo "No modules built!"
621 exit 1
622 fi
623
624 # EOF
This page took 0.052524 seconds and 5 git commands to generate.