By: fulinux
E-mail: fulinux@sina.com
Blog: https://blog.csdn.net/fulinus
喜歡的盆友歡迎點贊和訂閱!
你的喜歡就是我寫作的動力!
目錄
- 類函式的靈活繼承
- 去除黃色警告
我應該一開始創建一個functions的recipe來專門講這一系列的內容就好了,我從本篇開始也不遲,
poky]$ cd meta-mylayer/
meta-mylayer]$ cp recipes-example/ recipes-myfunctions -rf #賦值一份,改個名字
meta-mylayer]$ cd recipes-myfunctions/
recipes-myfunctions]$ mv example/ myfunctions
recipes-myfunctions]$ mv myfunctions/example_0.1.bb myfunctions/myfunctions_0.1.bb
#最終創建
meta-mylayer/recipes-myfunctions/myfunctions/myfunctions_0.1.bb
#并將檔案中的PN = 'example'改成:
PN = 'myfunctions'
后面我就基于這個檔案修改,
今天講類函式的靈活繼承
類函式的靈活繼承
這節講的是創建一個自己的class檔案,class檔案中定義一個do_faa函式,然后在myfunctions的bb檔案中繼承該class類,如何在myfunctions中重定義do_faa函式,又能呼叫class中定義的do_faa函式,怎么做呢?通過使用EXPORT_FUNCTIONS陳述句,
首先我們參考meta/classes/目錄中class檔案形式,在meta-mylayer/目錄中也定義一個自己的class檔案,名字叫bar.bbclass,參考如下:
poky]$ mkdir meta-mylayer/classes
poky]$ vim meta-mylayer/classes/bar.bbclass
好,下面在bar.bbclass檔案中定義一個do_faa函式,bar.bbclass檔案內容如下:
bar_do_faa() {
bbplain "I am do_faa function in bar.bbclass"
}
addtask faa after do_fetch before do_build
EXPORT_FUNCTIONS do_faa
等等,你不是說,定義do_faa函式嗎?怎么變成了bar_do_faa函式?
誒,這就是class中定義函式的規則了,函式名定義的陳述句格式如下:
classname_functionname
EXPORT_FUNCTIONS functionname
我們的函式名是do_faa,那么組合起來就是:
bar_do_faa() {
...
}
EXPORT_FUNCTIONS do_faa
然后addtask陳述句呢?又要去掉classname_或者classname_do, 這里去掉的就是bar_do, 然后EXPORT_FUNCTIONS陳述句宣告時也要去掉class檔案名bar_,
前面的yocto-第42篇-bb檔案中的幾個關鍵詞require DEPENDS inherit文章中我們講到recipe檔案中參考class檔案中的內容需要用inherit,因此我們在myfunctions_0.1.bb檔案中繼承該類:
poky]$ vim meta-mylayer/recipes-myfunctions/myfunctions/myfunctions_0.1.bb
PN = 'myfunctions'
inherit bar
...
我們繼承了,然后呢,安裝addtask陳述句的規則,是在do_fetch后在do_build前執行do_faa函式,那么理論上我們不做其他呼叫就能執行,編譯:
poky]$ source oe-init-build-env
build]$ bitbake myfunctions
看效果:

如上圖所示,列印了do_faa中的陳述句:
I am do_faa function in bar.bbclass
好,如果我們在recipe檔案,即myfunctions_0.1.bb檔案中定義了同名函式,那么會發生什么呢?上菜:
poky]$ vim meta-mylayer/recipes-myfunctions/myfunctions/myfunctions_0.1.bb
...
do_faa() {
bbplain "I am do_faa function in myfunctions recipe"
}
重編譯:
build]$ bitbake myfunctions -c cleansstate
build]$ bitbake myfunctions
...
WARNING: python should use 4 spaces indentation, but found tabs in myfunctions_0.1.bb, line 61
I am do_faa function in myfunctions recipe
...
說明我們覆寫了bar.bbclass中定義的do_faa函式,那么在重名的情況下,我們該如何呼叫class中的do_faa函式呢?
簡單,在recipe檔案中的do_faa函式中呼叫bar_do_faa函式,如下:
do_faa() {
bbplain "I am do_faa function in myfunctions recipe"
bar_do_faa #加了這一行
}
執行效果如下:
build]$ bitbake myfunctions -c cleansstate
build]$ bitbake myfunctions
...
WARNING: python should use 4 spaces indentation, but found tabs in myfunctions_0.1.bb, line 61
I am do_faa function in myfunctions recipe
I am do_faa function in bar.bbclass
...

通常是有個條件來觸發執行那個邏輯陳述句的,我將函式再改下:
FAACONDITION = "" #條件不存在
do_faa() {
if [ -e FAACONDITION ] ; then
bbplain "I am do_faa function in myfunctions recipe"
else
bar_do_faa
fi
}
看效果:

去除黃色警告
加個餐哦!
很多盆友看到圖片中黃色警告很煩,警告我們python函式的縮進應該是4個空格,但是實際是個tab鍵,怎么改呢?
build]$ vim ../meta-mylayer/recipes-myfunctions/myfunctions/myfunctions_0.1.bb
輸入
:set list
可以看到縮進的地方是“^I”,表示tab鍵,

這個時候我們可以將縮進改成4個空格,如下:
vim ~/.vimrc
#如果沒有`~/.vimrc`,可以修改
vim /etc/vim/vimrc
#去掉:
set noexpandtab
#改成:
set expandtab
#加上:
set shiftwidth=4
set tabstop=4
set softtabstop=4
再編輯myfunctions_0.1.bb檔案:
build]$ vim ../meta-mylayer/recipes-myfunctions/myfunctions/myfunctions_0.1.bb
#在命令列模式下輸入gg=G 就可以自動調整了
#不過呢,有些陳述句后面沒有“;”,會出現縮進例外,此時可以手動調整,或者在一些陳述句后面加上";"
#再set list就看不到“^I”了
我的部分截圖:

喏,再次執行就沒有這些討厭的黃色陳述句了:

給我點個贊唄!
CSDN認證博客專家
linux
uboot
yocto
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/263017.html
標籤:其他
