Android.mk - build all source file in a directory
Asked Answered
B

7

9

I am using Android NDK to build my cocos2dx project, within the Android.mk, there is a definition for LOCAL_SRC_FILES where each of the cpp file are listed. Whenever I added a new source file, I'd need to add it there as well... it looks like this:

LOCAL_SRC_FILES := hellocpp/main.cpp \
                   hellocpp/myclass.cpp \
                   hellocpp/mynextclass.cpp \
                   ../../Classes/Screens/LaunchScreen.cpp \

the header file, however, can specify the entire directory to include, it looks like this:

LOCAL_C_INCLUDES := $(LOCAL_PATH)/hellocpp
LOCAL_C_INCLUDES += $(LOCAL_PATH)/../../Classes/Screens

I have tried various ways to include the whole directory instead of single file for the LOCAL_SRC_FILES so that I don't need to modify the Android.mk build script whenever I add a new file, however, so far all my attempts failed.

I have tried this:

#SRC_PATH_HELLOCPP := $(wildcard hellocpp/*.cpp)
#SRC_PATH_CLASSES += $(wildcard ../../Classes/*.cpp)

#LOCAL_SRC_FILES := $(SRC_PATH_HELLOCPP:$(LOCAL_PATH/%=%)
#LOCAL_SRC_FILES += $(SRC_PATH_CLASSES:$(LOCAL_PATH/%=%)

as well as this:

#LOCAL_SRC_FILES += hellocpp/*.cpp
#LOCAL_SRC_FILES += ../../Classes/*.cpp

both are not working...

I have another project that works well with the first option though, I really do not understand why it doesn't work in the cocos2dx project... does anybody know why or know the solution? Or maybe I should just leave it as is and take the trouble, since everybody is doing that., but it is really troublesome, hope somebody can help so that all of us can be more productive..

Thanks!

Berkey answered 21/8, 2013 at 4:40 Comment(0)
B
17

The wildcard works for cocos2dx projects as well. I am using it on my own, just that your syntax is incorrect

Try:

HELLOCPP_FILES  := $(wildcard $(LOCAL_PATH)/hellocpp/*.cpp)
HELLOCPP_FILES  := $(HELLOCPP_FILES:$(LOCAL_PATH)/%=%)

CLASSES_FILES   := $(wildcard $(LOCAL_PATH)/../../Classes/*.cpp)
CLASSES_FILES   := $(CLASSES_FILES:$(LOCAL_PATH)/%=%)

LOCAL_SRC_FILES := $(HELLOCPP_FILES)
LOCAL_SRC_FILES += $(CLASSES_FILES)
Bergin answered 10/12, 2013 at 4:16 Comment(2)
Is it recursive? I mean if there are sub directories in Classesdirectory, the .cppfiles inside those directorise will be taken into account in CLASSES_FILES? I guess it is not. Is it a way to make it recursive?Deluge
No, it is not recursive, you have to add another line that specify the subdirectoryBergin
K
6

Actually wildcards do work and you were on the right track...

This is an example of what works fine for me:

UTILITIES := $(wildcard $(LOCAL_PATH)/Utility/*.cpp)
ZIP := $(wildcard $(LOCAL_PATH)/Utility/zip/jni/*.c)

Notice the inclusion of the $(LOCAL_PATH) variable, and then

SOURCES := $(UTILITIES:$(LOCAL_PATH)/%=%)
SOURCES += $(ZIP:$(LOCAL_PATH)/%=%)

That should allow you to drop in any source file and it will compile without going back to the Android.mk file.

Kunlun answered 21/8, 2013 at 10:19 Comment(2)
thanks! I've tried, but it still doesn't work... compile error with symbol not found..Berkey
Edited my answer, compare the first two lines. You are missing the $(LOCAL_PATH) variable. PS: a "#" in a makefile is for comments!Kunlun
D
1

i think you don't need to add entire header for new added .cpp files .. you should just added it this way

for example . if you want to add this LaunchScreen.cpp then you should include it this simple way

LOCAL_SRC_FILES:=hellocpp/main.cpp \
          ../../Classes/myclass.cpp\
          ../../Classes/mynextclass.cpp\ 
          ../../Classes/LaunchScreen.cpp\
Decide answered 23/8, 2013 at 11:51 Comment(0)
A
1

To build all your .cpp files under ../../Classes/, you can use the external find command if you build your project on a UNIX-like OS:

SRC_PATH_CLASSES := $(shell find ../../Classes/ -type f)
SRC_PATH_CLASSES := $(filter %.cpp, $(SRC_PATH_CLASSES))
LOCAL_SRC_FILES += $(SRC_PATH_CLASSES:$(LOCAL_PATH)/%=%)

As suggested in the link: Recursive wildcards in GNU make?, another way is using the following recursive wildcard. It is written with pure Makefile rules, thus it is portable and better.

rwildcard=$(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2) $(filter $(subst *,%,$2),$d))
SRC_PATH_CLASSES := $(call rwildcard, ../../Classes/, *.cpp)
LOCAL_SRC_FILES += $(SRC_PATH_CLASSES:$(LOCAL_PATH)/%=%)
Actinia answered 27/8, 2014 at 10:6 Comment(0)
R
0

This is a makefile. Makefiles don't work like that. You can't specify an entire directory for files to compile- it just isn't set up that way. It's been like that for 40 years or so. One of many reasons why people hate makefiles. The problem is that all the replacements have been just as bad.

Rucksack answered 21/8, 2013 at 4:42 Comment(0)
M
0

After hours of fighting with this I found a solution that finally works with my cocos2dx setup (cocos2dx 3.10)

LOCAL_C_INCLUDES += $(LOCAL_PATH)/../../../Classes
FILE_LIST := $(wildcard $(LOCAL_PATH)/../../../Classes/*.cpp)
LOCAL_SRC_FILES += $(FILE_LIST:$(LOCAL_PATH)/%=%)

Answering here for the sake of helping someone out with the same pain since the -actually- works, this question still being relevant to this day Notably, this also works for Windows which was the main problem I experienced with other solutions

Source: http://qiita.com/YosukeMitsugi/items/137f1b57f03945ad2d50

Mueller answered 13/2, 2016 at 8:22 Comment(0)
P
0

this is what worked for me in cocos2dx 3.10 under win 10:

FILE_LIST := $(wildcard $(LOCAL_PATH)/../../../Classes/*.cpp)

LOCAL_SRC_FILES := hellocpp/main.cpp \
LOCAL_SRC_FILES += $(FILE_LIST:$(LOCAL_PATH)/%=%)
Pyrazole answered 1/3, 2016 at 11:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.