Path: blob/next/scripts/compilation.sh
17683 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 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"506fi507508if [[ ${BOARDFAMILY} == cix ]]; then509[[ -d ${SRC}/output/cix ]] && rm -rf ${SRC}/output/cix510mkdir -p ${SRC}/output/cix/ > /dev/null 2>&1511cp ${kerneldir}/arch/arm64/boot/Image ${SRC}/output/cix/512cp ${kerneldir}/arch/arm64/boot/dts/cix/sky1-*dtb ${SRC}/output/cix/513fi514515# different packaging for 4.3+516if linux-version compare "${version}" ge 4.3; then517local kernel_packing="bindeb-pkg"518else519local kernel_packing="deb-pkg"520fi521522#if [[ $BRANCH == legacy && $LINUXFAMILY =~ sun50iw2|sun50iw6|sun50iw9 ]]; then523# make -C modules/gpu LICHEE_MOD_DIR=${SRC}/.tmp/gpu_modules_${LINUXFAMILY} LICHEE_KDIR=${kerneldir} CROSS_COMPILE=$toolchain/$KERNEL_COMPILER ARCH=$ARCHITECTURE524#fi525526display_alert "Creating packages"527528# produce deb packages: image, headers, firmware, dtb529echo -e "\n\t== deb packages: image, headers, firmware, dtb ==\n" >> "${DEST}"/${LOG_SUBPATH}/compilation.log530eval CCACHE_BASEDIR="$(pwd)" env PATH="${toolchain}:${PATH}" \531'make $CTHREADS $kernel_packing \532KDEB_PKGVERSION=$REVISION \533KDEB_COMPRESS=${DEB_COMPRESS} \534BRANCH=$BRANCH \535LOCALVERSION="-${LINUXFAMILY}" \536KBUILD_DEBARCH=$ARCH \537ARCH=$ARCHITECTURE \538DEBFULLNAME="$MAINTAINER" \539DEBEMAIL="$MAINTAINERMAIL" \540CROSS_COMPILE="$CCACHE $KERNEL_COMPILER" 2>>$DEST/${LOG_SUBPATH}/compilation.log' \541${PROGRESS_LOG_TO_FILE:+' | tee -a $DEST/${LOG_SUBPATH}/compilation.log'} \542${OUTPUT_DIALOG:+' | dialog --backtitle "$backtitle" --progressbox "Creating kernel packages..." $TTY_Y $TTY_X'} \543${OUTPUT_VERYSILENT:+' >/dev/null 2>/dev/null'}544545cd .. || exit546# remove firmare image packages here - easier than patching ~40 packaging scripts at once547rm -f linux-firmware-image-*.deb548549rsync --remove-source-files -rq ./*.deb "${DEB_STORAGE}/" || exit_with_error "Failed moving kernel DEBs"550551[[ $(type -t family_tweaks_kernel) == function ]] && family_tweaks_kernel552553# store git hash to the file and create a change log554#HASHTARGET="${EXTER}/cache/hash"$([[ ${BETA} == yes ]] && echo "-beta")"/linux-image-${BRANCH}-${LINUXFAMILY}"555#OLDHASHTARGET=$(head -1 "${HASHTARGET}.githash" 2>/dev/null)556557# check if OLDHASHTARGET commit exists otherwise use oldest558#if [[ -z ${KERNEL_VERSION_LEVEL} ]]; then559# git -C ${kerneldir} cat-file -t ${OLDHASHTARGET} >/dev/null 2>&1560# [[ $? -ne 0 ]] && OLDHASHTARGET=$(git -C ${kerneldir} show HEAD~199 --pretty=format:"%H" --no-patch)561# else562# git -C ${kerneldir} cat-file -t ${OLDHASHTARGET} >/dev/null 2>&1563# [[ $? -ne 0 ]] && OLDHASHTARGET=$(git -C ${kerneldir} rev-list --max-parents=0 HEAD)564#fi565566#[[ -z ${KERNELPATCHDIR} ]] && KERNELPATCHDIR=$LINUXFAMILY-$BRANCH567#[[ -z ${LINUXCONFIG} ]] && LINUXCONFIG=linux-$LINUXFAMILY-$BRANCH568569# calculate URL570#if [[ "$KERNELSOURCE" == *"github.com"* ]]; then571# URL="${KERNELSOURCE/git:/https:}/commit/${HASH}"572#elif [[ "$KERNELSOURCE" == *"kernel.org"* ]]; then573# URL="${KERNELSOURCE/git:/https:}/commit/?h=$(echo $KERNELBRANCH | cut -d":" -f2)&id=${HASH}"574#else575# URL="${KERNELSOURCE}/+/$HASH"576#fi577578# create change log579#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"580581#echo "${hash}" > "${HASHTARGET}.githash"582#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)583#hash_watch_2=$(cat "${EXTER}/config/kernel/${LINUXCONFIG}.config")584#echo "${hash_watch_1}${hash_watch_2}" | improved_git hash-object --stdin >> "${HASHTARGET}.githash"585586}587588589590591compile_firmware()592{593display_alert "Merging and packaging linux firmware" "@host" "info"594595local firmwaretempdir plugin_dir596597firmwaretempdir=$(mktemp -d)598chmod 700 ${firmwaretempdir}599trap "ret=\$?; rm -rf \"${firmwaretempdir}\" ; exit \$ret" 0 1 2 3 15600plugin_dir="orangepi-firmware${FULL}"601mkdir -p "${firmwaretempdir}/${plugin_dir}/lib/firmware"602603if [[ $GITEE_SERVER == yes ]]; then604[[ $IGNORE_UPDATES != yes ]] && fetch_from_repo "https://gitee.com/orangepi-xunlong/firmware" "${EXTER}/cache/sources/orangepi-firmware-git" "branch:master"605else606[[ $IGNORE_UPDATES != yes ]] && fetch_from_repo "https://github.com/orangepi-xunlong/firmware" "${EXTER}/cache/sources/orangepi-firmware-git" "branch:master"607fi608609if [[ -n $FULL ]]; then610[[ $IGNORE_UPDATES != yes ]] && fetch_from_repo "$MAINLINE_FIRMWARE_SOURCE" "${EXTER}/cache/sources/linux-firmware-git" "branch:master"611# cp : create hardlinks612cp -af --reflink=auto "${EXTER}"/cache/sources/linux-firmware-git/* "${firmwaretempdir}/${plugin_dir}/lib/firmware/"613fi614# overlay our firmware615# cp : create hardlinks616cp -af --reflink=auto "${EXTER}"/cache/sources/orangepi-firmware-git/* "${firmwaretempdir}/${plugin_dir}/lib/firmware/"617618# cleanup what's not needed for sure619rm -rf "${firmwaretempdir}/${plugin_dir}"/lib/firmware/{amdgpu,amd-ucode,radeon,nvidia,matrox,.git}620cd "${firmwaretempdir}/${plugin_dir}" || exit621622# set up control file623mkdir -p DEBIAN624cat <<-END > DEBIAN/control625Package: orangepi-firmware${FULL}626Version: $REVISION627Architecture: all628Maintainer: $MAINTAINER <$MAINTAINERMAIL>629Installed-Size: 1630Replaces: linux-firmware, firmware-brcm80211, firmware-ralink, firmware-samsung, firmware-realtek, orangepi-firmware${REPLACE}631Section: kernel632Priority: optional633Description: Linux firmware${FULL}634END635636cd "${firmwaretempdir}" || exit637# pack638mv "orangepi-firmware${FULL}" "orangepi-firmware${FULL}_${REVISION}_all"639display_alert "Building firmware package" "orangepi-firmware${FULL}_${REVISION}_all" "info"640fakeroot dpkg-deb -b -Z${DEB_COMPRESS} "orangepi-firmware${FULL}_${REVISION}_all" >> "${DEST}"/${LOG_SUBPATH}/install.log 2>&1641mv "orangepi-firmware${FULL}_${REVISION}_all" "orangepi-firmware${FULL}"642rsync -rq "orangepi-firmware${FULL}_${REVISION}_all.deb" "${DEB_STORAGE}/"643644# remove temp directory645rm -rf "${firmwaretempdir}"646}647648649650651compile_orangepi-zsh()652{653654local tmp_dir orangepi_zsh_dir655tmp_dir=$(mktemp -d)656chmod 700 ${tmp_dir}657trap "rm -rf \"${tmp_dir}\" ; exit 0" 0 1 2 3 15658orangepi_zsh_dir=orangepi-zsh_${REVISION}_all659display_alert "Building deb" "orangepi-zsh" "info"660661if [[ $GITEE_SERVER == yes ]]; then662[[ $IGNORE_UPDATES != yes ]] && fetch_from_repo "https://gitee.com/orangepi-xunlong/oh-my-zsh" "${EXTER}/cache/sources/oh-my-zsh" "branch:master"663[[ $IGNORE_UPDATES != yes ]] && fetch_from_repo "https://gitee.com/orangepi-xunlong/evalcache" "${EXTER}/cache/sources/evalcache" "branch:master"664else665[[ $IGNORE_UPDATES != yes ]] && fetch_from_repo "https://github.com/robbyrussell/oh-my-zsh" "${EXTER}/cache/sources/oh-my-zsh" "branch:master"666[[ $IGNORE_UPDATES != yes ]] && fetch_from_repo "https://github.com/mroth/evalcache" "${EXTER}/cache/sources/evalcache" "branch:master"667fi668669mkdir -p "${tmp_dir}/${orangepi_zsh_dir}"/{DEBIAN,etc/skel/,etc/oh-my-zsh/,/etc/skel/.oh-my-zsh/cache}670671# set up control file672cat <<-END > "${tmp_dir}/${orangepi_zsh_dir}"/DEBIAN/control673Package: orangepi-zsh674Version: $REVISION675Architecture: all676Maintainer: $MAINTAINER <$MAINTAINERMAIL>677Depends: zsh, tmux678Section: utils679Priority: optional680Description: Orange Pi improved ZShell681END682683# set up post install script684cat <<-END > "${tmp_dir}/${orangepi_zsh_dir}"/DEBIAN/postinst685#!/bin/sh686687# copy cache directory if not there yet688awk -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 {}'689awk -F'[:]' '{if (\$3 >= 1000 && \$3 != 65534 || \$3 == 0) print ""\$6"/.zshrc"}' /etc/passwd | xargs -i sh -c 'test ! -f {} && cp -R /etc/skel/.zshrc {}'690691# fix owner permissions in home directory692awk -F'[:]' '{if (\$3 >= 1000 && \$3 != 65534 || \$3 == 0) print ""\$1":"\$3" "\$6"/.oh-my-zsh"}' /etc/passwd | xargs -n2 chown -R693awk -F'[:]' '{if (\$3 >= 1000 && \$3 != 65534 || \$3 == 0) print ""\$1":"\$3" "\$6"/.zshrc"}' /etc/passwd | xargs -n2 chown -R694695# add support for bash profile696! grep emulate /etc/zsh/zprofile >/dev/null && echo "emulate sh -c 'source /etc/profile'" >> /etc/zsh/zprofile697exit 0698END699700cp -R "${EXTER}"/cache/sources/oh-my-zsh "${tmp_dir}/${orangepi_zsh_dir}"/etc/701cp -R "${EXTER}"/cache/sources/evalcache "${tmp_dir}/${orangepi_zsh_dir}"/etc/oh-my-zsh/plugins702cp "${tmp_dir}/${orangepi_zsh_dir}"/etc/oh-my-zsh/templates/zshrc.zsh-template "${tmp_dir}/${orangepi_zsh_dir}"/etc/skel/.zshrc703704chmod -R g-w,o-w "${tmp_dir}/${orangepi_zsh_dir}"/etc/oh-my-zsh/705706# we have common settings707sed -i "s/^export ZSH=.*/export ZSH=\/etc\/oh-my-zsh/" "${tmp_dir}/${orangepi_zsh_dir}"/etc/skel/.zshrc708709# user cache710sed -i "/^export ZSH=.*/a export ZSH_CACHE_DIR=~\/.oh-my-zsh\/cache" "${tmp_dir}/${orangepi_zsh_dir}"/etc/skel/.zshrc711712# define theme713sed -i 's/^ZSH_THEME=.*/ZSH_THEME="mrtazz"/' "${tmp_dir}/${orangepi_zsh_dir}"/etc/skel/.zshrc714715# disable prompt while update716sed -i 's/# DISABLE_UPDATE_PROMPT="true"/DISABLE_UPDATE_PROMPT="true"/g' "${tmp_dir}/${orangepi_zsh_dir}"/etc/skel/.zshrc717718# disable auto update since we provide update via package719sed -i 's/# DISABLE_AUTO_UPDATE="true"/DISABLE_AUTO_UPDATE="true"/g' "${tmp_dir}/${orangepi_zsh_dir}"/etc/skel/.zshrc720721# define default plugins722sed -i 's/^plugins=.*/plugins=(evalcache git git-extras debian tmux screen history extract colorize web-search docker)/' "${tmp_dir}/${orangepi_zsh_dir}"/etc/skel/.zshrc723724chmod 755 "${tmp_dir}/${orangepi_zsh_dir}"/DEBIAN/postinst725726fakeroot dpkg-deb -b -Z${DEB_COMPRESS} "${tmp_dir}/${orangepi_zsh_dir}" >> "${DEST}"/${LOG_SUBPATH}/output.log 2>&1727rsync --remove-source-files -rq "${tmp_dir}/${orangepi_zsh_dir}.deb" "${DEB_STORAGE}/"728rm -rf "${tmp_dir}"729730}731732733734compile_plymouth-theme-orangepi()735{736737local tmp_dir work_dir738tmp_dir=$(mktemp -d)739chmod 700 ${tmp_dir}740trap "ret=\$?; rm -rf \"${tmp_dir}\" ; exit \$ret" 0 1 2 3 15741plymouth_theme_orangepi_dir=orangepi-plymouth-theme_${REVISION}_all742display_alert "Building deb" "orangepi-plymouth-theme" "info"743744mkdir -p "${tmp_dir}/${plymouth_theme_orangepi_dir}"/{DEBIAN,usr/share/plymouth/themes/orangepi}745746# set up control file747cat <<- END > "${tmp_dir}/${plymouth_theme_orangepi_dir}"/DEBIAN/control748Package: orangepi-plymouth-theme749Version: $REVISION750Architecture: all751Maintainer: $MAINTAINER <$MAINTAINERMAIL>752Depends: plymouth, plymouth-themes753Section: universe/x11754Priority: optional755Description: boot animation, logger and I/O multiplexer - orangepi theme756END757758cp "${EXTER}"/packages/plymouth-theme-orangepi/debian/{postinst,prerm,postrm} \759"${tmp_dir}/${plymouth_theme_orangepi_dir}"/DEBIAN/760chmod 755 "${tmp_dir}/${plymouth_theme_orangepi_dir}"/DEBIAN/{postinst,prerm,postrm}761762#convert -resize 256x256 \763# "${EXTER}"/packages/plymouth-theme-orangepi/orangepi-logo.png \764# "${tmp_dir}/${plymouth_theme_orangepi_dir}"/usr/share/plymouth/themes/orangepi/bgrt-fallback.png765766# convert -resize 52x52 \767# "${EXTER}"/packages/plymouth-theme-orangepi/spinner.gif \768# "${tmp_dir}/${plymouth_theme_orangepi_dir}"/usr/share/plymouth/themes/orangepi/animation-%04d.png769770convert -resize 52x52 \771"${EXTER}"/packages/plymouth-theme-orangepi/spinner.gif \772"${tmp_dir}/${plymouth_theme_orangepi_dir}"/usr/share/plymouth/themes/orangepi/throbber-%04d.png773774cp "${EXTER}"/packages/plymouth-theme-orangepi/watermark.png \775"${tmp_dir}/${plymouth_theme_orangepi_dir}"/usr/share/plymouth/themes/orangepi/776777cp "${EXTER}"/packages/plymouth-theme-orangepi/{bullet,capslock,entry,keyboard,keymap-render,lock}.png \778"${tmp_dir}/${plymouth_theme_orangepi_dir}"/usr/share/plymouth/themes/orangepi/779780cp "${EXTER}"/packages/plymouth-theme-orangepi/orangepi.plymouth \781"${tmp_dir}/${plymouth_theme_orangepi_dir}"/usr/share/plymouth/themes/orangepi/782783fakeroot dpkg-deb -b -Z${DEB_COMPRESS} "${tmp_dir}/${plymouth_theme_orangepi_dir}" > /dev/null784rsync --remove-source-files -rq "${tmp_dir}/${plymouth_theme_orangepi_dir}.deb" "${DEB_STORAGE}/"785rm -rf "${tmp_dir}"786}787788compile_orangepi-config()789{790local tmpdir=${SRC}/.tmp/orangepi-config_${REVISION}_all791792display_alert "Building deb" "orangepi-config" "info"793794795mkdir -p "${tmpdir}"/{DEBIAN,usr/bin/,usr/sbin/,usr/lib/orangepi-config/}796797# set up control file798cat <<-END > "${tmpdir}"/DEBIAN/control799Package: orangepi-config800Version: $REVISION801Architecture: all802Maintainer: $MAINTAINER <$MAINTAINERMAIL>803Replaces: orangepi-bsp804Depends: bash, iperf3, psmisc, curl, bc, expect, dialog, pv, \805debconf-utils, unzip, build-essential, html2text, apt-transport-https, html2text, dirmngr, software-properties-common806Recommends: orangepi-bsp807Suggests: libpam-google-authenticator, qrencode, network-manager, sunxi-tools808Section: utils809Priority: optional810Description: Orange Pi configuration utility811END812813install -m 755 $EXTER/cache/sources/orangepi-config/scripts/tv_grab_file $tmpdir/usr/bin/tv_grab_file814install -m 755 $EXTER/cache/sources/orangepi-config/debian-config $tmpdir/usr/sbin/orangepi-config815install -m 644 $EXTER/cache/sources/orangepi-config/debian-config-jobs $tmpdir/usr/lib/orangepi-config/jobs.sh816install -m 644 $EXTER/cache/sources/orangepi-config/debian-config-submenu $tmpdir/usr/lib/orangepi-config/submenu.sh817install -m 644 $EXTER/cache/sources/orangepi-config/debian-config-functions $tmpdir/usr/lib/orangepi-config/functions.sh818install -m 644 $EXTER/cache/sources/orangepi-config/debian-config-functions-network $tmpdir/usr/lib/orangepi-config/functions-network.sh819install -m 755 $EXTER/cache/sources/orangepi-config/softy $tmpdir/usr/sbin/softy820# fallback to replace orangepi-config in BSP821ln -sf /usr/sbin/orangepi-config $tmpdir/usr/bin/orangepi-config822ln -sf /usr/sbin/softy "${tmpdir}"/usr/bin/softy823824fakeroot dpkg-deb -b -Z${DEB_COMPRESS} "${tmpdir}" >/dev/null825rsync --remove-source-files -rq "${tmpdir}.deb" "${DEB_STORAGE}/"826rm -rf "${tmpdir}"827}828829830831832compile_sunxi_tools()833{834# Compile and install only if git commit hash changed835cd $EXTER/cache/sources/sunxi-tools || exit836# need to check if /usr/local/bin/sunxi-fexc to detect new Docker containers with old cached sources837if [[ ! -f .commit_id || $(improved_git rev-parse @ 2>/dev/null) != $(<.commit_id) || ! -f /usr/local/bin/sunxi-fexc ]]; then838display_alert "Compiling" "sunxi-tools" "info"839make -s clean >/dev/null840make -s tools >/dev/null841mkdir -p /usr/local/bin/842make install-tools >/dev/null 2>&1843improved_git rev-parse @ 2>/dev/null > .commit_id844fi845}846847install_rkbin_tools()848{849# install only if git commit hash changed850cd "${EXTER}"/cache/sources/rkbin-tools || exit851# need to check if /usr/local/bin/sunxi-fexc to detect new Docker containers with old cached sources852if [[ ! -f .commit_id || $(improved_git rev-parse @ 2>/dev/null) != $(<.commit_id) || ! -f /usr/local/bin/loaderimage ]]; then853display_alert "Installing" "rkbin-tools" "info"854mkdir -p /usr/local/bin/855install -m 755 tools/loaderimage /usr/local/bin/856install -m 755 tools/trust_merger /usr/local/bin/857improved_git rev-parse @ 2>/dev/null > .commit_id858fi859}860861grab_version()862{863local ver=()864ver[0]=$(grep "^VERSION" "${1}"/Makefile | head -1 | awk '{print $(NF)}' | grep -oE '^[[:digit:]]+')865ver[1]=$(grep "^PATCHLEVEL" "${1}"/Makefile | head -1 | awk '{print $(NF)}' | grep -oE '^[[:digit:]]+')866ver[2]=$(grep "^SUBLEVEL" "${1}"/Makefile | head -1 | awk '{print $(NF)}' | grep -oE '^[[:digit:]]+')867ver[3]=$(grep "^EXTRAVERSION" "${1}"/Makefile | head -1 | awk '{print $(NF)}' | grep -oE '^-rc[[:digit:]]+')868echo "${ver[0]:-0}${ver[1]:+.${ver[1]}}${ver[2]:+.${ver[2]}}${ver[3]}"869}870871# find_toolchain <compiler_prefix> <expression>872#873# returns path to toolchain that satisfies <expression>874#875find_toolchain()876{877[[ "${SKIP_EXTERNAL_TOOLCHAINS}" == "yes" ]] && { echo "/usr/bin"; return; }878local compiler=$1879local expression=$2880local dist=10881local toolchain=""882# extract target major.minor version from expression883local target_ver884target_ver=$(grep -oE "[[:digit:]]+\.[[:digit:]]" <<< "$expression")885for dir in "${SRC}"/toolchains/*/; do886# check if is a toolchain for current $ARCH887[[ ! -f ${dir}bin/${compiler}gcc ]] && continue888# get toolchain major.minor version889local gcc_ver890gcc_ver=$("${dir}bin/${compiler}gcc" -dumpversion | grep -oE "^[[:digit:]]+\.[[:digit:]]")891# check if toolchain version satisfies requirement892awk "BEGIN{exit ! ($gcc_ver $expression)}" >/dev/null || continue893# check if found version is the closest to target894# may need different logic here with more than 1 digit minor version numbers895# numbers: 3.9 > 3.10; versions: 3.9 < 3.10896# dpkg --compare-versions can be used here if operators are changed897local d898d=$(awk '{x = $1 - $2}{printf "%.1f\n", (x > 0) ? x : -x}' <<< "$target_ver $gcc_ver")899if awk "BEGIN{exit ! ($d < $dist)}" >/dev/null ; then900dist=$d901toolchain=${dir}bin902fi903done904echo "$toolchain"905# logging a stack of used compilers.906if [[ -f "${DEST}"/${LOG_SUBPATH}/compiler.log ]]; then907if ! grep -q "$toolchain" "${DEST}"/${LOG_SUBPATH}/compiler.log; then908echo "$toolchain" >> "${DEST}"/${LOG_SUBPATH}/compiler.log;909fi910else911echo "$toolchain" >> "${DEST}"/${LOG_SUBPATH}/compiler.log;912fi913}914915# advanced_patch <dest> <family> <board> <target> <branch> <description>916#917# parameters:918# <dest>: u-boot, kernel, atf919# <family>: u-boot: u-boot; kernel: sunxi-next, ...920# <board>: orangepipcplus, orangepizero ...921# <target>: optional subdirectory922# <description>: additional description text923#924# priority:925# $USERPATCHES_PATH/<dest>/<family>/target_<target>926# $USERPATCHES_PATH/<dest>/<family>/board_<board>927# $USERPATCHES_PATH/<dest>/<family>/branch_<branch>928# $USERPATCHES_PATH/<dest>/<family>929# $EXTER/patch/<dest>/<family>/target_<target>930# $EXTER/patch/<dest>/<family>/board_<board>931# $EXTER/patch/<dest>/<family>/branch_<branch>932# $EXTER/patch/<dest>/<family>933#934advanced_patch()935{936local dest=$1937local family=$2938local board=$3939local target=$4940local branch=$5941local description=$6942943display_alert "Started patching process for" "$dest $description" "info"944display_alert "Looking for user patches in" "userpatches/$dest/$family" "info"945946local names=()947local dirs=(948"$USERPATCHES_PATH/$dest/$family/target_${target}:[\e[33mu\e[0m][\e[34mt\e[0m]"949"$USERPATCHES_PATH/$dest/$family/board_${board}:[\e[33mu\e[0m][\e[35mb\e[0m]"950"$USERPATCHES_PATH/$dest/$family/branch_${branch}:[\e[33mu\e[0m][\e[33mb\e[0m]"951"$USERPATCHES_PATH/$dest/$family:[\e[33mu\e[0m][\e[32mc\e[0m]"952"$EXTER/patch/$dest/$family/target_${target}:[\e[32ml\e[0m][\e[34mt\e[0m]"953"$EXTER/patch/$dest/$family/board_${board}:[\e[32ml\e[0m][\e[35mb\e[0m]"954"$EXTER/patch/$dest/$family/branch_${branch}:[\e[32ml\e[0m][\e[33mb\e[0m]"955"$EXTER/patch/$dest/$family:[\e[32ml\e[0m][\e[32mc\e[0m]"956)957local links=()958959# required for "for" command960shopt -s nullglob dotglob961# get patch file names962for dir in "${dirs[@]}"; do963for patch in ${dir%%:*}/*.patch; do964names+=($(basename "${patch}"))965done966# add linked patch directories967if [[ -d ${dir%%:*} ]]; then968local findlinks969findlinks=$(find "${dir%%:*}" -maxdepth 1 -type l -print0 2>&1 | xargs -0)970[[ -n $findlinks ]] && readarray -d '' links < <(find "${findlinks}" -maxdepth 1 -type f -follow -print -iname "*.patch" -print | grep "\.patch$" | sed "s|${dir%%:*}/||g" 2>&1)971fi972done973# merge static and linked974names=("${names[@]}" "${links[@]}")975# remove duplicates976local names_s=($(echo "${names[@]}" | tr ' ' '\n' | LC_ALL=C sort -u | tr '\n' ' '))977# apply patches978for name in "${names_s[@]}"; do979for dir in "${dirs[@]}"; do980if [[ -f ${dir%%:*}/$name ]]; then981if [[ -s ${dir%%:*}/$name ]]; then982process_patch_file "${dir%%:*}/$name" "${dir##*:}"983else984display_alert "* ${dir##*:} $name" "skipped"985fi986break # next name987fi988done989done990}991992# process_patch_file <file> <description>993#994# parameters:995# <file>: path to patch file996# <status>: additional status text997#998process_patch_file()999{1000local patch=$11001local status=$210021003# detect and remove files which patch will create1004lsdiff -s --strip=1 "${patch}" | grep '^+' | awk '{print $2}' | xargs -I % sh -c 'rm -f %'10051006echo "Processing file $patch" >> "${DEST}"/${LOG_SUBPATH}/patching.log1007patch --batch --silent -p1 -N < "${patch}" >> "${DEST}"/${LOG_SUBPATH}/patching.log 2>&110081009if [[ $? -ne 0 ]]; then1010display_alert "* $status $(basename "${patch}")" "failed" "wrn"1011[[ $EXIT_PATCHING_ERROR == yes ]] && exit_with_error "Aborting due to" "EXIT_PATCHING_ERROR"1012else1013display_alert "* $status $(basename "${patch}")" "" "info"1014fi1015echo >> "${DEST}"/${LOG_SUBPATH}/patching.log1016}10171018userpatch_create()1019{1020# create commit to start from clean source1021git add .1022git -c user.name='Orange Pi User' -c user.email='[email protected]' commit -q -m "Cleaning working copy"10231024local patch="$DEST/patch/$1-$LINUXFAMILY-$BRANCH.patch"10251026# apply previous user debug mode created patches1027if [[ -f $patch ]]; then1028display_alert "Applying existing $1 patch" "$patch" "wrn" && patch --batch --silent -p1 -N < "${patch}"1029# read title of a patch in case Git is configured1030if [[ -n $(git config user.email) ]]; then1031COMMIT_MESSAGE=$(cat "${patch}" | grep Subject | sed -n -e '0,/PATCH/s/.*PATCH]//p' | xargs)1032display_alert "Patch name extracted" "$COMMIT_MESSAGE" "wrn"1033fi1034fi10351036# prompt to alter source1037display_alert "Make your changes in this directory:" "$(pwd)" "wrn"1038display_alert "Press <Enter> after you are done" "waiting" "wrn"1039read -r </dev/tty1040tput cuu11041git add .1042# create patch out of changes1043if ! git diff-index --quiet --cached HEAD; then1044# If Git is configured, create proper patch and ask for a name1045if [[ -n $(git config user.email) ]]; then1046display_alert "Add / change patch name" "$COMMIT_MESSAGE" "wrn"1047read -e -p "Patch description: " -i "$COMMIT_MESSAGE" COMMIT_MESSAGE1048[[ -z "$COMMIT_MESSAGE" ]] && COMMIT_MESSAGE="Patching something"1049git commit -s -m "$COMMIT_MESSAGE"1050git format-patch -1 HEAD --stdout --signature="Created with orangepi build tools https://github.com/orangepi-xunlong/build" > "${patch}"1051PATCHFILE=$(git format-patch -1 HEAD)1052rm $PATCHFILE # delete the actual file1053# create a symlink to have a nice name ready1054find $DEST/patch/ -type l -delete # delete any existing1055ln -sf $patch $DEST/patch/$PATCHFILE1056else1057git diff --staged > "${patch}"1058fi1059display_alert "You will find your patch here:" "$patch" "info"1060else1061display_alert "No changes found, skipping patch creation" "" "wrn"1062fi1063git reset --soft HEAD~1064for i in {3..1..1}; do echo -n "$i." && sleep 1; done1065}10661067# overlayfs_wrapper <operation> <workdir> <description>1068#1069# <operation>: wrap|cleanup1070# <workdir>: path to source directory1071# <description>: suffix for merged directory to help locating it in /tmp1072# return value: new directory1073#1074# Assumptions/notes:1075# - Ubuntu Xenial host1076# - /tmp is mounted as tmpfs1077# - there is enough space on /tmp1078# - UB if running multiple compilation tasks in parallel1079# - should not be used with CREATE_PATCHES=yes1080#1081overlayfs_wrapper()1082{1083local operation="$1"1084if [[ $operation == wrap ]]; then1085local srcdir="$2"1086local description="$3"1087mkdir -p /tmp/overlay_components/ /tmp/orangepi_build/1088local tempdir workdir mergeddir1089tempdir=$(mktemp -d --tmpdir="/tmp/overlay_components/")1090workdir=$(mktemp -d --tmpdir="/tmp/overlay_components/")1091mergeddir=$(mktemp -d --suffix="_$description" --tmpdir="/tmp/orangepi_build/")1092mount -t overlay overlay -o lowerdir="$srcdir",upperdir="$tempdir",workdir="$workdir" "$mergeddir"1093# this is executed in a subshell, so use temp files to pass extra data outside1094echo "$tempdir" >> /tmp/.overlayfs_wrapper_cleanup1095echo "$mergeddir" >> /tmp/.overlayfs_wrapper_umount1096echo "$mergeddir" >> /tmp/.overlayfs_wrapper_cleanup1097echo "$mergeddir"1098return1099fi1100if [[ $operation == cleanup ]]; then1101if [[ -f /tmp/.overlayfs_wrapper_umount ]]; then1102for dir in $(</tmp/.overlayfs_wrapper_umount); do1103[[ $dir == /tmp/* ]] && umount -l "$dir" > /dev/null 2>&11104done1105fi1106if [[ -f /tmp/.overlayfs_wrapper_cleanup ]]; then1107for dir in $(</tmp/.overlayfs_wrapper_cleanup); do1108[[ $dir == /tmp/* ]] && rm -rf "$dir"1109done1110fi1111rm -f /tmp/.overlayfs_wrapper_umount /tmp/.overlayfs_wrapper_cleanup1112fi1113}111411151116