android-build.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #!/usr/bin/python
  2. # android-build.py
  3. # Build android samples
  4. import sys
  5. import os, os.path
  6. import shutil
  7. from optparse import OptionParser
  8. CPP_SAMPLES = ["HelloPlugins"]
  9. ALL_SAMPLES = CPP_SAMPLES
  10. def check_environment_variables():
  11. ''' Checking the environment NDK_ROOT, which will be used for building
  12. '''
  13. try:
  14. NDK_ROOT = os.environ['NDK_ROOT']
  15. except Exception:
  16. print "NDK_ROOT not defined. Please define NDK_ROOT in your environment"
  17. sys.exit(1)
  18. return NDK_ROOT
  19. def select_toolchain_version():
  20. '''Because ndk-r8e uses gcc4.6 as default. gcc4.6 doesn't support c++11. So we should select gcc4.7 when
  21. using ndk-r8e. But gcc4.7 is removed in ndk-r9, so we should determine whether gcc4.7 exist.
  22. Conclution:
  23. ndk-r8e -> use gcc4.7
  24. ndk-r9 -> use gcc4.8
  25. '''
  26. ndk_root = check_environment_variables()
  27. if os.path.isdir(os.path.join(ndk_root,"toolchains/arm-linux-androideabi-4.8")):
  28. os.environ['NDK_TOOLCHAIN_VERSION'] = '4.8'
  29. print "The Selected NDK toolchain version was 4.8 !"
  30. elif os.path.isdir(os.path.join(ndk_root,"toolchains/arm-linux-androideabi-4.7")):
  31. os.environ['NDK_TOOLCHAIN_VERSION'] = '4.7'
  32. print "The Selected NDK toolchain version was 4.7 !"
  33. else:
  34. print "Couldn't find the gcc toolchain."
  35. exit(1)
  36. def caculate_built_samples(args):
  37. ''' Compute the sampels to be built
  38. 'cpp' for short of all cpp samples
  39. 'jsb' for short of all javascript samples
  40. '''
  41. if 'all' in args:
  42. return ALL_SAMPLES
  43. if 'jsb' in args:
  44. return JSB_SAMPLES
  45. if 'cpp' in args:
  46. return CPP_SAMPLES
  47. targets = []
  48. targets += args
  49. # remove duplicate elements, for example
  50. # python android-build.py cpp hellocpp
  51. targets = set(targets)
  52. return list(targets)
  53. def do_build(plugin_root, cocos_root, ndk_root, app_android_root, ndk_build_param):
  54. ndk_path = os.path.join(ndk_root, "ndk-build")
  55. # windows should use ";" to seperate module paths
  56. platform = sys.platform
  57. if platform == 'win32':
  58. ndk_module_path = 'NDK_MODULE_PATH=%s/publish;%s;%s/external;%s/cocos' % (plugin_root, cocos_root, cocos_root, cocos_root)
  59. else:
  60. ndk_module_path = 'NDK_MODULE_PATH=%s/publish:%s:%s/external:%s/cocos' % (plugin_root, cocos_root, cocos_root, cocos_root)
  61. if ndk_build_param == None:
  62. command = '%s -C %s %s' % (ndk_path, app_android_root, ndk_module_path)
  63. else:
  64. command = '%s -C %s %s %s' % (ndk_path, app_android_root, ndk_build_param, ndk_module_path)
  65. os.system(command)
  66. def copy_files(src, dst):
  67. for item in os.listdir(src):
  68. path = os.path.join(src, item)
  69. # Android can not package the file that ends with ".gz"
  70. if not item.startswith('.') and not item.endswith('.gz') and os.path.isfile(path):
  71. shutil.copy(path, dst)
  72. if os.path.isdir(path):
  73. new_dst = os.path.join(dst, item)
  74. if not os.path.exists(new_dst):
  75. os.mkdir(new_dst)
  76. copy_files(path, new_dst)
  77. def copy_resources(target, app_android_root, plugin_root):
  78. # remove app_android_root/assets if it exists
  79. assets_dir = os.path.join(app_android_root, "assets")
  80. if os.path.isdir(assets_dir):
  81. shutil.rmtree(assets_dir)
  82. # copy resources(cpp samples and lua samples)
  83. os.mkdir(assets_dir)
  84. resources_dir = os.path.join(app_android_root, "../Resources")
  85. if os.path.isdir(resources_dir):
  86. copy_files(resources_dir, assets_dir)
  87. # jsb samples should copy javascript files and resources(shared with cocos2d-html5)
  88. # if target in JSB_SAMPLES:
  89. # resources_dir = os.path.join(app_android_root, "../../../../cocos/scripting/javascript/script")
  90. # copy_files(resources_dir, assets_dir)
  91. # resources_dir = os.path.join(plugin_root, "jsbindings/js")
  92. # copy_files(resources_dir, assets_dir)
  93. # copy plugin resources to the assets
  94. plugins_dir = os.path.join(plugin_root, "publish" + os.path.sep + "plugins")
  95. for item in os.listdir(plugins_dir):
  96. src = os.path.join(plugins_dir, item + os.path.sep + "android" + os.path.sep + "ForAssets")
  97. if os.path.isdir(src):
  98. copy_files(src, assets_dir)
  99. def copy_clibs(app_android_root, plugin_root):
  100. target_cpath = os.path.join(app_android_root, "libs")
  101. plugins_dir = os.path.join(plugin_root, "publish" + os.path.sep + "plugins")
  102. for item in os.listdir(plugins_dir):
  103. src = os.path.join(plugins_dir, item + os.path.sep + "android" + os.path.sep + "CLibs")
  104. if os.path.isdir(src):
  105. if not os.path.exists(target_cpath):
  106. os.mkdir(target_cpath)
  107. copy_files(src, target_cpath)
  108. def build_samples(target,ndk_build_param):
  109. ndk_root = check_environment_variables()
  110. select_toolchain_version()
  111. build_targets = caculate_built_samples(target)
  112. current_dir = os.path.dirname(os.path.realpath(__file__))
  113. cocos_root = os.path.join(current_dir, "../../")
  114. plugin_root = os.path.join(current_dir, "..")
  115. app_android_root = ''
  116. for target in build_targets:
  117. app_android_root = os.path.join(plugin_root, "samples" + os.path.sep + target + os.path.sep + "proj.android")
  118. copy_resources(target, app_android_root, plugin_root)
  119. do_build(plugin_root, cocos_root, ndk_root, app_android_root, ndk_build_param)
  120. copy_clibs(app_android_root, plugin_root)
  121. # -------------- main --------------
  122. if __name__ == '__main__':
  123. usage = "usage: %prog all"
  124. #parse the params
  125. parser = OptionParser(usage=usage)
  126. parser.add_option("-n", "--ndk", dest="ndk_build_param",
  127. help='parameter for ndk-build')
  128. (opts, args) = parser.parse_args()
  129. if len(args) == 0:
  130. parser.print_help()
  131. else:
  132. build_samples(args, opts.ndk_build_param)