Android.mk(5) 计算怎么办?

https://www.jianshu.com/p/57c01e97c9b8

 

计算怎么办?

前面我们把Makefile做为一门语言的主要特性大致做了一个描述,它集合了目标式的模式和函数式的模式,还有大量的宏的思想,已经可以写出相当复杂的Makefile了。
但是,很不幸的是,虽然已经很复杂了,我们在实际的Android.mk当中还是会发现有很多事情光用Makefile的字符串替换搞不定啊,这可如何是好?

其实,这就是我们在第一讲的最开始就讲shell函数的原因,因为这要靠shell脚本来解决了。

我们看个实际的例子:core.mk中判断make的版本号大于等于3.81版的脚本:

# Check for broken versions of make.
# (Allow any version under Cygwin since we don't actually build the platform there.)
ifeq (,$(findstring CYGWIN,$(shell uname -sm)))
ifneq (1,$(strip $(shell expr $(MAKE_VERSION) \>= 3.81)))
$(warning ********************************************************************************)
$(warning *  You are using version $(MAKE_VERSION) of make.)
$(warning *  Android can only be built by versions 3.81 and higher.)
$(warning *  see https://source.android.com/source/download.html)
$(warning ********************************************************************************)
$(error stopping)
endif
endif

计算表达式的值 - expr语句

expr语句可以用来计算变量计算结果的值。

  • 加法:+
  • 减法:-
  • 乘法:\*
  • 除法:/
  • 求余数: %

特别注意一下乘法,要在*之前加一个\

取消变量的定义 - unset语句

在实际写Makefile的过程中,有时候需要修改环境变量。shell中提供了unset语句

不多说,直接上例子:

java_version_str := $(shell unset _JAVA_OPTIONS && java -version 2>&1)
javac_version_str := $(shell unset _JAVA_OPTIONS && javac -version 2>&1)

shell也搞不定怎么办?

shell总算是可以做些简单的计算了,但是对于规模到一定程度的需求,还是搞不定,怎么办?
在Android.mk系统中,您可以看到python,ruby纷纷被引进来了,需要用的话,别客气。完成工作是主要的,方法不重要。



作者:Jtag特工
链接:https://www.jianshu.com/p/57c01e97c9b8
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
posted @ 2018-02-10 10:51  行走的思想  阅读(383)  评论(0编辑  收藏  举报