android-build.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #!/usr/bin/env python
  2. # android-build.py
  3. # Build android
  4. import sys
  5. import os, os.path
  6. CPP_SAMPLES = ['cpp-empty-test', 'cpp-tests', 'game-controller-test']
  7. LUA_SAMPLES = ['lua-empty-test', 'lua-tests', 'lua-game-controller-test']
  8. JS_SAMPLES = ['js-tests']
  9. ALL_SAMPLES = CPP_SAMPLES + LUA_SAMPLES + JS_SAMPLES
  10. def calculate_build_targets(args):
  11. ''' Calculate build targets from list of targets passed on command line.
  12. 'all' for all tests
  13. 'cpp' for all c++ tests
  14. 'lua' for all lua tests
  15. 'js' for all javascript tests
  16. '''
  17. targets = []
  18. for arg in args:
  19. if arg == 'all':
  20. targets += ALL_SAMPLES
  21. elif arg == 'cpp':
  22. targets += CPP_SAMPLES
  23. elif arg == 'lua':
  24. targets += LUA_SAMPLES
  25. elif arg == 'js':
  26. targets += JS_SAMPLES
  27. else:
  28. targets.append(arg)
  29. # remove duplicates
  30. targets = set(targets)
  31. return targets
  32. def do_build(app_android_root, build_mode, app_abi, platform):
  33. command = 'cocos compile -p android -s %s --ndk-mode %s --app-abi %s' % (app_android_root, build_mode, app_abi)
  34. if platform:
  35. command += ' --ap %s' % platform
  36. print command
  37. if os.system(command) != 0:
  38. raise Exception('Build dynamic library for project [ %s ] failed!' % app_android_root)
  39. def build_targets(targets, build_mode, app_abi, api_level):
  40. if api_level:
  41. platform = 'android-%s' % api_level
  42. else:
  43. platform = None
  44. build_targets = calculate_build_targets(targets)
  45. app_android_root = ''
  46. cocos_root = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..')
  47. for target in build_targets:
  48. app_android_root = os.path.join(cocos_root, 'tests', target)
  49. do_build(app_android_root, build_mode, app_abi, platform)
  50. def main():
  51. from argparse import ArgumentParser, RawTextHelpFormatter
  52. description = '''
  53. This script is mainly used for building tests built-in with cocos2d-x.
  54. If you are new to cocos2d-x, we recommend you to start with cpp-empty-test or lua-empty-test.'''
  55. parser = ArgumentParser(description=description, formatter_class=RawTextHelpFormatter)
  56. parser.add_argument('-n', '--ndk', dest='app_abi', default='armeabi-v7a',
  57. help='specifies Android ABI')
  58. parser.add_argument('-p', '--platform', dest='api_level',
  59. help='specifies Android API Level')
  60. parser.add_argument('-b', '--build', dest='build_mode', metavar='BUILD_MODE', default='debug', choices=['debug', 'release'],
  61. help='the build mode for java project, debug (default) or release. ' +
  62. 'To get more information, please refer to ' +
  63. 'http://developer.android.com/tools/building/building-cmdline.html')
  64. parser.add_argument('targets', nargs='+', metavar='targets',
  65. help='targets to build. A target is one of [cpp-empty-test|cpp-tests|lua-empty-test|lua-tests|js-tests|cpp|lua|js|all]',
  66. choices=['cpp-empty-test', 'cpp-tests', 'lua-empty-test', 'lua-tests', 'js-tests', 'cpp', 'lua', 'js', 'all'])
  67. args = parser.parse_args()
  68. try:
  69. build_targets(args.targets, args.build_mode, args.app_abi, args.api_level)
  70. except Exception as e:
  71. print e
  72. sys.exit(1)
  73. if __name__ == '__main__':
  74. main()