1 ........ 2 # 測試device是否存在且是一個目錄 而且 只查找device目錄4層以上的子目錄,名字爲vendorsetup.sh 而且 將命令執行的錯誤報告直接送往回收站 不顯示在屏幕上 3 # 測試vendor是否存在且是一個目錄 而且 只查找vendor目錄4層以上的子目錄,名字爲vendorsetup.sh 而且 將命令執行的錯誤報告直接送往回收站 不顯示在屏幕上 4 for f in `test -d device && find -L device -maxdepth 4 -name 'vendorsetup.sh' 2> /dev/null` \ 5 `test -d vendor && find -L vendor -maxdepth 4 -name 'vendorsetup.sh' 2> /dev/null` 6 do 7 # 打印包含這些vendorsetup.sh 並 執行這些vendorsetup.sh 8 echo "including $f" 9 . $f 10 done 11 unset f 12 ........
↑第一步:build/envsetup.sh中vendrosetup.sh腳本執行java
1 root@book-virtual-machine:/work/android-5.0.2# source build/envsetup.sh 2 including device/asus/deb/vendorsetup.sh 3 including device/asus/fugu/vendorsetup.sh 4 including device/asus/tilapia/vendorsetup.sh 5 including device/asus/flo/vendorsetup.sh 6 including device/asus/grouper/vendorsetup.sh 7 including device/samsung/manta/vendorsetup.sh 8 including device/friendly-arm/tiny4412/vendorsetup.sh 9 including device/lge/mako/vendorsetup.sh 10 including device/lge/hammerhead/vendorsetup.sh 11 including device/moto/shamu/vendorsetup.sh 12 including device/generic/mini-emulator-mips/vendorsetup.sh 13 including device/generic/mini-emulator-armv7-a-neon/vendorsetup.sh 14 including device/generic/mini-emulator-arm64/vendorsetup.sh 15 including device/generic/mini-emulator-x86/vendorsetup.sh 16 including device/generic/mini-emulator-x86_64/vendorsetup.sh 17 including sdk/bash_completion/adb.bash
↑第二步:build/envsetup.sh中vendrosetup.sh腳本執行部分的執行效果android
1 add_lunch_combo full_tiny4412-userdebug 2 add_lunch_combo full_tiny4412-eng api
↑第三步:舉例:including device/friendly-arm/tiny4412/vendorsetup.sh內容數組
1 # 清空LUNCH_MENU_CHOICES 2 unset LUNCH_MENU_CHOICES 3 function add_lunch_combo() 4 { 5 # new_combo局部變量化,且賦值爲add_lunch_combo的參數 6 # 即add_lunch_combo full_tiny4412-userdebug的full_tiny4412-userdebug 7 # 局部變量化c 8 local new_combo=$1 9 local c 10 # 若是LUNCH_MENU_CHOICES有參數 11 for c in ${LUNCH_MENU_CHOICES[@]} ; do 12 # 若是add_lunch_combo參數1存在於LUNCH_MENU_CHOICES中 則返回 13 if [ "$new_combo" = "$c" ] ; then 14 return 15 fi 16 done 17 # 若是LUNCH_MENU_CHOICES無參數,則將add_lunch_combo的參數1放入LUNCH_MENU_CHOICES 18 LUNCH_MENU_CHOICES=(${LUNCH_MENU_CHOICES[@]} $new_combo) 19 }
↑第四步:build/envsetup.sh中add_lunch_combo功能函數bash
1 function lunch() 2 { 3 # answer局部變量化 4 local answer 5 # lunch若是有參數則answer=參數1 6 if [ "$1" ] ; then 7 answer=$1 8 else 9 # 若是沒有參數則打印lunch目錄 10 print_lunch_menu 11 ....... 12 }
↑第五步:build/envsetup.sh中lunch功能函數的第一步,打印選項架構
1 function print_lunch_menu() 2 { 3 # 系統名稱局部變量化,並打印幫助信息 4 local uname=$(uname) 5 echo 6 echo "You're building on" $uname 7 echo 8 echo "Lunch menu... pick a combo:" 9 # 初始化局部變量i=1,這個是索引編號 10 local i=1 11 # 初始化局部變量,這個是用戶輸入編號或選項英文 12 local choice 13 # 從LUNCH_MENU_CHOICES中取出放到choice中 14 # 並打印 「編號.LUNCH_MENU_CHOICE中的英文選項」 15 # 編號i累加 16 for choice in ${LUNCH_MENU_CHOICES[@]} 17 do 18 echo " $i. $choice" 19 i=$(($i+1)) 20 done 21 22 echo 23 }
↑第六步:build/envsetup.sh中print_lunch_menu功能函數app
1 ......#接上面打印菜單 2 echo -n "Which would you like? [aosp_arm-eng] " 3 # 讀取用戶輸入的編號或者英文選項 4 read answer 5 fi 6 7 local selection= 8 # 若是沒有輸入編號或者英文選項,則默認選擇了aosp_arm-eng 9 if [ -z "$answer" ] 10 then 11 selection=aosp_arm-eng 12 # 若是輸入信息爲數字,且數字小於LUNCH_MENU_CHOICES個數 13 elif (echo -n $answer | grep -q -e "^[0-9][0-9]*$") 14 then 15 if [ $answer -le ${#LUNCH_MENU_CHOICES[@]} ] 16 # 將輸入編號-1的英文選項放入selection(由於數組是從0開始,0對應索引1) 17 then 18 selection=${LUNCH_MENU_CHOICES[$(($answer-1))]} 19 fi 20 # 若是輸入信息爲具備-符號的則爲英文 直接放入selection 21 elif (echo -n $answer | grep -q -e "^[^\-][^\-]*-[^\-][^\-]*$") 22 then 23 selection=$answer 24 fi 25 # 若是此時selection依舊爲0,則打印無效lunch combo:輸入值 26 if [ -z "$selection" ] 27 then 28 echo 29 echo "Invalid lunch combo: $answer" 30 return 1 31 fi 32 # 導出編譯使用的環境變量 33 export TARGET_BUILD_APPS= 34 # 提取"-"位置之前的字符串放入product,好比「full_tiny4412」 35 local product=$(echo -n $selection | sed -e "s/-.*$//") 36 check_product $product 37 if [ $? -ne 0 ] 38 then 39 echo 40 echo "** Don't have a product spec for: '$product'" 41 echo "** Do you have the right repo manifest?" 42 product= 43 fi 44 # 提取"-"位置之後的字符串放入variant,好比「userdebug」 45 local variant=$(echo -n $selection | sed -e "s/^[^\-]*-//") 46 check_variant $variant 47 if [ $? -ne 0 ] 48 then 49 echo 50 echo "** Invalid variant: '$variant'" 51 echo "** Must be one of ${VARIANT_CHOICES[@]}" 52 variant= 53 fi 54 # 若是product、variant爲空則返回1 55 if [ -z "$product" -o -z "$variant" ] 56 then 57 echo 58 return 1 59 fi 60 # 導出TARGET_PRODUCT=剛剛獲得的product 61 # 導出TARGET_BUILD_VARIANT=剛剛獲得的variant 62 # 導出TARGET_BUILD_TYPE=release 63 64 export TARGET_PRODUCT=$product 65 export TARGET_BUILD_VARIANT=$variant 66 export TARGET_BUILD_TYPE=release 67 68 echo 69 # 設置編譯相關的環境功能函數 70 set_stuff_for_environment 71 # 後面會調用get_build_var report_config 72 # 裏面會執行make --no-print-directory -C "$T" -f build/core/config.mk dumpvar-$1 73 # 即 編譯config.mk 74 printconfig 75 }
↑第七步:build/envsetup.sh中lunch功能函數函數
1 function set_stuff_for_environment() 2 { 3 # 設置名稱變量,好比架構、產品、主板、應用、命令行提示符 4 settitle 5 # 設置JAVA jdk環境變量 6 set_java_home 7 # 設置Android PATH、ANDROID_TOOLCHAIN、ANDROID_KERNEL_TOOLCHAIN_PATH、ANDROID_EMULATOR_PREBUILTS等路徑 8 setpaths 9 # 設置BUILD_ENV_SEQUENCE_NUMBER 10 set_sequence_number 11 12 export ANDROID_BUILD_TOP=$(gettop) 13 # 設置gcc的警告和錯誤能夠彩色顯示 14 export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01' 15 }
↑第八步:build/envsetup.sh中set_stuff_for_environment功能函數測試
1 function settitle() 2 { 3 if [ "$STAY_OFF_MY_LAWN" = "" ]; then 4 local arch=$(gettargetarch) 5 local product=$TARGET_PRODUCT 6 local variant=$TARGET_BUILD_VARIANT 7 local apps=$TARGET_BUILD_APPS 8 # 導出PROMPT_COMMAND,配合HISTORY_FILE設置的文件,進行記錄什麼用戶,在什麼時間,作了什麼操做。 9 # PROMPT_COMMAND主要是設置打印信息格式 10 if [ -z "$apps" ]; then 11 export PROMPT_COMMAND="echo -ne \"\033]0;[${arch}-${product}-${variant}] ${USER}@${HOSTNAME}: ${PWD}\007\"" 12 else 13 export PROMPT_COMMAND="echo -ne \"\033]0;[$arch $apps $variant] ${USER}@${HOSTNAME}: ${PWD}\007\"" 14 fi 15 fi 16 }
↑第九步:build/envsetup.sh中settitle功能函數ui
關於set_java_home setpaths set_sequence_number設置函數不在贅述,同類方法
1 function printconfig() 2 { 3 T=$(gettop) 4 if [ ! "$T" ]; then 5 echo "Couldn't locate the top of the tree. Try setting TOP." >&2 6 return 7 fi 8 # 這裏繼續調用get_build_var report_config 9 # get_build_var會編譯config.mk詳見hmm筆記 10 # report_config做爲dumpvar-report_config 11 # dumpvar.mk中ifneq ($(dumpvar_goals),report_config)中會將PRINT_BUILD_CONFIG置空 12 # ifneq ($(PRINT_BUILD_CONFIG),) :PRINT_BUILD_CONFIG爲空則執行打印描述編譯環境配置的變量信息 13 get_build_var report_config 14 }
↑第十步:build/envsetup.sh中printconfig功能函數與build/core/dumpvar.mk中打印編譯環境配置
最後編譯config.mk,config.mk內容見config.mk筆記