zoukankan      html  css  js  c++  java
  • Android.mk (2) 函数进阶教程

    https://www.jianshu.com/p/674dc7d7b4b0

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

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

    分支函数

    要走分支,一定是要有条件要判断。
    在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



    作者:Jtag特工
    链接:https://www.jianshu.com/p/674dc7d7b4b0
    來源:简书
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  • 相关阅读:
    转:Node.js邮件发送组件- Nodemailer 1.0发布
    USACO 5.4 Betsy's Tour(暴力)
    USACO 5.4 Character Recognition(DP)
    Codeforces Round #196 (Div. 2)
    HDU 4681 String(DP)
    HDU 4679 Terrorist’s destroy
    HDU 4669 Mutiples on a circle(环状DP)
    HDU 4666 Hyperspace(曼哈顿距离)
    HDU 2852 KiKi's K-Number(离线+树状数组)
    POJ 3335 Rotating Scoreboard(多边形的核)
  • 原文地址:https://www.cnblogs.com/onelikeone/p/8438250.html
Copyright © 2011-2022 走看看