Skip to content

Commit 81beb4a

Browse files
author
minggo
authored
generate lua bytecode according architecture (#379)
* compile 64bit lua bytecode * build lua bytecode according architecture * fix compiling and remove debug codes * remove unneeded codes * handle default situation and use class instead of magic number
1 parent 976a866 commit 81beb4a

File tree

2 files changed

+98
-15
lines changed

2 files changed

+98
-15
lines changed

plugins/plugin_compile/build_android.py

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,58 @@ def gradle_build_apk(self, build_mode):
411411
cmd = '"%s" --parallel --info assemble%s' % (gradle_path, mode_str)
412412
self._run_cmd(cmd, cwd=self.app_android_root)
413413

414+
class LuaBuildType:
415+
UNKNOWN = -1
416+
ONLY_BUILD_64BIT = 1
417+
ONLY_BUILD_32BIT = 2
418+
BUILD_32BIT_AND_64BIT = 3
419+
420+
def _do_get_build_type(self, str):
421+
# remove the '#' and the contents after it
422+
str = str.split('#')[0]
423+
build_64bit = str.find('arm64-v8a') != -1
424+
425+
# check if need to build other architecture
426+
build_other_arch = False
427+
other_archs = ('armeabi', 'armeabi-v7a', 'x86') # other arches are not supported
428+
for arch in other_archs:
429+
if str.find(arch) != -1:
430+
build_other_arch = True
431+
break
432+
433+
if build_64bit or build_other_arch:
434+
if build_64bit:
435+
if build_other_arch:
436+
print 'build 64bit and 32bit'
437+
return LuaBuildType.BUILD_32BIT_AND_64BIT
438+
else:
439+
print 'only build 64bit'
440+
return LuaBuildType.ONLY_BUILD_64BIT
441+
else:
442+
print 'only build 32bit'
443+
return LuaBuildType.ONLY_BUILD_32BIT
444+
445+
return LuaBuildType.UNKNOWN
446+
447+
# check if arm64-v8a is set in Application.mk
448+
def _get_build_type(self, param_of_appabi):
449+
450+
# get build type from parameter
451+
if param_of_appabi:
452+
return self._do_get_build_type(param_of_appabi)
453+
454+
# get build type from Application.mk
455+
applicationmk_path = os.path.join(self.app_android_root, "jni/Application.mk")
456+
with open(applicationmk_path) as f:
457+
for line in f:
458+
if line.find('APP_ABI') == -1:
459+
continue
460+
build_type = self._do_get_build_type(line)
461+
if build_type != LuaBuildType.UNKNOWN:
462+
return build_type
463+
464+
return LuaBuildType.UNKNOWN
465+
414466
def do_build_apk(self, build_mode, no_apk, output_dir, custom_step_args, compile_obj):
415467
if self.use_studio:
416468
assets_dir = os.path.join(self.app_android_root, "app", "assets")
@@ -441,11 +493,37 @@ def do_build_apk(self, build_mode, no_apk, output_dir, custom_step_args, compile
441493

442494
# copy resources
443495
self._copy_resources(custom_step_args, assets_dir)
444-
445496

446497
# check the project config & compile the script files
447498
if self._project._is_lua_project():
448-
compile_obj.compile_lua_scripts(assets_dir, assets_dir)
499+
print "generate byte code ............"
500+
src_dir = os.path.join(assets_dir, 'src')
501+
build_type = self._get_build_type(compile_obj.app_abi)
502+
503+
# only build 64bit
504+
if build_type == LuaBuildType.ONLY_BUILD_64BIT:
505+
dst_dir = os.path.join(assets_dir, 'src/64bit')
506+
compile_obj.compile_lua_scripts(src_dir, dst_dir, True)
507+
# remove unneeded lua files
508+
compile_obj._remove_file_with_ext(src_dir, '.lua')
509+
shutil.rmtree(os.path.join(src_dir, 'cocos'))
510+
511+
# only build 32bit
512+
if build_type == LuaBuildType.ONLY_BUILD_32BIT:
513+
# build 32-bit bytecode
514+
compile_obj.compile_lua_scripts(src_dir, src_dir, False)
515+
516+
# build 32bit and 64bit
517+
if build_type == LuaBuildType.BUILD_32BIT_AND_64BIT:
518+
# build 64-bit bytecode
519+
dst_dir = os.path.join(assets_dir, 'src/64bit')
520+
compile_obj.compile_lua_scripts(src_dir, dst_dir, True)
521+
# build 32-bit bytecode
522+
compile_obj.compile_lua_scripts(src_dir, src_dir, False)
523+
524+
if build_type == LuaBuildType.UNKNOWN:
525+
# haven't set APP_ABI in parameter and Application.mk, default build 32bit
526+
compile_obj.compile_lua_scripts(src_dir, src_dir, False)
449527

450528
if self._project._is_js_project():
451529
compile_obj.compile_js_scripts(assets_dir, assets_dir)

plugins/plugin_compile/project_compile.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -359,23 +359,23 @@ def _remove_file_with_ext(self, work_dir, ext):
359359
if cur_ext == ext:
360360
os.remove(full_path)
361361

362-
def compile_lua_scripts(self, src_dir, dst_dir, need_compile=None):
362+
def compile_lua_scripts(self, src_dir, dst_dir, build_64):
363363
if not self._project._is_lua_project():
364364
return
365365

366-
if need_compile is None:
367-
need_compile = self._compile_script
368-
369-
if not need_compile and not self._lua_encrypt:
366+
if not self._compile_script and not self._lua_encrypt:
370367
return
371368

372369
cocos_cmd_path = os.path.join(os.path.dirname(os.path.abspath(sys.argv[0])), "cocos")
373370
rm_ext = ".lua"
374371
compile_cmd = "\"%s\" luacompile -s \"%s\" -d \"%s\"" % (cocos_cmd_path, src_dir, dst_dir)
375372

376-
if not need_compile:
373+
if not self._compile_script:
377374
compile_cmd = "%s --disable-compile" % compile_cmd
378375

376+
if build_64:
377+
compile_cmd = "%s --bytecode-64bit" % compile_cmd
378+
379379
if self._lua_encrypt:
380380
add_para = ""
381381
if self._lua_encrypt_key is not None:
@@ -708,12 +708,15 @@ def build_ios(self):
708708
self.compile_js_scripts(engine_js_dir, engine_js_dir)
709709
need_reset_dir = True
710710

711-
if self._project._is_lua_project() and self._lua_encrypt:
712-
# on iOS, only invoke luacompile when lua encrypt is specified
711+
if self._project._is_lua_project():
713712
self.backup_dir(script_src_dir)
713+
# create 64-bit folder and build 64-bit bytecode
714+
# should build 64-bit first because `script_src_dir` will be deleted when building 32-bit bytecode
715+
folder_64bit = os.path.join(script_src_dir, '64bit')
716+
self.compile_lua_scripts(script_src_dir, folder_64bit, True)
717+
# build 32-bit bytecode
714718
self.compile_lua_scripts(script_src_dir, script_src_dir, False)
715719
need_reset_dir = True
716-
717720
try:
718721
cocos.Logging.info(MultiLanguage.get_string('COMPILE_INFO_BUILDING'))
719722

@@ -845,10 +848,11 @@ def build_mac(self):
845848
self.compile_js_scripts(engine_js_dir, engine_js_dir)
846849
need_reset_dir = True
847850

848-
if self._project._is_lua_project() and (self._lua_encrypt or self._compile_script):
849-
# on iOS, only invoke luacompile when lua encrypt is specified
851+
if self._project._is_lua_project():
850852
self.backup_dir(script_src_dir)
851-
self.compile_lua_scripts(script_src_dir, script_src_dir)
853+
# mac only support 64-bit bytecode
854+
folder_64bit = os.path.join(script_src_dir, '64bit')
855+
self.compile_lua_scripts(script_src_dir, folder_64bit, True)
852856
need_reset_dir = True
853857

854858
try:
@@ -1180,7 +1184,8 @@ def build_win32(self):
11801184
self.compile_js_scripts(output_dir, output_dir)
11811185

11821186
if self._project._is_lua_project():
1183-
self.compile_lua_scripts(output_dir, output_dir)
1187+
# windows only support 32-bit bytecode
1188+
self.compile_lua_scripts(output_dir, output_dir, False)
11841189

11851190
self.run_root = output_dir
11861191

0 commit comments

Comments
 (0)