Android.mk (1) 函數

來源:互聯網
上載者:User

標籤:http   構建   數列   過濾   問題   條件   思路   統計   res   

https://www.jianshu.com/p/46224d15fc5f

 

從函數說起

大家都習慣看從頭,從構建目標講起的,導致每篇文檔熟的都是前面的部分。很多教程也都是想辦法能夠觀其大略,從整體上給大家一個思路。比如《深入理解Android核心設計思想》的第4章,比如《Android核心剖析》的第18章,比如《深入解析Android 5.0系統》的第2章。

於是我打算反其道而行之,先從調用函數開始講。

最後一招:shell函數

我們最先把最後看家的絕招列出來吧,shell函數,可以用來執行shell命令。一切用之後講到的函數解決不了的問題,都可以靠shell函數調用外部功能來解決。
不過,調用shell需要啟動新進程,影響效能,只要有其它方法建議就不要使用它。

比如,我們想要列出目前的目錄下有哪些cpp檔案,makefile可以這樣寫:

files := $(shell ls *.cpp)all :    @echo -n "The files is:"    @echo $(files).PHONY : all
拼接字串

在Makefile中用途相當廣的就是拼接字串,join函數就是幹這事兒的。
格式:$(join 串1 , 串2)
也可以是多個串一起拼:$(join 串1 串2 串3, 串4 串5 串6)
多個串就是1和4拼,2和5拼。。。總之是逗號前的和逗號後的拼。

去空格

有事兒沒事兒,去去空格總不是壞事兒。
$(strip 字串)

檔案路徑操作函數

在實際的Makefile開發中,經常遇到要處理變數中儲存的路徑名。

先來個提綱:

  • 有一個不知道哪裡來的檔案名稱,想取它的路徑,用dir
  • 有一個不知道哪裡來的檔案名稱,想只取它的檔案名稱,用notdir
  • 如果只想要副檔名,判斷是什麼類型的,用suffix
  • 如果想不要副檔名,取出來拼個別的副檔名上去,比如xxx.c,只要xxx,將來拼個xxx.o或者就是xxx之類的,用basename
  • 想給檔案添加個副檔名,用addsuffix
  • 想要把檔案搞到另一個路徑上去,拼個目錄名上去,用addprefix
取一個檔案的目錄路徑函數dir

例:

.PHONY : all2oatfile := out/target/product/ali6753_65t_m0/obj/APPS/MusicFX_intermediates/oat/arm64/package.odexresult := $(dir $(oatfile))all2 :    @echo -n "The result is: "    @echo $(result)

輸出:The result is: out/target/product/ali6753_65t_m0/obj/APPS/MusicFX_intermediates/oat/arm64/

取檔案名稱函數

例:

.PHONY : all2oatfile := out/target/product/ali6753_65t_m0/obj/APPS/MusicFX_intermediates/oat/arm64/package.odexresult_dir := $(dir $(oatfile))result_notdir := $(notdir $(oatfile))all2 :    @echo "The result is: "    @echo $(result_dir)    @echo $(result_notdir)

輸出:
The result is:
out/target/product/ali6753_65t_m0/obj/APPS/MusicFX_intermediates/oat/arm64/
package.odex

取副檔名

例:

.PHONY : all2oatfile := out/target/product/ali6753_65t_m0/obj/APPS/MusicFX_intermediates/oat/arm64/package.odexresult_dir := $(dir $(oatfile))result_notdir := $(notdir $(oatfile))result_suffix := $(suffix $(oatfile))all2 :    @echo "The result is: "    @echo $(result_dir)    @echo $(result_notdir)    @echo $(result_suffix)

輸出:
The result is:
out/target/product/ali6753_65t_m0/obj/APPS/MusicFX_intermediates/oat/arm64/
package.odex
.odex

取檔案基本名

如果只用basename函數,是連路徑都有的,只去除掉了副檔名的名字。不過我們剛學過notdir,一起用下就是了。

例:

.PHONY : all2oatfile := out/target/product/ali6753_65t_m0/obj/APPS/MusicFX_intermediates/oat/arm64/package.odexresult_suffix := $(suffix $(oatfile))result_basename := $(basename $(oatfile))result_basename2 := $(basename $(notdir $(oatfile)))all2 :    @echo "The result is: "    @echo $(result_suffix)    @echo $(result_basename)    @echo $(result_basename2)

輸出:
The result is:
.odex
out/target/product/ali6753_65t_m0/obj/APPS/MusicFX_intermediates/oat/arm64/package
package

給名字加尾碼

使用addsuffix函數,$(addsuffix 檔案名稱,副檔名)
我們舉個例子說明:現在想把oat檔案壓縮成.tar.gz:

.PHONY : all2oatfile := out/target/product/ali6753_65t_m0/obj/APPS/MusicFX_intermediates/oat/arm64/package.odexresult_basename := $(basename $(oatfile))compress_oat := tar cfvz $(addsuffix .tar.gz , $(result_basename)) $(oatfile)all2 :    @echo "The result is: "    @echo $(compress_oat)

輸出如下:
The result is:
tar cfvz out/target/product/ali6753_65t_m0/obj/APPS/MusicFX_intermediates/oat/arm64/package.tar.gz out/target/product/ali6753_65t_m0/obj/APPS/MusicFX_intermediates/oat/arm64/package.odex

給檔案名稱加首碼

這個用於換路徑, addprefix。一個首碼可以後邊都加上。

例:

.PHONY : all3oatfile2 := system.odex music.odex Contacts.odexresult_oatfile2 = $(addprefix /data/dalvik-cache/, $(oatfile2))all3:    @echo $(result_oatfile2)

輸出:
/data/dalvik-cache/system.odex /data/dalvik-cache/music.odex /data/dalvik-cache/Contacts.odex

直接做字串替換

如果我們不想拆開加去的這麼麻煩,有一個簡易的方法是直接做字串替換。
subst函數的定義如下:$(subst 源串,目標串,要做替換的字串)

例,我們把剛才將.odex副檔名換成.tag.gz副檔名,並拼成一個tar命令的makefile重寫一下:

.PHONY : all4oatfile_targz := $(subst $(suffix $(oatfile)),.tar.gz,$(oatfile))compress_oat := tar cfvz $(oatfile_targz) $(oatfile)all4 :    @echo "The result is: "    @echo $(compress_oat)

輸出如下:
The result is:
tar cfvz out/target/product/ali6753_65t_m0/obj/APPS/MusicFX_intermediates/oat/arm64/package.tar.gz out/target/product/ali6753_65t_m0/obj/APPS/MusicFX_intermediates/oat/arm64/package.odex

過濾函數

過濾函數有兩種:

  • filter是合格留下
  • filter-out是合格去除掉

我們看一個例子:

all8:    @echo $(filter-out default interpreter jit optimizing,xoc)    @echo $(filter-out default interpreter jit optimizing,default)

輸出結果:

$ make all8xoc

filter-out default interpreter jit optimizing,default這一句,因為default在列表中,所以被過濾掉了,變成一個空串。
filter-out default interpreter jit optimizing,xoc:這句因為xoc不在過濾列表之中,所以留下了。

單詞的處理

檔案清單,參數列表等等可以看成對單詞的處理

比如下面的例子,我們想要查MAKEFILE_LIST中的最後一個檔案,可以這樣寫:

my-dir = $(call parent-dir,$(lastword $(MAKEFILE_LIST)))

針對單詞處理,有下面的常用函數:

  • firstword:取第一個單詞
  • lastword:取最後一個單詞
  • words:統計一共有多少個單詞
  • word:取第n個單詞
  • wordlist:取單詞的子集

word的取值是從1開始,不能取0。

我們看一個例子:

SETTINGS_ART_DST := out/target/product/6753_doov_l5_64_m/system/priv-app/Settings/oat/arm64/Settings.odex.PHONY : all10all10:    @echo "Install: [email protected]"    $(eval SETTINGS_ART_DST_LIST := $(subst /, ,$(SETTINGS_ART_DST)))    @echo $(words $(SETTINGS_ART_DST_LIST))    @echo $(word 1,$(SETTINGS_ART_DST_LIST))    @echo $(word 2,$(SETTINGS_ART_DST_LIST))    @echo $(wordlist 5,10,$(SETTINGS_ART_DST_LIST))

輸出如下:

$ make all10Install: all1010outtargetsystem priv-app Settings oat arm64 Settings.odex


Jtag特工
連結:https://www.jianshu.com/p/46224d15fc5f
來源:簡書
著作權歸作者所有。商業轉載請聯絡作者獲得授權,非商業轉載請註明出處。

 

Android.mk (1) 函數

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.