genbindings.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #!/usr/bin/python
  2. # This script is used to generate luabinding glue codes.
  3. # Android ndk version must be ndk-r9b.
  4. import sys
  5. import os, os.path
  6. import shutil
  7. import ConfigParser
  8. import subprocess
  9. import re
  10. from contextlib import contextmanager
  11. def _check_ndk_root_env():
  12. ''' Checking the environment NDK_ROOT, which will be used for building
  13. '''
  14. try:
  15. NDK_ROOT = os.environ['NDK_ROOT']
  16. except Exception:
  17. print "NDK_ROOT not defined. Please define NDK_ROOT in your environment."
  18. sys.exit(1)
  19. return NDK_ROOT
  20. def _check_python_bin_env():
  21. ''' Checking the environment PYTHON_BIN, which will be used for building
  22. '''
  23. try:
  24. PYTHON_BIN = os.environ['PYTHON_BIN']
  25. except Exception:
  26. print "PYTHON_BIN not defined, use current python."
  27. PYTHON_BIN = sys.executable
  28. return PYTHON_BIN
  29. class CmdError(Exception):
  30. pass
  31. @contextmanager
  32. def _pushd(newDir):
  33. previousDir = os.getcwd()
  34. os.chdir(newDir)
  35. yield
  36. os.chdir(previousDir)
  37. def _run_cmd(command):
  38. ret = subprocess.call(command, shell=True)
  39. if ret != 0:
  40. message = "Error running command"
  41. raise CmdError(message)
  42. def main():
  43. cur_platform= '??'
  44. llvm_path = '??'
  45. ndk_root = _check_ndk_root_env()
  46. # del the " in the path
  47. ndk_root = re.sub(r"\"", "", ndk_root)
  48. python_bin = _check_python_bin_env()
  49. platform = sys.platform
  50. if platform == 'win32':
  51. cur_platform = 'windows'
  52. elif platform == 'darwin':
  53. cur_platform = platform
  54. elif 'linux' in platform:
  55. cur_platform = 'linux'
  56. else:
  57. print 'Your platform is not supported!'
  58. sys.exit(1)
  59. x86_llvm_path = ""
  60. x64_llvm_path = os.path.abspath(os.path.join(ndk_root, 'toolchains/llvm/prebuilt', '%s-%s' % (cur_platform, 'x86_64')))
  61. if not os.path.exists(x64_llvm_path):
  62. x86_llvm_path = os.path.abspath(os.path.join(ndk_root, 'toolchains/llvm/prebuilt', '%s' % (cur_platform)))
  63. if not os.path.exists(x86_llvm_path):
  64. x86_llvm_path = os.path.abspath(os.path.join(ndk_root, 'toolchains/llvm/prebuilt', '%s-%s' % (cur_platform, 'x86')))
  65. if os.path.isdir(x64_llvm_path):
  66. llvm_path = x64_llvm_path
  67. elif os.path.isdir(x86_llvm_path):
  68. llvm_path = x86_llvm_path
  69. else:
  70. print 'llvm toolchain not found!'
  71. print 'path: %s or path: %s are not valid! ' % (x86_llvm_path, x64_llvm_path)
  72. sys.exit(1)
  73. x86_gcc_toolchain_path = ""
  74. x64_gcc_toolchain_path = os.path.abspath(os.path.join(ndk_root, 'toolchains/arm-linux-androideabi-4.9/prebuilt', '%s-%s' % (cur_platform, 'x86_64')))
  75. if not os.path.exists(x64_gcc_toolchain_path):
  76. x86_gcc_toolchain_path = os.path.abspath(os.path.join(ndk_root, 'toolchains/arm-linux-androideabi-4.9/prebuilt', '%s' % (cur_platform)))
  77. if not os.path.exists(x86_gcc_toolchain_path):
  78. x86_gcc_toolchain_path = os.path.abspath(os.path.join(ndk_root, 'toolchains/arm-linux-androideabi-4.9/prebuilt', '%s-%s' % (cur_platform, 'x86')))
  79. if os.path.isdir(x64_gcc_toolchain_path):
  80. gcc_toolchain_path = x64_gcc_toolchain_path
  81. elif os.path.isdir(x86_gcc_toolchain_path):
  82. gcc_toolchain_path = x86_gcc_toolchain_path
  83. else:
  84. print 'gcc toolchain not found!'
  85. print 'path: %s or path: %s are not valid! ' % (x64_gcc_toolchain_path, x86_gcc_toolchain_path)
  86. sys.exit(1)
  87. project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..'))
  88. cocos_root = os.path.abspath(os.path.join(project_root, ''))
  89. cxx_generator_root = os.path.abspath(os.path.join(project_root, 'tools/bindings-generator'))
  90. # save config to file
  91. config = ConfigParser.ConfigParser()
  92. config.set('DEFAULT', 'androidndkdir', ndk_root)
  93. config.set('DEFAULT', 'clangllvmdir', llvm_path)
  94. config.set('DEFAULT', 'gcc_toolchain_dir', gcc_toolchain_path)
  95. config.set('DEFAULT', 'cocosdir', cocos_root)
  96. config.set('DEFAULT', 'cxxgeneratordir', cxx_generator_root)
  97. config.set('DEFAULT', 'extra_flags', '')
  98. conf_ini_file = os.path.abspath(os.path.join(os.path.dirname(__file__), 'userconf.ini'))
  99. print 'generating userconf.ini...'
  100. with open(conf_ini_file, 'w') as configfile:
  101. config.write(configfile)
  102. # set proper environment variables
  103. if 'linux' in platform or platform == 'darwin':
  104. os.putenv('LD_LIBRARY_PATH', '%s/libclang' % cxx_generator_root)
  105. if platform == 'win32':
  106. path_env = os.environ['PATH']
  107. os.putenv('PATH', r'%s;%s\libclang;%s\tools\win32;' % (path_env, cxx_generator_root, cxx_generator_root))
  108. try:
  109. tolua_root = '%s/tools/tolua' % project_root
  110. output_dir = '%s/cocos/scripting/lua-bindings/auto' % project_root
  111. cmd_args = {'cocos2dx.ini' : ('cocos2d-x', 'lua_cocos2dx_auto'), \
  112. 'cocos2dx_extension.ini' : ('cocos2dx_extension', 'lua_cocos2dx_extension_auto'), \
  113. 'cocos2dx_ui.ini' : ('cocos2dx_ui', 'lua_cocos2dx_ui_auto'), \
  114. 'cocos2dx_studio.ini' : ('cocos2dx_studio', 'lua_cocos2dx_studio_auto'), \
  115. 'cocos2dx_spine.ini' : ('cocos2dx_spine', 'lua_cocos2dx_spine_auto'), \
  116. 'cocos2dx_physics.ini' : ('cocos2dx_physics', 'lua_cocos2dx_physics_auto'), \
  117. 'cocos2dx_experimental_video.ini' : ('cocos2dx_experimental_video', 'lua_cocos2dx_experimental_video_auto'), \
  118. 'cocos2dx_experimental.ini' : ('cocos2dx_experimental', 'lua_cocos2dx_experimental_auto'), \
  119. 'cocos2dx_controller.ini' : ('cocos2dx_controller', 'lua_cocos2dx_controller_auto'), \
  120. 'cocos2dx_cocosbuilder.ini': ('cocos2dx_cocosbuilder', 'lua_cocos2dx_cocosbuilder_auto'), \
  121. 'cocos2dx_cocosdenshion.ini': ('cocos2dx_cocosdenshion', 'lua_cocos2dx_cocosdenshion_auto'), \
  122. 'cocos2dx_3d.ini': ('cocos2dx_3d', 'lua_cocos2dx_3d_auto'), \
  123. 'cocos2dx_audioengine.ini': ('cocos2dx_audioengine', 'lua_cocos2dx_audioengine_auto'), \
  124. 'cocos2dx_csloader.ini' : ('cocos2dx_csloader', 'lua_cocos2dx_csloader_auto'), \
  125. 'cocos2dx_experimental_webview.ini' : ('cocos2dx_experimental_webview', 'lua_cocos2dx_experimental_webview_auto'), \
  126. 'cocos2dx_physics3d.ini' : ('cocos2dx_physics3d', 'lua_cocos2dx_physics3d_auto'), \
  127. 'cocos2dx_navmesh.ini' : ('cocos2dx_navmesh', 'lua_cocos2dx_navmesh_auto'), \
  128. }
  129. target = 'lua'
  130. generator_py = '%s/generator.py' % cxx_generator_root
  131. for key in cmd_args.keys():
  132. args = cmd_args[key]
  133. cfg = '%s/%s' % (tolua_root, key)
  134. print 'Generating bindings for %s...' % (key[:-4])
  135. command = '%s %s %s -s %s -t %s -o %s -n %s' % (python_bin, generator_py, cfg, args[0], target, output_dir, args[1])
  136. _run_cmd(command)
  137. print '---------------------------------'
  138. print 'Generating lua bindings succeeds.'
  139. print '---------------------------------'
  140. except Exception as e:
  141. if e.__class__.__name__ == 'CmdError':
  142. print '---------------------------------'
  143. print 'Generating lua bindings fails.'
  144. print '---------------------------------'
  145. sys.exit(1)
  146. else:
  147. raise
  148. # -------------- main --------------
  149. if __name__ == '__main__':
  150. main()