OpenWrt編譯系統(1)之make之前


OpenWrt的編譯系統負責編譯出可以最終燒錄的img文件。由於全志的Tina采用的是OpenWrt編譯系統,所以這個系列以Tina編譯為例進行分析。

在make之前,需要設置環境變量,用到的命令是:

source build/envsetup.sh
lunch

這兩條命令干了什么?怎么做的?

 

打開build/envsetup.sh腳本,發現這個腳本幾乎全部是一些命令,如mm、mmm、cgrep、jgrep等(做過Android系統開發的對這些肯定相當眼熟了)。不錯,OpenWrt用的和Android類似的編譯系統。
關於這些命令的作用和實現方式,這里不作解釋,只看下envsetup.sh腳本中最后幾行:

if [ "x$SHELL" != "x/bin/bash" ]; then
    case `ps -o command -p $$` in
        *bash*)
            ;;
        *)
            echo "WARNING: Only bash is supported, use of other shell would lead to erroneous results"
            ;;
    esac
fi

# Execute the contents of any vendorsetup.sh files we can find.
for f in `test -d target && find -L target -maxdepth 4 -name 'vendorsetup.sh' | sort 2> /dev/null`
do
    echo "including $f"
    . $f
done
unset f

SHELL是Linux的一個環境變量,指定打開終端時啟用的默認shell,以Ubuntu 14.04為例:

$ echo $SHELL
/bin/bash

所以第一行就是用來判定當前系統shell是不是bash,如果不是通過"ps -o command -p $$"查看當前shell的名稱。

"ps -o command -p $$"解釋:

--$$:shell本身的pid,示例:

$ ps -p $$
   PID TTY          TIME CMD
 62208 pts/0    00:00:00 bash

---o command:ps命令的-o參數允許用戶指定顯示信息的格式,command指示只顯示CMD對應的信息,這里就是"bash"。

我們完整執行下"ps -o command -p $$"這個命令串:

$ ps -o command -p $$
COMMAND
bash

至此,shell檢測完成。

 

接下來的for循環加載target目錄下的所有vendorsetup.sh腳本,實現步驟:

"test -d target":當前目錄下存在target且是一個目錄
"find -L target -maxdepth 4 -name 'vendorsetup.sh'":在target目錄下查找"vendorsetup.sh"文件

". $f":加載並執行對應的"vendorsetup.sh"文件,我們看下/target/allwinner/astar_parrot/目錄下一個"vendorsetup.sh"文件內容:

add_lunch_combo astar_parrot-tina

add_lunch_combo是envsetup.sh中的一個命令函數,用來添加一個方案,函數實現:

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

shell數組的知識:

1、數組用括號來表示,元素用"空格"符號分割開,語法格式如下:array_name=(value1 ... valuen)

2、讀取數組元素值的一般格式是:${array_name[index]}

3、@ 或 * 可以獲取數組中的所有元素,例如:${my_array[*]}或${my_array[@]}

4、獲取數組元素個數:${#my_array[*]}或${#my_array[@]}

至此,上述add_lunch_combo函數就明了了哈。

 

最后,lunch函數用來遍歷add_lunch_combo添加的所有方案,並提供用戶選擇的交互口。

接下來分析下lunch函數實現細節。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM