mattak's blog

気の向くままに書く

cocos2d-x Android.mkで任意のパスのクラスをbuildする

wildcardを使う

srcファイルを指定するのに、以下のようにファイル追加を行うごとに設定をいじるのは面倒。

LOCAL_SRC_FILES := hellocpp/main.cpp \
                   $(LOCAL_PATH)/../../Classes/Hoge.cpp
                   $(LOCAL_PATH)/../../Classes/Fuga.cpp

これはwildcardを利用すれば、以下のようにかける。

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

任意のディレクトリを許可したいなら、以下のように書き換えられる。 (階層が増えるごとに記述はひつようだけど...いい方法がわからんです)

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

ただこれだとjni/がすべてのパスについてしまうので、substをつかってjni/を消す。

LOCAL_SRC_FILES_JNI_UNPREFIXED := $(subst jni/,, $(LOCAL_SRC_FILES_JNI_PREFIXED))

以上を加味した、Android.mkの例

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := cocos2dcpp_shared

LOCAL_MODULE_FILENAME := libcocos2dcpp

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

LOCAL_SRC_FILES_JNI_UNPREFIXED := $(subst jni/,, $(LOCAL_SRC_FILES_JNI_PREFIXED))

LOCAL_SRC_FILES := hellocpp/main.cpp \
                   $(LOCAL_SRC_FILES_JNI_UNPREFIXED)

$(info $(LOCAL_SRC_FILES))

LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes

LOCAL_WHOLE_STATIC_LIBRARIES += cocos2dx_static
LOCAL_WHOLE_STATIC_LIBRARIES += cocosdenshion_static
LOCAL_WHOLE_STATIC_LIBRARIES += box2d_static
LOCAL_WHOLE_STATIC_LIBRARIES += chipmunk_static
LOCAL_WHOLE_STATIC_LIBRARIES += cocos_extension_static

include $(BUILD_SHARED_LIBRARY)

$(call import-module,cocos2dx)
$(call import-module,cocos2dx/platform/third_party/android/prebuilt/libcurl)
$(call import-module,CocosDenshion/android)
$(call import-module,extensions)
$(call import-module,external/Box2D)
$(call import-module,external/chipmunk)

Enjoy!