build/main.sh

266 lines
9 KiB
Bash
Raw Normal View History

2014-10-13 19:04:03 +02:00
#!/bin/bash
#
# Copyright (c) 2015 Igor Pecovnik, igor.pecovnik@gma**.com
2014-10-13 19:04:03 +02:00
#
# This file is licensed under the terms of the GNU General Public
# License version 2. This program is licensed "as is" without any
# warranty of any kind, whether express or implied.
2014-10-13 19:04:03 +02:00
#
# This file is a part of tool chain https://github.com/igorpecovnik/lib
#
#
# Main program
#
# destination
DEST=$SRC/output
# sources for compilation
SOURCES=$SRC/sources
2016-04-16 18:12:34 +03:00
TTY_X=$(($(stty size | awk '{print $2}')-6)) # determine terminal width
TTY_Y=$(($(stty size | awk '{print $1}')-6)) # determine terminal height
# We'll use this title on all menus
backtitle="Armbian building script, http://www.armbian.com | Author: Igor Pecovnik"
# if language not set, set to english
[[ -z $LANGUAGE ]] && export LANGUAGE="en_US:en"
# default console if not set
[[ -z $CONSOLE_CHAR ]] && export CONSOLE_CHAR="UTF-8"
# Load libraries
2016-08-09 18:26:22 +03:00
source $SRC/lib/debootstrap-ng.sh # System specific install
source $SRC/lib/distributions.sh # System specific install
source $SRC/lib/desktop.sh # Desktop specific install
source $SRC/lib/common.sh # Functions
source $SRC/lib/makeboarddeb.sh # Create board support package
source $SRC/lib/general.sh # General functions
source $SRC/lib/chroot-buildpackages.sh # Building packages in chroot
2015-12-24 19:37:34 +03:00
# compress and remove old logs
2016-01-07 11:47:38 +03:00
mkdir -p $DEST/debug
2016-07-05 22:16:15 +03:00
(cd $DEST/debug && tar -czf logs-$(<timestamp).tgz *.log) > /dev/null 2>&1
2015-12-24 19:37:34 +03:00
rm -f $DEST/debug/*.log > /dev/null 2>&1
2016-07-05 22:16:15 +03:00
date +"%d_%m_%Y-%H_%M_%S" > $DEST/debug/timestamp
2016-06-16 14:34:45 +03:00
# delete compressed logs older than 7 days
(cd $DEST/debug && find . -name '*.tgz' -atime +7 -delete) > /dev/null
2015-12-24 19:37:34 +03:00
# compile.sh version checking
2016-11-03 20:57:56 +03:00
ver1=$(awk -F"=" '/^# VERSION/ {print $2}' <$SRC/compile.sh )
ver2=$(awk -F"=" '/^# VERSION/ {print $2}' <$SRC/lib/compile.sh 2>/dev/null) || ver2=0
if [[ $BETA != yes && ( -z $ver1 || $ver1 -lt $ver2 ) ]]; then
display_alert "File $0 is outdated. Please overwrite it with an updated version from" "$SRC/lib" "wrn"
echo -e "Press \e[0;33m<Ctrl-C>\x1B[0m to abort compilation, \e[0;33m<Enter>\x1B[0m to ignore and continue"
read
fi
2015-12-11 20:49:10 +03:00
# Script parameters handling
for i in "$@"; do
if [[ $i == *=* ]]; then
2015-12-11 20:49:10 +03:00
parameter=${i%%=*}
value=${i##*=}
display_alert "Command line: setting $parameter to" "${value:-(empty)}" "info"
2015-12-11 20:49:10 +03:00
eval $parameter=$value
fi
2015-12-11 20:49:10 +03:00
done
if [[ $BETA == yes ]]; then
IMAGE_TYPE=nightly
else
IMAGE_TYPE=stable
fi
if [[ $PROGRESS_DISPLAY == none ]]; then
OUTPUT_VERYSILENT=yes
elif [[ $PROGRESS_DISPLAY != plain ]]; then
OUTPUT_DIALOG=yes
2015-12-11 20:49:10 +03:00
fi
if [[ $PROGRESS_LOG_TO_FILE != yes ]]; then unset PROGRESS_LOG_TO_FILE; fi
if [[ $USE_CCACHE != no ]]; then
CCACHE=ccache
2016-05-04 17:16:39 +03:00
export PATH="/usr/lib/ccache:$PATH"
2016-09-25 17:58:48 +03:00
# private ccache directory to avoid permission issues when using build script with "sudo"
# see https://ccache.samba.org/manual.html#_sharing_a_cache for alternative solution
[[ $PRIVATE_CCACHE == yes ]] && export CCACHE_DIR=$DEST/ccache
else
CCACHE=""
fi
2015-12-11 20:49:10 +03:00
# optimize build time with 100% CPU usage
CPUS=$(grep -c 'processor' /proc/cpuinfo)
if [[ $USEALLCORES != no ]]; then
CTHREADS="-j$(($CPUS + $CPUS/2))"
else
CTHREADS="-j1"
fi
2016-08-30 19:25:32 +03:00
# Check and install dependencies, directory structure and settings
prepare_host
2015-12-11 20:49:10 +03:00
# if KERNEL_ONLY, BOARD, BRANCH or RELEASE are not set, display selection menu
if [[ -z $KERNEL_ONLY ]]; then
options+=("yes" "Kernel and u-boot packages")
options+=("no" "OS image for installation to SD card")
2016-12-19 22:12:27 +03:00
KERNEL_ONLY=$(dialog --stdout --title "Choose an option" --backtitle "$backtitle" --no-tags --menu "Select what to build" \
$TTY_Y $TTY_X $(($TTY_Y - 8)) "${options[@]}")
unset options
[[ -z $KERNEL_ONLY ]] && exit_with_error "No option selected"
2015-12-11 20:49:10 +03:00
fi
EXT='conf'
if [[ -z $BOARD ]]; then
WIP_STATE='supported'
2016-12-20 12:46:49 +03:00
WIP_BUTTON='WIP'
[[ -n $(find $SRC/lib/config/boards/ -name '*.wip' -print -quit) ]] && DIALOG_EXTRA="--extra-button"
while true; do
options=()
for board in $SRC/lib/config/boards/*.${EXT}; do
options+=("$(basename $board | cut -d'.' -f1)" "$(head -1 $board | cut -d'#' -f2)")
done
2016-12-20 12:46:49 +03:00
BOARD=$(dialog --stdout --title "Choose a board" --backtitle "$backtitle" --scrollbar --extra-label "Show $WIP_BUTTON" $DIALOG_EXTRA \
--menu "Select the target board\nDisplaying $WIP_STATE boards" $TTY_Y $TTY_X $(($TTY_Y - 8)) "${options[@]}")
STATUS=$?
if [[ $STATUS == 3 ]]; then
if [[ $WIP_STATE == supported ]]; then
WIP_STATE='work-in-progress'
EXT='wip'
2016-12-20 12:46:49 +03:00
WIP_BUTTON='supported'
else
WIP_STATE='supported'
EXT='conf'
2016-12-20 12:46:49 +03:00
WIP_BUTTON='WIP'
fi
continue
elif [[ $STATUS == 0 ]]; then
break
fi
unset options
[[ -z $BOARD ]] && exit_with_error "No board selected"
done
2015-12-11 20:49:10 +03:00
fi
2016-12-20 12:46:49 +03:00
if [[ -f $SRC/lib/config/boards/${BOARD}.${EXT} ]]; then
source $SRC/lib/config/boards/${BOARD}.${EXT}
elif [[ -f $SRC/lib/config/boards/${BOARD}.wip ]]; then
# when launching build for WIP board from command line
source $SRC/lib/config/boards/${BOARD}.wip
fi
[[ -z $KERNEL_TARGET ]] && exit_with_error "Board configuration does not define valid kernel config"
if [[ -z $BRANCH ]]; then
options=()
[[ $KERNEL_TARGET == *default* ]] && options+=("default" "Vendor provided / legacy (3.4.x - 4.4.x)")
[[ $KERNEL_TARGET == *next* ]] && options+=("next" "Mainline (@kernel.org) (4.x)")
[[ $KERNEL_TARGET == *dev* ]] && options+=("dev" "Development version (4.x)")
# do not display selection dialog if only one kernel branch is available
if [[ "${#options[@]}" == 2 ]]; then
2016-04-17 15:12:09 +03:00
BRANCH="${options[0]}"
else
BRANCH=$(dialog --stdout --title "Choose a kernel" --backtitle "$backtitle" \
--menu "Select the target kernel branch\nExact kernel versions depend on selected board" \
$TTY_Y $TTY_X $(($TTY_Y - 8)) "${options[@]}")
fi
unset options
[[ -z $BRANCH ]] && exit_with_error "No kernel branch selected"
else
[[ $KERNEL_TARGET != *$BRANCH* ]] && exit_with_error "Kernel branch not defined for this board" "$BRANCH"
2015-12-11 20:49:10 +03:00
fi
if [[ $KERNEL_ONLY != yes && -z $RELEASE ]]; then
options=()
options+=("jessie" "Debian 8 Jessie")
options+=("xenial" "Ubuntu Xenial 16.04 LTS")
RELEASE=$(dialog --stdout --title "Choose a release" --backtitle "$backtitle" --menu "Select the target OS release" \
$TTY_Y $TTY_X $(($TTY_Y - 8)) "${options[@]}")
unset options
[[ -z $RELEASE ]] && exit_with_error "No release selected"
fi
if [[ $KERNEL_ONLY != yes && -z $BUILD_DESKTOP ]]; then
options=()
options+=("no" "Image with console interface (server)")
options+=("yes" "Image with desktop environment")
BUILD_DESKTOP=$(dialog --stdout --title "Choose image type" --backtitle "$backtitle" --no-tags --menu "Select the target image type" \
$TTY_Y $TTY_X $(($TTY_Y - 8)) "${options[@]}")
unset options
[[ -z $BUILD_DESKTOP ]] && exit_with_error "No option selected"
fi
2015-12-11 20:49:10 +03:00
source $SRC/lib/configuration.sh
2015-12-11 20:49:10 +03:00
# sync clock
2016-04-16 18:12:34 +03:00
if [[ $SYNC_CLOCK != no ]]; then
display_alert "Syncing clock" "host" "info"
2016-08-30 19:25:32 +03:00
ntpdate -s ${NTP_SERVER:- time.ijs.si}
2015-12-11 20:49:10 +03:00
fi
start=`date +%s`
2016-04-16 18:12:34 +03:00
[[ $CLEAN_LEVEL == *sources* ]] && cleaning "sources"
2015-12-11 20:49:10 +03:00
# ignore updates help on building all images - for internal purposes
# fetch_from_repo <url> <dir> <ref> <subdir_flag>
if [[ $IGNORE_UPDATES != yes ]]; then
display_alert "Downloading sources" "" "info"
fetch_from_repo "$BOOTSOURCE" "$BOOTDIR" "$BOOTBRANCH" "yes"
BOOTSOURCEDIR=$BOOTDIR/${BOOTBRANCH##*:}
fetch_from_repo "$KERNELSOURCE" "$KERNELDIR" "$KERNELBRANCH" "yes"
LINUXSOURCEDIR=$KERNELDIR/${KERNELBRANCH##*:}
fi
2016-08-30 19:25:32 +03:00
compile_sunxi_tools
2015-12-11 20:49:10 +03:00
2016-10-18 00:26:56 +03:00
# Here we want to rename LINUXFAMILY from sun4i, sun5i, etc for next and dev branches
# except for sun8i-dev which is separate from sunxi-dev
if [[ $LINUXFAMILY == sun*i && $BRANCH != default ]]; then
[[ ! ( $LINUXFAMILY == sun8i && $BRANCH == dev ) ]] && LINUXFAMILY="sunxi"
fi
2015-12-11 20:49:10 +03:00
# define package names
DEB_BRANCH=${BRANCH//default}
2016-04-02 13:40:09 +03:00
# if not empty, append hyphen
DEB_BRANCH=${DEB_BRANCH:+${DEB_BRANCH}-}
CHOSEN_UBOOT=linux-u-boot-${DEB_BRANCH}${BOARD}
CHOSEN_KERNEL=linux-image-${DEB_BRANCH}${LINUXFAMILY}
CHOSEN_ROOTFS=linux-${RELEASE}-root-${DEB_BRANCH}${BOARD}
2015-12-11 20:49:10 +03:00
for option in $(tr ',' ' ' <<< "$CLEAN_LEVEL"); do
[[ $option != sources ]] && cleaning "$option"
2015-12-11 20:49:10 +03:00
done
# Compile u-boot if packed .deb does not exist
if [[ ! -f $DEST/debs/${CHOSEN_UBOOT}_${REVISION}_${ARCH}.deb ]]; then
2016-11-03 20:57:56 +03:00
compile_uboot
fi
# Compile kernel if packed .deb does not exist
if [[ ! -f $DEST/debs/${CHOSEN_KERNEL}_${REVISION}_${ARCH}.deb ]]; then
2016-11-03 20:57:56 +03:00
compile_kernel
fi
2015-12-11 20:49:10 +03:00
overlayfs_wrapper "cleanup"
2016-08-30 19:25:32 +03:00
# extract kernel version from .deb package
VER=$(dpkg --info $DEST/debs/${CHOSEN_KERNEL}_${REVISION}_${ARCH}.deb | grep Descr | awk '{print $(NF)}')
VER="${VER/-$LINUXFAMILY/}"
# create board support package
[[ -n $RELEASE && ! -f $DEST/debs/$RELEASE/${CHOSEN_ROOTFS}_${REVISION}_${ARCH}.deb ]] && create_board_package
# build additional packages
[[ $EXTERNAL_NEW == compile ]] && chroot_build_packages
if [[ $KERNEL_ONLY != yes ]]; then
2016-08-09 18:26:22 +03:00
debootstrap_ng
else
display_alert "Kernel build done" "@host" "info"
display_alert "Target directory" "$DEST/debs/" "info"
2016-03-23 20:58:58 +01:00
display_alert "File name" "${CHOSEN_KERNEL}_${REVISION}_${ARCH}.deb" "info"
2015-12-11 20:49:10 +03:00
fi
end=`date +%s`
2016-04-16 18:12:34 +03:00
runtime=$(((end-start)/60))
display_alert "Runtime" "$runtime min" "info"