Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/next/scripts/compilation.sh
Views: 3960
#!/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 setup_write_uboot_platform)291EOF292293# set up control file294cat <<-EOF > "$uboottempdir/${uboot_name}/DEBIAN/control"295Package: linux-u-boot-${BOARD}-${BRANCH}296Version: $REVISION297Architecture: $ARCH298Maintainer: $MAINTAINER <$MAINTAINERMAIL>299Installed-Size: 1300Section: kernel301Priority: optional302Provides: orangepi-u-boot303Replaces: orangepi-u-boot304Conflicts: orangepi-u-boot, u-boot-sunxi305Description: Uboot loader $version306EOF307308# copy config file to the package309# useful for FEL boot with overlayfs_wrapper310[[ -f .config && -n $BOOTCONFIG ]] && cp .config "$uboottempdir/${uboot_name}/usr/lib/u-boot/${BOOTCONFIG}"311# copy license files from typical locations312[[ -f COPYING ]] && cp COPYING "$uboottempdir/${uboot_name}/usr/lib/u-boot/LICENSE"313[[ -f Licenses/README ]] && cp Licenses/README "$uboottempdir/${uboot_name}/usr/lib/u-boot/LICENSE"314[[ -n $atftempdir && -f $atftempdir/license.md ]] && cp "${atftempdir}/license.md" "$uboottempdir/${uboot_name}/usr/lib/u-boot/LICENSE.atf"315316display_alert "Building deb" "${uboot_name}.deb" "info"317fakeroot dpkg-deb -b -Z${DEB_COMPRESS} "$uboottempdir/${uboot_name}" "$uboottempdir/${uboot_name}.deb" >> "${DEST}"/${LOG_SUBPATH}/output.log 2>&1318rm -rf "$uboottempdir/${uboot_name}"319[[ -n $atftempdir ]] && rm -rf "${atftempdir}"320321[[ ! -f $uboottempdir/${uboot_name}.deb ]] && exit_with_error "Building u-boot package failed"322323rsync --remove-source-files -rq "$uboottempdir/${uboot_name}.deb" "${DEB_STORAGE}/u-boot/"324rm -rf "$uboottempdir"325326fi327}328329create_linux-source_package ()330{331ts=$(date +%s)332local sources_pkg_dir tmp_src_dir333tmp_src_dir=$(mktemp -d)334trap "ret=\$?; rm -rf \"${tmp_src_dir}\" ; exit \$ret" 0 1 2 3 15335sources_pkg_dir=${tmp_src_dir}/${CHOSEN_KSRC}_${REVISION}_all336mkdir -p "${sources_pkg_dir}"/usr/src/ \337"${sources_pkg_dir}"/usr/share/doc/linux-source-${version}-${LINUXFAMILY} \338"${sources_pkg_dir}"/DEBIAN339340cp "${EXTER}/config/kernel/${LINUXCONFIG}.config" "default_${LINUXCONFIG}.config"341xz < .config > "${sources_pkg_dir}/usr/src/${LINUXCONFIG}_${version}_${REVISION}_config.xz"342343display_alert "Compressing sources for the linux-source package"344tar cp --directory="$kerneldir" --exclude='.git' --owner=root . \345| pv -p -b -r -s "$(du -sb "$kerneldir" --exclude=='.git' | cut -f1)" \346| pixz -4 > "${sources_pkg_dir}/usr/src/linux-source-${version}-${LINUXFAMILY}.tar.xz"347cp COPYING "${sources_pkg_dir}/usr/share/doc/linux-source-${version}-${LINUXFAMILY}/LICENSE"348349cat <<-EOF > "${sources_pkg_dir}"/DEBIAN/control350Package: linux-source-${version}-${BRANCH}-${LINUXFAMILY}351Version: ${version}-${BRANCH}-${LINUXFAMILY}+${REVISION}352Architecture: all353Maintainer: $MAINTAINER <$MAINTAINERMAIL>354Section: kernel355Priority: optional356Depends: binutils, coreutils357Provides: linux-source, linux-source-${version}-${LINUXFAMILY}358Recommends: gcc, make359Description: This package provides the source code for the Linux kernel $version360EOF361362fakeroot dpkg-deb -b -Z${DEB_COMPRESS} -z0 "${sources_pkg_dir}" "${sources_pkg_dir}.deb"363rsync --remove-source-files -rq "${sources_pkg_dir}.deb" "${DEB_STORAGE}/"364365te=$(date +%s)366display_alert "Make the linux-source package" "$(($te - $ts)) sec." "info"367rm -rf "${tmp_src_dir}"368}369370compile_kernel()371{372if [[ $CLEAN_LEVEL == *make* ]]; then373display_alert "Cleaning" "$LINUXSOURCEDIR" "info"374(cd ${LINUXSOURCEDIR}; make ARCH="${ARCHITECTURE}" clean >/dev/null 2>&1)375fi376377if [[ $USE_OVERLAYFS == yes ]]; then378local kerneldir379kerneldir=$(overlayfs_wrapper "wrap" "$LINUXSOURCEDIR" "kernel_${LINUXFAMILY}_${BRANCH}")380else381local kerneldir="$LINUXSOURCEDIR"382fi383cd "${kerneldir}" || exit384385rm -f localversion386387# read kernel version388local version hash389version=$(grab_version "$kerneldir")390391# read kernel git hash392hash=$(improved_git --git-dir="$kerneldir"/.git rev-parse HEAD)393394# Apply a series of patches if a series file exists395if test -f "${EXTER}"/patch/kernel/${KERNELPATCHDIR}/series.conf; then396display_alert "series.conf file visible. Apply"397series_conf="${SRC}"/patch/kernel/${KERNELPATCHDIR}/series.conf398399# apply_patch_series <target dir> <full path to series file>400apply_patch_series "${kerneldir}" "$series_conf"401fi402403# build 3rd party drivers404# compilation_prepare405406advanced_patch "kernel" "$KERNELPATCHDIR" "$BOARD" "" "$BRANCH" "$LINUXFAMILY-$BRANCH"407408# create patch for manual source changes in debug mode409[[ $CREATE_PATCHES == yes ]] && userpatch_create "kernel"410411# re-read kernel version after patching412local version413version=$(grab_version "$kerneldir")414415display_alert "Compiling $BRANCH kernel" "$version" "info"416417# compare with the architecture of the current Debian node418# if it matches we use the system compiler419if $(dpkg-architecture -e "${ARCH}"); then420display_alert "Native compilation"421elif [[ $(dpkg --print-architecture) == amd64 ]]; then422local toolchain423toolchain=$(find_toolchain "$KERNEL_COMPILER" "$KERNEL_USE_GCC")424[[ -z $toolchain ]] && exit_with_error "Could not find required toolchain" "${KERNEL_COMPILER}gcc $KERNEL_USE_GCC"425else426exit_with_error "Architecture [$ARCH] is not supported"427fi428429display_alert "Compiler version" "${KERNEL_COMPILER}gcc $(eval env PATH="${toolchain}:${PATH}" "${KERNEL_COMPILER}gcc" -dumpversion)" "info"430431# copy kernel config432if [[ $KERNEL_KEEP_CONFIG == yes && -f "${DEST}"/config/$LINUXCONFIG.config ]]; then433display_alert "Using previous kernel config" "${DEST}/config/$LINUXCONFIG.config" "info"434cp -p "${DEST}/config/${LINUXCONFIG}.config" .config435else436if [[ -f $USERPATCHES_PATH/$LINUXCONFIG.config ]]; then437display_alert "Using kernel config provided by user" "userpatches/$LINUXCONFIG.config" "info"438cp -p "${USERPATCHES_PATH}/${LINUXCONFIG}.config" .config439else440display_alert "Using kernel config file" "${EXTER}/config/kernel/$LINUXCONFIG.config" "info"441cp -p "${EXTER}/config/kernel/${LINUXCONFIG}.config" .config442fi443fi444445call_extension_method "custom_kernel_config" << 'CUSTOM_KERNEL_CONFIG'446*Kernel .config is in place, still clean from git version*447Called after ${LINUXCONFIG}.config is put in place (.config).448Before any olddefconfig any Kconfig make is called.449A good place to customize the .config directly.450CUSTOM_KERNEL_CONFIG451452453# hack for deb builder. To pack what's missing in headers pack.454cp "$EXTER"/patch/misc/headers-debian-byteshift.patch /tmp455456if [[ $KERNEL_CONFIGURE != yes ]]; then457if [[ $BRANCH == legacy && ! $BOARDFAMILY =~ "rockchip-rk3588"|"rockchip-rk356x" ]]; then458eval CCACHE_BASEDIR="$(pwd)" env PATH="${toolchain}:${PATH}" \459'make ARCH=$ARCHITECTURE CROSS_COMPILE="$CCACHE $KERNEL_COMPILER" silentoldconfig'460else461# TODO: check if required462eval CCACHE_BASEDIR="$(pwd)" env PATH="${toolchain}:${PATH}" \463'make ARCH=$ARCHITECTURE CROSS_COMPILE="$CCACHE $KERNEL_COMPILER" olddefconfig'464fi465else466eval CCACHE_BASEDIR="$(pwd)" env PATH="${toolchain}:${PATH}" \467'make $CTHREADS ARCH=$ARCHITECTURE CROSS_COMPILE="$CCACHE $KERNEL_COMPILER" oldconfig'468eval CCACHE_BASEDIR="$(pwd)" env PATH="${toolchain}:${PATH}" \469'make $CTHREADS ARCH=$ARCHITECTURE CROSS_COMPILE="$CCACHE $KERNEL_COMPILER" ${KERNEL_MENUCONFIG:-menuconfig}'470471[[ ${PIPESTATUS[0]} -ne 0 ]] && exit_with_error "Error kernel menuconfig failed"472473# store kernel config in easily reachable place474display_alert "Exporting new kernel config" "$DEST/config/$LINUXCONFIG.config" "info"475cp .config "${DEST}/config/${LINUXCONFIG}.config"476cp .config "${EXTER}/config/kernel/${LINUXCONFIG}.config"477# export defconfig too if requested478if [[ $KERNEL_EXPORT_DEFCONFIG == yes ]]; then479eval CCACHE_BASEDIR="$(pwd)" env PATH="${toolchain}:${PATH}" \480'make ARCH=$ARCHITECTURE CROSS_COMPILE="$CCACHE $KERNEL_COMPILER" savedefconfig'481[[ -f defconfig ]] && cp defconfig "${DEST}/config/${LINUXCONFIG}.defconfig"482fi483fi484485# create linux-source package - with already patched sources486# We will build this package first and clear the memory.487if [[ $BUILD_KSRC != no ]]; then488create_linux-source_package489fi490491echo -e "\n\t== kernel ==\n" >> "${DEST}"/${LOG_SUBPATH}/compilation.log492eval CCACHE_BASEDIR="$(pwd)" env PATH="${toolchain}:${PATH}" \493'make $CTHREADS ARCH=$ARCHITECTURE \494CROSS_COMPILE="$CCACHE $KERNEL_COMPILER" \495$SRC_LOADADDR \496LOCALVERSION="-$LINUXFAMILY" \497$KERNEL_IMAGE_TYPE ${KERNEL_EXTRA_TARGETS:-modules dtbs} 2>>$DEST/${LOG_SUBPATH}/compilation.log' \498${PROGRESS_LOG_TO_FILE:+' | tee -a $DEST/${LOG_SUBPATH}/compilation.log'} \499${OUTPUT_DIALOG:+' | dialog --backtitle "$backtitle" \500--progressbox "Compiling kernel..." $TTY_Y $TTY_X'} \501${OUTPUT_VERYSILENT:+' >/dev/null 2>/dev/null'}502503if [[ ${PIPESTATUS[0]} -ne 0 || ! -f arch/$ARCHITECTURE/boot/$KERNEL_IMAGE_TYPE ]]; then504grep -i error $DEST/${LOG_SUBPATH}/compilation.log505exit_with_error "Kernel was not built" "@host"506fi507508# different packaging for 4.3+509if linux-version compare "${version}" ge 4.3; then510local kernel_packing="bindeb-pkg"511else512local kernel_packing="deb-pkg"513fi514515#if [[ $BRANCH == legacy && $LINUXFAMILY =~ sun50iw2|sun50iw6|sun50iw9 ]]; then516# make -C modules/gpu LICHEE_MOD_DIR=${SRC}/.tmp/gpu_modules_${LINUXFAMILY} LICHEE_KDIR=${kerneldir} CROSS_COMPILE=$toolchain/$KERNEL_COMPILER ARCH=$ARCHITECTURE517#fi518519display_alert "Creating packages"520521# produce deb packages: image, headers, firmware, dtb522echo -e "\n\t== deb packages: image, headers, firmware, dtb ==\n" >> "${DEST}"/${LOG_SUBPATH}/compilation.log523eval CCACHE_BASEDIR="$(pwd)" env PATH="${toolchain}:${PATH}" \524'make $CTHREADS $kernel_packing \525KDEB_PKGVERSION=$REVISION \526KDEB_COMPRESS=${DEB_COMPRESS} \527BRANCH=$BRANCH \528LOCALVERSION="-${LINUXFAMILY}" \529KBUILD_DEBARCH=$ARCH \530ARCH=$ARCHITECTURE \531DEBFULLNAME="$MAINTAINER" \532DEBEMAIL="$MAINTAINERMAIL" \533CROSS_COMPILE="$CCACHE $KERNEL_COMPILER" 2>>$DEST/${LOG_SUBPATH}/compilation.log' \534${PROGRESS_LOG_TO_FILE:+' | tee -a $DEST/${LOG_SUBPATH}/compilation.log'} \535${OUTPUT_DIALOG:+' | dialog --backtitle "$backtitle" --progressbox "Creating kernel packages..." $TTY_Y $TTY_X'} \536${OUTPUT_VERYSILENT:+' >/dev/null 2>/dev/null'}537538cd .. || exit539# remove firmare image packages here - easier than patching ~40 packaging scripts at once540rm -f linux-firmware-image-*.deb541542rsync --remove-source-files -rq ./*.deb "${DEB_STORAGE}/" || exit_with_error "Failed moving kernel DEBs"543544# store git hash to the file and create a change log545#HASHTARGET="${EXTER}/cache/hash"$([[ ${BETA} == yes ]] && echo "-beta")"/linux-image-${BRANCH}-${LINUXFAMILY}"546#OLDHASHTARGET=$(head -1 "${HASHTARGET}.githash" 2>/dev/null)547548# check if OLDHASHTARGET commit exists otherwise use oldest549#if [[ -z ${KERNEL_VERSION_LEVEL} ]]; then550# git -C ${kerneldir} cat-file -t ${OLDHASHTARGET} >/dev/null 2>&1551# [[ $? -ne 0 ]] && OLDHASHTARGET=$(git -C ${kerneldir} show HEAD~199 --pretty=format:"%H" --no-patch)552# else553# git -C ${kerneldir} cat-file -t ${OLDHASHTARGET} >/dev/null 2>&1554# [[ $? -ne 0 ]] && OLDHASHTARGET=$(git -C ${kerneldir} rev-list --max-parents=0 HEAD)555#fi556557#[[ -z ${KERNELPATCHDIR} ]] && KERNELPATCHDIR=$LINUXFAMILY-$BRANCH558#[[ -z ${LINUXCONFIG} ]] && LINUXCONFIG=linux-$LINUXFAMILY-$BRANCH559560# calculate URL561#if [[ "$KERNELSOURCE" == *"github.com"* ]]; then562# URL="${KERNELSOURCE/git:/https:}/commit/${HASH}"563#elif [[ "$KERNELSOURCE" == *"kernel.org"* ]]; then564# URL="${KERNELSOURCE/git:/https:}/commit/?h=$(echo $KERNELBRANCH | cut -d":" -f2)&id=${HASH}"565#else566# URL="${KERNELSOURCE}/+/$HASH"567#fi568569# create change log570#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"571572#echo "${hash}" > "${HASHTARGET}.githash"573#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)574#hash_watch_2=$(cat "${EXTER}/config/kernel/${LINUXCONFIG}.config")575#echo "${hash_watch_1}${hash_watch_2}" | improved_git hash-object --stdin >> "${HASHTARGET}.githash"576577}578579580581582compile_firmware()583{584display_alert "Merging and packaging linux firmware" "@host" "info"585586local firmwaretempdir plugin_dir587588firmwaretempdir=$(mktemp -d)589chmod 700 ${firmwaretempdir}590trap "ret=\$?; rm -rf \"${firmwaretempdir}\" ; exit \$ret" 0 1 2 3 15591plugin_dir="orangepi-firmware${FULL}"592mkdir -p "${firmwaretempdir}/${plugin_dir}/lib/firmware"593594[[ $IGNORE_UPDATES != yes ]] && fetch_from_repo "https://github.com/orangepi-xunlong/firmware" "${EXTER}/cache/sources/orangepi-firmware-git" "branch:master"595if [[ -n $FULL ]]; then596[[ $IGNORE_UPDATES != yes ]] && fetch_from_repo "$MAINLINE_FIRMWARE_SOURCE" "${EXTER}/cache/sources/linux-firmware-git" "branch:master"597# cp : create hardlinks598cp -af --reflink=auto "${EXTER}"/cache/sources/linux-firmware-git/* "${firmwaretempdir}/${plugin_dir}/lib/firmware/"599fi600# overlay our firmware601# cp : create hardlinks602cp -af --reflink=auto "${EXTER}"/cache/sources/orangepi-firmware-git/* "${firmwaretempdir}/${plugin_dir}/lib/firmware/"603604# cleanup what's not needed for sure605rm -rf "${firmwaretempdir}/${plugin_dir}"/lib/firmware/{amdgpu,amd-ucode,radeon,nvidia,matrox,.git}606cd "${firmwaretempdir}/${plugin_dir}" || exit607608# set up control file609mkdir -p DEBIAN610cat <<-END > DEBIAN/control611Package: orangepi-firmware${FULL}612Version: $REVISION613Architecture: all614Maintainer: $MAINTAINER <$MAINTAINERMAIL>615Installed-Size: 1616Replaces: linux-firmware, firmware-brcm80211, firmware-ralink, firmware-samsung, firmware-realtek, orangepi-firmware${REPLACE}617Section: kernel618Priority: optional619Description: Linux firmware${FULL}620END621622cd "${firmwaretempdir}" || exit623# pack624mv "orangepi-firmware${FULL}" "orangepi-firmware${FULL}_${REVISION}_all"625display_alert "Building firmware package" "orangepi-firmware${FULL}_${REVISION}_all" "info"626fakeroot dpkg-deb -b -Z${DEB_COMPRESS} "orangepi-firmware${FULL}_${REVISION}_all" >> "${DEST}"/${LOG_SUBPATH}/install.log 2>&1627mv "orangepi-firmware${FULL}_${REVISION}_all" "orangepi-firmware${FULL}"628rsync -rq "orangepi-firmware${FULL}_${REVISION}_all.deb" "${DEB_STORAGE}/"629630# remove temp directory631rm -rf "${firmwaretempdir}"632}633634635636637compile_orangepi-zsh()638{639640local tmp_dir orangepi_zsh_dir641tmp_dir=$(mktemp -d)642chmod 700 ${tmp_dir}643trap "rm -rf \"${tmp_dir}\" ; exit 0" 0 1 2 3 15644orangepi_zsh_dir=orangepi-zsh_${REVISION}_all645display_alert "Building deb" "orangepi-zsh" "info"646647[[ $IGNORE_UPDATES != yes ]] && fetch_from_repo "https://github.com/robbyrussell/oh-my-zsh" "${EXTER}/cache/sources/oh-my-zsh" "branch:master"648[[ $IGNORE_UPDATES != yes ]] && fetch_from_repo "https://github.com/mroth/evalcache" "${EXTER}/cache/sources/evalcache" "branch:master"649650mkdir -p "${tmp_dir}/${orangepi_zsh_dir}"/{DEBIAN,etc/skel/,etc/oh-my-zsh/,/etc/skel/.oh-my-zsh/cache}651652# set up control file653cat <<-END > "${tmp_dir}/${orangepi_zsh_dir}"/DEBIAN/control654Package: orangepi-zsh655Version: $REVISION656Architecture: all657Maintainer: $MAINTAINER <$MAINTAINERMAIL>658Depends: zsh, tmux659Section: utils660Priority: optional661Description: Orange Pi improved ZShell662END663664# set up post install script665cat <<-END > "${tmp_dir}/${orangepi_zsh_dir}"/DEBIAN/postinst666#!/bin/sh667668# copy cache directory if not there yet669awk -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 {}'670awk -F'[:]' '{if (\$3 >= 1000 && \$3 != 65534 || \$3 == 0) print ""\$6"/.zshrc"}' /etc/passwd | xargs -i sh -c 'test ! -f {} && cp -R /etc/skel/.zshrc {}'671672# fix owner permissions in home directory673awk -F'[:]' '{if (\$3 >= 1000 && \$3 != 65534 || \$3 == 0) print ""\$1":"\$3" "\$6"/.oh-my-zsh"}' /etc/passwd | xargs -n2 chown -R674awk -F'[:]' '{if (\$3 >= 1000 && \$3 != 65534 || \$3 == 0) print ""\$1":"\$3" "\$6"/.zshrc"}' /etc/passwd | xargs -n2 chown -R675676# add support for bash profile677! grep emulate /etc/zsh/zprofile >/dev/null && echo "emulate sh -c 'source /etc/profile'" >> /etc/zsh/zprofile678exit 0679END680681cp -R "${EXTER}"/cache/sources/oh-my-zsh "${tmp_dir}/${orangepi_zsh_dir}"/etc/682cp -R "${EXTER}"/cache/sources/evalcache "${tmp_dir}/${orangepi_zsh_dir}"/etc/oh-my-zsh/plugins683cp "${tmp_dir}/${orangepi_zsh_dir}"/etc/oh-my-zsh/templates/zshrc.zsh-template "${tmp_dir}/${orangepi_zsh_dir}"/etc/skel/.zshrc684685chmod -R g-w,o-w "${tmp_dir}/${orangepi_zsh_dir}"/etc/oh-my-zsh/686687# we have common settings688sed -i "s/^export ZSH=.*/export ZSH=\/etc\/oh-my-zsh/" "${tmp_dir}/${orangepi_zsh_dir}"/etc/skel/.zshrc689690# user cache691sed -i "/^export ZSH=.*/a export ZSH_CACHE_DIR=~\/.oh-my-zsh\/cache" "${tmp_dir}/${orangepi_zsh_dir}"/etc/skel/.zshrc692693# define theme694sed -i 's/^ZSH_THEME=.*/ZSH_THEME="mrtazz"/' "${tmp_dir}/${orangepi_zsh_dir}"/etc/skel/.zshrc695696# disable prompt while update697sed -i 's/# DISABLE_UPDATE_PROMPT="true"/DISABLE_UPDATE_PROMPT="true"/g' "${tmp_dir}/${orangepi_zsh_dir}"/etc/skel/.zshrc698699# disable auto update since we provide update via package700sed -i 's/# DISABLE_AUTO_UPDATE="true"/DISABLE_AUTO_UPDATE="true"/g' "${tmp_dir}/${orangepi_zsh_dir}"/etc/skel/.zshrc701702# define default plugins703sed -i 's/^plugins=.*/plugins=(evalcache git git-extras debian tmux screen history extract colorize web-search docker)/' "${tmp_dir}/${orangepi_zsh_dir}"/etc/skel/.zshrc704705chmod 755 "${tmp_dir}/${orangepi_zsh_dir}"/DEBIAN/postinst706707fakeroot dpkg-deb -b -Z${DEB_COMPRESS} "${tmp_dir}/${orangepi_zsh_dir}" >> "${DEST}"/${LOG_SUBPATH}/output.log 2>&1708rsync --remove-source-files -rq "${tmp_dir}/${orangepi_zsh_dir}.deb" "${DEB_STORAGE}/"709rm -rf "${tmp_dir}"710711}712713714715compile_plymouth-theme-orangepi()716{717718local tmp_dir work_dir719tmp_dir=$(mktemp -d)720chmod 700 ${tmp_dir}721trap "ret=\$?; rm -rf \"${tmp_dir}\" ; exit \$ret" 0 1 2 3 15722plymouth_theme_orangepi_dir=orangepi-plymouth-theme_${REVISION}_all723display_alert "Building deb" "orangepi-plymouth-theme" "info"724725mkdir -p "${tmp_dir}/${plymouth_theme_orangepi_dir}"/{DEBIAN,usr/share/plymouth/themes/orangepi}726727# set up control file728cat <<- END > "${tmp_dir}/${plymouth_theme_orangepi_dir}"/DEBIAN/control729Package: orangepi-plymouth-theme730Version: $REVISION731Architecture: all732Maintainer: $MAINTAINER <$MAINTAINERMAIL>733Depends: plymouth, plymouth-themes734Section: universe/x11735Priority: optional736Description: boot animation, logger and I/O multiplexer - orangepi theme737END738739cp "${EXTER}"/packages/plymouth-theme-orangepi/debian/{postinst,prerm,postrm} \740"${tmp_dir}/${plymouth_theme_orangepi_dir}"/DEBIAN/741chmod 755 "${tmp_dir}/${plymouth_theme_orangepi_dir}"/DEBIAN/{postinst,prerm,postrm}742743#convert -resize 256x256 \744# "${EXTER}"/packages/plymouth-theme-orangepi/orangepi-logo.png \745# "${tmp_dir}/${plymouth_theme_orangepi_dir}"/usr/share/plymouth/themes/orangepi/bgrt-fallback.png746747# convert -resize 52x52 \748# "${EXTER}"/packages/plymouth-theme-orangepi/spinner.gif \749# "${tmp_dir}/${plymouth_theme_orangepi_dir}"/usr/share/plymouth/themes/orangepi/animation-%04d.png750751convert -resize 52x52 \752"${EXTER}"/packages/plymouth-theme-orangepi/spinner.gif \753"${tmp_dir}/${plymouth_theme_orangepi_dir}"/usr/share/plymouth/themes/orangepi/throbber-%04d.png754755cp "${EXTER}"/packages/plymouth-theme-orangepi/watermark.png \756"${tmp_dir}/${plymouth_theme_orangepi_dir}"/usr/share/plymouth/themes/orangepi/757758cp "${EXTER}"/packages/plymouth-theme-orangepi/{bullet,capslock,entry,keyboard,keymap-render,lock}.png \759"${tmp_dir}/${plymouth_theme_orangepi_dir}"/usr/share/plymouth/themes/orangepi/760761cp "${EXTER}"/packages/plymouth-theme-orangepi/orangepi.plymouth \762"${tmp_dir}/${plymouth_theme_orangepi_dir}"/usr/share/plymouth/themes/orangepi/763764fakeroot dpkg-deb -b -Z${DEB_COMPRESS} "${tmp_dir}/${plymouth_theme_orangepi_dir}" > /dev/null765rsync --remove-source-files -rq "${tmp_dir}/${plymouth_theme_orangepi_dir}.deb" "${DEB_STORAGE}/"766rm -rf "${tmp_dir}"767}768769compile_orangepi-config()770{771local tmpdir=${SRC}/.tmp/orangepi-config_${REVISION}_all772773display_alert "Building deb" "orangepi-config" "info"774775776mkdir -p "${tmpdir}"/{DEBIAN,usr/bin/,usr/sbin/,usr/lib/orangepi-config/}777778# set up control file779cat <<-END > "${tmpdir}"/DEBIAN/control780Package: orangepi-config781Version: $REVISION782Architecture: all783Maintainer: $MAINTAINER <$MAINTAINERMAIL>784Replaces: orangepi-bsp785Depends: bash, iperf3, psmisc, curl, bc, expect, dialog, pv, \786debconf-utils, unzip, build-essential, html2text, apt-transport-https, html2text, dirmngr, software-properties-common787Recommends: orangepi-bsp788Suggests: libpam-google-authenticator, qrencode, network-manager, sunxi-tools789Section: utils790Priority: optional791Description: Orange Pi configuration utility792END793794install -m 755 $EXTER/cache/sources/orangepi-config/scripts/tv_grab_file $tmpdir/usr/bin/tv_grab_file795install -m 755 $EXTER/cache/sources/orangepi-config/debian-config $tmpdir/usr/sbin/orangepi-config796install -m 644 $EXTER/cache/sources/orangepi-config/debian-config-jobs $tmpdir/usr/lib/orangepi-config/jobs.sh797install -m 644 $EXTER/cache/sources/orangepi-config/debian-config-submenu $tmpdir/usr/lib/orangepi-config/submenu.sh798install -m 644 $EXTER/cache/sources/orangepi-config/debian-config-functions $tmpdir/usr/lib/orangepi-config/functions.sh799install -m 644 $EXTER/cache/sources/orangepi-config/debian-config-functions-network $tmpdir/usr/lib/orangepi-config/functions-network.sh800install -m 755 $EXTER/cache/sources/orangepi-config/softy $tmpdir/usr/sbin/softy801# fallback to replace orangepi-config in BSP802ln -sf /usr/sbin/orangepi-config $tmpdir/usr/bin/orangepi-config803ln -sf /usr/sbin/softy "${tmpdir}"/usr/bin/softy804805fakeroot dpkg-deb -b -Z${DEB_COMPRESS} "${tmpdir}" >/dev/null806rsync --remove-source-files -rq "${tmpdir}.deb" "${DEB_STORAGE}/"807rm -rf "${tmpdir}"808}809810811812813compile_sunxi_tools()814{815# Compile and install only if git commit hash changed816cd $EXTER/cache/sources/sunxi-tools || exit817# need to check if /usr/local/bin/sunxi-fexc to detect new Docker containers with old cached sources818if [[ ! -f .commit_id || $(improved_git rev-parse @ 2>/dev/null) != $(<.commit_id) || ! -f /usr/local/bin/sunxi-fexc ]]; then819display_alert "Compiling" "sunxi-tools" "info"820make -s clean >/dev/null821make -s tools >/dev/null822mkdir -p /usr/local/bin/823make install-tools >/dev/null 2>&1824improved_git rev-parse @ 2>/dev/null > .commit_id825fi826}827828install_rkbin_tools()829{830# install only if git commit hash changed831cd "${EXTER}"/cache/sources/rkbin-tools || exit832# need to check if /usr/local/bin/sunxi-fexc to detect new Docker containers with old cached sources833if [[ ! -f .commit_id || $(improved_git rev-parse @ 2>/dev/null) != $(<.commit_id) || ! -f /usr/local/bin/loaderimage ]]; then834display_alert "Installing" "rkbin-tools" "info"835mkdir -p /usr/local/bin/836install -m 755 tools/loaderimage /usr/local/bin/837install -m 755 tools/trust_merger /usr/local/bin/838improved_git rev-parse @ 2>/dev/null > .commit_id839fi840}841842grab_version()843{844local ver=()845ver[0]=$(grep "^VERSION" "${1}"/Makefile | head -1 | awk '{print $(NF)}' | grep -oE '^[[:digit:]]+')846ver[1]=$(grep "^PATCHLEVEL" "${1}"/Makefile | head -1 | awk '{print $(NF)}' | grep -oE '^[[:digit:]]+')847ver[2]=$(grep "^SUBLEVEL" "${1}"/Makefile | head -1 | awk '{print $(NF)}' | grep -oE '^[[:digit:]]+')848ver[3]=$(grep "^EXTRAVERSION" "${1}"/Makefile | head -1 | awk '{print $(NF)}' | grep -oE '^-rc[[:digit:]]+')849echo "${ver[0]:-0}${ver[1]:+.${ver[1]}}${ver[2]:+.${ver[2]}}${ver[3]}"850}851852# find_toolchain <compiler_prefix> <expression>853#854# returns path to toolchain that satisfies <expression>855#856find_toolchain()857{858[[ "${SKIP_EXTERNAL_TOOLCHAINS}" == "yes" ]] && { echo "/usr/bin"; return; }859local compiler=$1860local expression=$2861local dist=10862local toolchain=""863# extract target major.minor version from expression864local target_ver865target_ver=$(grep -oE "[[:digit:]]+\.[[:digit:]]" <<< "$expression")866for dir in "${SRC}"/toolchains/*/; do867# check if is a toolchain for current $ARCH868[[ ! -f ${dir}bin/${compiler}gcc ]] && continue869# get toolchain major.minor version870local gcc_ver871gcc_ver=$("${dir}bin/${compiler}gcc" -dumpversion | grep -oE "^[[:digit:]]+\.[[:digit:]]")872# check if toolchain version satisfies requirement873awk "BEGIN{exit ! ($gcc_ver $expression)}" >/dev/null || continue874# check if found version is the closest to target875# may need different logic here with more than 1 digit minor version numbers876# numbers: 3.9 > 3.10; versions: 3.9 < 3.10877# dpkg --compare-versions can be used here if operators are changed878local d879d=$(awk '{x = $1 - $2}{printf "%.1f\n", (x > 0) ? x : -x}' <<< "$target_ver $gcc_ver")880if awk "BEGIN{exit ! ($d < $dist)}" >/dev/null ; then881dist=$d882toolchain=${dir}bin883fi884done885echo "$toolchain"886# logging a stack of used compilers.887if [[ -f "${DEST}"/${LOG_SUBPATH}/compiler.log ]]; then888if ! grep -q "$toolchain" "${DEST}"/${LOG_SUBPATH}/compiler.log; then889echo "$toolchain" >> "${DEST}"/${LOG_SUBPATH}/compiler.log;890fi891else892echo "$toolchain" >> "${DEST}"/${LOG_SUBPATH}/compiler.log;893fi894}895896# advanced_patch <dest> <family> <board> <target> <branch> <description>897#898# parameters:899# <dest>: u-boot, kernel, atf900# <family>: u-boot: u-boot; kernel: sunxi-next, ...901# <board>: orangepipcplus, orangepizero ...902# <target>: optional subdirectory903# <description>: additional description text904#905# priority:906# $USERPATCHES_PATH/<dest>/<family>/target_<target>907# $USERPATCHES_PATH/<dest>/<family>/board_<board>908# $USERPATCHES_PATH/<dest>/<family>/branch_<branch>909# $USERPATCHES_PATH/<dest>/<family>910# $EXTER/patch/<dest>/<family>/target_<target>911# $EXTER/patch/<dest>/<family>/board_<board>912# $EXTER/patch/<dest>/<family>/branch_<branch>913# $EXTER/patch/<dest>/<family>914#915advanced_patch()916{917local dest=$1918local family=$2919local board=$3920local target=$4921local branch=$5922local description=$6923924display_alert "Started patching process for" "$dest $description" "info"925display_alert "Looking for user patches in" "userpatches/$dest/$family" "info"926927local names=()928local dirs=(929"$USERPATCHES_PATH/$dest/$family/target_${target}:[\e[33mu\e[0m][\e[34mt\e[0m]"930"$USERPATCHES_PATH/$dest/$family/board_${board}:[\e[33mu\e[0m][\e[35mb\e[0m]"931"$USERPATCHES_PATH/$dest/$family/branch_${branch}:[\e[33mu\e[0m][\e[33mb\e[0m]"932"$USERPATCHES_PATH/$dest/$family:[\e[33mu\e[0m][\e[32mc\e[0m]"933"$EXTER/patch/$dest/$family/target_${target}:[\e[32ml\e[0m][\e[34mt\e[0m]"934"$EXTER/patch/$dest/$family/board_${board}:[\e[32ml\e[0m][\e[35mb\e[0m]"935"$EXTER/patch/$dest/$family/branch_${branch}:[\e[32ml\e[0m][\e[33mb\e[0m]"936"$EXTER/patch/$dest/$family:[\e[32ml\e[0m][\e[32mc\e[0m]"937)938local links=()939940# required for "for" command941shopt -s nullglob dotglob942# get patch file names943for dir in "${dirs[@]}"; do944for patch in ${dir%%:*}/*.patch; do945names+=($(basename "${patch}"))946done947# add linked patch directories948if [[ -d ${dir%%:*} ]]; then949local findlinks950findlinks=$(find "${dir%%:*}" -maxdepth 1 -type l -print0 2>&1 | xargs -0)951[[ -n $findlinks ]] && readarray -d '' links < <(find "${findlinks}" -maxdepth 1 -type f -follow -print -iname "*.patch" -print | grep "\.patch$" | sed "s|${dir%%:*}/||g" 2>&1)952fi953done954# merge static and linked955names=("${names[@]}" "${links[@]}")956# remove duplicates957local names_s=($(echo "${names[@]}" | tr ' ' '\n' | LC_ALL=C sort -u | tr '\n' ' '))958# apply patches959for name in "${names_s[@]}"; do960for dir in "${dirs[@]}"; do961if [[ -f ${dir%%:*}/$name ]]; then962if [[ -s ${dir%%:*}/$name ]]; then963process_patch_file "${dir%%:*}/$name" "${dir##*:}"964else965display_alert "* ${dir##*:} $name" "skipped"966fi967break # next name968fi969done970done971}972973# process_patch_file <file> <description>974#975# parameters:976# <file>: path to patch file977# <status>: additional status text978#979process_patch_file()980{981local patch=$1982local status=$2983984# detect and remove files which patch will create985lsdiff -s --strip=1 "${patch}" | grep '^+' | awk '{print $2}' | xargs -I % sh -c 'rm -f %'986987echo "Processing file $patch" >> "${DEST}"/${LOG_SUBPATH}/patching.log988patch --batch --silent -p1 -N < "${patch}" >> "${DEST}"/${LOG_SUBPATH}/patching.log 2>&1989990if [[ $? -ne 0 ]]; then991display_alert "* $status $(basename "${patch}")" "failed" "wrn"992[[ $EXIT_PATCHING_ERROR == yes ]] && exit_with_error "Aborting due to" "EXIT_PATCHING_ERROR"993else994display_alert "* $status $(basename "${patch}")" "" "info"995fi996echo >> "${DEST}"/${LOG_SUBPATH}/patching.log997}998999userpatch_create()1000{1001# create commit to start from clean source1002git add .1003git -c user.name='Orange Pi User' -c user.email='[email protected]' commit -q -m "Cleaning working copy"10041005local patch="$DEST/patch/$1-$LINUXFAMILY-$BRANCH.patch"10061007# apply previous user debug mode created patches1008if [[ -f $patch ]]; then1009display_alert "Applying existing $1 patch" "$patch" "wrn" && patch --batch --silent -p1 -N < "${patch}"1010# read title of a patch in case Git is configured1011if [[ -n $(git config user.email) ]]; then1012COMMIT_MESSAGE=$(cat "${patch}" | grep Subject | sed -n -e '0,/PATCH/s/.*PATCH]//p' | xargs)1013display_alert "Patch name extracted" "$COMMIT_MESSAGE" "wrn"1014fi1015fi10161017# prompt to alter source1018display_alert "Make your changes in this directory:" "$(pwd)" "wrn"1019display_alert "Press <Enter> after you are done" "waiting" "wrn"1020read -r </dev/tty1021tput cuu11022git add .1023# create patch out of changes1024if ! git diff-index --quiet --cached HEAD; then1025# If Git is configured, create proper patch and ask for a name1026if [[ -n $(git config user.email) ]]; then1027display_alert "Add / change patch name" "$COMMIT_MESSAGE" "wrn"1028read -e -p "Patch description: " -i "$COMMIT_MESSAGE" COMMIT_MESSAGE1029[[ -z "$COMMIT_MESSAGE" ]] && COMMIT_MESSAGE="Patching something"1030git commit -s -m "$COMMIT_MESSAGE"1031git format-patch -1 HEAD --stdout --signature="Created with orangepi build tools https://github.com/orangepi-xunlong/build" > "${patch}"1032PATCHFILE=$(git format-patch -1 HEAD)1033rm $PATCHFILE # delete the actual file1034# create a symlink to have a nice name ready1035find $DEST/patch/ -type l -delete # delete any existing1036ln -sf $patch $DEST/patch/$PATCHFILE1037else1038git diff --staged > "${patch}"1039fi1040display_alert "You will find your patch here:" "$patch" "info"1041else1042display_alert "No changes found, skipping patch creation" "" "wrn"1043fi1044git reset --soft HEAD~1045for i in {3..1..1}; do echo -n "$i." && sleep 1; done1046}10471048# overlayfs_wrapper <operation> <workdir> <description>1049#1050# <operation>: wrap|cleanup1051# <workdir>: path to source directory1052# <description>: suffix for merged directory to help locating it in /tmp1053# return value: new directory1054#1055# Assumptions/notes:1056# - Ubuntu Xenial host1057# - /tmp is mounted as tmpfs1058# - there is enough space on /tmp1059# - UB if running multiple compilation tasks in parallel1060# - should not be used with CREATE_PATCHES=yes1061#1062overlayfs_wrapper()1063{1064local operation="$1"1065if [[ $operation == wrap ]]; then1066local srcdir="$2"1067local description="$3"1068mkdir -p /tmp/overlay_components/ /tmp/orangepi_build/1069local tempdir workdir mergeddir1070tempdir=$(mktemp -d --tmpdir="/tmp/overlay_components/")1071workdir=$(mktemp -d --tmpdir="/tmp/overlay_components/")1072mergeddir=$(mktemp -d --suffix="_$description" --tmpdir="/tmp/orangepi_build/")1073mount -t overlay overlay -o lowerdir="$srcdir",upperdir="$tempdir",workdir="$workdir" "$mergeddir"1074# this is executed in a subshell, so use temp files to pass extra data outside1075echo "$tempdir" >> /tmp/.overlayfs_wrapper_cleanup1076echo "$mergeddir" >> /tmp/.overlayfs_wrapper_umount1077echo "$mergeddir" >> /tmp/.overlayfs_wrapper_cleanup1078echo "$mergeddir"1079return1080fi1081if [[ $operation == cleanup ]]; then1082if [[ -f /tmp/.overlayfs_wrapper_umount ]]; then1083for dir in $(</tmp/.overlayfs_wrapper_umount); do1084[[ $dir == /tmp/* ]] && umount -l "$dir" > /dev/null 2>&11085done1086fi1087if [[ -f /tmp/.overlayfs_wrapper_cleanup ]]; then1088for dir in $(</tmp/.overlayfs_wrapper_cleanup); do1089[[ $dir == /tmp/* ]] && rm -rf "$dir"1090done1091fi1092rm -f /tmp/.overlayfs_wrapper_umount /tmp/.overlayfs_wrapper_cleanup1093fi1094}109510961097