ANDROID.MK (2) function advanced Tutorial-branching, looping, subroutines

Source: Internet
Author: User

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

function Advanced Tutorial-branching, looping, subroutines

According to the standard process of process-oriented programming, we have finished the sequential structure, we need to talk about branching, looping and subroutines. Let's start with the functions used for branching, looping, and subroutine invocation functions.

Branch functions

To go to the branch, must be conditional to judge.
In makefile, the main judgment is to see if the string can be found.
The FindString function is used to make this judgment, and then uses the IF function to use the result of the FindString function.

Cases:

$(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)

Output:
Found Boot.oat
Cannot find Boot.oat

Loop function

The support for loop structures in makefile is the foreach function
The syntax format is: $ (foreach variable, list, action on variable pair)

Such loops are everywhere in the android.mk, such as traversing a module, traversing a product, and so on.

Let's look at an example, this example is written in build/core/main.mk:

# A helper goal printing out install paths.PHONY: GET-INSTALL-PATHGET-INSTALL-PATH:    @$(foreach m, $(ALL_MODULES), $(if $(ALL_MODULES.$(m).INSTALLED), echo ‘INSTALL-PATH: $(m) $(ALL_MODULES.$(m).INSTALLED)‘;))

Look at a more complicated, deeper impression:

# A static Java library needs to explicily set LOCAL_RESOURCE_DIR.ifdef LOCAL_RESOURCE_DIRneed_compile_res := trueall_resources := $(strip     $(foreach dir, $(LOCAL_RESOURCE_DIR), $(addprefix $(dir)/, $(patsubst res/%,%, $(call find-subdir-assets,$(dir)) ) ) ))
Subroutine calls

The subroutine structure of makefile is of course the function.

Defining a function is as simple as defining a variable. Parameters can be represented by $ (1), $ (2), etc.
Cases:

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

When calling, be aware that the call function is called with a space between the calls and function names, after which the arguments are separated by commas.

Let's look at a simple example that encapsulates the function of the findstring mentioned above as a function.
Cases:

$(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)

Output:
$ make All6
Found Boot.oat
Cannot find Boot.oat



Agent Jtag.
Links: https://www.jianshu.com/p/674dc7d7b4b0
Source: Pinterest
Copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source.

ANDROID.MK (2) function advanced Tutorial-branching, looping, subroutines

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.