From 9054cc788a22980e8688dfaacc9b0d3f19304c95 2018-10-22 19:32:24 From: drtyhlpr Date: 2018-10-22 19:32:24 Subject: [PATCH] fix: kernel compilation, add ccache support --- diff --git a/README.md b/README.md index 0ba14f8..d470b0f 100644 --- a/README.md +++ b/README.md @@ -295,6 +295,12 @@ Install kernel headers with built kernel. ##### `KERNEL_MENUCONFIG`=false Start `make menuconfig` interactive menu-driven kernel configuration. The script will continue after `make menuconfig` was terminated. +##### `KERNEL_OLDDEFCONFIG`=false +Run `make olddefconfig` to automatically set all new kernel configuration options to their recommended default values. + +##### `KERNEL_CCACHE`=false +Compile the kernel using ccache. This speeds up kernel recompilation by caching previous compilations and detecting when the same compilation is being done again. + ##### `KERNEL_REMOVESRC`=true Remove all kernel sources from the generated OS image after it was built and installed. diff --git a/bootstrap.d/13-kernel.sh b/bootstrap.d/13-kernel.sh index 45934be..17d89bf 100644 --- a/bootstrap.d/13-kernel.sh +++ b/bootstrap.d/13-kernel.sh @@ -25,9 +25,9 @@ if [ "$BUILD_KERNEL" = true ] ; then # Fetch current RPi2/3 kernel sources if [ -z "${KERNEL_BRANCH}" ] ; then - as_nobody git -C "${temp_dir}" clone --depth=1 "${KERNEL_URL}" linux + as_nobody -H git -C "${temp_dir}" clone --depth=1 "${KERNEL_URL}" linux else - as_nobody git -C "${temp_dir}" clone --depth=1 --branch "${KERNEL_BRANCH}" "${KERNEL_URL}" linux + as_nobody -H git -C "${temp_dir}" clone --depth=1 --branch "${KERNEL_BRANCH}" "${KERNEL_URL}" linux fi # Copy downloaded kernel sources @@ -87,18 +87,36 @@ if [ "$BUILD_KERNEL" = true ] ; then # Load default raspberry kernel configuration make -C "${KERNEL_DIR}" ARCH="${KERNEL_ARCH}" CROSS_COMPILE="${CROSS_COMPILE}" "${KERNEL_DEFCONFIG}" + # Copy custom kernel configuration file if [ ! -z "$KERNELSRC_USRCONFIG" ] ; then cp $KERNELSRC_USRCONFIG ${KERNEL_DIR}/.config fi + # Set kernel configuration parameters to their default values + if [ "$KERNEL_OLDDEFCONFIG" = true ] ; then + make -C "${KERNEL_DIR}" ARCH="${KERNEL_ARCH}" CROSS_COMPILE="${CROSS_COMPILE}" olddefconfig + fi + # Start menu-driven kernel configuration (interactive) if [ "$KERNEL_MENUCONFIG" = true ] ; then make -C "${KERNEL_DIR}" ARCH="${KERNEL_ARCH}" CROSS_COMPILE="${CROSS_COMPILE}" menuconfig fi fi - # Cross compile kernel and modules - make -C "${KERNEL_DIR}" -j${KERNEL_THREADS} ARCH="${KERNEL_ARCH}" CROSS_COMPILE="${CROSS_COMPILE}" "${KERNEL_BIN_IMAGE}" modules dtbs + # Use ccache to cross compile the kernel + if [ "$KERNEL_CCACHE" = true ] ; then + cc="ccache ${CROSS_COMPILE}gcc" + else + cc="${CROSS_COMPILE}gcc" + fi + + # Cross compile kernel and dtbs + make -C "${KERNEL_DIR}" -j${KERNEL_THREADS} ARCH="${KERNEL_ARCH}" CROSS_COMPILE="${CROSS_COMPILE}" CC="${cc}" "${KERNEL_BIN_IMAGE}" dtbs + + # Cross compile kernel modules + if [ $(grep "CONFIG_MODULES=y" "${KERNEL_DIR}/.config") ] ; then + make -C "${KERNEL_DIR}" -j${KERNEL_THREADS} ARCH="${KERNEL_ARCH}" CROSS_COMPILE="${CROSS_COMPILE}" CC="${cc}" modules + fi fi # Check if kernel compilation was successful @@ -110,12 +128,16 @@ if [ "$BUILD_KERNEL" = true ] ; then # Install kernel modules if [ "$ENABLE_REDUCE" = true ] ; then - make -C "${KERNEL_DIR}" ARCH="${KERNEL_ARCH}" CROSS_COMPILE="${CROSS_COMPILE}" INSTALL_MOD_STRIP=1 INSTALL_MOD_PATH=../../.. modules_install + if [ $(grep "CONFIG_MODULES=y" "${KERNEL_DIR}/.config") ] ; then + make -C "${KERNEL_DIR}" ARCH="${KERNEL_ARCH}" CROSS_COMPILE="${CROSS_COMPILE}" INSTALL_MOD_STRIP=1 INSTALL_MOD_PATH=../../.. modules_install + fi else - make -C "${KERNEL_DIR}" ARCH="${KERNEL_ARCH}" CROSS_COMPILE="${CROSS_COMPILE}" INSTALL_MOD_PATH=../../.. modules_install + if [ $(grep "CONFIG_MODULES=y" "${KERNEL_DIR}/.config") ] ; then + make -C "${KERNEL_DIR}" ARCH="${KERNEL_ARCH}" CROSS_COMPILE="${CROSS_COMPILE}" INSTALL_MOD_PATH=../../.. modules_install + fi # Install kernel firmware - if [ $(cat ./Makefile | grep "^firmware_install:") ] ; then + if [ $(grep "^firmware_install:" "${KERNEL_DIR}/Makefile") ] ; then make -C "${KERNEL_DIR}" ARCH="${KERNEL_ARCH}" CROSS_COMPILE="${CROSS_COMPILE}" INSTALL_FW_PATH=../../../lib firmware_install fi fi @@ -134,18 +156,36 @@ if [ "$BUILD_KERNEL" = true ] ; then # Copy kernel configuration file to the boot directory install_readonly "${KERNEL_DIR}/.config" "${R}/boot/config-${KERNEL_VERSION}" - # Copy dts and dtb device tree sources and binaries + # Prepare device tree directory mkdir "${BOOT_DIR}/overlays" # Ensure the proper .dtb is located if [ "$KERNEL_ARCH" = "arm" ] ; then - install_readonly "${KERNEL_DIR}/arch/${KERNEL_ARCH}/boot/dts/"*.dtb "${BOOT_DIR}/" + for dtb in "${KERNEL_DIR}/arch/${KERNEL_ARCH}/boot/dts/"*.dtb ; do + if [ -f "${dtb}" ] ; then + install_readonly "${dtb}" "${BOOT_DIR}/" + fi + done else - install_readonly "${KERNEL_DIR}/arch/${KERNEL_ARCH}/boot/dts/broadcom/"*.dtb "${BOOT_DIR}/" + for dtb in "${KERNEL_DIR}/arch/${KERNEL_ARCH}/boot/dts/broadcom/"*.dtb ; do + if [ -f "${dtb}" ] ; then + install_readonly "${dtb}" "${BOOT_DIR}/" + fi + done fi - install_readonly "${KERNEL_DIR}/arch/${KERNEL_ARCH}/boot/dts/overlays/"*.dtb* "${BOOT_DIR}/overlays/" - install_readonly "${KERNEL_DIR}/arch/${KERNEL_ARCH}/boot/dts/overlays/README" "${BOOT_DIR}/overlays/README" + # Copy compiled dtb device tree files + if [ -d "${KERNEL_DIR}/arch/${KERNEL_ARCH}/boot/dts/overlays" ] ; then + for dtb in "${KERNEL_DIR}/arch/${KERNEL_ARCH}/boot/dts/overlays/"*.dtb ; do + if [ -f "${dtb}" ] ; then + install_readonly "${dtb}" "${BOOT_DIR}/overlays/" + fi + done + + if [ -f "${KERNEL_DIR}/arch/${KERNEL_ARCH}/boot/dts/overlays/README" ] ; then + install_readonly "${KERNEL_DIR}/arch/${KERNEL_ARCH}/boot/dts/overlays/README" "${BOOT_DIR}/overlays/README" + fi + fi if [ "$ENABLE_UBOOT" = false ] ; then # Convert and copy kernel image to the boot directory @@ -159,11 +199,16 @@ if [ "$BUILD_KERNEL" = true ] ; then if [ "$KERNEL_REMOVESRC" = true ] ; then rm -fr "${KERNEL_DIR}" else - make -C "${KERNEL_DIR}" ARCH="${KERNEL_ARCH}" CROSS_COMPILE="${CROSS_COMPILE}" modules_prepare + # Prepare compiled kernel modules + if [ $(grep "CONFIG_MODULES=y" "${KERNEL_DIR}/.config") ] ; then + if [ $(grep "^modules_prepare:" "${KERNEL_DIR}/Makefile") ] ; then + make -C "${KERNEL_DIR}" ARCH="${KERNEL_ARCH}" CROSS_COMPILE="${CROSS_COMPILE}" modules_prepare + fi - # Create symlinks for kernel modules - chroot_exec ln -sf /usr/src/linux "/lib/modules/${KERNEL_VERSION}/build" - chroot_exec ln -sf /usr/src/linux "/lib/modules/${KERNEL_VERSION}/source" + # Create symlinks for kernel modules + chroot_exec ln -sf /usr/src/linux "/lib/modules/${KERNEL_VERSION}/build" + chroot_exec ln -sf /usr/src/linux "/lib/modules/${KERNEL_VERSION}/source" + fi fi else # BUILD_KERNEL=false diff --git a/rpi23-gen-image.sh b/rpi23-gen-image.sh index 7a0adb3..7f6970b 100755 --- a/rpi23-gen-image.sh +++ b/rpi23-gen-image.sh @@ -175,6 +175,9 @@ KERNEL_THREADS=${KERNEL_THREADS:=1} KERNEL_HEADERS=${KERNEL_HEADERS:=true} KERNEL_MENUCONFIG=${KERNEL_MENUCONFIG:=false} KERNEL_REMOVESRC=${KERNEL_REMOVESRC:=true} +KERNEL_OLDDEFCONFIG=${KERNEL_OLDDEFCONFIG:=false} +KERNEL_CCACHE=${KERNEL_CCACHE:=false} + if [ "$KERNEL_ARCH" = "arm64" ] ; then KERNEL_BIN_IMAGE=${KERNEL_BIN_IMAGE:="Image"} else @@ -269,6 +272,11 @@ if [ "$KERNEL_MENUCONFIG" = true ] ; then REQUIRED_PACKAGES="${REQUIRED_PACKAGES} libncurses5-dev" fi +# Add ccache compiler cache for (faster) kernel cross (re)compilation +if [ "$KERNEL_CCACHE" = true ] ; then + REQUIRED_PACKAGES="${REQUIRED_PACKAGES} ccache" +fi + # Stop the Crypto Wars if [ "$DISABLE_FBI" = true ] ; then ENABLE_CRYPTFS=true