Android.mk: include multiple source files using wildcards


In case your Android project consist in a lot of source files you should, normally, add one by one to the LOCAL_SRC_FILES label and this could be a very boring task to complete. Fortunately there is the possibility to make addition of multiple files using wildcards in a very easy way. Let's go to see how to make it.


There is no need of many explanations, just check the Android.mk example below:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := MyProjectName

LOCAL_C_INCLUDES += $(LOCAL_PATH)/SourcesFolder1
LOCAL_C_INCLUDES += $(LOCAL_PATH)/SourcesFolder2

PROJECT_FILES := $(wildcard $(LOCAL_PATH)/SourcesFolder1/*.cpp)
PROJECT_FILES += $(wildcard $(LOCAL_PATH)/SourcesFolder2/*.c)

PROJECT_FILES := $(PROJECT_FILES:$(LOCAL_PATH)/%=%)

LOCAL_SRC_FILES := $(PROJECT_FILES)

include $(BUILD_SHARED_LIBRARY)

As you can see in this example project we have two subfolders (SourcesFolder 1 and 2) containing the project files we want to compile. Wildcards allow to specify the source files extension (.c and .cpp in our example) to add to the project. You can add as many folder you want but you need to be carefully about some points. The first folder to add need to have the assignment command:

PROJECT_FILES := $(wildcard $(LOCAL_PATH)/SourcesFolder1/*.cpp)

Each new additional folder need to have the command for add to the list:

PROJECT_FILES += $(wildcard $(LOCAL_PATH)/SourcesFolder2/*.c)

Once finished to add all the folders the last instruction line remove the $(LOCAL_PATH)/ prefix from the list. This is very important since without this "patch" the generated files list will not work and the compiler will return an error:

PROJECT_FILES := $(PROJECT_FILES:$(LOCAL_PATH)/%=%)

Here our files list is ready and we can assign to the compiler label:

LOCAL_SRC_FILES := $(PROJECT_FILES)

In case you have some specific source file you want to add "manually" you can change as follow:

LOCAL_SRC_FILES := \
    Source1.c \
    $(PROJECT_FILES)

Now you can start compilation and verify if  the mk file will work as expected.

Comments

  1. Hi. How will you link the files if the sources folders are not inside LOCAL_PATH? Thanks.

    ReplyDelete
  2. Hi
    As you can see LOCAL_PATH is defined on the top of the makefile and basically point to the current folder where .mk file is called. If you want to use an absolute path like for example /home/username/mypath1/mypath2/myfiles/ you can use this absolute path instead of LOCAL_PATH or, as easy alternative, define a LOCAL_PATH_1 with your secondary path.

    ReplyDelete

Post a Comment

Popular posts from this blog

Access GPIO from Linux user space

Android: adb push and read-only file system error

Tree in SQL database: The Nested Set Model