The ultimate technique for writing local_src_files in Android.mk

Source: Internet
Author: User

Turn from: Introduction of http://blog.csdn.net/fu_zk/article/details/12836431 problem

In the process of compiling C + + projects using the NDK, it is unavoidable to write android.mk files, the most important of which is the LOCAL_SRC_FILES list of source files.
Consider a situation where the following source file distributions are available:

cpp文件全部位于android项目下的jni文件夹下,结构如下    jni      |---1.cpp     |---2.cpp     |---Android.mk     |---Application.mk     |---ndk_test.cpp     |---src         |    |---core     |    |    |---core1.cpp     |    |    |---core2.cpp     |    |---src1.cpp     |    |---src2.cpp

According to the usual notation, in the android.mk , should write

LOCAL_SRC_FILES := ndk_test.cpp                 1.cpp                 2.cpp                 src/src1.cpp                 src/src2.cpp                 src/core/core1.cpp                 src/core/core2.cpp

Tedious!

Preliminary solution: One sentence introduces all CPP source files under a single directory (excluding subdirectories)

Continue with the above situation for example, I can write this

MY_CPP_LIST := $(wildcard $(LOCAL_PATH)/*.cpp)MY_CPP_LIST += $(wildcard $(LOCAL_PATH)/src/*.cpp)MY_CPP_LIST += $(wildcard $(LOCAL_PATH)/src/core/*.cpp)LOCAL_SRC_FILES := $(MY_CPP_LIST:$(LOCAL_PATH)/%=%)

Problem solving. Just a few words to explain the above.

    1. my_cpp_list: = $ (wildcard $ (local_path)/*.cpp) , which means to get $ (wildcard) using the Local_path function The directory is the file with all suffixes named CPP under the JNI directory and puts the result in the variable my_cpp_list . We know that $ (local_path) refers to the directory where the current android.mk file is located , so by this sentence, the value in my_cpp_list should be jni/1.cpp jni/2.cpp jni/ndk_test.cpp .
    2. My_cpp_list + = $ (wildcard $ (local_path)/src/*.cpp) , get jni/src directory source file and append to variable my_ Cpp_list in
    3. my_cpp_list + = $ (wildcard $ (local_path)/src/core/*.cpp) , ibid., Get jni/src/ The source file under the core directory
    4. gets my_cpp_list in the previous steps: jni/1.cpp jni/2.cpp jni/ndk_test.cpp jni/ Src/src1.cpp jni/src/src2.cpp jni/src/core/core1.cpp jni/src/core/core2.cpp
    5. local_src_files: = $ (my_cpp_list:$ (Local_path)/%=%) , the previously obtained files are all beginning with JNI , and the files needed for the actual compilation should be directly from the JNI Directory starts, so we use pattern substitution to remove all jni/ from the front of the file name.

Here I explain $(MY_CPP_LIST:$(LOCAL_PATH)/%=%) the grammatical meaning, it means to each of the MY_CPP_LIST items, apply the rule after the colon, what is the rule? The rule is $(LOCAL_PATH)/%=% , it means, find all the $(LOCAL_PATH)/ items that begin with, and intercept the later sections

The last sentence can also be written using the SUBST function:

#替换每一项中的 "$(LOCAL_PATH)/" 为 ""(空)LOCAL_SRC_FILES := $(subst $(LOCAL_PATH)/, , $(MY_CPP_LIST))  

or use the PATSUBST function to write

#同模式替换,这里使用patsubst函数LOCAL_SRC_FILES := $(patsubst $(LOCAL_PATH)/%, %, $(MY_CPP_LIST))  

For specific syntax, refer to: Functions for String Substitution and analysis

In practice, you can put the code in a directory other than the JNI directory, as long as you modify the relative path in the wildcard function, you can even use the absolute path, as long as you want.

The above code is enough to deal with most of the situation, but people's laziness is unlimited, like the above situation all my source files are in the JNI directory, why do you want to write each subdirectory is a line, not too elegant, it is best to write a sentence to the JNI directory of all the source files are introduced.

Advanced: Introduction of all CPP source files under a single directory (including subdirectories)

In order to reach all the source files in the introduction directory, including the sub-directories, I android.mk wrote

#声明一个变量MY_CPP_PATH表示源码目录MY_CPP_PATH := $(LOCAL_PATH)/ #获取目录下的所有文件 My_All_Files := $(shell find $(MY_CPP_PATH)/.)My_All_Files := $(My_All_Files:$(MY_CPP_PATH)/./%=$(MY_CPP_PATH)%)#从My_All_Files中再次提取所有的cpp文件,这里也可以使用filter函数MY_CPP_LIST := $(foreach c_file,$(My_All_Files), $(wildcard $(c_file)/*.cpp) ) MY_CPP_LIST := $(MY_CPP_LIST:$(LOCAL_PATH)/%=%)LOCAL_SRC_FILES := $(MY_CPP_LIST)

Through the above lines, successfully obtained the JNI directory contains its subdirectories of all the CPP source files, and compiled correctly. In practice, the code is not necessarily stored in the JNI directory, the modification MY_CPP_PATH is OK, note: It is MY_CPP_PATH best to use $(LOCAL_PATH) the relative directory at the beginning

This writing greatly facilitates the development of the project, previously in the source directory under the new CPP source files, the new directory does not need to modify android.mk the file.

Another problem, the above code is just the introduction of CPP files, if the source folder has a C file, how to do? Write a few more lines?

Step 2.0: Introduce all *.cpp and *.c source files under a single directory (including subdirectories)

Here, I give the code directly

MY_CPP_PATH  := $(LOCAL_PATH)/My_All_Files := $(shell find $(MY_CPP_PATH)/.)My_All_Files := $(My_All_Files:$(MY_CPP_PATH)/./%=$(MY_CPP_PATH)%)MY_CPP_LIST  := $(filter %.cpp %.c,$(My_All_Files)) MY_CPP_LIST  := $(MY_CPP_LIST:$(LOCAL_PATH)/%=%)LOCAL_SRC_FILES := $(MY_CPP_LIST)

The filter function is used in the code.

Not satisfied? If the project source code has multiple directories in different places, and there are multiple suffixes, what to do?

Ultimate Advanced: A source file that introduces multiple suffix names under multiple directories (including subdirectories)

Code (as amended on October 9, 2013):

# 扫描目录下的所有源文件MY_FILES_PATH  :=  $(LOCAL_PATH)                    $(LOCAL_PATH)/../../ClassesMY_FILES_SUFFIX := %.cpp %.c %.ccMy_All_Files := $(foreach src_path,$(MY_FILES_PATH), $(shell find "$(src_path)" -type f) ) My_All_Files := $(My_All_Files:$(MY_CPP_PATH)/./%=$(MY_CPP_PATH)%)MY_SRC_LIST  := $(filter $(MY_FILES_SUFFIX),$(My_All_Files)) MY_SRC_LIST  := $(MY_SRC_LIST:$(LOCAL_PATH)/%=%)LOCAL_SRC_FILES := $(MY_SRC_LIST)

In the above code, the variable MY_FILES_PATH holds the directory where the source file resides, and MY_FILES_SUFFIX the suffix name of the source file is saved

Original articles, reproduced please specify, thank you!

PS: How to debug a android.mk file

There is a way, that is, in the compilation process output android.mk file value of the variable, you can observe the analysis of the problem, the use of code

$(warning $(LOCAL_SRC_FILES))

The value of the variable can be observed from the terminal window during the compilation process LOCAL_SRC_FILES

The ultimate technique for writing local_src_files in Android.mk (RPM)

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.