两个android程序调用函数,Android.mk (2) 函数进阶教程 - 分支、循环、子程序

函数进阶教程 - 分支、循环、子程序

按照面向过程程序设计的标准流程,我们讲完了顺序结构,就要讲分支、循环和子程序。下面我们就开始讲用于分支、循环和子程序调用功能的函数。

分支函数

要走分支,一定是要有条件要判断。

在Makefile里,最主要的判断就是看字符串能不能找到了。

通过findstring函数来进行这个判断,然后用if函数使用findstring函数的结果。

例:

.PHONY : all5

bootoatfile := out/target/product/ali6753_65t_m0/dex_bootjars/system/framework/arm64/boot.oat

result_findString := $(findstring boot.oat,$(bootoatfile))

result_findString2 := $(findstring boot.oat,$(oatfile))

all5 :

$(if $(result_findString), @echo found boot.oat, @echo cannot find boot.oat)

$(if $(result_findString2), @echo found boot.oat, @echo cannot find boot.oat)

输出:

found boot.oat

cannot find boot.oat

循环函数

Makefile中对于循环结构的支持是foreach函数

语法格式为:$(foreach 变量,列表,对变量对操作)

这样的循环在Android.mk中遍地都是,比如对模块进行遍历,对产品进行遍历等。

我们看一个例子,这个例子写在build/core/main.mk中:

# A helper goal printing out install paths

.PHONY: GET-INSTALL-PATH

GET-INSTALL-PATH:

@$(foreach m, $(ALL_MODULES), $(if $(ALL_MODULES.$(m).INSTALLED), echo ‘INSTALL-PATH: $(m) $(ALL_MODULES.$(m).INSTALLED)‘;))

再看一个更复杂一点的,加深一下印象:

# A static Java library needs to explicily set LOCAL_RESOURCE_DIR.

ifdef LOCAL_RESOURCE_DIR

need_compile_res := true

all_resources := $(strip $(foreach dir, $(LOCAL_RESOURCE_DIR), $(addprefix $(dir)/, $(patsubst res/%,%, $(call find-subdir-assets,$(dir)) ) ) ))

子程序调用

makefile的子程序结构当然就是函数了。

定义函数很简单,就是定义一个变量就是了。参数可以用$(1),$(2)等等来表示

例:

isBootOat = $(findstring boot.oat,$(1))

调用的时候要注意,用call函数来调用,call和函数名之间是空格,之后的参数要用逗号分隔。

我们看个简单的例子,将前边讲过的findstring的功能封装成一个函数。

例:

.PHONY : all6

isBootOat = $(findstring boot.oat,$(1))

all6 :

$(if $(call isBootOat,$(bootoatfile)), @echo found boot.oat, @echo cannot find boot.oat)

$(if $(call isBootOat,$(oatfile)), @echo found boot.oat, @echo cannot find boot.oat)

输出:

$ make all6

found boot.oat

cannot find boot.oat

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值