cocos2d-x 3.0 beta 自动生成Android.mk

            设COCOS2DX为cocos2d-x-3.0beta2路径,PROJECT为工程目录

偶然发现${COCOS2DX}/tools/下有个android-mk-generator目录,惊喜之下发现cocos2dx已经提供了生成android.mk的脚本,真是太方便了。

准确来说脚本是更新Android.mk里的LOCAL_SRC_FILES字段

脚本配置方法

切到android-mk-generator里有android_mk_generator.py和config.py

android_mk_generator.py如下

#!/usr/bin/python

import sys
import os
import os.path
import cStringIO
import re

def get_cur_dir():
    path = sys.path[0]
    if os.path.isdir(path):
        return path
    elif os.path.isfile(path):
        return os.path.dirname(path)

CUR_DIR = get_cur_dir()
COCOS_ROOT = os.path.abspath(os.path.join(CUR_DIR, "../../"))
CONFIG = os.path.abspath(os.path.join(CUR_DIR, "./config.py"))
# print 'CONFIG:' + CONFIG
# print 'COCOS_ROOT:' + COCOS_ROOT

try:
    import PathUtils
except ImportError, e:
    sys.path.append(os.path.abspath(os.path.join(CUR_DIR, "../pylib")))
    import PathUtils



def gen_android_mk(mkfile, pathes, suffix = ("c", "cpp",), exclude = ()):
    utils = PathUtils.PathUtils(COCOS_ROOT)
    filelst = utils.find_files(pathes, suffix, exclude)
    
    print "mkfile",mkfile
    print "pathes",pathes
    print "suffix",suffix
    print "exclude",exclude
    print "filelst",filelst
    # generate file list string
    filestrio = cStringIO.StringIO()
    mkfilepath = os.path.dirname(os.path.join(COCOS_ROOT, mkfile))
    for filename in filelst:
        filestrio.write(' \\\n')
        filepath = os.path.relpath(filename, mkfilepath)
        filestrio.write(filepath.replace('\\', '/'))
    filestrio.write('\n')
    
    # read mk file
    file = open(os.path.join(COCOS_ROOT, mkfile))
    mkstrio = cStringIO.StringIO()
    rep = re.compile("\s*LOCAL_SRC_FILES\s*:=")
    try:
        # read lines before encounter "LOCAL_EXPORT_C_INCLUDES"
        for line in file:
            if rep.match(line):
                mkstrio.write("LOCAL_SRC_FILES :=")
                break
            else:
                mkstrio.write(line)
    #mkstrio.write('\n')

        # write file list
        mkstrio.write(filestrio.getvalue())

        # write remaining lines
        delete = True if line[len(line) - 2] == '\\' else False
        for line in file:
            if delete:
                delete = True if line[len(line) - 2] == '\\' else False
            else:
                mkstrio.write(line)
#mkstrio.write('\n')
    finally:
        file.close()



    file = open(os.path.join(COCOS_ROOT, mkfile), "w")
    file.write(mkstrio.getvalue())
    file.close()

    filestrio.close()
    mkstrio.close()

def main():
    config = open(CONFIG)
    params = eval(config.read())
    config.close()
    
    for param in params:
        gen_android_mk(**param)

if __name__ == "__main__":
    from optparse import OptionParser

    parser = OptionParser()

    parser.add_option('-c', '--config',
                      type='string',
                      dest='config',
                      help="config file path.")

    parser.add_option('-r', '--rootpath',
                      action='store',
                      dest='rootpath',
                      help='class root path for mkfile, pathes, exclude.')

    options, args = parser.parse_args()

    if options.config:
        CONFIG = os.path.abspath(os.path.join(os.curdir, options.config))

    if options.rootpath:
        COCOS_ROOT = os.path.abspath(os.path.join(os.curdir, options.rootpath))

    print 'CONFIG:', CONFIG
    print 'COCOS_ROOT:', COCOS_ROOT

    error = ''
    if not os.path.isfile(CONFIG):
        error+='config must be file.\n'

    if not os.path.isdir(COCOS_ROOT):
        error+='rootpath must be directory.\n'

    if error != '':
        parser.exit(2, "{exception}".format(exception=error))
    
    sys.exit(main())

config.py如下

[
{
    'mkfile' : 'cocos/2d/Android.mk',
    'pathes' : ("cocos/2d/","cocos/base","cocos/math","cocos/physics","external/tinyxml2","external/unzip"),
    'exclude' : ("cocos/2d/platform/android", "cocos/2d/platform/emscripten", "cocos/2d/platform/ios", "cocos/2d/platform/apple", "cocos/2d/platform/linux", "cocos/2d/platform/mac", "cocos/2d/platform/win32")
},
{
    'mkfile' : 'samples/Cpp/TestCpp/Android.mk',
    'pathes' : ("samples/Cpp/TestCpp/Classes",),
},
{
    'mkfile' : 'extensions/Android.mk',
    'pathes' : ("extensions/",),
    'exclude' : ("extensions/proj.win32", "extensions/proj.emscripten", "extensions/proj.ios", "extensions/proj.linux", "extensions/proj.mac", "extensions/proj.nacl", "extensions/proj.qt5", "extensions/proj.tizen")
},
{
    'mkfile' : 'external/Box2D/Android.mk',
    'pathes' : ("external/Box2D/",),
},
{
    'mkfile' : 'external/chipmunk/Android.mk',
    'pathes' : ("external/chipmunk/",),
},
]

显然mkfile指定生成的android的"相对路径",pathes指定工程c,cpp源码"相对路径",exclude指定排除编译源码"相对路径",suffix指定编译源码的后缀名(默认cpp,c)

"相对路径"指的是相对${COCOS2DX}/路径。新建工程只要在config.py里新加字段就行了。


运行脚本

新建工程${COCOS2DX}/project/MyGame888用于测试

在${COCOS2DX}/tools/android-mk-generator/config.py新加入字段

{
    'mkfile' : 'project/MyGame888/proj.android/jni/Android.mk',
    'pathes' : ("project/MyGame888/Classes","project/MyGame888/proj.android/jni/hellocpp"),
    'suffix' : ('cpp','c','cxx')
}
切到${COCOS2DX}/tools/android-mk-generator

python android_mk_generator.py






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值