compilation.sh 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095
  1. #!/bin/bash
  2. #
  3. # Copyright (c) 2013-2021 Igor Pecovnik, igor.pecovnik@gma**.com
  4. #
  5. # This file is licensed under the terms of the GNU General Public
  6. # License version 2. This program is licensed "as is" without any
  7. # warranty of any kind, whether express or implied.
  8. # Functions:
  9. # compile_atf
  10. # compile_uboot
  11. # compile_kernel
  12. # compile_firmware
  13. # compile_orangepi-config
  14. # compile_sunxi_tools
  15. # install_rkbin_tools
  16. # grab_version
  17. # find_toolchain
  18. # advanced_patch
  19. # process_patch_file
  20. # userpatch_create
  21. # overlayfs_wrapper
  22. compile_atf()
  23. {
  24. if [[ $CLEAN_LEVEL == *make* ]]; then
  25. display_alert "Cleaning" "$ATFSOURCEDIR" "info"
  26. (cd "${EXTER}/cache/sources/${ATFSOURCEDIR}"; make distclean > /dev/null 2>&1)
  27. fi
  28. if [[ $USE_OVERLAYFS == yes ]]; then
  29. local atfdir
  30. atfdir=$(overlayfs_wrapper "wrap" "$EXTER/cache/sources/$ATFSOURCEDIR" "atf_${LINUXFAMILY}_${BRANCH}")
  31. else
  32. local atfdir="$EXTER/cache/sources/$ATFSOURCEDIR"
  33. fi
  34. cd "$atfdir" || exit
  35. display_alert "Compiling ATF" "" "info"
  36. # build aarch64
  37. if [[ $(dpkg --print-architecture) == amd64 ]]; then
  38. local toolchain
  39. toolchain=$(find_toolchain "$ATF_COMPILER" "$ATF_USE_GCC")
  40. [[ -z $toolchain ]] && exit_with_error "Could not find required toolchain" "${ATF_COMPILER}gcc $ATF_USE_GCC"
  41. if [[ -n $ATF_TOOLCHAIN2 ]]; then
  42. local toolchain2_type toolchain2_ver toolchain2
  43. toolchain2_type=$(cut -d':' -f1 <<< "${ATF_TOOLCHAIN2}")
  44. toolchain2_ver=$(cut -d':' -f2 <<< "${ATF_TOOLCHAIN2}")
  45. toolchain2=$(find_toolchain "$toolchain2_type" "$toolchain2_ver")
  46. [[ -z $toolchain2 ]] && exit_with_error "Could not find required toolchain" "${toolchain2_type}gcc $toolchain2_ver"
  47. fi
  48. # build aarch64
  49. fi
  50. display_alert "Compiler version" "${ATF_COMPILER}gcc $(eval env PATH="${toolchain}:${PATH}" "${ATF_COMPILER}gcc" -dumpversion)" "info"
  51. local target_make target_patchdir target_files
  52. target_make=$(cut -d';' -f1 <<< "${ATF_TARGET_MAP}")
  53. target_patchdir=$(cut -d';' -f2 <<< "${ATF_TARGET_MAP}")
  54. target_files=$(cut -d';' -f3 <<< "${ATF_TARGET_MAP}")
  55. advanced_patch "atf" "${ATFPATCHDIR}" "$BOARD" "$target_patchdir" "$BRANCH" "${LINUXFAMILY}-${BOARD}-${BRANCH}"
  56. # create patch for manual source changes
  57. [[ $CREATE_PATCHES == yes ]] && userpatch_create "atf"
  58. echo -e "\n\t== atf ==\n" >> "${DEST}"/${LOG_SUBPATH}/compilation.log
  59. # ENABLE_BACKTRACE="0" has been added to workaround a regression in ATF.
  60. # Check: https://github.com/armbian/build/issues/1157
  61. eval CCACHE_BASEDIR="$(pwd)" env PATH="${toolchain}:${toolchain2}:${PATH}" \
  62. 'make ENABLE_BACKTRACE="0" $target_make $CTHREADS \
  63. CROSS_COMPILE="$CCACHE $ATF_COMPILER"' 2>> "${DEST}"/${LOG_SUBPATH}/compilation.log \
  64. ${PROGRESS_LOG_TO_FILE:+' | tee -a $DEST/${LOG_SUBPATH}/compilation.log'} \
  65. ${OUTPUT_DIALOG:+' | dialog --backtitle "$backtitle" --progressbox "Compiling ATF..." $TTY_Y $TTY_X'} \
  66. ${OUTPUT_VERYSILENT:+' >/dev/null 2>/dev/null'}
  67. [[ ${PIPESTATUS[0]} -ne 0 ]] && exit_with_error "ATF compilation failed"
  68. [[ $(type -t atf_custom_postprocess) == function ]] && atf_custom_postprocess
  69. atftempdir=$(mktemp -d)
  70. chmod 700 ${atftempdir}
  71. trap "rm -rf \"${atftempdir}\" ; exit 0" 0 1 2 3 15
  72. # copy files to temp directory
  73. for f in $target_files; do
  74. local f_src
  75. f_src=$(cut -d':' -f1 <<< "${f}")
  76. if [[ $f == *:* ]]; then
  77. local f_dst
  78. f_dst=$(cut -d':' -f2 <<< "${f}")
  79. else
  80. local f_dst
  81. f_dst=$(basename "${f_src}")
  82. fi
  83. [[ ! -f $f_src ]] && exit_with_error "ATF file not found" "$(basename "${f_src}")"
  84. cp "${f_src}" "${atftempdir}/${f_dst}"
  85. done
  86. # copy license file to pack it to u-boot package later
  87. [[ -f license.md ]] && cp license.md "${atftempdir}"/
  88. }
  89. compile_uboot()
  90. {
  91. if [[ ${BOARDFAMILY} == "sun50iw9" && ${BRANCH} =~ legacy|current && $(dpkg --print-architecture) == arm64 ]]; then
  92. local uboot_name=${CHOSEN_UBOOT}_${REVISION}_${ARCH}.deb
  93. display_alert "Compile u-boot is not supported, only copy precompiled deb package" "$uboot_name" "info"
  94. cp "${EXTER}/cache/debs/h618/$uboot_name" "${DEB_STORAGE}/u-boot/"
  95. else
  96. # not optimal, but extra cleaning before overlayfs_wrapper should keep sources directory clean
  97. if [[ $CLEAN_LEVEL == *make* ]]; then
  98. display_alert "Cleaning" "$BOOTSOURCEDIR" "info"
  99. (cd $BOOTSOURCEDIR; make clean > /dev/null 2>&1)
  100. fi
  101. if [[ $USE_OVERLAYFS == yes ]]; then
  102. local ubootdir
  103. ubootdir=$(overlayfs_wrapper "wrap" "$BOOTSOURCEDIR" "u-boot_${LINUXFAMILY}_${BRANCH}")
  104. else
  105. local ubootdir="$BOOTSOURCEDIR"
  106. fi
  107. cd "${ubootdir}" || exit
  108. # read uboot version
  109. local version hash
  110. version=$(grab_version "$ubootdir")
  111. hash=$(improved_git --git-dir="$ubootdir"/.git rev-parse HEAD)
  112. display_alert "Compiling u-boot" "v$version" "info"
  113. # build aarch64
  114. if [[ $(dpkg --print-architecture) == amd64 ]]; then
  115. local toolchain
  116. toolchain=$(find_toolchain "$UBOOT_COMPILER" "$UBOOT_USE_GCC")
  117. [[ -z $toolchain ]] && exit_with_error "Could not find required toolchain" "${UBOOT_COMPILER}gcc $UBOOT_USE_GCC"
  118. if [[ -n $UBOOT_TOOLCHAIN2 ]]; then
  119. local toolchain2_type toolchain2_ver toolchain2
  120. toolchain2_type=$(cut -d':' -f1 <<< "${UBOOT_TOOLCHAIN2}")
  121. toolchain2_ver=$(cut -d':' -f2 <<< "${UBOOT_TOOLCHAIN2}")
  122. toolchain2=$(find_toolchain "$toolchain2_type" "$toolchain2_ver")
  123. [[ -z $toolchain2 ]] && exit_with_error "Could not find required toolchain" "${toolchain2_type}gcc $toolchain2_ver"
  124. fi
  125. # build aarch64
  126. fi
  127. display_alert "Compiler version" "${UBOOT_COMPILER}gcc $(eval env PATH="${toolchain}:${toolchain2}:${PATH}" "${UBOOT_COMPILER}gcc" -dumpversion)" "info"
  128. [[ -n $toolchain2 ]] && display_alert "Additional compiler version" "${toolchain2_type}gcc $(eval env PATH="${toolchain}:${toolchain2}:${PATH}" "${toolchain2_type}gcc" -dumpversion)" "info"
  129. # create directory structure for the .deb package
  130. uboottempdir=$(mktemp -d)
  131. chmod 700 ${uboottempdir}
  132. trap "ret=\$?; rm -rf \"${uboottempdir}\" ; exit \$ret" 0 1 2 3 15
  133. local uboot_name=${CHOSEN_UBOOT}_${REVISION}_${ARCH}
  134. rm -rf $uboottempdir/$uboot_name
  135. mkdir -p $uboottempdir/$uboot_name/usr/lib/{u-boot,$uboot_name} $uboottempdir/$uboot_name/DEBIAN
  136. # process compilation for one or multiple targets
  137. while read -r target; do
  138. local target_make target_patchdir target_files
  139. target_make=$(cut -d';' -f1 <<< "${target}")
  140. target_patchdir=$(cut -d';' -f2 <<< "${target}")
  141. target_files=$(cut -d';' -f3 <<< "${target}")
  142. # needed for multiple targets and for calling compile_uboot directly
  143. #display_alert "Checking out to clean sources"
  144. #improved_git checkout -f -q HEAD
  145. if [[ $CLEAN_LEVEL == *make* ]]; then
  146. display_alert "Cleaning" "$BOOTSOURCEDIR" "info"
  147. (cd "${BOOTSOURCEDIR}"; make clean > /dev/null 2>&1)
  148. fi
  149. advanced_patch "u-boot" "$BOOTPATCHDIR" "$BOARD" "$target_patchdir" "$BRANCH" "${LINUXFAMILY}-${BOARD}-${BRANCH}"
  150. # create patch for manual source changes
  151. [[ $CREATE_PATCHES == yes ]] && userpatch_create "u-boot"
  152. if [[ -n $ATFSOURCE ]]; then
  153. cp -Rv "${atftempdir}"/*.bin .
  154. rm -rf "${atftempdir}"
  155. fi
  156. echo -e "\n\t== u-boot make $BOOTCONFIG ==\n" >> "${DEST}"/${LOG_SUBPATH}/compilation.log
  157. eval CCACHE_BASEDIR="$(pwd)" env PATH="${toolchain}:${toolchain2}:${PATH}" \
  158. 'make $CTHREADS $BOOTCONFIG \
  159. CROSS_COMPILE="$CCACHE $UBOOT_COMPILER"' 2>> "${DEST}"/${LOG_SUBPATH}/compilation.log \
  160. ${PROGRESS_LOG_TO_FILE:+' | tee -a $DEST/${LOG_SUBPATH}/compilation.log'} \
  161. ${OUTPUT_VERYSILENT:+' >/dev/null 2>/dev/null'}
  162. if [[ "${version}" != 2014.07 ]]; then
  163. # orangepi specifics u-boot settings
  164. [[ -f .config ]] && sed -i 's/CONFIG_LOCALVERSION=""/CONFIG_LOCALVERSION="-orangepi"/g' .config
  165. [[ -f .config ]] && sed -i 's/CONFIG_LOCALVERSION_AUTO=.*/# CONFIG_LOCALVERSION_AUTO is not set/g' .config
  166. # for modern kernel and non spi targets
  167. if [[ ${BOOTBRANCH} =~ ^tag:v201[8-9](.*) && ${target} != "spi" && -f .config ]]; then
  168. sed -i 's/^.*CONFIG_ENV_IS_IN_FAT.*/# CONFIG_ENV_IS_IN_FAT is not set/g' .config
  169. sed -i 's/^.*CONFIG_ENV_IS_IN_EXT4.*/CONFIG_ENV_IS_IN_EXT4=y/g' .config
  170. sed -i 's/^.*CONFIG_ENV_IS_IN_MMC.*/# CONFIG_ENV_IS_IN_MMC is not set/g' .config
  171. sed -i 's/^.*CONFIG_ENV_IS_NOWHERE.*/# CONFIG_ENV_IS_NOWHERE is not set/g' .config | echo \
  172. "# CONFIG_ENV_IS_NOWHERE is not set" >> .config
  173. echo 'CONFIG_ENV_EXT4_INTERFACE="mmc"' >> .config
  174. echo 'CONFIG_ENV_EXT4_DEVICE_AND_PART="0:auto"' >> .config
  175. echo 'CONFIG_ENV_EXT4_FILE="/boot/boot.env"' >> .config
  176. fi
  177. [[ -f tools/logos/udoo.bmp ]] && cp "${EXTER}"/packages/blobs/splash/udoo.bmp tools/logos/udoo.bmp
  178. touch .scmversion
  179. # $BOOTDELAY can be set in board family config, ensure autoboot can be stopped even if set to 0
  180. [[ $BOOTDELAY == 0 ]] && echo -e "CONFIG_ZERO_BOOTDELAY_CHECK=y" >> .config
  181. [[ -n $BOOTDELAY ]] && sed -i "s/^CONFIG_BOOTDELAY=.*/CONFIG_BOOTDELAY=${BOOTDELAY}/" .config || [[ -f .config ]] && echo "CONFIG_BOOTDELAY=${BOOTDELAY}" >> .config
  182. fi
  183. # workaround when two compilers are needed
  184. cross_compile="CROSS_COMPILE=$CCACHE $UBOOT_COMPILER";
  185. [[ -n $UBOOT_TOOLCHAIN2 ]] && cross_compile="ORANGEPI=foe"; # empty parameter is not allowed
  186. echo -e "\n\t== u-boot make $target_make ==\n" >> "${DEST}"/${LOG_SUBPATH}/compilation.log
  187. eval CCACHE_BASEDIR="$(pwd)" env PATH="${toolchain}:${toolchain2}:${PATH}" \
  188. 'make $target_make $CTHREADS \
  189. "${cross_compile}"' 2>>"${DEST}"/${LOG_SUBPATH}/compilation.log \
  190. ${PROGRESS_LOG_TO_FILE:+' | tee -a "${DEST}"/${LOG_SUBPATH}/compilation.log'} \
  191. ${OUTPUT_DIALOG:+' | dialog --backtitle "$backtitle" --progressbox "Compiling u-boot..." $TTY_Y $TTY_X'} \
  192. ${OUTPUT_VERYSILENT:+' >/dev/null 2>/dev/null'} ';EVALPIPE=(${PIPESTATUS[@]})'
  193. [[ ${EVALPIPE[0]} -ne 0 ]] && exit_with_error "U-boot compilation failed"
  194. [[ $(type -t uboot_custom_postprocess) == function ]] && uboot_custom_postprocess
  195. # copy files to build directory
  196. for f in $target_files; do
  197. local f_src
  198. f_src=$(cut -d':' -f1 <<< "${f}")
  199. if [[ $f == *:* ]]; then
  200. local f_dst
  201. f_dst=$(cut -d':' -f2 <<< "${f}")
  202. else
  203. local f_dst
  204. f_dst=$(basename "${f_src}")
  205. fi
  206. [[ ! -f $f_src ]] && exit_with_error "U-boot file not found" "$(basename "${f_src}")"
  207. if [[ "${version}" =~ 2014.07|2011.09 ]]; then
  208. cp "${f_src}" "$uboottempdir/packout/${f_dst}"
  209. else
  210. cp "${f_src}" "$uboottempdir/${uboot_name}/usr/lib/${uboot_name}/${f_dst}"
  211. fi
  212. done
  213. done <<< "$UBOOT_TARGET_MAP"
  214. if [[ $PACK_UBOOT == "yes" ]];then
  215. if [[ $BOARDFAMILY =~ sun50iw1 ]]; then
  216. if [[ $(type -t u-boot_tweaks) == function ]]; then
  217. u-boot_tweaks ${uboot_name}
  218. else
  219. exit_with_error "U-boot pack failed"
  220. fi
  221. else
  222. pack_uboot
  223. cp $uboottempdir/packout/{boot0_sdcard.fex,boot_package.fex} "${SRC}/.tmp/${uboot_name}/usr/lib/${uboot_name}/"
  224. cp $uboottempdir/packout/dts/${BOARD}-u-boot.dts "${SRC}/.tmp/${uboot_name}/usr/lib/u-boot/"
  225. fi
  226. cd "${ubootdir}" || exit
  227. fi
  228. # declare -f on non-defined function does not do anything
  229. cat <<-EOF > "$uboottempdir/${uboot_name}/usr/lib/u-boot/platform_install.sh"
  230. DIR=/usr/lib/$uboot_name
  231. $(declare -f write_uboot_platform)
  232. $(declare -f write_uboot_platform_mtd)
  233. $(declare -f setup_write_uboot_platform)
  234. EOF
  235. # set up control file
  236. cat <<-EOF > "$uboottempdir/${uboot_name}/DEBIAN/control"
  237. Package: linux-u-boot-${BOARD}-${BRANCH}
  238. Version: $REVISION
  239. Architecture: $ARCH
  240. Maintainer: $MAINTAINER <$MAINTAINERMAIL>
  241. Installed-Size: 1
  242. Section: kernel
  243. Priority: optional
  244. Provides: orangepi-u-boot
  245. Replaces: orangepi-u-boot
  246. Conflicts: orangepi-u-boot, u-boot-sunxi
  247. Description: Uboot loader $version
  248. EOF
  249. # copy config file to the package
  250. # useful for FEL boot with overlayfs_wrapper
  251. [[ -f .config && -n $BOOTCONFIG ]] && cp .config "$uboottempdir/${uboot_name}/usr/lib/u-boot/${BOOTCONFIG}"
  252. # copy license files from typical locations
  253. [[ -f COPYING ]] && cp COPYING "$uboottempdir/${uboot_name}/usr/lib/u-boot/LICENSE"
  254. [[ -f Licenses/README ]] && cp Licenses/README "$uboottempdir/${uboot_name}/usr/lib/u-boot/LICENSE"
  255. [[ -n $atftempdir && -f $atftempdir/license.md ]] && cp "${atftempdir}/license.md" "$uboottempdir/${uboot_name}/usr/lib/u-boot/LICENSE.atf"
  256. display_alert "Building deb" "${uboot_name}.deb" "info"
  257. fakeroot dpkg-deb -b -Z${DEB_COMPRESS} "$uboottempdir/${uboot_name}" "$uboottempdir/${uboot_name}.deb" >> "${DEST}"/${LOG_SUBPATH}/output.log 2>&1
  258. rm -rf "$uboottempdir/${uboot_name}"
  259. [[ -n $atftempdir ]] && rm -rf "${atftempdir}"
  260. [[ ! -f $uboottempdir/${uboot_name}.deb ]] && exit_with_error "Building u-boot package failed"
  261. rsync --remove-source-files -rq "$uboottempdir/${uboot_name}.deb" "${DEB_STORAGE}/u-boot/"
  262. rm -rf "$uboottempdir"
  263. fi
  264. }
  265. create_linux-source_package ()
  266. {
  267. ts=$(date +%s)
  268. local sources_pkg_dir tmp_src_dir
  269. tmp_src_dir=$(mktemp -d)
  270. trap "ret=\$?; rm -rf \"${tmp_src_dir}\" ; exit \$ret" 0 1 2 3 15
  271. sources_pkg_dir=${tmp_src_dir}/${CHOSEN_KSRC}_${REVISION}_all
  272. mkdir -p "${sources_pkg_dir}"/usr/src/ \
  273. "${sources_pkg_dir}"/usr/share/doc/linux-source-${version}-${LINUXFAMILY} \
  274. "${sources_pkg_dir}"/DEBIAN
  275. cp "${EXTER}/config/kernel/${LINUXCONFIG}.config" "default_${LINUXCONFIG}.config"
  276. xz < .config > "${sources_pkg_dir}/usr/src/${LINUXCONFIG}_${version}_${REVISION}_config.xz"
  277. display_alert "Compressing sources for the linux-source package"
  278. tar cp --directory="$kerneldir" --exclude='.git' --owner=root . \
  279. | pv -p -b -r -s "$(du -sb "$kerneldir" --exclude=='.git' | cut -f1)" \
  280. | pixz -4 > "${sources_pkg_dir}/usr/src/linux-source-${version}-${LINUXFAMILY}.tar.xz"
  281. cp COPYING "${sources_pkg_dir}/usr/share/doc/linux-source-${version}-${LINUXFAMILY}/LICENSE"
  282. cat <<-EOF > "${sources_pkg_dir}"/DEBIAN/control
  283. Package: linux-source-${version}-${BRANCH}-${LINUXFAMILY}
  284. Version: ${version}-${BRANCH}-${LINUXFAMILY}+${REVISION}
  285. Architecture: all
  286. Maintainer: $MAINTAINER <$MAINTAINERMAIL>
  287. Section: kernel
  288. Priority: optional
  289. Depends: binutils, coreutils
  290. Provides: linux-source, linux-source-${version}-${LINUXFAMILY}
  291. Recommends: gcc, make
  292. Description: This package provides the source code for the Linux kernel $version
  293. EOF
  294. fakeroot dpkg-deb -b -Z${DEB_COMPRESS} -z0 "${sources_pkg_dir}" "${sources_pkg_dir}.deb"
  295. rsync --remove-source-files -rq "${sources_pkg_dir}.deb" "${DEB_STORAGE}/"
  296. te=$(date +%s)
  297. display_alert "Make the linux-source package" "$(($te - $ts)) sec." "info"
  298. rm -rf "${tmp_src_dir}"
  299. }
  300. compile_kernel()
  301. {
  302. if [[ $CLEAN_LEVEL == *make* ]]; then
  303. display_alert "Cleaning" "$LINUXSOURCEDIR" "info"
  304. (cd ${LINUXSOURCEDIR}; make ARCH="${ARCHITECTURE}" clean >/dev/null 2>&1)
  305. fi
  306. if [[ $USE_OVERLAYFS == yes ]]; then
  307. local kerneldir
  308. kerneldir=$(overlayfs_wrapper "wrap" "$LINUXSOURCEDIR" "kernel_${LINUXFAMILY}_${BRANCH}")
  309. else
  310. local kerneldir="$LINUXSOURCEDIR"
  311. fi
  312. cd "${kerneldir}" || exit
  313. rm -f localversion
  314. # read kernel version
  315. local version hash
  316. version=$(grab_version "$kerneldir")
  317. # read kernel git hash
  318. hash=$(improved_git --git-dir="$kerneldir"/.git rev-parse HEAD)
  319. # Apply a series of patches if a series file exists
  320. if test -f "${EXTER}"/patch/kernel/${KERNELPATCHDIR}/series.conf; then
  321. display_alert "series.conf file visible. Apply"
  322. series_conf="${SRC}"/patch/kernel/${KERNELPATCHDIR}/series.conf
  323. # apply_patch_series <target dir> <full path to series file>
  324. apply_patch_series "${kerneldir}" "$series_conf"
  325. fi
  326. # build 3rd party drivers
  327. # compilation_prepare
  328. advanced_patch "kernel" "$KERNELPATCHDIR" "$BOARD" "" "$BRANCH" "$LINUXFAMILY-$BRANCH"
  329. # create patch for manual source changes in debug mode
  330. [[ $CREATE_PATCHES == yes ]] && userpatch_create "kernel"
  331. # re-read kernel version after patching
  332. local version
  333. version=$(grab_version "$kerneldir")
  334. display_alert "Compiling $BRANCH kernel" "$version" "info"
  335. # compare with the architecture of the current Debian node
  336. # if it matches we use the system compiler
  337. if $(dpkg-architecture -e "${ARCH}"); then
  338. display_alert "Native compilation"
  339. elif [[ $(dpkg --print-architecture) == amd64 ]]; then
  340. local toolchain
  341. toolchain=$(find_toolchain "$KERNEL_COMPILER" "$KERNEL_USE_GCC")
  342. [[ -z $toolchain ]] && exit_with_error "Could not find required toolchain" "${KERNEL_COMPILER}gcc $KERNEL_USE_GCC"
  343. else
  344. exit_with_error "Architecture [$ARCH] is not supported"
  345. fi
  346. display_alert "Compiler version" "${KERNEL_COMPILER}gcc $(eval env PATH="${toolchain}:${PATH}" "${KERNEL_COMPILER}gcc" -dumpversion)" "info"
  347. # copy kernel config
  348. if [[ $KERNEL_KEEP_CONFIG == yes && -f "${DEST}"/config/$LINUXCONFIG.config ]]; then
  349. display_alert "Using previous kernel config" "${DEST}/config/$LINUXCONFIG.config" "info"
  350. cp -p "${DEST}/config/${LINUXCONFIG}.config" .config
  351. else
  352. if [[ -f $USERPATCHES_PATH/$LINUXCONFIG.config ]]; then
  353. display_alert "Using kernel config provided by user" "userpatches/$LINUXCONFIG.config" "info"
  354. cp -p "${USERPATCHES_PATH}/${LINUXCONFIG}.config" .config
  355. else
  356. display_alert "Using kernel config file" "${EXTER}/config/kernel/$LINUXCONFIG.config" "info"
  357. cp -p "${EXTER}/config/kernel/${LINUXCONFIG}.config" .config
  358. fi
  359. fi
  360. call_extension_method "custom_kernel_config" << 'CUSTOM_KERNEL_CONFIG'
  361. *Kernel .config is in place, still clean from git version*
  362. Called after ${LINUXCONFIG}.config is put in place (.config).
  363. Before any olddefconfig any Kconfig make is called.
  364. A good place to customize the .config directly.
  365. CUSTOM_KERNEL_CONFIG
  366. # hack for deb builder. To pack what's missing in headers pack.
  367. cp "$EXTER"/patch/misc/headers-debian-byteshift.patch /tmp
  368. if [[ $KERNEL_CONFIGURE != yes ]]; then
  369. if [[ $BRANCH == legacy && ! $BOARDFAMILY =~ "rockchip-rk3588"|"rockchip-rk356x" ]]; then
  370. eval CCACHE_BASEDIR="$(pwd)" env PATH="${toolchain}:${PATH}" \
  371. 'make ARCH=$ARCHITECTURE CROSS_COMPILE="$CCACHE $KERNEL_COMPILER" silentoldconfig'
  372. else
  373. # TODO: check if required
  374. eval CCACHE_BASEDIR="$(pwd)" env PATH="${toolchain}:${PATH}" \
  375. 'make ARCH=$ARCHITECTURE CROSS_COMPILE="$CCACHE $KERNEL_COMPILER" olddefconfig'
  376. fi
  377. else
  378. eval CCACHE_BASEDIR="$(pwd)" env PATH="${toolchain}:${PATH}" \
  379. 'make $CTHREADS ARCH=$ARCHITECTURE CROSS_COMPILE="$CCACHE $KERNEL_COMPILER" oldconfig'
  380. eval CCACHE_BASEDIR="$(pwd)" env PATH="${toolchain}:${PATH}" \
  381. 'make $CTHREADS ARCH=$ARCHITECTURE CROSS_COMPILE="$CCACHE $KERNEL_COMPILER" ${KERNEL_MENUCONFIG:-menuconfig}'
  382. [[ ${PIPESTATUS[0]} -ne 0 ]] && exit_with_error "Error kernel menuconfig failed"
  383. # store kernel config in easily reachable place
  384. display_alert "Exporting new kernel config" "$DEST/config/$LINUXCONFIG.config" "info"
  385. cp .config "${DEST}/config/${LINUXCONFIG}.config"
  386. cp .config "${EXTER}/config/kernel/${LINUXCONFIG}.config"
  387. # export defconfig too if requested
  388. if [[ $KERNEL_EXPORT_DEFCONFIG == yes ]]; then
  389. eval CCACHE_BASEDIR="$(pwd)" env PATH="${toolchain}:${PATH}" \
  390. 'make ARCH=$ARCHITECTURE CROSS_COMPILE="$CCACHE $KERNEL_COMPILER" savedefconfig'
  391. [[ -f defconfig ]] && cp defconfig "${DEST}/config/${LINUXCONFIG}.defconfig"
  392. fi
  393. fi
  394. # create linux-source package - with already patched sources
  395. # We will build this package first and clear the memory.
  396. if [[ $BUILD_KSRC != no ]]; then
  397. create_linux-source_package
  398. fi
  399. echo -e "\n\t== kernel ==\n" >> "${DEST}"/${LOG_SUBPATH}/compilation.log
  400. eval CCACHE_BASEDIR="$(pwd)" env PATH="${toolchain}:${PATH}" \
  401. 'make $CTHREADS ARCH=$ARCHITECTURE \
  402. CROSS_COMPILE="$CCACHE $KERNEL_COMPILER" \
  403. $SRC_LOADADDR \
  404. LOCALVERSION="-$LINUXFAMILY" \
  405. $KERNEL_IMAGE_TYPE ${KERNEL_EXTRA_TARGETS:-modules dtbs} 2>>$DEST/${LOG_SUBPATH}/compilation.log' \
  406. ${PROGRESS_LOG_TO_FILE:+' | tee -a $DEST/${LOG_SUBPATH}/compilation.log'} \
  407. ${OUTPUT_DIALOG:+' | dialog --backtitle "$backtitle" \
  408. --progressbox "Compiling kernel..." $TTY_Y $TTY_X'} \
  409. ${OUTPUT_VERYSILENT:+' >/dev/null 2>/dev/null'}
  410. if [[ ${PIPESTATUS[0]} -ne 0 || ! -f arch/$ARCHITECTURE/boot/$KERNEL_IMAGE_TYPE ]]; then
  411. grep -i error $DEST/${LOG_SUBPATH}/compilation.log
  412. exit_with_error "Kernel was not built" "@host"
  413. fi
  414. # different packaging for 4.3+
  415. if linux-version compare "${version}" ge 4.3; then
  416. local kernel_packing="bindeb-pkg"
  417. else
  418. local kernel_packing="deb-pkg"
  419. fi
  420. #if [[ $BRANCH == legacy && $LINUXFAMILY =~ sun50iw2|sun50iw6|sun50iw9 ]]; then
  421. # make -C modules/gpu LICHEE_MOD_DIR=${SRC}/.tmp/gpu_modules_${LINUXFAMILY} LICHEE_KDIR=${kerneldir} CROSS_COMPILE=$toolchain/$KERNEL_COMPILER ARCH=$ARCHITECTURE
  422. #fi
  423. display_alert "Creating packages"
  424. # produce deb packages: image, headers, firmware, dtb
  425. echo -e "\n\t== deb packages: image, headers, firmware, dtb ==\n" >> "${DEST}"/${LOG_SUBPATH}/compilation.log
  426. eval CCACHE_BASEDIR="$(pwd)" env PATH="${toolchain}:${PATH}" \
  427. 'make $CTHREADS $kernel_packing \
  428. KDEB_PKGVERSION=$REVISION \
  429. KDEB_COMPRESS=${DEB_COMPRESS} \
  430. BRANCH=$BRANCH \
  431. LOCALVERSION="-${LINUXFAMILY}" \
  432. KBUILD_DEBARCH=$ARCH \
  433. ARCH=$ARCHITECTURE \
  434. DEBFULLNAME="$MAINTAINER" \
  435. DEBEMAIL="$MAINTAINERMAIL" \
  436. CROSS_COMPILE="$CCACHE $KERNEL_COMPILER" 2>>$DEST/${LOG_SUBPATH}/compilation.log' \
  437. ${PROGRESS_LOG_TO_FILE:+' | tee -a $DEST/${LOG_SUBPATH}/compilation.log'} \
  438. ${OUTPUT_DIALOG:+' | dialog --backtitle "$backtitle" --progressbox "Creating kernel packages..." $TTY_Y $TTY_X'} \
  439. ${OUTPUT_VERYSILENT:+' >/dev/null 2>/dev/null'}
  440. cd .. || exit
  441. # remove firmare image packages here - easier than patching ~40 packaging scripts at once
  442. rm -f linux-firmware-image-*.deb
  443. rsync --remove-source-files -rq ./*.deb "${DEB_STORAGE}/" || exit_with_error "Failed moving kernel DEBs"
  444. # store git hash to the file and create a change log
  445. #HASHTARGET="${EXTER}/cache/hash"$([[ ${BETA} == yes ]] && echo "-beta")"/linux-image-${BRANCH}-${LINUXFAMILY}"
  446. #OLDHASHTARGET=$(head -1 "${HASHTARGET}.githash" 2>/dev/null)
  447. # check if OLDHASHTARGET commit exists otherwise use oldest
  448. #if [[ -z ${KERNEL_VERSION_LEVEL} ]]; then
  449. # git -C ${kerneldir} cat-file -t ${OLDHASHTARGET} >/dev/null 2>&1
  450. # [[ $? -ne 0 ]] && OLDHASHTARGET=$(git -C ${kerneldir} show HEAD~199 --pretty=format:"%H" --no-patch)
  451. # else
  452. # git -C ${kerneldir} cat-file -t ${OLDHASHTARGET} >/dev/null 2>&1
  453. # [[ $? -ne 0 ]] && OLDHASHTARGET=$(git -C ${kerneldir} rev-list --max-parents=0 HEAD)
  454. #fi
  455. #[[ -z ${KERNELPATCHDIR} ]] && KERNELPATCHDIR=$LINUXFAMILY-$BRANCH
  456. #[[ -z ${LINUXCONFIG} ]] && LINUXCONFIG=linux-$LINUXFAMILY-$BRANCH
  457. # calculate URL
  458. #if [[ "$KERNELSOURCE" == *"github.com"* ]]; then
  459. # URL="${KERNELSOURCE/git:/https:}/commit/${HASH}"
  460. #elif [[ "$KERNELSOURCE" == *"kernel.org"* ]]; then
  461. # URL="${KERNELSOURCE/git:/https:}/commit/?h=$(echo $KERNELBRANCH | cut -d":" -f2)&id=${HASH}"
  462. #else
  463. # URL="${KERNELSOURCE}/+/$HASH"
  464. #fi
  465. # create change log
  466. #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"
  467. #echo "${hash}" > "${HASHTARGET}.githash"
  468. #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)
  469. #hash_watch_2=$(cat "${EXTER}/config/kernel/${LINUXCONFIG}.config")
  470. #echo "${hash_watch_1}${hash_watch_2}" | improved_git hash-object --stdin >> "${HASHTARGET}.githash"
  471. }
  472. compile_firmware()
  473. {
  474. display_alert "Merging and packaging linux firmware" "@host" "info"
  475. local firmwaretempdir plugin_dir
  476. firmwaretempdir=$(mktemp -d)
  477. chmod 700 ${firmwaretempdir}
  478. trap "ret=\$?; rm -rf \"${firmwaretempdir}\" ; exit \$ret" 0 1 2 3 15
  479. plugin_dir="orangepi-firmware${FULL}"
  480. mkdir -p "${firmwaretempdir}/${plugin_dir}/lib/firmware"
  481. [[ $IGNORE_UPDATES != yes ]] && fetch_from_repo "https://github.com/orangepi-xunlong/firmware" "${EXTER}/cache/sources/orangepi-firmware-git" "branch:master"
  482. if [[ -n $FULL ]]; then
  483. [[ $IGNORE_UPDATES != yes ]] && fetch_from_repo "$MAINLINE_FIRMWARE_SOURCE" "${EXTER}/cache/sources/linux-firmware-git" "branch:master"
  484. # cp : create hardlinks
  485. cp -af --reflink=auto "${EXTER}"/cache/sources/linux-firmware-git/* "${firmwaretempdir}/${plugin_dir}/lib/firmware/"
  486. fi
  487. # overlay our firmware
  488. # cp : create hardlinks
  489. cp -af --reflink=auto "${EXTER}"/cache/sources/orangepi-firmware-git/* "${firmwaretempdir}/${plugin_dir}/lib/firmware/"
  490. # cleanup what's not needed for sure
  491. rm -rf "${firmwaretempdir}/${plugin_dir}"/lib/firmware/{amdgpu,amd-ucode,radeon,nvidia,matrox,.git}
  492. cd "${firmwaretempdir}/${plugin_dir}" || exit
  493. # set up control file
  494. mkdir -p DEBIAN
  495. cat <<-END > DEBIAN/control
  496. Package: orangepi-firmware${FULL}
  497. Version: $REVISION
  498. Architecture: all
  499. Maintainer: $MAINTAINER <$MAINTAINERMAIL>
  500. Installed-Size: 1
  501. Replaces: linux-firmware, firmware-brcm80211, firmware-ralink, firmware-samsung, firmware-realtek, orangepi-firmware${REPLACE}
  502. Section: kernel
  503. Priority: optional
  504. Description: Linux firmware${FULL}
  505. END
  506. cd "${firmwaretempdir}" || exit
  507. # pack
  508. mv "orangepi-firmware${FULL}" "orangepi-firmware${FULL}_${REVISION}_all"
  509. display_alert "Building firmware package" "orangepi-firmware${FULL}_${REVISION}_all" "info"
  510. fakeroot dpkg-deb -b -Z${DEB_COMPRESS} "orangepi-firmware${FULL}_${REVISION}_all" >> "${DEST}"/${LOG_SUBPATH}/install.log 2>&1
  511. mv "orangepi-firmware${FULL}_${REVISION}_all" "orangepi-firmware${FULL}"
  512. rsync -rq "orangepi-firmware${FULL}_${REVISION}_all.deb" "${DEB_STORAGE}/"
  513. # remove temp directory
  514. rm -rf "${firmwaretempdir}"
  515. }
  516. compile_orangepi-zsh()
  517. {
  518. local tmp_dir orangepi_zsh_dir
  519. tmp_dir=$(mktemp -d)
  520. chmod 700 ${tmp_dir}
  521. trap "rm -rf \"${tmp_dir}\" ; exit 0" 0 1 2 3 15
  522. orangepi_zsh_dir=orangepi-zsh_${REVISION}_all
  523. display_alert "Building deb" "orangepi-zsh" "info"
  524. [[ $IGNORE_UPDATES != yes ]] && fetch_from_repo "https://github.com/robbyrussell/oh-my-zsh" "${EXTER}/cache/sources/oh-my-zsh" "branch:master"
  525. [[ $IGNORE_UPDATES != yes ]] && fetch_from_repo "https://github.com/mroth/evalcache" "${EXTER}/cache/sources/evalcache" "branch:master"
  526. mkdir -p "${tmp_dir}/${orangepi_zsh_dir}"/{DEBIAN,etc/skel/,etc/oh-my-zsh/,/etc/skel/.oh-my-zsh/cache}
  527. # set up control file
  528. cat <<-END > "${tmp_dir}/${orangepi_zsh_dir}"/DEBIAN/control
  529. Package: orangepi-zsh
  530. Version: $REVISION
  531. Architecture: all
  532. Maintainer: $MAINTAINER <$MAINTAINERMAIL>
  533. Depends: zsh, tmux
  534. Section: utils
  535. Priority: optional
  536. Description: Orange Pi improved ZShell
  537. END
  538. # set up post install script
  539. cat <<-END > "${tmp_dir}/${orangepi_zsh_dir}"/DEBIAN/postinst
  540. #!/bin/sh
  541. # copy cache directory if not there yet
  542. awk -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 {}'
  543. awk -F'[:]' '{if (\$3 >= 1000 && \$3 != 65534 || \$3 == 0) print ""\$6"/.zshrc"}' /etc/passwd | xargs -i sh -c 'test ! -f {} && cp -R /etc/skel/.zshrc {}'
  544. # fix owner permissions in home directory
  545. awk -F'[:]' '{if (\$3 >= 1000 && \$3 != 65534 || \$3 == 0) print ""\$1":"\$3" "\$6"/.oh-my-zsh"}' /etc/passwd | xargs -n2 chown -R
  546. awk -F'[:]' '{if (\$3 >= 1000 && \$3 != 65534 || \$3 == 0) print ""\$1":"\$3" "\$6"/.zshrc"}' /etc/passwd | xargs -n2 chown -R
  547. # add support for bash profile
  548. ! grep emulate /etc/zsh/zprofile >/dev/null && echo "emulate sh -c 'source /etc/profile'" >> /etc/zsh/zprofile
  549. exit 0
  550. END
  551. cp -R "${EXTER}"/cache/sources/oh-my-zsh "${tmp_dir}/${orangepi_zsh_dir}"/etc/
  552. cp -R "${EXTER}"/cache/sources/evalcache "${tmp_dir}/${orangepi_zsh_dir}"/etc/oh-my-zsh/plugins
  553. cp "${tmp_dir}/${orangepi_zsh_dir}"/etc/oh-my-zsh/templates/zshrc.zsh-template "${tmp_dir}/${orangepi_zsh_dir}"/etc/skel/.zshrc
  554. chmod -R g-w,o-w "${tmp_dir}/${orangepi_zsh_dir}"/etc/oh-my-zsh/
  555. # we have common settings
  556. sed -i "s/^export ZSH=.*/export ZSH=\/etc\/oh-my-zsh/" "${tmp_dir}/${orangepi_zsh_dir}"/etc/skel/.zshrc
  557. # user cache
  558. sed -i "/^export ZSH=.*/a export ZSH_CACHE_DIR=~\/.oh-my-zsh\/cache" "${tmp_dir}/${orangepi_zsh_dir}"/etc/skel/.zshrc
  559. # define theme
  560. sed -i 's/^ZSH_THEME=.*/ZSH_THEME="mrtazz"/' "${tmp_dir}/${orangepi_zsh_dir}"/etc/skel/.zshrc
  561. # disable prompt while update
  562. sed -i 's/# DISABLE_UPDATE_PROMPT="true"/DISABLE_UPDATE_PROMPT="true"/g' "${tmp_dir}/${orangepi_zsh_dir}"/etc/skel/.zshrc
  563. # disable auto update since we provide update via package
  564. sed -i 's/# DISABLE_AUTO_UPDATE="true"/DISABLE_AUTO_UPDATE="true"/g' "${tmp_dir}/${orangepi_zsh_dir}"/etc/skel/.zshrc
  565. # define default plugins
  566. sed -i 's/^plugins=.*/plugins=(evalcache git git-extras debian tmux screen history extract colorize web-search docker)/' "${tmp_dir}/${orangepi_zsh_dir}"/etc/skel/.zshrc
  567. chmod 755 "${tmp_dir}/${orangepi_zsh_dir}"/DEBIAN/postinst
  568. fakeroot dpkg-deb -b -Z${DEB_COMPRESS} "${tmp_dir}/${orangepi_zsh_dir}" >> "${DEST}"/${LOG_SUBPATH}/output.log 2>&1
  569. rsync --remove-source-files -rq "${tmp_dir}/${orangepi_zsh_dir}.deb" "${DEB_STORAGE}/"
  570. rm -rf "${tmp_dir}"
  571. }
  572. compile_plymouth-theme-orangepi()
  573. {
  574. local tmp_dir work_dir
  575. tmp_dir=$(mktemp -d)
  576. chmod 700 ${tmp_dir}
  577. trap "ret=\$?; rm -rf \"${tmp_dir}\" ; exit \$ret" 0 1 2 3 15
  578. plymouth_theme_orangepi_dir=orangepi-plymouth-theme_${REVISION}_all
  579. display_alert "Building deb" "orangepi-plymouth-theme" "info"
  580. mkdir -p "${tmp_dir}/${plymouth_theme_orangepi_dir}"/{DEBIAN,usr/share/plymouth/themes/orangepi}
  581. # set up control file
  582. cat <<- END > "${tmp_dir}/${plymouth_theme_orangepi_dir}"/DEBIAN/control
  583. Package: orangepi-plymouth-theme
  584. Version: $REVISION
  585. Architecture: all
  586. Maintainer: $MAINTAINER <$MAINTAINERMAIL>
  587. Depends: plymouth, plymouth-themes
  588. Section: universe/x11
  589. Priority: optional
  590. Description: boot animation, logger and I/O multiplexer - orangepi theme
  591. END
  592. cp "${EXTER}"/packages/plymouth-theme-orangepi/debian/{postinst,prerm,postrm} \
  593. "${tmp_dir}/${plymouth_theme_orangepi_dir}"/DEBIAN/
  594. chmod 755 "${tmp_dir}/${plymouth_theme_orangepi_dir}"/DEBIAN/{postinst,prerm,postrm}
  595. #convert -resize 256x256 \
  596. # "${EXTER}"/packages/plymouth-theme-orangepi/orangepi-logo.png \
  597. # "${tmp_dir}/${plymouth_theme_orangepi_dir}"/usr/share/plymouth/themes/orangepi/bgrt-fallback.png
  598. # convert -resize 52x52 \
  599. # "${EXTER}"/packages/plymouth-theme-orangepi/spinner.gif \
  600. # "${tmp_dir}/${plymouth_theme_orangepi_dir}"/usr/share/plymouth/themes/orangepi/animation-%04d.png
  601. convert -resize 52x52 \
  602. "${EXTER}"/packages/plymouth-theme-orangepi/spinner.gif \
  603. "${tmp_dir}/${plymouth_theme_orangepi_dir}"/usr/share/plymouth/themes/orangepi/throbber-%04d.png
  604. cp "${EXTER}"/packages/plymouth-theme-orangepi/watermark.png \
  605. "${tmp_dir}/${plymouth_theme_orangepi_dir}"/usr/share/plymouth/themes/orangepi/
  606. cp "${EXTER}"/packages/plymouth-theme-orangepi/{bullet,capslock,entry,keyboard,keymap-render,lock}.png \
  607. "${tmp_dir}/${plymouth_theme_orangepi_dir}"/usr/share/plymouth/themes/orangepi/
  608. cp "${EXTER}"/packages/plymouth-theme-orangepi/orangepi.plymouth \
  609. "${tmp_dir}/${plymouth_theme_orangepi_dir}"/usr/share/plymouth/themes/orangepi/
  610. fakeroot dpkg-deb -b -Z${DEB_COMPRESS} "${tmp_dir}/${plymouth_theme_orangepi_dir}" > /dev/null
  611. rsync --remove-source-files -rq "${tmp_dir}/${plymouth_theme_orangepi_dir}.deb" "${DEB_STORAGE}/"
  612. rm -rf "${tmp_dir}"
  613. }
  614. compile_orangepi-config()
  615. {
  616. local tmpdir=${SRC}/.tmp/orangepi-config_${REVISION}_all
  617. display_alert "Building deb" "orangepi-config" "info"
  618. mkdir -p "${tmpdir}"/{DEBIAN,usr/bin/,usr/sbin/,usr/lib/orangepi-config/}
  619. # set up control file
  620. cat <<-END > "${tmpdir}"/DEBIAN/control
  621. Package: orangepi-config
  622. Version: $REVISION
  623. Architecture: all
  624. Maintainer: $MAINTAINER <$MAINTAINERMAIL>
  625. Replaces: orangepi-bsp
  626. Depends: bash, iperf3, psmisc, curl, bc, expect, dialog, pv, \
  627. debconf-utils, unzip, build-essential, html2text, apt-transport-https, html2text, dirmngr, software-properties-common
  628. Recommends: orangepi-bsp
  629. Suggests: libpam-google-authenticator, qrencode, network-manager, sunxi-tools
  630. Section: utils
  631. Priority: optional
  632. Description: Orange Pi configuration utility
  633. END
  634. install -m 755 $EXTER/cache/sources/orangepi-config/scripts/tv_grab_file $tmpdir/usr/bin/tv_grab_file
  635. install -m 755 $EXTER/cache/sources/orangepi-config/debian-config $tmpdir/usr/sbin/orangepi-config
  636. install -m 644 $EXTER/cache/sources/orangepi-config/debian-config-jobs $tmpdir/usr/lib/orangepi-config/jobs.sh
  637. install -m 644 $EXTER/cache/sources/orangepi-config/debian-config-submenu $tmpdir/usr/lib/orangepi-config/submenu.sh
  638. install -m 644 $EXTER/cache/sources/orangepi-config/debian-config-functions $tmpdir/usr/lib/orangepi-config/functions.sh
  639. install -m 644 $EXTER/cache/sources/orangepi-config/debian-config-functions-network $tmpdir/usr/lib/orangepi-config/functions-network.sh
  640. install -m 755 $EXTER/cache/sources/orangepi-config/softy $tmpdir/usr/sbin/softy
  641. # fallback to replace orangepi-config in BSP
  642. ln -sf /usr/sbin/orangepi-config $tmpdir/usr/bin/orangepi-config
  643. ln -sf /usr/sbin/softy "${tmpdir}"/usr/bin/softy
  644. fakeroot dpkg-deb -b -Z${DEB_COMPRESS} "${tmpdir}" >/dev/null
  645. rsync --remove-source-files -rq "${tmpdir}.deb" "${DEB_STORAGE}/"
  646. rm -rf "${tmpdir}"
  647. }
  648. compile_sunxi_tools()
  649. {
  650. # Compile and install only if git commit hash changed
  651. cd $EXTER/cache/sources/sunxi-tools || exit
  652. # need to check if /usr/local/bin/sunxi-fexc to detect new Docker containers with old cached sources
  653. if [[ ! -f .commit_id || $(improved_git rev-parse @ 2>/dev/null) != $(<.commit_id) || ! -f /usr/local/bin/sunxi-fexc ]]; then
  654. display_alert "Compiling" "sunxi-tools" "info"
  655. make -s clean >/dev/null
  656. make -s tools >/dev/null
  657. mkdir -p /usr/local/bin/
  658. make install-tools >/dev/null 2>&1
  659. improved_git rev-parse @ 2>/dev/null > .commit_id
  660. fi
  661. }
  662. install_rkbin_tools()
  663. {
  664. # install only if git commit hash changed
  665. cd "${EXTER}"/cache/sources/rkbin-tools || exit
  666. # need to check if /usr/local/bin/sunxi-fexc to detect new Docker containers with old cached sources
  667. if [[ ! -f .commit_id || $(improved_git rev-parse @ 2>/dev/null) != $(<.commit_id) || ! -f /usr/local/bin/loaderimage ]]; then
  668. display_alert "Installing" "rkbin-tools" "info"
  669. mkdir -p /usr/local/bin/
  670. install -m 755 tools/loaderimage /usr/local/bin/
  671. install -m 755 tools/trust_merger /usr/local/bin/
  672. improved_git rev-parse @ 2>/dev/null > .commit_id
  673. fi
  674. }
  675. grab_version()
  676. {
  677. local ver=()
  678. ver[0]=$(grep "^VERSION" "${1}"/Makefile | head -1 | awk '{print $(NF)}' | grep -oE '^[[:digit:]]+')
  679. ver[1]=$(grep "^PATCHLEVEL" "${1}"/Makefile | head -1 | awk '{print $(NF)}' | grep -oE '^[[:digit:]]+')
  680. ver[2]=$(grep "^SUBLEVEL" "${1}"/Makefile | head -1 | awk '{print $(NF)}' | grep -oE '^[[:digit:]]+')
  681. ver[3]=$(grep "^EXTRAVERSION" "${1}"/Makefile | head -1 | awk '{print $(NF)}' | grep -oE '^-rc[[:digit:]]+')
  682. echo "${ver[0]:-0}${ver[1]:+.${ver[1]}}${ver[2]:+.${ver[2]}}${ver[3]}"
  683. }
  684. # find_toolchain <compiler_prefix> <expression>
  685. #
  686. # returns path to toolchain that satisfies <expression>
  687. #
  688. find_toolchain()
  689. {
  690. [[ "${SKIP_EXTERNAL_TOOLCHAINS}" == "yes" ]] && { echo "/usr/bin"; return; }
  691. local compiler=$1
  692. local expression=$2
  693. local dist=10
  694. local toolchain=""
  695. # extract target major.minor version from expression
  696. local target_ver
  697. target_ver=$(grep -oE "[[:digit:]]+\.[[:digit:]]" <<< "$expression")
  698. for dir in "${SRC}"/toolchains/*/; do
  699. # check if is a toolchain for current $ARCH
  700. [[ ! -f ${dir}bin/${compiler}gcc ]] && continue
  701. # get toolchain major.minor version
  702. local gcc_ver
  703. gcc_ver=$("${dir}bin/${compiler}gcc" -dumpversion | grep -oE "^[[:digit:]]+\.[[:digit:]]")
  704. # check if toolchain version satisfies requirement
  705. awk "BEGIN{exit ! ($gcc_ver $expression)}" >/dev/null || continue
  706. # check if found version is the closest to target
  707. # may need different logic here with more than 1 digit minor version numbers
  708. # numbers: 3.9 > 3.10; versions: 3.9 < 3.10
  709. # dpkg --compare-versions can be used here if operators are changed
  710. local d
  711. d=$(awk '{x = $1 - $2}{printf "%.1f\n", (x > 0) ? x : -x}' <<< "$target_ver $gcc_ver")
  712. if awk "BEGIN{exit ! ($d < $dist)}" >/dev/null ; then
  713. dist=$d
  714. toolchain=${dir}bin
  715. fi
  716. done
  717. echo "$toolchain"
  718. # logging a stack of used compilers.
  719. if [[ -f "${DEST}"/${LOG_SUBPATH}/compiler.log ]]; then
  720. if ! grep -q "$toolchain" "${DEST}"/${LOG_SUBPATH}/compiler.log; then
  721. echo "$toolchain" >> "${DEST}"/${LOG_SUBPATH}/compiler.log;
  722. fi
  723. else
  724. echo "$toolchain" >> "${DEST}"/${LOG_SUBPATH}/compiler.log;
  725. fi
  726. }
  727. # advanced_patch <dest> <family> <board> <target> <branch> <description>
  728. #
  729. # parameters:
  730. # <dest>: u-boot, kernel, atf
  731. # <family>: u-boot: u-boot; kernel: sunxi-next, ...
  732. # <board>: orangepipcplus, orangepizero ...
  733. # <target>: optional subdirectory
  734. # <description>: additional description text
  735. #
  736. # priority:
  737. # $USERPATCHES_PATH/<dest>/<family>/target_<target>
  738. # $USERPATCHES_PATH/<dest>/<family>/board_<board>
  739. # $USERPATCHES_PATH/<dest>/<family>/branch_<branch>
  740. # $USERPATCHES_PATH/<dest>/<family>
  741. # $EXTER/patch/<dest>/<family>/target_<target>
  742. # $EXTER/patch/<dest>/<family>/board_<board>
  743. # $EXTER/patch/<dest>/<family>/branch_<branch>
  744. # $EXTER/patch/<dest>/<family>
  745. #
  746. advanced_patch()
  747. {
  748. local dest=$1
  749. local family=$2
  750. local board=$3
  751. local target=$4
  752. local branch=$5
  753. local description=$6
  754. display_alert "Started patching process for" "$dest $description" "info"
  755. display_alert "Looking for user patches in" "userpatches/$dest/$family" "info"
  756. local names=()
  757. local dirs=(
  758. "$USERPATCHES_PATH/$dest/$family/target_${target}:[\e[33mu\e[0m][\e[34mt\e[0m]"
  759. "$USERPATCHES_PATH/$dest/$family/board_${board}:[\e[33mu\e[0m][\e[35mb\e[0m]"
  760. "$USERPATCHES_PATH/$dest/$family/branch_${branch}:[\e[33mu\e[0m][\e[33mb\e[0m]"
  761. "$USERPATCHES_PATH/$dest/$family:[\e[33mu\e[0m][\e[32mc\e[0m]"
  762. "$EXTER/patch/$dest/$family/target_${target}:[\e[32ml\e[0m][\e[34mt\e[0m]"
  763. "$EXTER/patch/$dest/$family/board_${board}:[\e[32ml\e[0m][\e[35mb\e[0m]"
  764. "$EXTER/patch/$dest/$family/branch_${branch}:[\e[32ml\e[0m][\e[33mb\e[0m]"
  765. "$EXTER/patch/$dest/$family:[\e[32ml\e[0m][\e[32mc\e[0m]"
  766. )
  767. local links=()
  768. # required for "for" command
  769. shopt -s nullglob dotglob
  770. # get patch file names
  771. for dir in "${dirs[@]}"; do
  772. for patch in ${dir%%:*}/*.patch; do
  773. names+=($(basename "${patch}"))
  774. done
  775. # add linked patch directories
  776. if [[ -d ${dir%%:*} ]]; then
  777. local findlinks
  778. findlinks=$(find "${dir%%:*}" -maxdepth 1 -type l -print0 2>&1 | xargs -0)
  779. [[ -n $findlinks ]] && readarray -d '' links < <(find "${findlinks}" -maxdepth 1 -type f -follow -print -iname "*.patch" -print | grep "\.patch$" | sed "s|${dir%%:*}/||g" 2>&1)
  780. fi
  781. done
  782. # merge static and linked
  783. names=("${names[@]}" "${links[@]}")
  784. # remove duplicates
  785. local names_s=($(echo "${names[@]}" | tr ' ' '\n' | LC_ALL=C sort -u | tr '\n' ' '))
  786. # apply patches
  787. for name in "${names_s[@]}"; do
  788. for dir in "${dirs[@]}"; do
  789. if [[ -f ${dir%%:*}/$name ]]; then
  790. if [[ -s ${dir%%:*}/$name ]]; then
  791. process_patch_file "${dir%%:*}/$name" "${dir##*:}"
  792. else
  793. display_alert "* ${dir##*:} $name" "skipped"
  794. fi
  795. break # next name
  796. fi
  797. done
  798. done
  799. }
  800. # process_patch_file <file> <description>
  801. #
  802. # parameters:
  803. # <file>: path to patch file
  804. # <status>: additional status text
  805. #
  806. process_patch_file()
  807. {
  808. local patch=$1
  809. local status=$2
  810. # detect and remove files which patch will create
  811. lsdiff -s --strip=1 "${patch}" | grep '^+' | awk '{print $2}' | xargs -I % sh -c 'rm -f %'
  812. echo "Processing file $patch" >> "${DEST}"/${LOG_SUBPATH}/patching.log
  813. patch --batch --silent -p1 -N < "${patch}" >> "${DEST}"/${LOG_SUBPATH}/patching.log 2>&1
  814. if [[ $? -ne 0 ]]; then
  815. display_alert "* $status $(basename "${patch}")" "failed" "wrn"
  816. [[ $EXIT_PATCHING_ERROR == yes ]] && exit_with_error "Aborting due to" "EXIT_PATCHING_ERROR"
  817. else
  818. display_alert "* $status $(basename "${patch}")" "" "info"
  819. fi
  820. echo >> "${DEST}"/${LOG_SUBPATH}/patching.log
  821. }
  822. userpatch_create()
  823. {
  824. # create commit to start from clean source
  825. git add .
  826. git -c user.name='Orange Pi User' -c user.email='user@example.org' commit -q -m "Cleaning working copy"
  827. local patch="$DEST/patch/$1-$LINUXFAMILY-$BRANCH.patch"
  828. # apply previous user debug mode created patches
  829. if [[ -f $patch ]]; then
  830. display_alert "Applying existing $1 patch" "$patch" "wrn" && patch --batch --silent -p1 -N < "${patch}"
  831. # read title of a patch in case Git is configured
  832. if [[ -n $(git config user.email) ]]; then
  833. COMMIT_MESSAGE=$(cat "${patch}" | grep Subject | sed -n -e '0,/PATCH/s/.*PATCH]//p' | xargs)
  834. display_alert "Patch name extracted" "$COMMIT_MESSAGE" "wrn"
  835. fi
  836. fi
  837. # prompt to alter source
  838. display_alert "Make your changes in this directory:" "$(pwd)" "wrn"
  839. display_alert "Press <Enter> after you are done" "waiting" "wrn"
  840. read -r </dev/tty
  841. tput cuu1
  842. git add .
  843. # create patch out of changes
  844. if ! git diff-index --quiet --cached HEAD; then
  845. # If Git is configured, create proper patch and ask for a name
  846. if [[ -n $(git config user.email) ]]; then
  847. display_alert "Add / change patch name" "$COMMIT_MESSAGE" "wrn"
  848. read -e -p "Patch description: " -i "$COMMIT_MESSAGE" COMMIT_MESSAGE
  849. [[ -z "$COMMIT_MESSAGE" ]] && COMMIT_MESSAGE="Patching something"
  850. git commit -s -m "$COMMIT_MESSAGE"
  851. git format-patch -1 HEAD --stdout --signature="Created with orangepi build tools https://github.com/orangepi-xunlong/build" > "${patch}"
  852. PATCHFILE=$(git format-patch -1 HEAD)
  853. rm $PATCHFILE # delete the actual file
  854. # create a symlink to have a nice name ready
  855. find $DEST/patch/ -type l -delete # delete any existing
  856. ln -sf $patch $DEST/patch/$PATCHFILE
  857. else
  858. git diff --staged > "${patch}"
  859. fi
  860. display_alert "You will find your patch here:" "$patch" "info"
  861. else
  862. display_alert "No changes found, skipping patch creation" "" "wrn"
  863. fi
  864. git reset --soft HEAD~
  865. for i in {3..1..1}; do echo -n "$i." && sleep 1; done
  866. }
  867. # overlayfs_wrapper <operation> <workdir> <description>
  868. #
  869. # <operation>: wrap|cleanup
  870. # <workdir>: path to source directory
  871. # <description>: suffix for merged directory to help locating it in /tmp
  872. # return value: new directory
  873. #
  874. # Assumptions/notes:
  875. # - Ubuntu Xenial host
  876. # - /tmp is mounted as tmpfs
  877. # - there is enough space on /tmp
  878. # - UB if running multiple compilation tasks in parallel
  879. # - should not be used with CREATE_PATCHES=yes
  880. #
  881. overlayfs_wrapper()
  882. {
  883. local operation="$1"
  884. if [[ $operation == wrap ]]; then
  885. local srcdir="$2"
  886. local description="$3"
  887. mkdir -p /tmp/overlay_components/ /tmp/orangepi_build/
  888. local tempdir workdir mergeddir
  889. tempdir=$(mktemp -d --tmpdir="/tmp/overlay_components/")
  890. workdir=$(mktemp -d --tmpdir="/tmp/overlay_components/")
  891. mergeddir=$(mktemp -d --suffix="_$description" --tmpdir="/tmp/orangepi_build/")
  892. mount -t overlay overlay -o lowerdir="$srcdir",upperdir="$tempdir",workdir="$workdir" "$mergeddir"
  893. # this is executed in a subshell, so use temp files to pass extra data outside
  894. echo "$tempdir" >> /tmp/.overlayfs_wrapper_cleanup
  895. echo "$mergeddir" >> /tmp/.overlayfs_wrapper_umount
  896. echo "$mergeddir" >> /tmp/.overlayfs_wrapper_cleanup
  897. echo "$mergeddir"
  898. return
  899. fi
  900. if [[ $operation == cleanup ]]; then
  901. if [[ -f /tmp/.overlayfs_wrapper_umount ]]; then
  902. for dir in $(</tmp/.overlayfs_wrapper_umount); do
  903. [[ $dir == /tmp/* ]] && umount -l "$dir" > /dev/null 2>&1
  904. done
  905. fi
  906. if [[ -f /tmp/.overlayfs_wrapper_cleanup ]]; then
  907. for dir in $(</tmp/.overlayfs_wrapper_cleanup); do
  908. [[ $dir == /tmp/* ]] && rm -rf "$dir"
  909. done
  910. fi
  911. rm -f /tmp/.overlayfs_wrapper_umount /tmp/.overlayfs_wrapper_cleanup
  912. fi
  913. }