Android source目錄添加編譯工程指令碼(含ccache)

來源:互聯網
上載者:User

標籤:指令碼   android編譯   編譯   提速   核心   

將此指令碼置於android 工程源碼根目錄,即可用此指令碼起build,且取名為compile.sh

#!/bin/bash
#
# Copyright (c) 2012, The Linux Foundation. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials provided
# with the distribution.
# * Neither the name of The Linux Foundation nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
# IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#

set -o errexit

usage() {
cat <<USAGE

Usage:
bash $0 <TARGET_PRODUCT> [OPTIONS]

Description:
Builds Android tree for given TARGET_PRODUCT

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

-d, --debug
Enable debugging - captures all commands while doing the build

-h, --help
Display this help message

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

-j, --jobs
Specifies the number of jobs to run simultaneously (Default: 8)

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

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

-m, --module
Module to be build

-p, --project
Project to be build

-s, --setup_ccache
Set CCACHE for faster incremental builds (true/false - Default: true)

-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
}

build_android() {
echo -e "\nINFO: Build Android tree for $TARGET\n"
make [email protected] | tee $LOG_FILE.log
}

build_bootimg() {
echo -e "\nINFO: Build bootimage for $TARGET\n"
make bootimage [email protected] | tee $LOG_FILE.log
}

build_sysimg() {
echo -e "\nINFO: Build systemimage for $TARGET\n"
make systemimage [email protected] | tee $LOG_FILE.log
}

build_usrimg() {
echo -e "\nINFO: Build userdataimage for $TARGET\n"
make userdataimage [email protected] | tee $LOG_FILE.log
}

build_module() {
echo -e "\nINFO: Build $MODULE for $TARGET\n"
make $MODULE [email protected] | tee $LOG_FILE.log
}

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

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

setup_ccache() {
export CCACHE_DIR=../.ccache
export USE_CCACHE=1
}

delete_ccache() {
prebuilts/misc/linux-x86/ccache/ccache -C
rm -rf $CCACHE_DIR
}

create_ccache() {
echo -e "\nINFO: Setting CCACHE with 10 GB\n"
setup_ccache
delete_ccache
prebuilts/misc/linux-x86/ccache/ccache -M 10G
}

# Set defaults
VARIANT="userdebug"
JOBS=8
CCACHE="true"

# Setup getopt.
long_opts="clean_build,debug,help,image:,jobs:,kernel_defconf:,log_file:,module:,"
long_opts+="project:,setup_ccache:,update-api,build_variant:"
getopt_cmd=$(getopt -o cdhi:j:k:l:m:p:s:uv: --long "$long_opts" \
-n $(basename $0) -- "[email protected]") || \
{ 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";;
-d|--debug) DEBUG="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";;
-s|--setup_ccache) CCACHE="$2"; shift;;
-v|--build_variant) VARIANT="$2"; shift;;
--) shift; break;;
esac
shift
done

# Mandatory argument
if [ $# -eq 0 ]; then
echo -e "\nERROR: Missing mandatory argument: TARGET_PRODUCT\n"
usage
exit 1
fi
if [ $# -gt 1 ]; then
echo -e "\nERROR: Extra inputs. Need TARGET_PRODUCT only\n"
usage
exit 1
fi
TARGET="$1"; shift

if [ -z $LOG_FILE ]; then
LOG_FILE=$TARGET
fi

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

if [ "$CCACHE" = "true" ]; then
setup_ccache
fi

source build/envsetup.sh
lunch $TARGET-$VARIANT

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

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

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

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

if [ -n "$IMAGE" ]; then
build_$IMAGE "$CMD"
fi

build_android "$CMD"

指令碼說明:

1,比如產品名稱為mifi,則最簡單的編譯命令即為:./compile.sh s2,則此時會根據指令碼預設的配置去編譯,開啟8個jobs起build,預設為userdebug版本。

2,版本類型控制,可通過-v選項控制,後跟user,userdebug,eng。

3,此指令碼預設配置使用ccache,何謂ccache,compile cache簡稱ccache,顧名思義,編譯快取。啟動ccache編譯工程可以省去編譯過程中的重複工作環節,例如,某一.c檔案會include很多標頭檔,而標頭檔裡可能還會include很多標頭檔,如此編譯不同的檔案可能這些先行編譯的標頭檔可能會被重複load多次,從而耗費大量時間,這時候ccache的作用便發揮出來,編譯系統會從ccache的緩衝解析資料中擷取解析後的標頭檔資料,直接載入編譯。

4,本條作為3基礎之上的附加說明,此指令碼只是開啟了android編譯系統的ccache,而kernel部分則需要如下操作方可開啟:

kernel/Makefile檔案中的
S  = $(CROSS_COMPILE)as
LD  = $(CROSS_COMPILE)ld.bfd
CC  = $(CROSS_COMPILE)gcc
CPP  = $(CC) -E
之後添加:
ifneq ($(USE_CCACHE),)
  export CCACHE_COMPILERCHECK := content
  export CCACHE_SLOPPINESS := time_macros,include_file_mtime,file_macro
  export CCACHE_BASEDIR := /
  ccache := $(strip $(wildcard $(PWD)/../prebuilts/misc/linux-x86/ccache/ccache))
  ifdef ccache
    ifneq ($(ccache),$(firstword $(CC)))
      CC := $(ccache) $(CC)
    endif
    ccache =
  endif
endif

如此,kernel部分的編譯效率亦大增也。


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

Android source目錄添加編譯工程指令碼(含ccache)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.