1 #!/usr/bin/python
2
3 #This script is used to generate luabinding glue codes.
4 #Android ndk version must be ndk-r9b.
5
6
7 importsys8 importos, os.path9 importshutil10 importConfigParser11 importsubprocess12 importre13 from contextlib importcontextmanager14
15
16 def_check_ndk_root_env():17 '''Checking the environment NDK_ROOT, which will be used for building18 '''
19
20 try:21 NDK_ROOT = os.environ['NDK_ROOT']22 exceptException:23 print "NDK_ROOT not defined. Please define NDK_ROOT in your environment."
24 sys.exit(1)25
26 returnNDK_ROOT27
28 def_check_python_bin_env():29 '''Checking the environment PYTHON_BIN, which will be used for building30 '''
31
32 try:33 PYTHON_BIN = os.environ['PYTHON_BIN']34 exceptException:35 print "PYTHON_BIN not defined, use current python."
36 PYTHON_BIN =sys.executable37
38 returnPYTHON_BIN39
40
41 classCmdError(Exception):42 pass
43
44
45 @contextmanager46 def_pushd(newDir):47 previousDir =os.getcwd()48 os.chdir(newDir)49 yield
50 os.chdir(previousDir)51
52 def_run_cmd(command):53 ret = subprocess.call(command, shell=True)54 if ret !=0:55 message = "Error running command"
56 raiseCmdError(message)57
58 defmain():59
60 cur_platform= '??'
61 llvm_path = '??'
62 ndk_root =_check_ndk_root_env()63 #del the " in the path
64 ndk_root = re.sub(r"\"", "", ndk_root)65 python_bin =_check_python_bin_env()66
67 platform =sys.platform68 if platform == 'win32':69 cur_platform = 'windows'
70 elif platform == 'darwin':71 cur_platform =platform72 elif 'linux' inplatform:73 cur_platform = 'linux'
74 else:75 print 'Your platform is not supported!'
76 sys.exit(1)77
78 x86_llvm_path = ""
79 x64_llvm_path = os.path.abspath(os.path.join(ndk_root, 'toolchains/llvm/prebuilt', '%s-%s' % (cur_platform, 'x86_64')))80 if notos.path.exists(x64_llvm_path):81 x86_llvm_path = os.path.abspath(os.path.join(ndk_root, 'toolchains/llvm/prebuilt', '%s' %(cur_platform)))82 if notos.path.exists(x86_llvm_path):83 x86_llvm_path = os.path.abspath(os.path.join(ndk_root, 'toolchains/llvm/prebuilt', '%s-%s' % (cur_platform, 'x86')))84
85 ifos.path.isdir(x64_llvm_path):86 llvm_path =x64_llvm_path87 elifos.path.isdir(x86_llvm_path):88 llvm_path =x86_llvm_path89 else:90 print 'llvm toolchain not found!'
91 print 'path: %s or path: %s are not valid!' %(x86_llvm_path, x64_llvm_path)92 sys.exit(1)93
94 x86_gcc_toolchain_path = ""
95 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')))96 if notos.path.exists(x64_gcc_toolchain_path):97 x86_gcc_toolchain_path = os.path.abspath(os.path.join(ndk_root, 'toolchains/arm-linux-androideabi-4.9/prebuilt', '%s' %(cur_platform)))98 if notos.path.exists(x86_gcc_toolchain_path):99 x86_gcc_toolchain_path = os.path.abspath(os.path.join(ndk_root, 'toolchains/arm-linux-androideabi-4.9/prebuilt', '%s-%s' % (cur_platform, 'x86')))100
101 ifos.path.isdir(x64_gcc_toolchain_path):102 gcc_toolchain_path =x64_gcc_toolchain_path103 elifos.path.isdir(x86_gcc_toolchain_path):104 gcc_toolchain_path =x86_gcc_toolchain_path105 else:106 print 'gcc toolchain not found!'
107 print 'path: %s or path: %s are not valid!' %(x64_gcc_toolchain_path, x86_gcc_toolchain_path)108 sys.exit(1)109
110
111 project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..'))112 cocos_root = os.path.abspath(os.path.join(project_root, ''))113 cxx_generator_root = os.path.abspath(os.path.join(project_root, 'tools/bindings-generator'))114
115 #save config to file
116 config =ConfigParser.ConfigParser()117 config.set('DEFAULT', 'androidndkdir', ndk_root)118 config.set('DEFAULT', 'clangllvmdir', llvm_path)119 config.set('DEFAULT', 'gcc_toolchain_dir', gcc_toolchain_path)120 config.set('DEFAULT', 'cocosdir', cocos_root)121 config.set('DEFAULT', 'cxxgeneratordir', cxx_generator_root)122 config.set('DEFAULT', 'extra_flags', '')123
124 conf_ini_file = os.path.abspath(os.path.join(os.path.dirname(__file__), 'userconf.ini'))125
126 print 'generating userconf.ini...'
127 with open(conf_ini_file, 'w') as configfile:128 config.write(configfile)129
130
131 #set proper environment variables
132 if 'linux' in platform or platform == 'darwin':133 os.putenv('LD_LIBRARY_PATH', '%s/libclang' %cxx_generator_root)134 if platform == 'win32':135 path_env = os.environ['PATH']136 os.putenv('PATH', r'%s;%s\libclang;%s\tools\win32;' %(path_env, cxx_generator_root, cxx_generator_root))137
138
139 try:140
141 tolua_root = '%s/tools/tolua' %project_root142 output_dir = '%s/cocos/scripting/lua-bindings/auto' %project_root143
144 cmd_args ={145 'cocos2dx_fairygui.ini' : ('cocos2dx_fairygui', 'lua_cocos2dx_fairygui_auto'), \146 }147 target = 'lua'
148 generator_py = '%s/generator.py' %cxx_generator_root149 for key incmd_args.keys():150 args =cmd_args[key]151 cfg = '%s/%s' %(tolua_root, key)152 print 'Generating bindings for %s...' % (key[:-4])153 command = '%s %s %s -s %s -t %s -o %s -n %s' % (python_bin, generator_py, cfg, args[0], target, output_dir, args[1])154 _run_cmd(command)155
156 print '---------------------------------'
157 print 'Generating lua bindings succeeds.'
158 print '---------------------------------'
159
160 exceptException as e:161 if e.__class__.__name__ == 'CmdError':162 print '---------------------------------'
163 print 'Generating lua bindings fails.'
164 print '---------------------------------'
165 sys.exit(1)166 else:167 raise
168
169
170 #-------------- main --------------
171 if __name__ == '__main__':172 main()