Android源代码编译脚本

前言

在Android源代码编译过程中经常要source,lunch等一系列命令,这个脚本实现了默认保存编译log,无参可编译,可根据需求使用参数,可以单编,清编 update-api等

脚本代码

#!/bin/bash
##creat by Deep Lee for android build
# set -o errexit

usage() {
    cat <<USAGE

Usage:
    bash $0  [OPTIONS]

Description:
    Builds Android tree for given TARGET_PRODUCT

OPTIONS:
    -c, --clean_build
        Clean build - build from scratch by removing entire out dir

    -h, --help
        Display this help message

    -i, --image
        Specify image to be build/re-build (bootimg/sysimg/usrimg/all Default: all)

    -j, --jobs
        Specifies the number of jobs to run simultaneously (Default: CPU core Number)

    -k, --kernel_defconf
        Specify defconf file to be used for compiling Kernel

    -l, --log_file
        Log file to store build logs (Default: build.log)

    -m, --module
        Module to be build

    -p, --project
        Project to be build

    -u, --update-api
        Update APIs

    -v, --build_variant
        Build variant (Default: userdebug)

USAGE
}

clean_build() {
    echo -e "\nINFO: Removing entire out dir. . .\n"
    make clobber
}

set_jack_vm_arg() {

    if [ $MEMGB -gt 12 ];then
        echo -e "\nINFO: Build MEMGB $MEMGB for $TARGET And Use Default\n\n"
    elif [ $MEMGB -gt  8 ];then
        echo -e "\nINFO: Build MEMGB $MEMGB for $TARGET And Set Xmx to 8g\n\n"
        export JACK_SERVER_VM_ARGUMENTS="-Dfile.encoding=UTF-8 -XX:+TieredCompilation -Xmx8g"
    elif [ $MEMGB -gt  6 ];then
        echo -e "\nINFO: Build MEMGB $MEMGB for $TARGET And Set Xmx to 4g\n\n"
        export JACK_SERVER_VM_ARGUMENTS="-Dfile.encoding=UTF-8 -XX:+TieredCompilation -Xmx4g"
    fi

    prebuilts/sdk/tools/jack-admin kill-server
    prebuilts/sdk/tools/jack-admin start-server

}


build_android() {

    echo -e "\nINFO: Build Android tree for $TARGET\n"
    make $@ | tee $LOG_FILE
}

build_bootimg() {
    echo -e "\nINFO: Build bootimage for $TARGET\n"
    make bootimage $@ | tee $LOG_FILE
}

build_sysimg() {
    echo -e "\nINFO: Build systemimage for $TARGET\n"
    make systemimage $@ | tee $LOG_FILE
}

build_usrimg() {
    echo -e "\nINFO: Build userdataimage for $TARGET\n"
    make userdataimage $@ | tee $LOG_FILE
}

build_module() {
    echo -e "\nINFO: Build $MODULE for $TARGET\n"
    make $MODULE $@ | tee $LOG_FILE
}

build_project() {
    echo -e "\nINFO: Build $PROJECT for $TARGET\n"
    mmm $PROJECT | tee $LOG_FILE
}

update_api() {
    echo -e "\nINFO: Updating APIs\n"
    make update-api | tee $LOG_FILE
}

# Set defaults
#need modify here for your target board
TARGET="aosp_arm64"
VARIANT="userdebug"
#default make jobs is same as cpu core processor number
JOBS=$(cat /proc/cpuinfo | grep processor | wc -l)
#can modify here if need store more or less log file
LOG_MAX=5
LOG_DIR="Log"
LOG_FILE="${LOG_DIR}/$(date +"%Y%m%d_%H%M%S").log"
MEMTOTAL=$(cat /proc/meminfo |grep MemTotal | awk '{print $2;}')
MEMGB=$(( $MEMTOTAL/1024/1024 ))
IMAGE="all"
BUILDIMG="true";
# Setup getopt.
long_opts="clean_build,help,image:,jobs:,kernel_defconf:,log_file:,module:,build_variant:"
long_opts+="project:,update-api"
getopt_cmd=$(getopt -o chi:j:k:l:m:p:uv: --long "$long_opts" \
    -n $(basename $0) -- "$@") || \
    { echo -e "\nERROR: Getopt failed. Extra args\n"; usage; exit 1;}

eval set -- "$getopt_cmd"

while true; do
    case "$1" in
        -c|--clean_build) CLEAN_BUILD="true";;
        -h|--help) usage; exit 0;;
        -i|--image) IMAGE="$2"; shift;;
        -j|--jobs) JOBS="$2"; shift;;
        -k|--kernel_defconf) DEFCONFIG="$2"; shift;;
        -l|--log_file) LOG_FILE="$2"; shift;;
        -m|--module) MODULE="$2"; shift;;
        -p|--project) PROJECT="$2"; shift;;
        -u|--update-api) UPDATE_API="true";;
        -v|--build_variant) VARIANT="$2";shift;;
        --) shift; break;;
    esac
    shift
done


if [[ "${VARIANT}" != "user" ]] && [[ "${VARIANT}" != "userdebug" ]] && [[ "${VARIANT}" != "eng" ]]
then
    echo -e "ERROR: VARIANT=${VARIANT}"
    usage
    exit 1;
fi

CMD="-j $JOBS"
if [ "$DEBUG" = "true" ]; then
    CMD+=" showcommands"
fi
if [ -n "$DEFCONFIG" ]; then
    CMD+=" KERNEL_DEFCONFIG=$DEFCONFIG"
fi

export LC_ALL=C
export AUTO_TREE_ID=msmnile_gvmq
source build/envsetup.sh np
lunch msmnile_gvmq-${VARIANT}
set_jack_vm_arg

if [[ ! -d ${LOG_DIR} ]]
then
    mkdir ${LOG_DIR}
else
    log_num=$(ls ${LOG_DIR} | wc -l)
    echo "log number $log_num"

    while [[ ${log_num} -ge ${LOG_MAX} ]]
    do
        old_log=$(ls ${LOG_DIR} | head -1)
        echo $old_log
        rm -f  ${LOG_DIR}/${old_log}
        log_num=$(ls ${LOG_DIR} | wc -l)
    done
fi

if [ "$CLEAN_BUILD" = "true" ]; then
    clean_build
fi

if [ "$UPDATE_API" = "true" ]; then
    update_api
fi

if [ -n "$MODULE" ]; then
    BUILDIMG="false"
    build_module "$CMD"
fi

if [ -n "$PROJECT" ]; then
    BUILDIMG="false"
    build_project
fi

if [ "$BUILDIMG" == "true" ]; then
    if [ "$IMAGE" == "all" ]; then
        build_android "$CMD"
    else
        build_$IMAGE "$CMD"
    fi
fi

ret=$(tail -n 2 ${LOG_FILE})
if [[ "${ret}" == *"failed"* ]]; then
    exit 8
else
    exit 0
fi

你可能感兴趣的:(Android,ROM开发,Android,软件开发,Linux)