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