Path: blob/next/scripts/compilation.sh
23658 views
#!/bin/bash1#2# Copyright (c) 2013-2021 Igor Pecovnik, igor.pecovnik@gma**.com3#4# This file is licensed under the terms of the GNU General Public5# License version 2. This program is licensed "as is" without any6# warranty of any kind, whether express or implied.789# Functions:1011# compile_atf12# compile_uboot13# compile_kernel14# compile_firmware15# compile_orangepi-config16# compile_sunxi_tools17# install_rkbin_tools18# grab_version19# find_toolchain20# advanced_patch21# process_patch_file22# userpatch_create23# overlayfs_wrapper2425262728compile_atf()29{30if [[ $CLEAN_LEVEL == *make* ]]; then31display_alert "Cleaning" "$ATFSOURCEDIR" "info"32(cd "${EXTER}/cache/sources/${ATFSOURCEDIR}"; make distclean > /dev/null 2>&1)33fi3435if [[ $USE_OVERLAYFS == yes ]]; then36local atfdir37atfdir=$(overlayfs_wrapper "wrap" "$EXTER/cache/sources/$ATFSOURCEDIR" "atf_${LINUXFAMILY}_${BRANCH}")38else39local atfdir="$EXTER/cache/sources/$ATFSOURCEDIR"40fi41cd "$atfdir" || exit4243display_alert "Compiling ATF" "" "info"4445# build aarch6446if [[ $(dpkg --print-architecture) == amd64 ]]; then4748local toolchain49toolchain=$(find_toolchain "$ATF_COMPILER" "$ATF_USE_GCC")50[[ -z $toolchain ]] && exit_with_error "Could not find required toolchain" "${ATF_COMPILER}gcc $ATF_USE_GCC"5152if [[ -n $ATF_TOOLCHAIN2 ]]; then53local toolchain2_type toolchain2_ver toolchain254toolchain2_type=$(cut -d':' -f1 <<< "${ATF_TOOLCHAIN2}")55toolchain2_ver=$(cut -d':' -f2 <<< "${ATF_TOOLCHAIN2}")56toolchain2=$(find_toolchain "$toolchain2_type" "$toolchain2_ver")57[[ -z $toolchain2 ]] && exit_with_error "Could not find required toolchain" "${toolchain2_type}gcc $toolchain2_ver"58fi5960# build aarch6461fi6263display_alert "Compiler version" "${ATF_COMPILER}gcc $(eval env PATH="${toolchain}:${PATH}" "${ATF_COMPILER}gcc" -dumpversion)" "info"6465local target_make target_patchdir target_files66target_make=$(cut -d';' -f1 <<< "${ATF_TARGET_MAP}")67target_patchdir=$(cut -d';' -f2 <<< "${ATF_TARGET_MAP}")68target_files=$(cut -d';' -f3 <<< "${ATF_TARGET_MAP}")6970advanced_patch "atf" "${ATFPATCHDIR}" "$BOARD" "$target_patchdir" "$BRANCH" "${LINUXFAMILY}-${BOARD}-${BRANCH}"7172# create patch for manual source changes73[[ $CREATE_PATCHES == yes ]] && userpatch_create "atf"7475echo -e "\n\t== atf ==\n" >> "${DEST}"/${LOG_SUBPATH}/compilation.log76# ENABLE_BACKTRACE="0" has been added to workaround a regression in ATF.77# Check: https://github.com/armbian/build/issues/115778eval CCACHE_BASEDIR="$(pwd)" env PATH="${toolchain}:${toolchain2}:${PATH}" \79'make ENABLE_BACKTRACE="0" $target_make $CTHREADS \80CROSS_COMPILE="$CCACHE $ATF_COMPILER"' 2>> "${DEST}"/${LOG_SUBPATH}/compilation.log \81${PROGRESS_LOG_TO_FILE:+' | tee -a $DEST/${LOG_SUBPATH}/compilation.log'} \82${OUTPUT_DIALOG:+' | dialog --backtitle "$backtitle" --progressbox "Compiling ATF..." $TTY_Y $TTY_X'} \83${OUTPUT_VERYSILENT:+' >/dev/null 2>/dev/null'}8485[[ ${PIPESTATUS[0]} -ne 0 ]] && exit_with_error "ATF compilation failed"8687[[ $(type -t atf_custom_postprocess) == function ]] && atf_custom_postprocess8889atftempdir=$(mktemp -d)90chmod 700 ${atftempdir}91trap "rm -rf \"${atftempdir}\" ; exit 0" 0 1 2 3 159293# copy files to temp directory94for f in $target_files; do95local f_src96f_src=$(cut -d':' -f1 <<< "${f}")97if [[ $f == *:* ]]; then98local f_dst99f_dst=$(cut -d':' -f2 <<< "${f}")100else101local f_dst102f_dst=$(basename "${f_src}")103fi104[[ ! -f $f_src ]] && exit_with_error "ATF file not found" "$(basename "${f_src}")"105cp "${f_src}" "${atftempdir}/${f_dst}"106done107108# copy license file to pack it to u-boot package later109[[ -f license.md ]] && cp license.md "${atftempdir}"/110}111112compile_uboot()113{114115if [[ ${BOARDFAMILY} == "sun50iw9" && ${BRANCH} =~ legacy|current && $(dpkg --print-architecture) == arm64 ]]; then116117local uboot_name=${CHOSEN_UBOOT}_${REVISION}_${ARCH}.deb118display_alert "Compile u-boot is not supported, only copy precompiled deb package" "$uboot_name" "info"119cp "${EXTER}/cache/debs/h618/$uboot_name" "${DEB_STORAGE}/u-boot/"120else121122# not optimal, but extra cleaning before overlayfs_wrapper should keep sources directory clean123if [[ $CLEAN_LEVEL == *make* ]]; then124display_alert "Cleaning" "$BOOTSOURCEDIR" "info"125(cd $BOOTSOURCEDIR; make clean > /dev/null 2>&1)126fi127128if [[ $USE_OVERLAYFS == yes ]]; then129local ubootdir130ubootdir=$(overlayfs_wrapper "wrap" "$BOOTSOURCEDIR" "u-boot_${LINUXFAMILY}_${BRANCH}")131else132local ubootdir="$BOOTSOURCEDIR"133fi134cd "${ubootdir}" || exit135136# read uboot version137local version hash138version=$(grab_version "$ubootdir")139hash=$(improved_git --git-dir="$ubootdir"/.git rev-parse HEAD)140141display_alert "Compiling u-boot" "v$version" "info"142143# build aarch64144if [[ $(dpkg --print-architecture) == amd64 ]]; then145146local toolchain147toolchain=$(find_toolchain "$UBOOT_COMPILER" "$UBOOT_USE_GCC")148[[ -z $toolchain ]] && exit_with_error "Could not find required toolchain" "${UBOOT_COMPILER}gcc $UBOOT_USE_GCC"149150if [[ -n $UBOOT_TOOLCHAIN2 ]]; then151local toolchain2_type toolchain2_ver toolchain2152toolchain2_type=$(cut -d':' -f1 <<< "${UBOOT_TOOLCHAIN2}")153toolchain2_ver=$(cut -d':' -f2 <<< "${UBOOT_TOOLCHAIN2}")154toolchain2=$(find_toolchain "$toolchain2_type" "$toolchain2_ver")155[[ -z $toolchain2 ]] && exit_with_error "Could not find required toolchain" "${toolchain2_type}gcc $toolchain2_ver"156fi157158# build aarch64159fi160161display_alert "Compiler version" "${UBOOT_COMPILER}gcc $(eval env PATH="${toolchain}:${toolchain2}:${PATH}" "${UBOOT_COMPILER}gcc" -dumpversion)" "info"162[[ -n $toolchain2 ]] && display_alert "Additional compiler version" "${toolchain2_type}gcc $(eval env PATH="${toolchain}:${toolchain2}:${PATH}" "${toolchain2_type}gcc" -dumpversion)" "info"163164# create directory structure for the .deb package165uboottempdir=$(mktemp -d)166chmod 700 ${uboottempdir}167trap "ret=\$?; rm -rf \"${uboottempdir}\" ; exit \$ret" 0 1 2 3 15168local uboot_name=${CHOSEN_UBOOT}_${REVISION}_${ARCH}169rm -rf $uboottempdir/$uboot_name170mkdir -p $uboottempdir/$uboot_name/usr/lib/{u-boot,$uboot_name} $uboottempdir/$uboot_name/DEBIAN171172# process compilation for one or multiple targets173while read -r target; do174local target_make target_patchdir target_files175target_make=$(cut -d';' -f1 <<< "${target}")176target_patchdir=$(cut -d';' -f2 <<< "${target}")177target_files=$(cut -d';' -f3 <<< "${target}")178179# needed for multiple targets and for calling compile_uboot directly180#display_alert "Checking out to clean sources"181#improved_git checkout -f -q HEAD182183if [[ $CLEAN_LEVEL == *make* ]]; then184display_alert "Cleaning" "$BOOTSOURCEDIR" "info"185(cd "${BOOTSOURCEDIR}"; make clean > /dev/null 2>&1)186fi187188advanced_patch "u-boot" "$BOOTPATCHDIR" "$BOARD" "$target_patchdir" "$BRANCH" "${LINUXFAMILY}-${BOARD}-${BRANCH}"189190# create patch for manual source changes191[[ $CREATE_PATCHES == yes ]] && userpatch_create "u-boot"192193if [[ -n $ATFSOURCE ]]; then194cp -Rv "${atftempdir}"/*.bin .195rm -rf "${atftempdir}"196fi197198echo -e "\n\t== u-boot make $BOOTCONFIG ==\n" >> "${DEST}"/${LOG_SUBPATH}/compilation.log199eval CCACHE_BASEDIR="$(pwd)" env PATH="${toolchain}:${toolchain2}:${PATH}" \200'make $CTHREADS $BOOTCONFIG \201CROSS_COMPILE="$CCACHE $UBOOT_COMPILER"' 2>> "${DEST}"/${LOG_SUBPATH}/compilation.log \202${PROGRESS_LOG_TO_FILE:+' | tee -a $DEST/${LOG_SUBPATH}/compilation.log'} \203${OUTPUT_VERYSILENT:+' >/dev/null 2>/dev/null'}204205if [[ "${version}" != 2014.07 ]]; then206207# orangepi specifics u-boot settings208[[ -f .config ]] && sed -i 's/CONFIG_LOCALVERSION=""/CONFIG_LOCALVERSION="-orangepi"/g' .config209[[ -f .config ]] && sed -i 's/CONFIG_LOCALVERSION_AUTO=.*/# CONFIG_LOCALVERSION_AUTO is not set/g' .config210211# for modern kernel and non spi targets212if [[ ${BOOTBRANCH} =~ ^tag:v201[8-9](.*) && ${target} != "spi" && -f .config ]]; then213214sed -i 's/^.*CONFIG_ENV_IS_IN_FAT.*/# CONFIG_ENV_IS_IN_FAT is not set/g' .config215sed -i 's/^.*CONFIG_ENV_IS_IN_EXT4.*/CONFIG_ENV_IS_IN_EXT4=y/g' .config216sed -i 's/^.*CONFIG_ENV_IS_IN_MMC.*/# CONFIG_ENV_IS_IN_MMC is not set/g' .config217sed -i 's/^.*CONFIG_ENV_IS_NOWHERE.*/# CONFIG_ENV_IS_NOWHERE is not set/g' .config | echo \218"# CONFIG_ENV_IS_NOWHERE is not set" >> .config219echo 'CONFIG_ENV_EXT4_INTERFACE="mmc"' >> .config220echo 'CONFIG_ENV_EXT4_DEVICE_AND_PART="0:auto"' >> .config221echo 'CONFIG_ENV_EXT4_FILE="/boot/boot.env"' >> .config222223fi224225[[ -f tools/logos/udoo.bmp ]] && cp "${EXTER}"/packages/blobs/splash/udoo.bmp tools/logos/udoo.bmp226touch .scmversion227228# $BOOTDELAY can be set in board family config, ensure autoboot can be stopped even if set to 0229[[ $BOOTDELAY == 0 ]] && echo -e "CONFIG_ZERO_BOOTDELAY_CHECK=y" >> .config230[[ -n $BOOTDELAY ]] && sed -i "s/^CONFIG_BOOTDELAY=.*/CONFIG_BOOTDELAY=${BOOTDELAY}/" .config || [[ -f .config ]] && echo "CONFIG_BOOTDELAY=${BOOTDELAY}" >> .config231232fi233234# workaround when two compilers are needed235cross_compile="CROSS_COMPILE=$CCACHE $UBOOT_COMPILER";236[[ -n $UBOOT_TOOLCHAIN2 ]] && cross_compile="ORANGEPI=foe"; # empty parameter is not allowed237238echo -e "\n\t== u-boot make $target_make ==\n" >> "${DEST}"/${LOG_SUBPATH}/compilation.log239eval CCACHE_BASEDIR="$(pwd)" env PATH="${toolchain}:${toolchain2}:${PATH}" \240'make $target_make $CTHREADS \241"${cross_compile}"' 2>>"${DEST}"/${LOG_SUBPATH}/compilation.log \242${PROGRESS_LOG_TO_FILE:+' | tee -a "${DEST}"/${LOG_SUBPATH}/compilation.log'} \243${OUTPUT_DIALOG:+' | dialog --backtitle "$backtitle" --progressbox "Compiling u-boot..." $TTY_Y $TTY_X'} \244${OUTPUT_VERYSILENT:+' >/dev/null 2>/dev/null'} ';EVALPIPE=(${PIPESTATUS[@]})'245246[[ ${EVALPIPE[0]} -ne 0 ]] && exit_with_error "U-boot compilation failed"247248[[ $(type -t uboot_custom_postprocess) == function ]] && uboot_custom_postprocess249250# copy files to build directory251for f in $target_files; do252local f_src253f_src=$(cut -d':' -f1 <<< "${f}")254if [[ $f == *:* ]]; then255local f_dst256f_dst=$(cut -d':' -f2 <<< "${f}")257else258local f_dst259f_dst=$(basename "${f_src}")260fi261[[ ! -f $f_src ]] && exit_with_error "U-boot file not found" "$(basename "${f_src}")"262if [[ "${version}" =~ 2014.07|2011.09 ]]; then263cp "${f_src}" "$uboottempdir/packout/${f_dst}"264else265cp "${f_src}" "$uboottempdir/${uboot_name}/usr/lib/${uboot_name}/${f_dst}"266fi267done268done <<< "$UBOOT_TARGET_MAP"269270if [[ $PACK_UBOOT == "yes" ]];then271if [[ $BOARDFAMILY =~ sun50iw1 ]]; then272if [[ $(type -t u-boot_tweaks) == function ]]; then273u-boot_tweaks ${uboot_name}274else275exit_with_error "U-boot pack failed"276fi277else278pack_uboot279cp $uboottempdir/packout/{boot0_sdcard.fex,boot_package.fex} "${SRC}/.tmp/${uboot_name}/usr/lib/${uboot_name}/"280cp $uboottempdir/packout/dts/${BOARD}-u-boot.dts "${SRC}/.tmp/${uboot_name}/usr/lib/u-boot/"281fi282cd "${ubootdir}" || exit283fi284285# declare -f on non-defined function does not do anything286cat <<-EOF > "$uboottempdir/${uboot_name}/usr/lib/u-boot/platform_install.sh"287DIR=/usr/lib/$uboot_name288$(declare -f write_uboot_platform)289$(declare -f write_uboot_platform_mtd)290$(declare -f write_uboot_platform_ufs)291$(declare -f setup_write_uboot_platform)292EOF293294# set up control file295cat <<-EOF > "$uboottempdir/${uboot_name}/DEBIAN/control"296Package: linux-u-boot-${BOARD}-${BRANCH}297Version: $REVISION298Architecture: $ARCH299Maintainer: $MAINTAINER <$MAINTAINERMAIL>300Installed-Size: 1301Section: kernel302Priority: optional303Provides: orangepi-u-boot304Replaces: orangepi-u-boot305Conflicts: orangepi-u-boot, u-boot-sunxi306Description: Uboot loader $version307EOF308309# copy config file to the package310# useful for FEL boot with overlayfs_wrapper311[[ -f .config && -n $BOOTCONFIG ]] && cp .config "$uboottempdir/${uboot_name}/usr/lib/u-boot/${BOOTCONFIG}"312# copy license files from typical locations313[[ -f COPYING ]] && cp COPYING "$uboottempdir/${uboot_name}/usr/lib/u-boot/LICENSE"314[[ -f Licenses/README ]] && cp Licenses/README "$uboottempdir/${uboot_name}/usr/lib/u-boot/LICENSE"315[[ -n $atftempdir && -f $atftempdir/license.md ]] && cp "${atftempdir}/license.md" "$uboottempdir/${uboot_name}/usr/lib/u-boot/LICENSE.atf"316317display_alert "Building deb" "${uboot_name}.deb" "info"318fakeroot dpkg-deb -b -Z${DEB_COMPRESS} "$uboottempdir/${uboot_name}" "$uboottempdir/${uboot_name}.deb" >> "${DEST}"/${LOG_SUBPATH}/output.log 2>&1319rm -rf "$uboottempdir/${uboot_name}"320[[ -n $atftempdir ]] && rm -rf "${atftempdir}"321322[[ ! -f $uboottempdir/${uboot_name}.deb ]] && exit_with_error "Building u-boot package failed"323324rsync --remove-source-files -rq "$uboottempdir/${uboot_name}.deb" "${DEB_STORAGE}/u-boot/"325rm -rf "$uboottempdir"326327fi328}329330create_linux-source_package ()331{332ts=$(date +%s)333local sources_pkg_dir tmp_src_dir334tmp_src_dir=$(mktemp -d)335trap "ret=\$?; rm -rf \"${tmp_src_dir}\" ; exit \$ret" 0 1 2 3 15336sources_pkg_dir=${tmp_src_dir}/${CHOSEN_KSRC}_${REVISION}_all337mkdir -p "${sources_pkg_dir}"/usr/src/ \338"${sources_pkg_dir}"/usr/share/doc/linux-source-${version}-${LINUXFAMILY} \339"${sources_pkg_dir}"/DEBIAN340341cp "${EXTER}/config/kernel/${LINUXCONFIG}.config" "default_${LINUXCONFIG}.config"342xz < .config > "${sources_pkg_dir}/usr/src/${LINUXCONFIG}_${version}_${REVISION}_config.xz"343344display_alert "Compressing sources for the linux-source package"345tar cp --directory="$kerneldir" --exclude='.git' --owner=root . \346| pv -p -b -r -s "$(du -sb "$kerneldir" --exclude=='.git' | cut -f1)" \347| pixz -4 > "${sources_pkg_dir}/usr/src/linux-source-${version}-${LINUXFAMILY}.tar.xz"348cp COPYING "${sources_pkg_dir}/usr/share/doc/linux-source-${version}-${LINUXFAMILY}/LICENSE"349350cat <<-EOF > "${sources_pkg_dir}"/DEBIAN/control351Package: linux-source-${version}-${BRANCH}-${LINUXFAMILY}352Version: ${version}-${BRANCH}-${LINUXFAMILY}+${REVISION}353Architecture: all354Maintainer: $MAINTAINER <$MAINTAINERMAIL>355Section: kernel356Priority: optional357Depends: binutils, coreutils358Provides: linux-source, linux-source-${version}-${LINUXFAMILY}359Recommends: gcc, make360Description: This package provides the source code for the Linux kernel $version361EOF362363fakeroot dpkg-deb -b -Z${DEB_COMPRESS} -z0 "${sources_pkg_dir}" "${sources_pkg_dir}.deb"364rsync --remove-source-files -rq "${sources_pkg_dir}.deb" "${DEB_STORAGE}/"365366te=$(date +%s)367display_alert "Make the linux-source package" "$(($te - $ts)) sec." "info"368rm -rf "${tmp_src_dir}"369}370371compile_kernel()372{373if [[ $CLEAN_LEVEL == *make* ]]; then374display_alert "Cleaning" "$LINUXSOURCEDIR" "info"375(cd ${LINUXSOURCEDIR}; make ARCH="${ARCHITECTURE}" clean >/dev/null 2>&1)376fi377378if [[ $USE_OVERLAYFS == yes ]]; then379local kerneldir380kerneldir=$(overlayfs_wrapper "wrap" "$LINUXSOURCEDIR" "kernel_${LINUXFAMILY}_${BRANCH}")381else382local kerneldir="$LINUXSOURCEDIR"383fi384cd "${kerneldir}" || exit385386rm -f localversion387388# read kernel version389local version hash390version=$(grab_version "$kerneldir")391392# read kernel git hash393hash=$(improved_git --git-dir="$kerneldir"/.git rev-parse HEAD)394395# Apply a series of patches if a series file exists396if test -f "${EXTER}"/patch/kernel/${KERNELPATCHDIR}/series.conf; then397display_alert "series.conf file visible. Apply"398series_conf="${SRC}"/patch/kernel/${KERNELPATCHDIR}/series.conf399400# apply_patch_series <target dir> <full path to series file>401apply_patch_series "${kerneldir}" "$series_conf"402fi403404# build 3rd party drivers405# compilation_prepare406407advanced_patch "kernel" "$KERNELPATCHDIR" "$BOARD" "" "$BRANCH" "$LINUXFAMILY-$BRANCH"408409# create patch for manual source changes in debug mode410[[ $CREATE_PATCHES == yes ]] && userpatch_create "kernel"411412# re-read kernel version after patching413local version414version=$(grab_version "$kerneldir")415416display_alert "Compiling $BRANCH kernel" "$version" "info"417418# compare with the architecture of the current Debian node419# if it matches we use the system compiler420if $(dpkg-architecture -e "${ARCH}"); then421display_alert "Native compilation"422elif [[ $(dpkg --print-architecture) == amd64 ]]; then423local toolchain424toolchain=$(find_toolchain "$KERNEL_COMPILER" "$KERNEL_USE_GCC")425[[ -z $toolchain ]] && exit_with_error "Could not find required toolchain" "${KERNEL_COMPILER}gcc $KERNEL_USE_GCC"426else427exit_with_error "Architecture [$ARCH] is not supported"428fi429430display_alert "Compiler version" "${KERNEL_COMPILER}gcc $(eval env PATH="${toolchain}:${PATH}" "${KERNEL_COMPILER}gcc" -dumpversion)" "info"431432# copy kernel config433if [[ $KERNEL_KEEP_CONFIG == yes && -f "${DEST}"/config/$LINUXCONFIG.config ]]; then434display_alert "Using previous kernel config" "${DEST}/config/$LINUXCONFIG.config" "info"435cp -p "${DEST}/config/${LINUXCONFIG}.config" .config436else437if [[ -f $USERPATCHES_PATH/$LINUXCONFIG.config ]]; then438display_alert "Using kernel config provided by user" "userpatches/$LINUXCONFIG.config" "info"439cp -p "${USERPATCHES_PATH}/${LINUXCONFIG}.config" .config440else441display_alert "Using kernel config file" "${EXTER}/config/kernel/$LINUXCONFIG.config" "info"442cp -p "${EXTER}/config/kernel/${LINUXCONFIG}.config" .config443fi444fi445446call_extension_method "custom_kernel_config" << 'CUSTOM_KERNEL_CONFIG'447*Kernel .config is in place, still clean from git version*448Called after ${LINUXCONFIG}.config is put in place (.config).449Before any olddefconfig any Kconfig make is called.450A good place to customize the .config directly.451CUSTOM_KERNEL_CONFIG452453454# hack for deb builder. To pack what's missing in headers pack.455cp "$EXTER"/patch/misc/headers-debian-byteshift.patch /tmp456457if [[ $KERNEL_CONFIGURE != yes ]]; then458if [[ $BRANCH == legacy && ! $BOARDFAMILY =~ "rockchip-rk3588"|"rockchip-rk356x"|"sun60iw2" ]]; then459eval CCACHE_BASEDIR="$(pwd)" env PATH="${toolchain}:${PATH}" \460'make ARCH=$ARCHITECTURE CROSS_COMPILE="$CCACHE $KERNEL_COMPILER" silentoldconfig'461else462# TODO: check if required463eval CCACHE_BASEDIR="$(pwd)" env PATH="${toolchain}:${PATH}" \464'make ARCH=$ARCHITECTURE CROSS_COMPILE="$CCACHE $KERNEL_COMPILER" olddefconfig'465fi466else467eval CCACHE_BASEDIR="$(pwd)" env PATH="${toolchain}:${PATH}" \468'make $CTHREADS ARCH=$ARCHITECTURE CROSS_COMPILE="$CCACHE $KERNEL_COMPILER" oldconfig'469eval CCACHE_BASEDIR="$(pwd)" env PATH="${toolchain}:${PATH}" \470'make $CTHREADS ARCH=$ARCHITECTURE CROSS_COMPILE="$CCACHE $KERNEL_COMPILER" ${KERNEL_MENUCONFIG:-menuconfig}'471472[[ ${PIPESTATUS[0]} -ne 0 ]] && exit_with_error "Error kernel menuconfig failed"473474# store kernel config in easily reachable place475display_alert "Exporting new kernel config" "$DEST/config/$LINUXCONFIG.config" "info"476cp .config "${DEST}/config/${LINUXCONFIG}.config"477cp .config "${EXTER}/config/kernel/${LINUXCONFIG}.config"478# export defconfig too if requested479if [[ $KERNEL_EXPORT_DEFCONFIG == yes ]]; then480eval CCACHE_BASEDIR="$(pwd)" env PATH="${toolchain}:${PATH}" \481'make ARCH=$ARCHITECTURE CROSS_COMPILE="$CCACHE $KERNEL_COMPILER" savedefconfig'482[[ -f defconfig ]] && cp defconfig "${DEST}/config/${LINUXCONFIG}.defconfig"483fi484fi485486# create linux-source package - with already patched sources487# We will build this package first and clear the memory.488if [[ $BUILD_KSRC != no ]]; then489create_linux-source_package490fi491492echo -e "\n\t== kernel ==\n" >> "${DEST}"/${LOG_SUBPATH}/compilation.log493eval CCACHE_BASEDIR="$(pwd)" env PATH="${toolchain}:${PATH}" \494'make $CTHREADS ARCH=$ARCHITECTURE \495CROSS_COMPILE="$CCACHE $KERNEL_COMPILER" \496$SRC_LOADADDR \497LOCALVERSION="-$LINUXFAMILY" \498$KERNEL_IMAGE_TYPE ${KERNEL_EXTRA_TARGETS:-modules dtbs} 2>>$DEST/${LOG_SUBPATH}/compilation.log' \499${PROGRESS_LOG_TO_FILE:+' | tee -a $DEST/${LOG_SUBPATH}/compilation.log'} \500${OUTPUT_DIALOG:+' | dialog --backtitle "$backtitle" \501--progressbox "Compiling kernel..." $TTY_Y $TTY_X'} \502${OUTPUT_VERYSILENT:+' >/dev/null 2>/dev/null'}503504if [[ ${PIPESTATUS[0]} -ne 0 || ! -f arch/$ARCHITECTURE/boot/$KERNEL_IMAGE_TYPE ]]; then505grep -i error $DEST/${LOG_SUBPATH}/compilation.log506exit_with_error "Kernel was not built" "@host"507fi508509if [[ ${BOARDFAMILY} == cix ]]; then510[[ -d ${SRC}/output/cix ]] && rm -rf ${SRC}/output/cix511mkdir -p ${SRC}/output/cix/ > /dev/null 2>&1512cp ${kerneldir}/arch/arm64/boot/Image ${SRC}/output/cix/513cp ${kerneldir}/arch/arm64/boot/dts/cix/sky1-*dtb ${SRC}/output/cix/514fi515516# different packaging for 4.3+517if linux-version compare "${version}" ge 4.3; then518local kernel_packing="bindeb-pkg"519else520local kernel_packing="deb-pkg"521fi522523#if [[ $BRANCH == legacy && $LINUXFAMILY =~ sun50iw2|sun50iw6|sun50iw9 ]]; then524# make -C modules/gpu LICHEE_MOD_DIR=${SRC}/.tmp/gpu_modules_${LINUXFAMILY} LICHEE_KDIR=${kerneldir} CROSS_COMPILE=$toolchain/$KERNEL_COMPILER ARCH=$ARCHITECTURE525#fi526527if [[ $LINUXFAMILY =~ sun60iw2 ]]; then528make -C bsp/modules/gpu LICHEE_TOOLCHAIN_PATH=$toolchain LICHEE_CROSS_COMPILER=$KERNEL_COMPILER LICHEE_PLATFORM=linux LICHEE_MOD_DIR=${SRC}/.tmp/gpu_modules_${LINUXFAMILY} LICHEE_KERN_DIR=${kerneldir} CROSS_COMPILE=$toolchain/$KERNEL_COMPILER ARCH=$ARCHITECTURE529make -C bsp/modules/gpu modules_install LICHEE_TOOLCHAIN_PATH=$toolchain LICHEE_CROSS_COMPILER=$KERNEL_COMPILER LICHEE_PLATFORM=linux LICHEE_MOD_DIR=${SRC}/.tmp/gpu_modules_${LINUXFAMILY} LICHEE_KERN_DIR=${kerneldir} CROSS_COMPILE=$toolchain/$KERNEL_COMPILER ARCH=$ARCHITECTURE530fi531532display_alert "Creating packages"533534# produce deb packages: image, headers, firmware, dtb535echo -e "\n\t== deb packages: image, headers, firmware, dtb ==\n" >> "${DEST}"/${LOG_SUBPATH}/compilation.log536eval CCACHE_BASEDIR="$(pwd)" env PATH="${toolchain}:${PATH}" \537'make $CTHREADS $kernel_packing \538KDEB_PKGVERSION=$REVISION \539KDEB_COMPRESS=${DEB_COMPRESS} \540BRANCH=$BRANCH \541LOCALVERSION="-${LINUXFAMILY}" \542KBUILD_DEBARCH=$ARCH \543ARCH=$ARCHITECTURE \544DEBFULLNAME="$MAINTAINER" \545DEBEMAIL="$MAINTAINERMAIL" \546CROSS_COMPILE="$CCACHE $KERNEL_COMPILER" 2>>$DEST/${LOG_SUBPATH}/compilation.log' \547${PROGRESS_LOG_TO_FILE:+' | tee -a $DEST/${LOG_SUBPATH}/compilation.log'} \548${OUTPUT_DIALOG:+' | dialog --backtitle "$backtitle" --progressbox "Creating kernel packages..." $TTY_Y $TTY_X'} \549${OUTPUT_VERYSILENT:+' >/dev/null 2>/dev/null'}550551cd .. || exit552# remove firmare image packages here - easier than patching ~40 packaging scripts at once553rm -f linux-firmware-image-*.deb554555rsync --remove-source-files -rq ./*.deb "${DEB_STORAGE}/" || exit_with_error "Failed moving kernel DEBs"556557[[ $(type -t family_tweaks_kernel) == function ]] && family_tweaks_kernel558559# store git hash to the file and create a change log560#HASHTARGET="${EXTER}/cache/hash"$([[ ${BETA} == yes ]] && echo "-beta")"/linux-image-${BRANCH}-${LINUXFAMILY}"561#OLDHASHTARGET=$(head -1 "${HASHTARGET}.githash" 2>/dev/null)562563# check if OLDHASHTARGET commit exists otherwise use oldest564#if [[ -z ${KERNEL_VERSION_LEVEL} ]]; then565# git -C ${kerneldir} cat-file -t ${OLDHASHTARGET} >/dev/null 2>&1566# [[ $? -ne 0 ]] && OLDHASHTARGET=$(git -C ${kerneldir} show HEAD~199 --pretty=format:"%H" --no-patch)567# else568# git -C ${kerneldir} cat-file -t ${OLDHASHTARGET} >/dev/null 2>&1569# [[ $? -ne 0 ]] && OLDHASHTARGET=$(git -C ${kerneldir} rev-list --max-parents=0 HEAD)570#fi571572#[[ -z ${KERNELPATCHDIR} ]] && KERNELPATCHDIR=$LINUXFAMILY-$BRANCH573#[[ -z ${LINUXCONFIG} ]] && LINUXCONFIG=linux-$LINUXFAMILY-$BRANCH574575# calculate URL576#if [[ "$KERNELSOURCE" == *"github.com"* ]]; then577# URL="${KERNELSOURCE/git:/https:}/commit/${HASH}"578#elif [[ "$KERNELSOURCE" == *"kernel.org"* ]]; then579# URL="${KERNELSOURCE/git:/https:}/commit/?h=$(echo $KERNELBRANCH | cut -d":" -f2)&id=${HASH}"580#else581# URL="${KERNELSOURCE}/+/$HASH"582#fi583584# create change log585#git --no-pager -C ${kerneldir} log --abbrev-commit --oneline --no-patch --no-merges --date-order --date=format:'%Y-%m-%d %H:%M:%S' --pretty=format:'%C(black bold)%ad%Creset%C(auto) | %s | <%an> | <a href='$URL'%H>%H</a>' ${OLDHASHTARGET}..${hash} > "${HASHTARGET}.gitlog"586587#echo "${hash}" > "${HASHTARGET}.githash"588#hash_watch_1=$(LC_COLLATE=C find -L "${EXTER}/patch/kernel/${KERNELPATCHDIR}"/ -name '*.patch' -mindepth 1 -maxdepth 1 -printf '%s %P\n' 2> /dev/null | LC_COLLATE=C sort -n)589#hash_watch_2=$(cat "${EXTER}/config/kernel/${LINUXCONFIG}.config")590#echo "${hash_watch_1}${hash_watch_2}" | improved_git hash-object --stdin >> "${HASHTARGET}.githash"591592}593594595596597compile_firmware()598{599display_alert "Merging and packaging linux firmware" "@host" "info"600601local firmwaretempdir plugin_dir602603firmwaretempdir=$(mktemp -d)604chmod 700 ${firmwaretempdir}605trap "ret=\$?; rm -rf \"${firmwaretempdir}\" ; exit \$ret" 0 1 2 3 15606plugin_dir="orangepi-firmware${FULL}"607mkdir -p "${firmwaretempdir}/${plugin_dir}/lib/firmware"608609if [[ $GITEE_SERVER == yes ]]; then610[[ $IGNORE_UPDATES != yes ]] && fetch_from_repo "https://gitee.com/orangepi-xunlong/firmware" "${EXTER}/cache/sources/orangepi-firmware-git" "branch:master"611else612[[ $IGNORE_UPDATES != yes ]] && fetch_from_repo "https://github.com/orangepi-xunlong/firmware" "${EXTER}/cache/sources/orangepi-firmware-git" "branch:master"613fi614615if [[ -n $FULL ]]; then616[[ $IGNORE_UPDATES != yes ]] && fetch_from_repo "$MAINLINE_FIRMWARE_SOURCE" "${EXTER}/cache/sources/linux-firmware-git" "branch:master"617# cp : create hardlinks618cp -af --reflink=auto "${EXTER}"/cache/sources/linux-firmware-git/* "${firmwaretempdir}/${plugin_dir}/lib/firmware/"619fi620# overlay our firmware621# cp : create hardlinks622cp -af --reflink=auto "${EXTER}"/cache/sources/orangepi-firmware-git/* "${firmwaretempdir}/${plugin_dir}/lib/firmware/"623624# cleanup what's not needed for sure625rm -rf "${firmwaretempdir}/${plugin_dir}"/lib/firmware/{amdgpu,amd-ucode,radeon,nvidia,matrox,.git}626cd "${firmwaretempdir}/${plugin_dir}" || exit627628# set up control file629mkdir -p DEBIAN630cat <<-END > DEBIAN/control631Package: orangepi-firmware${FULL}632Version: $REVISION633Architecture: all634Maintainer: $MAINTAINER <$MAINTAINERMAIL>635Installed-Size: 1636Replaces: linux-firmware, firmware-brcm80211, firmware-ralink, firmware-samsung, firmware-realtek, orangepi-firmware${REPLACE}637Section: kernel638Priority: optional639Description: Linux firmware${FULL}640END641642cd "${firmwaretempdir}" || exit643# pack644mv "orangepi-firmware${FULL}" "orangepi-firmware${FULL}_${REVISION}_all"645display_alert "Building firmware package" "orangepi-firmware${FULL}_${REVISION}_all" "info"646fakeroot dpkg-deb -b -Z${DEB_COMPRESS} "orangepi-firmware${FULL}_${REVISION}_all" >> "${DEST}"/${LOG_SUBPATH}/install.log 2>&1647mv "orangepi-firmware${FULL}_${REVISION}_all" "orangepi-firmware${FULL}"648rsync -rq "orangepi-firmware${FULL}_${REVISION}_all.deb" "${DEB_STORAGE}/"649650# remove temp directory651rm -rf "${firmwaretempdir}"652}653654655656657compile_orangepi-zsh()658{659660local tmp_dir orangepi_zsh_dir661tmp_dir=$(mktemp -d)662chmod 700 ${tmp_dir}663trap "rm -rf \"${tmp_dir}\" ; exit 0" 0 1 2 3 15664orangepi_zsh_dir=orangepi-zsh_${REVISION}_all665display_alert "Building deb" "orangepi-zsh" "info"666667if [[ $GITEE_SERVER == yes ]]; then668[[ $IGNORE_UPDATES != yes ]] && fetch_from_repo "https://gitee.com/orangepi-xunlong/oh-my-zsh" "${EXTER}/cache/sources/oh-my-zsh" "branch:master"669[[ $IGNORE_UPDATES != yes ]] && fetch_from_repo "https://gitee.com/orangepi-xunlong/evalcache" "${EXTER}/cache/sources/evalcache" "branch:master"670else671[[ $IGNORE_UPDATES != yes ]] && fetch_from_repo "https://github.com/robbyrussell/oh-my-zsh" "${EXTER}/cache/sources/oh-my-zsh" "branch:master"672[[ $IGNORE_UPDATES != yes ]] && fetch_from_repo "https://github.com/mroth/evalcache" "${EXTER}/cache/sources/evalcache" "branch:master"673fi674675mkdir -p "${tmp_dir}/${orangepi_zsh_dir}"/{DEBIAN,etc/skel/,etc/oh-my-zsh/,/etc/skel/.oh-my-zsh/cache}676677# set up control file678cat <<-END > "${tmp_dir}/${orangepi_zsh_dir}"/DEBIAN/control679Package: orangepi-zsh680Version: $REVISION681Architecture: all682Maintainer: $MAINTAINER <$MAINTAINERMAIL>683Depends: zsh, tmux684Section: utils685Priority: optional686Description: Orange Pi improved ZShell687END688689# set up post install script690cat <<-END > "${tmp_dir}/${orangepi_zsh_dir}"/DEBIAN/postinst691#!/bin/sh692693# copy cache directory if not there yet694awk -F'[:]' '{if (\$3 >= 1000 && \$3 != 65534 || \$3 == 0) print ""\$6"/.oh-my-zsh"}' /etc/passwd | xargs -i sh -c 'test ! -d {} && cp -R --attributes-only /etc/skel/.oh-my-zsh {}'695awk -F'[:]' '{if (\$3 >= 1000 && \$3 != 65534 || \$3 == 0) print ""\$6"/.zshrc"}' /etc/passwd | xargs -i sh -c 'test ! -f {} && cp -R /etc/skel/.zshrc {}'696697# fix owner permissions in home directory698awk -F'[:]' '{if (\$3 >= 1000 && \$3 != 65534 || \$3 == 0) print ""\$1":"\$3" "\$6"/.oh-my-zsh"}' /etc/passwd | xargs -n2 chown -R699awk -F'[:]' '{if (\$3 >= 1000 && \$3 != 65534 || \$3 == 0) print ""\$1":"\$3" "\$6"/.zshrc"}' /etc/passwd | xargs -n2 chown -R700701# add support for bash profile702! grep emulate /etc/zsh/zprofile >/dev/null && echo "emulate sh -c 'source /etc/profile'" >> /etc/zsh/zprofile703exit 0704END705706cp -R "${EXTER}"/cache/sources/oh-my-zsh "${tmp_dir}/${orangepi_zsh_dir}"/etc/707cp -R "${EXTER}"/cache/sources/evalcache "${tmp_dir}/${orangepi_zsh_dir}"/etc/oh-my-zsh/plugins708cp "${tmp_dir}/${orangepi_zsh_dir}"/etc/oh-my-zsh/templates/zshrc.zsh-template "${tmp_dir}/${orangepi_zsh_dir}"/etc/skel/.zshrc709710chmod -R g-w,o-w "${tmp_dir}/${orangepi_zsh_dir}"/etc/oh-my-zsh/711712# we have common settings713sed -i "s/^export ZSH=.*/export ZSH=\/etc\/oh-my-zsh/" "${tmp_dir}/${orangepi_zsh_dir}"/etc/skel/.zshrc714715# user cache716sed -i "/^export ZSH=.*/a export ZSH_CACHE_DIR=~\/.oh-my-zsh\/cache" "${tmp_dir}/${orangepi_zsh_dir}"/etc/skel/.zshrc717718# define theme719sed -i 's/^ZSH_THEME=.*/ZSH_THEME="mrtazz"/' "${tmp_dir}/${orangepi_zsh_dir}"/etc/skel/.zshrc720721# disable prompt while update722sed -i 's/# DISABLE_UPDATE_PROMPT="true"/DISABLE_UPDATE_PROMPT="true"/g' "${tmp_dir}/${orangepi_zsh_dir}"/etc/skel/.zshrc723724# disable auto update since we provide update via package725sed -i 's/# DISABLE_AUTO_UPDATE="true"/DISABLE_AUTO_UPDATE="true"/g' "${tmp_dir}/${orangepi_zsh_dir}"/etc/skel/.zshrc726727# define default plugins728sed -i 's/^plugins=.*/plugins=(evalcache git git-extras debian tmux screen history extract colorize web-search docker)/' "${tmp_dir}/${orangepi_zsh_dir}"/etc/skel/.zshrc729730chmod 755 "${tmp_dir}/${orangepi_zsh_dir}"/DEBIAN/postinst731732fakeroot dpkg-deb -b -Z${DEB_COMPRESS} "${tmp_dir}/${orangepi_zsh_dir}" >> "${DEST}"/${LOG_SUBPATH}/output.log 2>&1733rsync --remove-source-files -rq "${tmp_dir}/${orangepi_zsh_dir}.deb" "${DEB_STORAGE}/"734rm -rf "${tmp_dir}"735736}737738739740compile_plymouth-theme-orangepi()741{742743local tmp_dir work_dir744tmp_dir=$(mktemp -d)745chmod 700 ${tmp_dir}746trap "ret=\$?; rm -rf \"${tmp_dir}\" ; exit \$ret" 0 1 2 3 15747plymouth_theme_orangepi_dir=orangepi-plymouth-theme_${REVISION}_all748display_alert "Building deb" "orangepi-plymouth-theme" "info"749750mkdir -p "${tmp_dir}/${plymouth_theme_orangepi_dir}"/{DEBIAN,usr/share/plymouth/themes/orangepi}751752# set up control file753cat <<- END > "${tmp_dir}/${plymouth_theme_orangepi_dir}"/DEBIAN/control754Package: orangepi-plymouth-theme755Version: $REVISION756Architecture: all757Maintainer: $MAINTAINER <$MAINTAINERMAIL>758Depends: plymouth, plymouth-themes759Section: universe/x11760Priority: optional761Description: boot animation, logger and I/O multiplexer - orangepi theme762END763764cp "${EXTER}"/packages/plymouth-theme-orangepi/debian/{postinst,prerm,postrm} \765"${tmp_dir}/${plymouth_theme_orangepi_dir}"/DEBIAN/766chmod 755 "${tmp_dir}/${plymouth_theme_orangepi_dir}"/DEBIAN/{postinst,prerm,postrm}767768#convert -resize 256x256 \769# "${EXTER}"/packages/plymouth-theme-orangepi/orangepi-logo.png \770# "${tmp_dir}/${plymouth_theme_orangepi_dir}"/usr/share/plymouth/themes/orangepi/bgrt-fallback.png771772# convert -resize 52x52 \773# "${EXTER}"/packages/plymouth-theme-orangepi/spinner.gif \774# "${tmp_dir}/${plymouth_theme_orangepi_dir}"/usr/share/plymouth/themes/orangepi/animation-%04d.png775776convert -resize 52x52 \777"${EXTER}"/packages/plymouth-theme-orangepi/spinner.gif \778"${tmp_dir}/${plymouth_theme_orangepi_dir}"/usr/share/plymouth/themes/orangepi/throbber-%04d.png779780cp "${EXTER}"/packages/plymouth-theme-orangepi/watermark.png \781"${tmp_dir}/${plymouth_theme_orangepi_dir}"/usr/share/plymouth/themes/orangepi/782783cp "${EXTER}"/packages/plymouth-theme-orangepi/{bullet,capslock,entry,keyboard,keymap-render,lock}.png \784"${tmp_dir}/${plymouth_theme_orangepi_dir}"/usr/share/plymouth/themes/orangepi/785786cp "${EXTER}"/packages/plymouth-theme-orangepi/orangepi.plymouth \787"${tmp_dir}/${plymouth_theme_orangepi_dir}"/usr/share/plymouth/themes/orangepi/788789fakeroot dpkg-deb -b -Z${DEB_COMPRESS} "${tmp_dir}/${plymouth_theme_orangepi_dir}" > /dev/null790rsync --remove-source-files -rq "${tmp_dir}/${plymouth_theme_orangepi_dir}.deb" "${DEB_STORAGE}/"791rm -rf "${tmp_dir}"792}793794compile_orangepi-config()795{796local tmpdir=${SRC}/.tmp/orangepi-config_${REVISION}_all797798display_alert "Building deb" "orangepi-config" "info"799800801mkdir -p "${tmpdir}"/{DEBIAN,usr/bin/,usr/sbin/,usr/lib/orangepi-config/}802803# set up control file804cat <<-END > "${tmpdir}"/DEBIAN/control805Package: orangepi-config806Version: $REVISION807Architecture: all808Maintainer: $MAINTAINER <$MAINTAINERMAIL>809Replaces: orangepi-bsp810Depends: bash, iperf3, psmisc, curl, bc, expect, dialog, pv, \811debconf-utils, unzip, build-essential, html2text, apt-transport-https, html2text, dirmngr, software-properties-common812Recommends: orangepi-bsp813Suggests: libpam-google-authenticator, qrencode, network-manager, sunxi-tools814Section: utils815Priority: optional816Description: Orange Pi configuration utility817END818819install -m 755 $EXTER/cache/sources/orangepi-config/scripts/tv_grab_file $tmpdir/usr/bin/tv_grab_file820install -m 755 $EXTER/cache/sources/orangepi-config/debian-config $tmpdir/usr/sbin/orangepi-config821install -m 644 $EXTER/cache/sources/orangepi-config/debian-config-jobs $tmpdir/usr/lib/orangepi-config/jobs.sh822install -m 644 $EXTER/cache/sources/orangepi-config/debian-config-submenu $tmpdir/usr/lib/orangepi-config/submenu.sh823install -m 644 $EXTER/cache/sources/orangepi-config/debian-config-functions $tmpdir/usr/lib/orangepi-config/functions.sh824install -m 644 $EXTER/cache/sources/orangepi-config/debian-config-functions-network $tmpdir/usr/lib/orangepi-config/functions-network.sh825install -m 755 $EXTER/cache/sources/orangepi-config/softy $tmpdir/usr/sbin/softy826# fallback to replace orangepi-config in BSP827ln -sf /usr/sbin/orangepi-config $tmpdir/usr/bin/orangepi-config828ln -sf /usr/sbin/softy "${tmpdir}"/usr/bin/softy829830fakeroot dpkg-deb -b -Z${DEB_COMPRESS} "${tmpdir}" >/dev/null831rsync --remove-source-files -rq "${tmpdir}.deb" "${DEB_STORAGE}/"832rm -rf "${tmpdir}"833}834835836837838compile_sunxi_tools()839{840# Compile and install only if git commit hash changed841cd $EXTER/cache/sources/sunxi-tools || exit842# need to check if /usr/local/bin/sunxi-fexc to detect new Docker containers with old cached sources843if [[ ! -f .commit_id || $(improved_git rev-parse @ 2>/dev/null) != $(<.commit_id) || ! -f /usr/local/bin/sunxi-fexc ]]; then844display_alert "Compiling" "sunxi-tools" "info"845make -s clean >/dev/null846make -s tools >/dev/null847mkdir -p /usr/local/bin/848make install-tools >/dev/null 2>&1849improved_git rev-parse @ 2>/dev/null > .commit_id850fi851}852853install_rkbin_tools()854{855# install only if git commit hash changed856cd "${EXTER}"/cache/sources/rkbin-tools || exit857# need to check if /usr/local/bin/sunxi-fexc to detect new Docker containers with old cached sources858if [[ ! -f .commit_id || $(improved_git rev-parse @ 2>/dev/null) != $(<.commit_id) || ! -f /usr/local/bin/loaderimage ]]; then859display_alert "Installing" "rkbin-tools" "info"860mkdir -p /usr/local/bin/861install -m 755 tools/loaderimage /usr/local/bin/862install -m 755 tools/trust_merger /usr/local/bin/863improved_git rev-parse @ 2>/dev/null > .commit_id864fi865}866867grab_version()868{869local ver=()870ver[0]=$(grep "^VERSION" "${1}"/Makefile | head -1 | awk '{print $(NF)}' | grep -oE '^[[:digit:]]+')871ver[1]=$(grep "^PATCHLEVEL" "${1}"/Makefile | head -1 | awk '{print $(NF)}' | grep -oE '^[[:digit:]]+')872ver[2]=$(grep "^SUBLEVEL" "${1}"/Makefile | head -1 | awk '{print $(NF)}' | grep -oE '^[[:digit:]]+')873ver[3]=$(grep "^EXTRAVERSION" "${1}"/Makefile | head -1 | awk '{print $(NF)}' | grep -oE '^-rc[[:digit:]]+')874echo "${ver[0]:-0}${ver[1]:+.${ver[1]}}${ver[2]:+.${ver[2]}}${ver[3]}"875}876877# find_toolchain <compiler_prefix> <expression>878#879# returns path to toolchain that satisfies <expression>880#881find_toolchain()882{883[[ "${SKIP_EXTERNAL_TOOLCHAINS}" == "yes" ]] && { echo "/usr/bin"; return; }884local compiler=$1885local expression=$2886local dist=10887local toolchain=""888# extract target major.minor version from expression889local target_ver890target_ver=$(grep -oE "[[:digit:]]+\.[[:digit:]]" <<< "$expression")891for dir in "${SRC}"/toolchains/*/; do892# check if is a toolchain for current $ARCH893[[ ! -f ${dir}bin/${compiler}gcc ]] && continue894# get toolchain major.minor version895local gcc_ver896gcc_ver=$("${dir}bin/${compiler}gcc" -dumpversion | grep -oE "^[[:digit:]]+\.[[:digit:]]")897# check if toolchain version satisfies requirement898awk "BEGIN{exit ! ($gcc_ver $expression)}" >/dev/null || continue899# check if found version is the closest to target900# may need different logic here with more than 1 digit minor version numbers901# numbers: 3.9 > 3.10; versions: 3.9 < 3.10902# dpkg --compare-versions can be used here if operators are changed903local d904d=$(awk '{x = $1 - $2}{printf "%.1f\n", (x > 0) ? x : -x}' <<< "$target_ver $gcc_ver")905if awk "BEGIN{exit ! ($d < $dist)}" >/dev/null ; then906dist=$d907toolchain=${dir}bin908fi909done910echo "$toolchain"911# logging a stack of used compilers.912if [[ -f "${DEST}"/${LOG_SUBPATH}/compiler.log ]]; then913if ! grep -q "$toolchain" "${DEST}"/${LOG_SUBPATH}/compiler.log; then914echo "$toolchain" >> "${DEST}"/${LOG_SUBPATH}/compiler.log;915fi916else917echo "$toolchain" >> "${DEST}"/${LOG_SUBPATH}/compiler.log;918fi919}920921# advanced_patch <dest> <family> <board> <target> <branch> <description>922#923# parameters:924# <dest>: u-boot, kernel, atf925# <family>: u-boot: u-boot; kernel: sunxi-next, ...926# <board>: orangepipcplus, orangepizero ...927# <target>: optional subdirectory928# <description>: additional description text929#930# priority:931# $USERPATCHES_PATH/<dest>/<family>/target_<target>932# $USERPATCHES_PATH/<dest>/<family>/board_<board>933# $USERPATCHES_PATH/<dest>/<family>/branch_<branch>934# $USERPATCHES_PATH/<dest>/<family>935# $EXTER/patch/<dest>/<family>/target_<target>936# $EXTER/patch/<dest>/<family>/board_<board>937# $EXTER/patch/<dest>/<family>/branch_<branch>938# $EXTER/patch/<dest>/<family>939#940advanced_patch()941{942local dest=$1943local family=$2944local board=$3945local target=$4946local branch=$5947local description=$6948949display_alert "Started patching process for" "$dest $description" "info"950display_alert "Looking for user patches in" "userpatches/$dest/$family" "info"951952local names=()953local dirs=(954"$USERPATCHES_PATH/$dest/$family/target_${target}:[\e[33mu\e[0m][\e[34mt\e[0m]"955"$USERPATCHES_PATH/$dest/$family/board_${board}:[\e[33mu\e[0m][\e[35mb\e[0m]"956"$USERPATCHES_PATH/$dest/$family/branch_${branch}:[\e[33mu\e[0m][\e[33mb\e[0m]"957"$USERPATCHES_PATH/$dest/$family:[\e[33mu\e[0m][\e[32mc\e[0m]"958"$EXTER/patch/$dest/$family/target_${target}:[\e[32ml\e[0m][\e[34mt\e[0m]"959"$EXTER/patch/$dest/$family/board_${board}:[\e[32ml\e[0m][\e[35mb\e[0m]"960"$EXTER/patch/$dest/$family/branch_${branch}:[\e[32ml\e[0m][\e[33mb\e[0m]"961"$EXTER/patch/$dest/$family:[\e[32ml\e[0m][\e[32mc\e[0m]"962)963local links=()964965# required for "for" command966shopt -s nullglob dotglob967# get patch file names968for dir in "${dirs[@]}"; do969for patch in ${dir%%:*}/*.patch; do970names+=($(basename "${patch}"))971done972# add linked patch directories973if [[ -d ${dir%%:*} ]]; then974local findlinks975findlinks=$(find "${dir%%:*}" -maxdepth 1 -type l -print0 2>&1 | xargs -0)976[[ -n $findlinks ]] && readarray -d '' links < <(find "${findlinks}" -maxdepth 1 -type f -follow -print -iname "*.patch" -print | grep "\.patch$" | sed "s|${dir%%:*}/||g" 2>&1)977fi978done979# merge static and linked980names=("${names[@]}" "${links[@]}")981# remove duplicates982local names_s=($(echo "${names[@]}" | tr ' ' '\n' | LC_ALL=C sort -u | tr '\n' ' '))983# apply patches984for name in "${names_s[@]}"; do985for dir in "${dirs[@]}"; do986if [[ -f ${dir%%:*}/$name ]]; then987if [[ -s ${dir%%:*}/$name ]]; then988process_patch_file "${dir%%:*}/$name" "${dir##*:}"989else990display_alert "* ${dir##*:} $name" "skipped"991fi992break # next name993fi994done995done996}997998# process_patch_file <file> <description>999#1000# parameters:1001# <file>: path to patch file1002# <status>: additional status text1003#1004process_patch_file()1005{1006local patch=$11007local status=$210081009# detect and remove files which patch will create1010lsdiff -s --strip=1 "${patch}" | grep '^+' | awk '{print $2}' | xargs -I % sh -c 'rm -f %'10111012echo "Processing file $patch" >> "${DEST}"/${LOG_SUBPATH}/patching.log1013patch --batch --silent -p1 -N < "${patch}" >> "${DEST}"/${LOG_SUBPATH}/patching.log 2>&110141015if [[ $? -ne 0 ]]; then1016display_alert "* $status $(basename "${patch}")" "failed" "wrn"1017[[ $EXIT_PATCHING_ERROR == yes ]] && exit_with_error "Aborting due to" "EXIT_PATCHING_ERROR"1018else1019display_alert "* $status $(basename "${patch}")" "" "info"1020fi1021echo >> "${DEST}"/${LOG_SUBPATH}/patching.log1022}10231024userpatch_create()1025{1026# create commit to start from clean source1027git add .1028git -c user.name='Orange Pi User' -c user.email='[email protected]' commit -q -m "Cleaning working copy"10291030local patch="$DEST/patch/$1-$LINUXFAMILY-$BRANCH.patch"10311032# apply previous user debug mode created patches1033if [[ -f $patch ]]; then1034display_alert "Applying existing $1 patch" "$patch" "wrn" && patch --batch --silent -p1 -N < "${patch}"1035# read title of a patch in case Git is configured1036if [[ -n $(git config user.email) ]]; then1037COMMIT_MESSAGE=$(cat "${patch}" | grep Subject | sed -n -e '0,/PATCH/s/.*PATCH]//p' | xargs)1038display_alert "Patch name extracted" "$COMMIT_MESSAGE" "wrn"1039fi1040fi10411042# prompt to alter source1043display_alert "Make your changes in this directory:" "$(pwd)" "wrn"1044display_alert "Press <Enter> after you are done" "waiting" "wrn"1045read -r </dev/tty1046tput cuu11047git add .1048# create patch out of changes1049if ! git diff-index --quiet --cached HEAD; then1050# If Git is configured, create proper patch and ask for a name1051if [[ -n $(git config user.email) ]]; then1052display_alert "Add / change patch name" "$COMMIT_MESSAGE" "wrn"1053read -e -p "Patch description: " -i "$COMMIT_MESSAGE" COMMIT_MESSAGE1054[[ -z "$COMMIT_MESSAGE" ]] && COMMIT_MESSAGE="Patching something"1055git commit -s -m "$COMMIT_MESSAGE"1056git format-patch -1 HEAD --stdout --signature="Created with orangepi build tools https://github.com/orangepi-xunlong/build" > "${patch}"1057PATCHFILE=$(git format-patch -1 HEAD)1058rm $PATCHFILE # delete the actual file1059# create a symlink to have a nice name ready1060find $DEST/patch/ -type l -delete # delete any existing1061ln -sf $patch $DEST/patch/$PATCHFILE1062else1063git diff --staged > "${patch}"1064fi1065display_alert "You will find your patch here:" "$patch" "info"1066else1067display_alert "No changes found, skipping patch creation" "" "wrn"1068fi1069git reset --soft HEAD~1070for i in {3..1..1}; do echo -n "$i." && sleep 1; done1071}10721073# overlayfs_wrapper <operation> <workdir> <description>1074#1075# <operation>: wrap|cleanup1076# <workdir>: path to source directory1077# <description>: suffix for merged directory to help locating it in /tmp1078# return value: new directory1079#1080# Assumptions/notes:1081# - Ubuntu Xenial host1082# - /tmp is mounted as tmpfs1083# - there is enough space on /tmp1084# - UB if running multiple compilation tasks in parallel1085# - should not be used with CREATE_PATCHES=yes1086#1087overlayfs_wrapper()1088{1089local operation="$1"1090if [[ $operation == wrap ]]; then1091local srcdir="$2"1092local description="$3"1093mkdir -p /tmp/overlay_components/ /tmp/orangepi_build/1094local tempdir workdir mergeddir1095tempdir=$(mktemp -d --tmpdir="/tmp/overlay_components/")1096workdir=$(mktemp -d --tmpdir="/tmp/overlay_components/")1097mergeddir=$(mktemp -d --suffix="_$description" --tmpdir="/tmp/orangepi_build/")1098mount -t overlay overlay -o lowerdir="$srcdir",upperdir="$tempdir",workdir="$workdir" "$mergeddir"1099# this is executed in a subshell, so use temp files to pass extra data outside1100echo "$tempdir" >> /tmp/.overlayfs_wrapper_cleanup1101echo "$mergeddir" >> /tmp/.overlayfs_wrapper_umount1102echo "$mergeddir" >> /tmp/.overlayfs_wrapper_cleanup1103echo "$mergeddir"1104return1105fi1106if [[ $operation == cleanup ]]; then1107if [[ -f /tmp/.overlayfs_wrapper_umount ]]; then1108for dir in $(</tmp/.overlayfs_wrapper_umount); do1109[[ $dir == /tmp/* ]] && umount -l "$dir" > /dev/null 2>&11110done1111fi1112if [[ -f /tmp/.overlayfs_wrapper_cleanup ]]; then1113for dir in $(</tmp/.overlayfs_wrapper_cleanup); do1114[[ $dir == /tmp/* ]] && rm -rf "$dir"1115done1116fi1117rm -f /tmp/.overlayfs_wrapper_umount /tmp/.overlayfs_wrapper_cleanup1118fi1119}112011211122