Android Source directory Add compile Engineering script (with CCache)

Source: Internet
Author: User

Put this script in the Android project source root directory, you can use this script to build, and named compile.sh

#!/bin/bash
#
# Copyright (c), the Linux Foundation. All rights reserved.
#
# redistribution and use in source and binary forms, with or without
# modification, is permitted provided that the following conditions is
# 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 is 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
# is 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-out-of-the----software, even
# IF advised of the possibility of SUCH DAMAGE.
#

Set-o Errexit

Usage () {
Cat <<usage

Usage:
Bash <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 is used for compiling Kernel

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

-M,--module
Module to BES Build

-P,--project
Project to BES 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 ten 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 $)--"[email protected]") | | \
{ECHO-E "\nerror:getopt failed. Extra args\n "; Usage Exit 1;}

Eval set--"$getopt _cmd"

While true; Do
Case "$" in
-c|--clean_build) clean_build= "true";;
-d|--debug) debug= "true";;
-H|--HELP) usage; Exit 0;;
-i|--image) image= "$"; shift;
-j|--jobs) jobs= "$"; shift;
-k|--kernel_defconf) defconfig= "$"; shift;
-l|--log_file) log_file= "$"; shift;
-m|--module) module= "$"; shift;
-p|--project) project= "$"; shift;
-U|--UPDATE-API) update_api= "true";;
-s|--setup_ccache) ccache= "$"; shift;
-v|--build_variant) variant= "$"; 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= "$"; 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"

Script Description:

1, for example, the product name is MiFi, then the simplest compile command is:./compile.sh S2, this time will be compiled according to the script default configuration, open 8 jobs build, the default is Userdebug version.

2, Version type control, can be controlled by the-v option followed by User,userdebug,eng.

3, this script default configuration uses CCache, what is the ccache,compile cache abbreviation CCache, as the name implies, the compilation cache. Starting the CCache compilation project can eliminate the duplication of work during the compilation process, for example, A. c file will include a lot of header files, and the header file may include many header files, so compile different files may be the precompiled header files may be repeated load multiple times, thus consuming a lot of time, when the role of CCache, the compilation system will parse the data from the CCache cache Gets the parsed header file data and loads it directly into the compilation.

4, this article as an additional note on the basis of 3, this script only opens the Android compiler system CCache, and the kernel section needs the following operation to open:

In the Kernel/makefile file
S = $ (cross_compile) as
LD = $ (cross_compile) LD.BFD
CC = $ (cross_compile) gcc
CPP = $ (CC)-E
Add later:
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

Thus, the compilation efficiency of the kernel part is also greatly increased.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Source directory Add compile Engineering script (with CCache)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.