Android source build/envsetup.sh 及lunch 過程

1,source build/envsetup.sh
        source 是用來運行 shell 腳本的命令  功能和 "."  和相同因此 也可以寫作: . build/envsetup.sh
運行效果結果如下:
xxx@xxx:~/xxx/kitkat$ . build/envsetup.sh
including device/generic/armv7-a-neon/vendorsetup.sh
including device/generic/x86/vendorsetup.sh
including device/generic/mips/vendorsetup.sh
including sdk/bash_completion/adb.bash
buddy@RD3-199:~/C_BQ_T628VX_01/kitkat-mstar-master$
那麼上面的結果是如何產生的呢?運行envsetup.sh的發生了什麼呢?我們自己添加的客戶分支是如何加入到 lunch 的選項中去的呢?
a,關於envsetup
envsetup 爲我們提供了很多的shell腳本函數,例如 envsetup 開頭的第一個函數 hmm,
function hmm() {
cat <<EOF
Invoke ". build/envsetup.sh" from your shell to add the following functions to your environment:
- lunch: lunch <product_name>-<build_variant>
- tapas: tapas [<App1> <App2> ...] [arm|x86|mips|armv5] [eng|userdebug|user]
- croot: Changes directory to the top of the tree.
- m: Makes from the top of the tree.
- mm: Builds all of the modules in the current directory, but not their dependencies.
- mmm: Builds all of the modules in the supplied directories, but not their dependencies.
- mma: Builds all of the modules in the current directory, and their dependencies.
- mmma: Builds all of the modules in the supplied directories, and their dependencies.
- cgrep: Greps on all local C/C++ files.
- jgrep: Greps on all local Java files.
- resgrep: Greps on all local res/*.xml files.
- godir: Go to the directory containing a file.
 
Look at the source to view more functions. The complete list is:
EOF
T=$(gettop)    #gettop也是envsetup.sh 中定義的一個 函數,有興趣的童鞋可以看一下具體實現
local A
A=""
for i in `cat $T/build/envsetup.sh | sed -n "/^function /s/function \([a-z_]*\).*/\1/p" | sort`; do
A="$A $i"
done
echo $A
}
在運行envsetup .sh以後 就可以使用 hmm命令了,此時終端會輸出命令幫助提示。
簡單來說,envsetup.sh  給我們提供了一組自定義的 shell命令,而lunch 就是這些自定義命令中的一個。

2,lunch 命令
我們依然來看  envsetup.sh,在envsetup.sh 中可以找到 lunch 定義
function lunch()
{
local answer
 
if [ "$1" ] ; then #lunch可以接受一個輸入參數,例如 lunch xxx-userdebug 此時直接選擇了xxx。
answer=$1
else # 如果沒有輸入參數,則打印選擇菜單,並等待選擇輸入
print_lunch_menu
echo -n "Which would you like? [aosp_arm-eng] "
read answer
fi
 
local selection=
 
if [ -z "$answer" ] #如果answer爲空
then
selection=aosp_arm-eng #默認選擇aosp_arm-eng
elif (echo -n $answer | grep -q -e "^[0-9][0-9]*$")
then
if [ $answer -le ${#LUNCH_MENU_CHOICES[@]} ]
then
selection=${LUNCH_MENU_CHOICES[$(($answer-1))]}
fi
elif (echo -n $answer | grep -q -e "^[^\-][^\-]*-[^\-][^\-]*$") #不以 ‘-’ 開頭和結束
then
selection=$answer
fi
 
if [ -z "$selection" ]
then
echo
echo "Invalid lunch combo: $answer"
return 1
fi
#導出編譯使用的環境變量 
export TARGET_BUILD_APPS=
#提取"-"位置以前的字符串 這裏是“xxx” 
local product=$(echo -n $selection | sed -e "s/-.*$//")
check_product $product
if [ $? -ne 0 ]
then
echo
echo "** Don't have a product spec for: '$product'"
echo "** Do you have the right repo manifest?"
product=
fi
#提取"-"位置以後的字符串“ userdebug  
local variant=$(echo -n $selection | sed -e "s/^[^\-]*-//")
check_variant $variant
if [ $? -ne 0 ]
then
echo
echo "** Invalid variant: '$variant'"
echo "** Must be one of ${VARIANT_CHOICES[@]}"
variant=
fi
 
if [ -z "$product" -o -z "$variant" ] #如果爲空則返回
then
echo
return 1
fi
 
#導出編譯使用的環境變量
export TARGET_PRODUCT=$product
export TARGET_BUILD_VARIANT=$variant
export TARGET_BUILD_TYPE=release
 
echo
 
set_stuff_for_environment
printconfig
}
看一看其中調用的幾個函數

print_lunch_menu
function print_lunch_menu()
{
local uname=$(uname)
echo
echo "You're building on" $uname
echo
echo "Lunch menu... pick a combo:"
 
local i=1
local choice
for choice in ${LUNCH_MENU_CHOICES[@]} #這裏輸出的就是我們看到的 lunch 命令的輸出結果
do
echo " $i. $choice"
i=$(($i+1))
done
 
echo
}
那麼 LUNCH_MENU_CHOICES 變量的數據又是從何而來呢?
# Clear this variable. It will be built up again when the vendorsetup.sh
# files are included at the end of this file.
unset LUNCH_MENU_CHOICES
#這函數就是向lunch命令添加選擇項 客戶分支裏 vendorsetup.sh 裏的那一行代碼的用途就是添加lunch選擇項
function add_lunch_combo()
{
local new_combo=$1
local c
for c in ${LUNCH_MENU_CHOICES[@]} ; do
if [ "$new_combo" = "$c" ] ; then
return
fi
done
LUNCH_MENU_CHOICES=(${LUNCH_MENU_CHOICES[@]} $new_combo)
}
 
# add the default one here
add_lunch_combo aosp_arm-eng
add_lunch_combo aosp_x86-eng
add_lunch_combo aosp_mips-eng
add_lunch_combo vbox_x86-eng
我們來看看 check_product 的作用
# check to see if the supplied product is one we can build
function check_product()
{
T=$(gettop)
if [ ! "$T" ]; then
echo "Couldn't locate the top of the tree. Try setting TOP." >&2
return
fi
#設置環境變量
CALLED_FROM_SETUP=true BUILD_SYSTEM=build/core \
TARGET_PRODUCT=$1 \
TARGET_BUILD_VARIANT= \
TARGET_BUILD_TYPE= \
TARGET_BUILD_APPS= \
get_build_var TARGET_DEVICE > /dev/null #get_build_var
# hide successful answers, but allow the errors to show
}
# Get the exact value of a build variable.
function get_build_var()
{
T=$(gettop)
if [ ! "$T" ]; then
echo "Couldn't locate the top of the tree. Try setting TOP." >&2
return
fi
    #執行config.mk配置相關的內容,
    #進一步依次執行envsetup.mk,到product_config.mk,
    #最終獲得TARGET_DEVICE變量的內容。
CALLED_FROM_SETUP=true BUILD_SYSTEM=build/core \
make --no-print-directory -C "$T" -f build/core/config.mk dumpvar-$1
}
check_variant 函數
#三種模式 user userdebug eng
VARIANT_CHOICES=(user userdebug eng)
# check to see if the supplied variant is valid
function check_variant()
{
for v in ${VARIANT_CHOICES[@]}
do
if [ "$v" = "$1" ]
then
return 0
fi
done
return 1
}
以下內容待驗證
#1.eng,那麼其編譯進去的內容包括:
· Intended for platform-level debugging
· Installs modules tagged with: eng, debug, user, and/or development
· Installs non-APK modules that have no tags specified
· Installs APKs according to the product definition files, in addition to tagged APKs
· Sets ro.secure=1
· Sets ro.debuggable=0
· Sets ro.kernel.android.checkjni=1
· adbd is enabled by default
 
#2.user,那麼其編譯進去的內容包括:
· Intended to be the final release
· Installs modules tagged as user
· Installs non-APK modules that have no tags specified
· Installs APKs according to the product definition files (tags are ignored for APK modules)
· Sets ro.secure=1
· Sets ro.debuggable=0
· adbd is disabled by default
 
#3.userdebug,那麼其編譯進去的內容包括:
the same as user, except:
· Intended for limited debugging
· Installs modules tagged with debug
· Sets ro.debuggable=1
· adbd is enabled by default
set_stuff_for_environment函數,主要設備編譯相關的環境
function set_stuff_for_environment()
{
settitle
set_java_home
setpaths
set_sequence_number
# MStar Android Patch Begin
set_ota
# MStar Android Patch End
 
export ANDROID_BUILD_TOP=$(gettop)
}
關於lunch 命令的過程已經基本清晰,那麼系統是如何找到客戶分支下的 vendorsetup.sh 呢?

3,掃描系統中的vendorsetup.sh 文件
# Execute the contents of any vendorsetup.sh files we can find.
# 檢測 device 目錄和vendor 目錄是否存在 ,並且找到 目錄下的vendorsetup.sh .最多目錄層次四層
for f in `test -d device && find device -maxdepth 4 -name 'vendorsetup.sh' 2> /dev/null` \
`test -d vendor && find vendor -maxdepth 4 -name 'vendorsetup.sh' 2> /dev/null`
do
echo "including $f"
. $f # 找到vendorsetup.sh 並執行,
done
unset f
 
addcompletions
function addcompletions()
{
local T dir f
 
# Keep us from trying to run in something that isn't bash.
if [ -z "${BASH_VERSION}" ]; then
return
fi
 
# Keep us from trying to run in bash that's too old.
if [ ${BASH_VERSINFO[0]} -lt 3 ]; then
return
fi
 
dir="sdk/bash_completion"
if [ -d ${dir} ]; then
# 掃描 /sdk/bash_completion 目錄並允許 .bash 文件
for f in `/bin/ls ${dir}/[a-z]*.bash 2> /dev/null`; do
echo "including $f"
. $f #運行bash文件
done
fi
}

source ./build/envsetup.sh
lunch
命令的基本過程大致如上所說。主要初始化編譯所需要的系統變量









發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章