def update_bundle_version(self): build_date = date.today().strftime("%Y%m%d") if cocos.os_is_mac() and self.build_mac: # mac info_plist_path = os.path.join(self.simulator_abs_path, "frameworks/runtime-src/proj.ios_mac/mac/Info.plist") info_plist_content = self.get_content_from_file(info_plist_path) match = re.compile('<key>CFBundleVersion</key>(\s)*<string>(.*?)</string>').findall(info_plist_content) if len(match): build_date_tag = "<string>%s</string>" % match[0][1] keyword_map = { build_date_tag : "<string>%s</string>" % build_date } self.replace_keyword_with_file(info_plist_path, keyword_map) match = re.compile('<key>CFBundleShortVersionString</key>(\s)*<string>(.*?)</string>').findall(info_plist_content) if len(match): build_date_tag = "<string>%s</string>" % match[0][1] keyword_map = { build_date_tag : "<string>%s</string>" % self.engine_version } self.replace_keyword_with_file(info_plist_path, keyword_map) if cocos.os_is_win32() and self.build_win: # win32 game_rc_path = os.path.join(self.simulator_abs_path,"frameworks/runtime-src/proj.win32/game.rc") game_rc_content = self.get_content_from_file(game_rc_path) match = re.compile('"Version[^\(]*\(.*\)"').findall(game_rc_content) if len(match): build_info_str = match[0] target_str = '"Version %s (%s)"' % (self.engine_version, build_date) keyword_map = { build_info_str : target_str} self.replace_keyword_with_file(game_rc_path,keyword_map)
def do_ndk_build(self, ndk_root, ndk_build_param, build_mode): select_toolchain_version(ndk_root) app_android_root = self.app_android_root cocos_root = self.cocos_root ndk_path = os.path.join(ndk_root, "ndk-build") module_paths = [os.path.join(app_android_root, path) for path in self.ndk_module_paths] # windows should use ";" to seperate module paths if cocos.os_is_win32(): ndk_module_path = ';'.join(module_paths) else: ndk_module_path = ':'.join(module_paths) ndk_module_path= 'NDK_MODULE_PATH=' + ndk_module_path if ndk_build_param == None: ndk_build_cmd = '%s -C %s %s' % (ndk_path, app_android_root, ndk_module_path) else: ndk_build_cmd = '%s -C %s %s %s' % (ndk_path, app_android_root, ''.join(str(e) for e in ndk_build_param), ndk_module_path) if build_mode == 'debug': ndk_build_cmd = '%s NDK_DEBUG=1' % ndk_build_cmd self._run_cmd(ndk_build_cmd)
def run(self, argv, dependencies): self.parse_args(argv) if self.is_clean_before_build: utils.rmdir(self.simulator_output_dir) # backup some files modify_files = self.get_depend_project_file_list() if cocos.os_is_mac() and self.build_mac: modify_files.append(os.path.join(self.simulator_abs_path, 'frameworks/runtime-src/proj.ios_mac/mac/Info.plist')) if cocos.os_is_win32() and self.build_win: modify_files.append(os.path.join(self.simulator_abs_path, 'frameworks/runtime-src/proj.win32/game.rc')) self.backup_files(modify_files) try: # modify bundle version self.update_bundle_version() # modify project config files self.change_cocos2d_debug_macro_to_1(modify_files) # compile simulator self.do_compile() except Exception as e: raise e finally: # roll back modified files self.rollback_files(modify_files) Logging.info("") Logging.info(self.build_log) Logging.info("") return 0
def do_ndk_build(self, ndk_build_param, build_mode, compile_obj): cocos.Logging.info(MultiLanguage.get_string('COMPILE_INFO_NDK_MODE', build_mode)) ndk_root = cocos.check_environment_variable('NDK_ROOT') toolchain_version = self.get_toolchain_version(ndk_root, compile_obj) if self.use_studio: ndk_work_dir = os.path.join(self.app_android_root, 'app') else: ndk_work_dir = self.app_android_root reload(sys) sys.setdefaultencoding('utf8') ndk_path = cocos.CMDRunner.convert_path_to_cmd(os.path.join(ndk_root, "ndk-build")) module_paths = [] for cfg_path in self.ndk_module_paths: if cfg_path.find("${ENGINE_ROOT}") >= 0: cocos_root = cocos.check_environment_variable("COCOS_X_ROOT") module_paths.append(cfg_path.replace("${ENGINE_ROOT}", cocos_root)) elif cfg_path.find("${COCOS_FRAMEWORKS}") >= 0: cocos_frameworks = cocos.check_environment_variable("COCOS_FRAMEWORKS") module_paths.append(cfg_path.replace("${COCOS_FRAMEWORKS}", cocos_frameworks)) else: module_paths.append(os.path.join(self.app_android_root, cfg_path)) # delete template static and dynamic files obj_local_dir = os.path.join(ndk_work_dir, "obj", "local") if os.path.isdir(obj_local_dir): for abi_dir in os.listdir(obj_local_dir): static_file_path = os.path.join(ndk_work_dir, "obj", "local", abi_dir) if os.path.isdir(static_file_path): self.remove_c_libs(static_file_path) # windows should use ";" to seperate module paths if cocos.os_is_win32(): ndk_module_path = ';'.join(module_paths) else: ndk_module_path = ':'.join(module_paths) #ndk_module_path= 'NDK_MODULE_PATH=' + ndk_module_path ndk_module_path = '' if ndk_build_param is None: ndk_build_cmd = '%s -C %s %s' % (ndk_path, ndk_work_dir, ndk_module_path) else: ndk_build_cmd = '%s -C %s %s %s' % (ndk_path, ndk_work_dir, ' '.join(ndk_build_param), ndk_module_path) #ndk_build_cmd = '%s NDK_TOOLCHAIN_VERSION=%s' % (ndk_build_cmd, toolchain_version) #ndk_build_cmd = "ndk-build -j8 -C . NDK_DEBUG=0" print ndk_build_cmd print 'xujian' print ndk_build_param print ndk_work_dir print 'xujian' if build_mode == 'debug': ndk_build_cmd = '%s NDK_DEBUG=1' % ndk_build_cmd self._run_cmd(ndk_build_cmd)
def get_luajit_path(self): ret = None if cocos.os_is_win32(): ret = os.path.join(self._workingdir, "bin", "luajit.exe") elif cocos.os_is_mac(): ret = os.path.join(self._workingdir, "bin", "luajit-mac") elif cocos.os_is_linux(): ret = os.path.join(self._workingdir, "bin", "luajit-linux") return ret
def get_devenv_path(vs_version): if cocos.os_is_win32(): import _winreg else: return None if isinstance(vs_version, int): # The value of vs_version is int. such as : 2013, 2015 if vs_version in VS_VERSION_MAP.keys(): vs_ver = VS_VERSION_MAP[vs_version] else: # not supported VS version return None elif isinstance(vs_version, str): # The value of vs_version is string. such as: "12.0", "14.0" vs_ver = vs_version else: return None # If the system is 64bit, find VS in both 32bit & 64bit registry # If the system is 32bit, only find VS in 32bit registry if cocos.os_is_32bit_windows(): reg_flag_list = [ _winreg.KEY_WOW64_32KEY ] else: reg_flag_list = [ _winreg.KEY_WOW64_64KEY, _winreg.KEY_WOW64_32KEY ] devenv_path = None for reg_flag in reg_flag_list: try: vs = _winreg.OpenKey( _winreg.HKEY_LOCAL_MACHINE, r"SOFTWARE\Microsoft\VisualStudio", 0, _winreg.KEY_READ | reg_flag ) except: continue # find specified VS try: key = _winreg.OpenKey(vs, r"SxS\VS7") devenv_path, type = _winreg.QueryValueEx(key, vs_ver) except: pass if devenv_path is not None: devenv_path = os.path.join(devenv_path, "Common7", "IDE", "devenv.com") if os.path.exists(devenv_path): break else: devenv_path = None return devenv_path
def get_luajit_path(self): ret = None bit_prefix = "64bit" if self._bytecode_64bit else "32bit" if cocos.os_is_win32(): ret = os.path.join(self._workingdir, "bin", bit_prefix, "luajit-win32.exe") elif cocos.os_is_mac(): ret = os.path.join(self._workingdir, "bin", bit_prefix, "luajit-mac") elif cocos.os_is_linux(): ret = os.path.join(self._workingdir, "bin", bit_prefix, "luajit-linux") print("luajit bin path: " + ret) return ret
def do_compile(self): if cocos.os_is_mac(): if self.build_mac: self.compile_for_osx() if self.build_ios: self.compile_for_ios() if cocos.os_is_win32(): if self.build_win: self.compile_for_win32() if self.build_android: self.compile_for_android()
def compile(self): if self.clean: self.clean_libs() if cocos.os_is_mac(): if self.build_mac or self.build_ios: self.compile_mac_ios() if cocos.os_is_win32(): if self.build_win: self.compile_win() if self.build_android: self.compile_android()
def _filter_platforms(self, platforms): ret = [] for p in platforms: if cocos.os_is_linux(): if p == Platforms.WEB or p == Platforms.LINUX or p == Platforms.ANDROID: ret.append(p) if cocos.os_is_mac(): if p == Platforms.WEB or p == Platforms.IOS or p == Platforms.MAC or p == Platforms.ANDROID: ret.append(p) if cocos.os_is_win32(): if p == Platforms.WEB or p == Platforms.WIN32 or p == Platforms.ANDROID: ret.append(p) return ret
def trip_libs(self, strip_cmd, folder): if not os.path.isdir(folder): return if cocos.os_is_win32(): for name in os.listdir(folder): basename, ext = os.path.splitext(name) if ext == ".a": full_name = os.path.join(folder, name) command = "%s -S %s" % (strip_cmd, full_name) self._run_cmd(command) else: strip_cmd = "%s -S %s/*.a" % (strip_cmd, folder) self._run_cmd(strip_cmd)
def do_ndk_build(self, ndk_build_param, build_mode, compile_obj): cocos.Logging.info(MultiLanguage.get_string('COMPILE_INFO_NDK_MODE', build_mode)) ndk_root = cocos.check_environment_variable('NDK_ROOT') toolchain_version = self.get_toolchain_version(ndk_root, compile_obj) if self.use_studio: ndk_work_dir = os.path.join(self.app_android_root, 'app') else: ndk_work_dir = self.app_android_root reload(sys) sys.setdefaultencoding('utf8') ndk_path = cocos.CMDRunner.convert_path_to_cmd(os.path.join(ndk_root, "ndk-build")) module_paths = [] for cfg_path in self.ndk_module_paths: the_path = cocos.replace_env_variable(cfg_path) if not os.path.isabs(the_path): the_path = os.path.normpath(os.path.join(self.app_android_root, the_path)) module_paths.append(the_path) # delete template static and dynamic files obj_local_dir = os.path.join(ndk_work_dir, "obj", "local") if os.path.isdir(obj_local_dir): for abi_dir in os.listdir(obj_local_dir): static_file_path = os.path.join(ndk_work_dir, "obj", "local", abi_dir) if os.path.isdir(static_file_path): self.remove_c_libs(static_file_path) # windows should use ";" to seperate module paths if cocos.os_is_win32(): ndk_module_path = ';'.join(module_paths) else: ndk_module_path = ':'.join(module_paths) ndk_module_path= 'NDK_MODULE_PATH=' + ndk_module_path if ndk_build_param is None: ndk_build_cmd = '%s -C %s %s' % (ndk_path, ndk_work_dir, ndk_module_path) else: ndk_build_cmd = '%s -C %s %s %s' % (ndk_path, ndk_work_dir, ' '.join(ndk_build_param), ndk_module_path) ndk_build_cmd = '%s NDK_TOOLCHAIN_VERSION=%s' % (ndk_build_cmd, toolchain_version) if build_mode == 'debug': ndk_build_cmd = '%s NDK_DEBUG=1' % ndk_build_cmd self._run_cmd(ndk_build_cmd)
def do_ndk_build(self, ndk_build_param, build_mode, compile_obj): cocos.Logging.info(cocos.MultiLanguage.get_string('COMPILE_INFO_NDK_MODE') % build_mode) ndk_root = cocos.check_environment_variable('NDK_ROOT') toolchain_version = self.get_toolchain_version(ndk_root, compile_obj) app_android_root = self.app_android_root reload(sys) sys.setdefaultencoding('utf8') ndk_path = cocos.CMDRunner.convert_path_to_cmd(os.path.join(ndk_root, "ndk-build")) module_paths = [] for cfg_path in self.ndk_module_paths: if cfg_path.find("${QUICK_V3_ROOT}") >= 0: cocos_root = cocos.check_environment_variable("QUICK_V3_ROOT") module_paths.append(cfg_path.replace("${QUICK_V3_ROOT}", cocos_root)) elif cfg_path.find("${COCOS_FRAMEWORKS}") >= 0: cocos_frameworks = cocos.check_environment_variable("COCOS_FRAMEWORKS") module_paths.append(cfg_path.replace("${COCOS_FRAMEWORKS}", cocos_frameworks)) else: module_paths.append(os.path.join(app_android_root, cfg_path)) # delete template static and dynamic files obj_local_dir = os.path.join(self.app_android_root, "obj", "local") if os.path.isdir(obj_local_dir): for abi_dir in os.listdir(obj_local_dir): static_file_path = os.path.join(self.app_android_root, "obj", "local", abi_dir) if os.path.isdir(static_file_path): self.remove_c_libs(static_file_path) # windows should use ";" to seperate module paths if cocos.os_is_win32(): ndk_module_path = ';'.join(module_paths) else: ndk_module_path = ':'.join(module_paths) ndk_module_path= 'NDK_MODULE_PATH=' + ndk_module_path if ndk_build_param is None: ndk_build_cmd = '%s -C %s %s' % (ndk_path, app_android_root, ndk_module_path) else: ndk_build_cmd = '%s -C %s %s %s' % (ndk_path, app_android_root, ' '.join(ndk_build_param), ndk_module_path) ndk_build_cmd = '%s NDK_TOOLCHAIN_VERSION=%s' % (ndk_build_cmd, toolchain_version) if build_mode == 'debug': ndk_build_cmd = '%s NDK_DEBUG=1' % ndk_build_cmd self._run_cmd(ndk_build_cmd)
def gradle_build_apk(self, build_mode): # check the compileSdkVersion & buildToolsVersion check_file = os.path.join(self.app_android_root, 'app', 'build.gradle') f = open(check_file) lines = f.readlines() f.close() compile_sdk_ver = None build_tools_ver = None compile_sdk_pattern = r'compileSdkVersion[ \t]+([\d]+)' build_tools_pattern = r'buildToolsVersion[ \t]+"(.+)"' for line in lines: line_str = line.strip() match1 = re.match(compile_sdk_pattern, line_str) if match1: compile_sdk_ver = match1.group(1) match2 = re.match(build_tools_pattern, line_str) if match2: build_tools_ver = match2.group(1) if compile_sdk_ver is not None: # check the compileSdkVersion check_folder_name = 'android-%s' % compile_sdk_ver check_path = os.path.join(self.sdk_root, 'platforms', check_folder_name) if not os.path.isdir(check_path): cocos.Logging.warning(MultiLanguage.get_string('COMPILE_WARNING_COMPILE_SDK_FMT', (compile_sdk_ver, check_path))) if build_tools_ver is not None: # check the buildToolsVersion check_path = os.path.join(self.sdk_root, 'build-tools', build_tools_ver) if not os.path.isdir(check_path): cocos.Logging.warning(MultiLanguage.get_string('COMPILE_WARNING_BUILD_TOOLS_FMT', (build_tools_ver, check_path))) # invoke gradlew for gradle building if cocos.os_is_win32(): gradle_path = os.path.join(self.app_android_root, 'gradlew.bat') else: gradle_path = os.path.join(self.app_android_root, 'gradlew') if not os.path.isfile(gradle_path): raise cocos.CCPluginError(MultiLanguage.get_string('COMPILE_ERROR_GRALEW_NOT_EXIST_FMT', gradle_path), cocos.CCPluginError.ERROR_PATH_NOT_FOUND) mode_str = 'Debug' if build_mode == 'debug' else 'Release' cmd = '"%s" --parallel --info assemble%s' % (gradle_path, mode_str) self._run_cmd(cmd, cwd=self.app_android_root)
def _convert_path_to_cmd(self, path): """ Convert path which include space to correct style which bash(mac) and cmd(windows) can treat correctly. eg: on mac: convert '/usr/xxx/apache-ant 1.9.3' to '/usr/xxx/apache-ant\ 1.9.3' eg: on windows: convert '"c:\apache-ant 1.9.3"\bin' to '"c:\apache-ant 1.9.3\bin"' """ ret = path if cocos.os_is_mac(): ret = path.replace("\ ", " ").replace(" ", "\ ") if cocos.os_is_win32(): ret = "\"%s\"" % (path.replace("\"", "")) # print("!!!!! Convert %s to %s\n" % (path, ret)) return ret
def _convert_path_to_python(self, path): """ COnvert path which include space to correct style which python can treat correctly. eg: on mac: convert '/usr/xxx/apache-ant\ 1.9.3' to '/usr/xxx/apache-ant 1.9.3' eg: on windows: convert '"c:\apache-ant 1.9.3"\bin' to 'c:\apache-ant 1.9.3\bin' """ ret = path if cocos.os_is_mac(): ret = path.replace("\ ", " ") if cocos.os_is_win32(): ret = ret.replace("\"", "") # print("!!!!! Convert %s to %s\n" % (path, ret)) return ret
def get_msbuild_path(vs_version): if cocos.os_is_win32(): import _winreg else: return None if isinstance(vs_version, int): # The value of vs_version is int. such as : 2013, 2015 if vs_version in VS_VERSION_MAP.keys(): vs_ver = VS_VERSION_MAP[vs_version] else: # not supported VS version return None elif isinstance(vs_version, str): # The value of vs_version is string. such as: "12.0", "14.0" vs_ver = vs_version else: return None # If the system is 64bit, find VS in both 32bit & 64bit registry # If the system is 32bit, only find VS in 32bit registry if cocos.os_is_32bit_windows(): reg_flag_list = [ _winreg.KEY_WOW64_32KEY ] else: reg_flag_list = [ _winreg.KEY_WOW64_64KEY, _winreg.KEY_WOW64_32KEY ] # Find VS path msbuild_path = None for reg_flag in reg_flag_list: try: vs = _winreg.OpenKey( _winreg.HKEY_LOCAL_MACHINE, r"SOFTWARE\Microsoft\MSBuild\ToolsVersions\%s" % vs_ver, 0, _winreg.KEY_READ | reg_flag ) msbuild_path, type = _winreg.QueryValueEx(vs, 'MSBuildToolsPath') except: continue if msbuild_path is not None: msbuild_path = os.path.join(msbuild_path, "MSBuild.exe") if os.path.exists(msbuild_path): break else: msbuild_path = None return msbuild_path
def _write_local_properties(self, folder_path): local_porps_path = os.path.join(folder_path, 'local.properties') sdk_dir = self.sdk_root ndk_dir = cocos.check_environment_variable('NDK_ROOT') if cocos.os_is_win32(): # On Windows, the path should be like: # sdk.dir = C:\\path\\android-sdk sdk_dir = sdk_dir.replace('\\', '\\\\') ndk_dir = ndk_dir.replace('\\', '\\\\') lines = [ 'sdk.dir=%s\n' % sdk_dir, 'ndk.dir=%s\n' % ndk_dir ] f = open(local_porps_path, 'w') f.writelines(lines) f.close()
def get_luajit_path(self): ret = None bit_prefix = "64bit" if self._bytecode_64bit else "32bit" if cocos.os_is_win32(): ret = os.path.join(self._workingdir, "bin", bit_prefix, "luajit-win.exe") elif cocos.os_is_mac(): ret = os.path.join(self._workingdir, "bin", bit_prefix, "luajit-mac") elif cocos.os_is_linux(): ret = os.path.join(self._workingdir, "bin", bit_prefix, "luajit-linux") print("luajit bin path: " + ret) return ret
def get_system_info(): if cocos.os_is_win32(): ret_str = "windows" ret_str += "_%s" % platform.release() if cocos.os_is_32bit_windows(): ret_str += "_%s" % "32bit" else: ret_str += "_%s" % "64bit" elif cocos.os_is_mac(): ret_str = "mac_%s" % (platform.mac_ver()[0]).replace(".", "_") elif cocos.os_is_linux(): ret_str = "linux_%s" % platform.linux_distribution()[0] else: ret_str = "unknown" return ret_str
def compile(self): if self.clean: self.clean_libs() if cocos.os_is_mac(): if self.build_mac or self.build_ios: self.compile_mac_ios() if cocos.os_is_win32(): if self.build_win: self.compile_win() if self.build_android: self.compile_android() # generate prebuilt mk files self.modify_binary_mk()
def get_system_info(): if cocos.os_is_win32(): ret_str = "windows" ret_str += "_%s" % platform.release() if cocos.os_is_32bit_windows(): ret_str += "_%s" % "32bit" else: ret_str += "_%s" % "64bit" elif cocos.os_is_mac(): ret_str = "mac_%s" % (platform.mac_ver()[0]).replace('.', '_') elif cocos.os_is_linux(): ret_str = "linux_%s" % platform.linux_distribution()[0] else: ret_str = "unknown" return ret_str
def get_keywords(self): osx_keyword = { "CC_TARGET_OS_IPHONE,":"CC_TARGET_OS_IPHONE,\n\"COCOS2D_DEBUG=1\",", "CC_TARGET_OS_MAC,":"CC_TARGET_OS_MAC,\n\"COCOS2D_DEBUG=1\",", "COCOS2D_DEBUG=0":"COCOS2D_DEBUG=1", } win_keyword = { "_WINDOWS":"_WINDOWS;COCOS2D_DEBUG=1", } if cocos.os_is_mac(): return osx_keyword if cocos.os_is_win32(): return win_keyword return {}
def get_depend_project_file_list(self): file_list = [] if cocos.os_is_mac() and (self.build_mac or self.build_ios): IOS_MAC_PROJECT_SUFFIX = "project.pbxproj" IOS_MAC_PROJECT_REFERENCES_TAG = 'ProjectRef =' IOS_MAC_PROJECT_NAME_RE = r'\w+.xcodeproj' IOS_MAC_PROJECT_PATH_RE = r'name = %s; path = (.)*.xcodeproj' project_file_path = os.path.join(self.simulator_abs_path, SimulatorCompiler.SIMULATOR_XCODE_PATH, IOS_MAC_PROJECT_SUFFIX) contents_str = self.get_content_from_file(project_file_path) lines = re.split(r'\n', contents_str) simulator_mac_project_path = os.path.dirname(os.path.dirname(project_file_path)) project_references = [] for l in lines: if IOS_MAC_PROJECT_REFERENCES_TAG in l: ret = re.search(IOS_MAC_PROJECT_NAME_RE, l) if ret: project_references.append(ret.group(0)) for references in project_references: re_str = IOS_MAC_PROJECT_PATH_RE % references ret = re.search(re_str, contents_str) if ret: match_str = ret.group(0) match_str = match_str.replace("name = %s; path = " % references, "") match_str = match_str.replace('"', "") file_list.append(os.path.join(simulator_mac_project_path, match_str, IOS_MAC_PROJECT_SUFFIX)) if cocos.os_is_win32() and self.build_android: WIN32_PROJECT_TAG = "Project(\"" project_file_path = os.path.join(self.simulator_abs_path, SimulatorCompiler.SIMULATOR_SLN_PATH) simulator_win32_project_path = os.path.dirname(project_file_path) content_str = self.get_content_from_file(project_file_path) lines = content_str.split('\n') for l in lines: if l.startswith(WIN32_PROJECT_TAG): ret = re.compile('"(.*?)"').findall(l.split(',')[1]) if ret: path = self.convert_path_to_win32(os.path.join(simulator_win32_project_path, ret[0])) file_list.append(path) return file_list
def get_vs_versions(): # Get the VS versions ret = [] if cocos.os_is_win32(): import _winreg else: return ret # If the system is 64bit, find VS in both 32bit & 64bit registry # If the system is 32bit, only find VS in 32bit registry if cocos.os_is_32bit_windows(): reg_flag_list = [ _winreg.KEY_WOW64_32KEY ] else: reg_flag_list = [ _winreg.KEY_WOW64_64KEY, _winreg.KEY_WOW64_32KEY ] version_pattern = re.compile(r'(\d+)\.(\d+)') for reg_flag in reg_flag_list: try: vs = _winreg.OpenKey( _winreg.HKEY_LOCAL_MACHINE, r"SOFTWARE\Microsoft\VisualStudio", 0, _winreg.KEY_READ | reg_flag ) except: continue i = 0 while True: # enum the keys in vs reg try: version = _winreg.EnumKey(vs, i) except: break i += 1 match = re.match(version_pattern, version) if match is None: continue ver_str = '%s.%s' % (match.group(1), match.group(2)) if ver_str not in ret: ret.append(ver_str) return ret
def get_msbuild_path(vs_version): if cocos.os_is_win32(): import _winreg else: return None if vs_version == 2013: vs_ver = "12.0" elif vs_version == 2015: vs_ver = "14.0" else: # not supported VS version return None # If the system is 64bit, find VS in both 32bit & 64bit registry # If the system is 32bit, only find VS in 32bit registry if cocos.os_is_32bit_windows(): reg_flag_list = [ _winreg.KEY_WOW64_32KEY ] else: reg_flag_list = [ _winreg.KEY_WOW64_64KEY, _winreg.KEY_WOW64_32KEY ] # Find VS path msbuild_path = None for reg_flag in reg_flag_list: try: vs = _winreg.OpenKey( _winreg.HKEY_LOCAL_MACHINE, r"SOFTWARE\Microsoft\MSBuild\ToolsVersions\%s" % vs_ver, 0, _winreg.KEY_READ | reg_flag ) msbuild_path, type = _winreg.QueryValueEx(vs, 'MSBuildToolsPath') except: continue if msbuild_path is not None and os.path.exists(msbuild_path): break # generate msbuild path if msbuild_path is not None: commandPath = os.path.join(msbuild_path, "MSBuild.exe") else: commandPath = None return commandPath
def get_user_agent(): ret_str = None if cocos.os_is_win32(): ver_info = sys.getwindowsversion() ver_str = '%d.%d' % (ver_info[0], ver_info[1]) if cocos.os_is_32bit_windows(): arch_str = "WOW32" else: arch_str = "WOW64" ret_str = "Mozilla/5.0 (Windows NT %s; %s) Chrome/33.0.1750.154 Safari/537.36" % (ver_str, arch_str) elif cocos.os_is_mac(): ver_str = (platform.mac_ver()[0]).replace('.', '_') ret_str = "Mozilla/5.0 (Macintosh; Intel Mac OS X %s) Chrome/35.0.1916.153 Safari/537.36" % ver_str elif cocos.os_is_linux(): arch_str = platform.machine() ret_str = "Mozilla/5.0 (X11; Linux %s) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1636.0 Safari/537.36" % arch_str return ret_str
def _find_project_dir(self, start_path): path = start_path while True: if cocos.os_is_win32(): # windows root path, eg. c:\ if re.match(".+:\\\\$", path): break else: # unix like use '/' as root path if path == '/': break cfg_path = os.path.join(path, Project.CONFIG) if (os.path.exists(cfg_path) and os.path.isfile(cfg_path)): return path path = os.path.dirname(path) return None
def _find_project_dir(self, start_path): path = start_path while True: if cocos.os_is_win32(): # windows root path, eg. c:\ if re.match(".+:\\\\$", path): break else: # unix like use '/' as root path if path == '/' : break cfg_path = os.path.join(path, Project.CONFIG) if (os.path.exists(cfg_path) and os.path.isfile(cfg_path)): return path path = os.path.dirname(path) return None
def update_bundle_version(self): build_date = date.today().strftime("%Y%m%d") if cocos.os_is_mac() and self.build_mac: # mac info_plist_path = os.path.join( self.simulator_abs_path, "frameworks/runtime-src/proj.ios_mac/mac/Info.plist") info_plist_content = self.get_content_from_file(info_plist_path) match = re.compile( '<key>CFBundleVersion</key>(\s)*<string>(.*?)</string>' ).findall(info_plist_content) if len(match): build_date_tag = "<string>%s</string>" % match[0][1] keyword_map = { build_date_tag: "<string>%s</string>" % build_date } self.replace_keyword_with_file(info_plist_path, keyword_map) match = re.compile( '<key>CFBundleShortVersionString</key>(\s)*<string>(.*?)</string>' ).findall(info_plist_content) if len(match): build_date_tag = "<string>%s</string>" % match[0][1] keyword_map = { build_date_tag: "<string>%s</string>" % self.engine_version } self.replace_keyword_with_file(info_plist_path, keyword_map) if cocos.os_is_win32() and self.build_win: # win32 game_rc_path = os.path.join( self.simulator_abs_path, "frameworks/runtime-src/proj.win32/game.rc") game_rc_content = self.get_content_from_file(game_rc_path) match = re.compile('"Version[^\(]*\(.*\)"').findall( game_rc_content) if len(match): build_info_str = match[0] target_str = '"Version %s (%s)"' % (self.engine_version, build_date) keyword_map = {build_info_str: target_str} self.replace_keyword_with_file(game_rc_path, keyword_map)
def do_ndk_build(self, ndk_build_param, build_mode): cocos.Logging.info('NDK build mode: %s' % build_mode) ndk_root = cocos.check_environment_variable('NDK_ROOT') app_android_root = self.app_android_root cocos_root = self.cocos_root reload(sys) sys.setdefaultencoding('utf8') ndk_path = os.path.join(ndk_root, "ndk-build") module_paths = [ os.path.join(app_android_root, path) for path in self.ndk_module_paths ] # delete template static and dynamic files obj_local_dir = os.path.join(self.app_android_root, "obj", "local") if os.path.isdir(obj_local_dir): for abi_dir in os.listdir(obj_local_dir): static_file_path = os.path.join(self.app_android_root, "obj", "local", abi_dir) if os.path.isdir(static_file_path): self.remove_c_libs(static_file_path) # windows should use ";" to seperate module paths if cocos.os_is_win32(): ndk_module_path = ';'.join(module_paths) else: ndk_module_path = ':'.join(module_paths) ndk_module_path = 'NDK_MODULE_PATH=' + ndk_module_path if ndk_build_param is None: ndk_build_cmd = '%s -C %s %s' % (ndk_path, app_android_root, ndk_module_path) else: ndk_build_cmd = '%s -C %s %s %s' % (ndk_path, app_android_root, ' '.join(ndk_build_param), ndk_module_path) if build_mode == 'debug': ndk_build_cmd = '%s NDK_DEBUG=1' % ndk_build_cmd self._run_cmd(ndk_build_cmd)
def __init__(self, lang, cocos_root, project_name, project_dir, tp_name, tp_dir, project_package, mac_id, ios_id): replace_cocos_root = cocos_root if cocos.os_is_win32(): cocos_root = cocos_root.replace('\\', '/') self.replace_pattern_str = {'COCOS_ROOT_REPLACE': cocos_root} self.lang = lang self.cocos_root = cocos_root self.project_dir = project_dir self.project_name = project_name self.package_name = project_package self.mac_bundleid = mac_id self.ios_bundleid = ios_id self.tp_name = tp_name self.tp_dir = tp_dir self.tp_json = 'cocos-project-template.json' # search in 'template_metadata' first tp_json_path = os.path.join(tp_dir, 'template_metadata', self.tp_json) if not os.path.exists(tp_json_path): # if not, search in the old place tp_json_path = os.path.join(tp_dir, self.tp_json) if not os.path.exists(tp_json_path): message = MultiLanguage.get_string( 'NEW_WARNING_FILE_NOT_FOUND_FMT', tp_json_path) raise cocos.CCPluginError(message, cocos.CCPluginError.ERROR_PATH_NOT_FOUND) f = open(tp_json_path) # keep the key order tpinfo = json.load(f, encoding='utf8', object_pairs_hook=OrderedDict) # read the default creating step if 'do_default' not in tpinfo: message = (MultiLanguage.get_string( 'NEW_ERROR_DEFAILT_CFG_NOT_FOUND_FMT', tp_json_path)) raise cocos.CCPluginError(message, cocos.CCPluginError.ERROR_WRONG_CONFIG) self.tp_default_step = tpinfo.pop('do_default') # keep the other steps self.tp_other_step = tpinfo
def get_bi_params(events, event_value, multi_events=False, engine_versio=''): if cocos.os_is_win32(): system_str = 'windows' ver_info = sys.getwindowsversion() ver_str = '%d.%d' % (ver_info[0], ver_info[1]) if cocos.os_is_32bit_windows(): arch_str = "_32bit" else: arch_str = "_64bit" system_ver = '%s%s' % (ver_str, arch_str) elif cocos.os_is_mac(): system_str = 'mac' system_ver = (platform.mac_ver()[0]) elif cocos.os_is_linux(): system_str = 'linux' system_ver = platform.machine() else: system_str = 'unknown' system_ver = 'unknown' events_param = [] if multi_events: for e in events: events_param.append(gen_bi_event(e, event_value)) else: events_param.append(gen_bi_event(events, event_value)) params = { 'device': { '10' : system_ver, '11' : system_str }, 'app': { '7' : BI_APPID, '8' : engine_version, '9' : get_language() }, 'time' : get_time_stamp(), 'events' : events_param } return params
def do_ndk_build(self, ndk_build_param, build_mode): cocos.Logging.info("NDK build mode: %s" % build_mode) ndk_root = cocos.check_environment_variable("NDK_ROOT") select_toolchain_version(ndk_root) app_android_root = self.app_android_root cocos_root = self.cocos_root reload(sys) sys.setdefaultencoding("utf8") ndk_path = os.path.join(ndk_root, "ndk-build") module_paths = [os.path.join(app_android_root, path) for path in self.ndk_module_paths] # delete template static and dynamic files obj_local_dir = os.path.join(self.app_android_root, "obj", "local") if os.path.isdir(obj_local_dir): for abi_dir in os.listdir(obj_local_dir): static_file_path = os.path.join(self.app_android_root, "obj", "local", abi_dir) if os.path.isdir(static_file_path): self.remove_c_libs(static_file_path) # windows should use ";" to seperate module paths if cocos.os_is_win32(): ndk_module_path = ";".join(module_paths) else: ndk_module_path = ":".join(module_paths) ndk_module_path = "NDK_MODULE_PATH=" + ndk_module_path if ndk_build_param == None: ndk_build_cmd = "%s -C %s %s" % (ndk_path, app_android_root, ndk_module_path) else: ndk_build_cmd = "%s -C %s %s %s" % ( ndk_path, app_android_root, "".join(str(e) for e in ndk_build_param), ndk_module_path, ) if build_mode == "debug": ndk_build_cmd = "%s NDK_DEBUG=1" % ndk_build_cmd self._run_cmd(ndk_build_cmd)
def get_bi_params(events, event_value, multi_events=False): if cocos.os_is_win32(): system_str = 'windows' ver_info = sys.getwindowsversion() ver_str = '%d.%d' % (ver_info[0], ver_info[1]) if cocos.os_is_32bit_windows(): arch_str = "_32bit" else: arch_str = "_64bit" system_ver = '%s%s' % (ver_str, arch_str) elif cocos.os_is_mac(): system_str = 'mac' system_ver = (platform.mac_ver()[0]) elif cocos.os_is_linux(): system_str = 'linux' system_ver = platform.machine() else: system_str = 'unknown' system_ver = 'unknown' events_param = [] if multi_events: for e in events: events_param.append(gen_bi_event(e, event_value)) else: events_param.append(gen_bi_event(events, event_value)) params = { 'device': { '10': system_ver, '11': system_str }, 'app': { '7': BI_APPID, '8': cocos.COCOS2D_CONSOLE_VERSION, '9': get_language() }, 'time': get_time_stamp(), 'events': events_param } return params
def _filter_platforms(self, platforms): ret = [] platforms_for_os = { "linux" : [ Platforms.WEB, Platforms.LINUX, Platforms.ANDROID, Platforms.TIZEN ], "mac" : [ Platforms.WEB, Platforms.IOS, Platforms.MAC, Platforms.ANDROID, Platforms.TIZEN ], "win32" : [ Platforms.WEB, Platforms.WIN32, Platforms.ANDROID, Platforms.METRO, Platforms.TIZEN ] } for p in platforms: if cocos.os_is_linux(): if p in platforms_for_os["linux"]: ret.append(p) if cocos.os_is_mac(): if p in platforms_for_os["mac"]: ret.append(p) if cocos.os_is_win32(): if p in platforms_for_os["win32"]: ret.append(p) return ret
def _filter_platforms(self, platforms): ret = [] platforms_for_os = { "linux" : [ Platforms.WEB, Platforms.LINUX, Platforms.ANDROID ], "mac" : [ Platforms.WEB, Platforms.IOS, Platforms.MAC, Platforms.ANDROID ], "win32" : [ Platforms.WEB, Platforms.WIN32, Platforms.ANDROID, Platforms.WP8, Platforms.WP8_1, Platforms.METRO] } for p in platforms: if cocos.os_is_linux(): if p in platforms_for_os["linux"]: ret.append(p) if cocos.os_is_mac(): if p in platforms_for_os["mac"]: ret.append(p) if cocos.os_is_win32(): if p in platforms_for_os["win32"]: ret.append(p) return ret
def run(self, argv, dependencies): self.parse_args(argv) if self.is_clean_before_build: utils.rmdir(self.simulator_output_dir) # backup some files modify_files = self.get_depend_project_file_list() if cocos.os_is_mac() and self.build_mac: modify_files.append( os.path.join( self.simulator_abs_path, 'frameworks/runtime-src/proj.ios_mac/mac/Info.plist')) if cocos.os_is_win32() and self.build_win: modify_files.append( os.path.join(self.simulator_abs_path, 'frameworks/runtime-src/proj.win32/game.rc')) self.backup_files(modify_files) try: # modify bundle version self.update_bundle_version() # modify project config files self.change_cocos2d_debug_macro_to_1(modify_files) # compile simulator self.do_compile() except Exception as e: raise e finally: # roll back modified files self.rollback_files(modify_files) Logging.info("") Logging.info(self.build_log) Logging.info("") return 0
def do_ndk_build(self, ndk_build_param, build_mode): cocos.Logging.info('NDK build mode: %s' % build_mode) ndk_root = cocos.check_environment_variable('NDK_ROOT') app_android_root = self.app_android_root cocos_root = self.cocos_root reload(sys) sys.setdefaultencoding('utf8') ndk_path = os.path.join(ndk_root, "ndk-build") module_paths = [os.path.join(app_android_root, path) for path in self.ndk_module_paths] # delete template static and dynamic files obj_local_dir = os.path.join(self.app_android_root, "obj", "local") if os.path.isdir(obj_local_dir): for abi_dir in os.listdir(obj_local_dir): static_file_path = os.path.join(self.app_android_root, "obj", "local", abi_dir) if os.path.isdir(static_file_path): self.remove_c_libs(static_file_path) # windows should use ";" to seperate module paths if cocos.os_is_win32(): ndk_module_path = ';'.join(module_paths) else: ndk_module_path = ':'.join(module_paths) ndk_module_path= 'NDK_MODULE_PATH=' + ndk_module_path if ndk_build_param is None: ndk_build_cmd = '%s -C %s %s' % (ndk_path, app_android_root, ndk_module_path) else: ndk_build_cmd = '%s -C %s %s %s' % (ndk_path, app_android_root, ' '.join(ndk_build_param), ndk_module_path) if build_mode == 'debug': ndk_build_cmd = '%s NDK_DEBUG=1' % ndk_build_cmd self._run_cmd(ndk_build_cmd)
def build_web(self): if not self._platforms.is_web_active(): return project_dir = self._platforms.project_path() # store env for run cfg_obj = self._platforms.get_current_config() if cfg_obj.run_root_dir is not None: self.run_root = cfg_obj.run_root_dir else: self.run_root = project_dir if cfg_obj.sub_url is not None: self.sub_url = cfg_obj.sub_url else: self.sub_url = '/' output_dir = "publish" if self._is_debug_mode(): output_dir = "runtime" if not self._web_advanced: return self.sub_url = '%s%s/html5/' % (self.sub_url, output_dir) f = open(os.path.join(project_dir, "project.json")) project_json = json.load(f) f.close() engine_dir = os.path.join(project_json["engineDir"]) realEngineDir = os.path.normpath(os.path.join(project_dir, engine_dir)) publish_dir = os.path.normpath( os.path.join(project_dir, output_dir, "html5")) # need to config in options of command buildOpt = { "outputFileName": "game.min.js", "debug": "true" if self._is_debug_mode() else "false", "compilationLevel": "advanced" if self._web_advanced else "simple", "sourceMapOpened": True if self._has_sourcemap else False } if os.path.exists(publish_dir) == False: os.makedirs(publish_dir) # generate build.xml build_web.gen_buildxml(project_dir, project_json, publish_dir, buildOpt) outputJsPath = os.path.join(publish_dir, buildOpt["outputFileName"]) if os.path.exists(outputJsPath) == True: os.remove(outputJsPath) # call closure compiler ant_root = cocos.check_environment_variable('ANT_ROOT') ant_path = os.path.join(ant_root, 'ant') self._run_cmd("%s -f %s" % (ant_path, os.path.join(publish_dir, 'build.xml'))) # handle sourceMap sourceMapPath = os.path.join(publish_dir, "sourcemap") if os.path.exists(sourceMapPath): smFile = open(sourceMapPath) try: smContent = smFile.read() finally: smFile.close() dir_to_replace = project_dir if cocos.os_is_win32(): dir_to_replace = project_dir.replace('\\', '\\\\') smContent = smContent.replace( dir_to_replace, os.path.relpath(project_dir, publish_dir)) smContent = smContent.replace( realEngineDir, os.path.relpath(realEngineDir, publish_dir)) smContent = smContent.replace('\\\\', '/') smContent = smContent.replace('\\', '/') smFile = open(sourceMapPath, "w") smFile.write(smContent) smFile.close() # handle project.json del project_json["engineDir"] del project_json["modules"] del project_json["jsList"] project_json_output_file = open( os.path.join(publish_dir, "project.json"), "w") project_json_output_file.write(json.dumps(project_json)) project_json_output_file.close() # handle index.html indexHtmlFile = open(os.path.join(project_dir, "index.html")) try: indexContent = indexHtmlFile.read() finally: indexHtmlFile.close() reg1 = re.compile( r'<script\s+src\s*=\s*("|\')[^"\']*CCBoot\.js("|\')\s*><\/script>') indexContent = reg1.sub("", indexContent) mainJs = project_json.get("main") or "main.js" indexContent = indexContent.replace(mainJs, buildOpt["outputFileName"]) indexHtmlOutputFile = open(os.path.join(publish_dir, "index.html"), "w") indexHtmlOutputFile.write(indexContent) indexHtmlOutputFile.close() # copy res dir dst_dir = os.path.join(publish_dir, 'res') src_dir = os.path.join(project_dir, 'res') if os.path.exists(dst_dir): shutil.rmtree(dst_dir) shutil.copytree(src_dir, dst_dir)
def __init__(self): self.process_pool = [] if cocos.os_is_win32(): multiprocessing.freeze_support()
def compile_android(self): # build .so for android cmd_path = self._get_cocos_cmd_path() engine_dir = self.repo_x # build the simulator project proj_path = os.path.join(engine_dir, 'tests/cpp-empty-test') if self.language == 'lua': proj_path = os.path.join(engine_dir, 'tests/lua-empty-test') elif self.language == 'js': proj_path = os.path.join(engine_dir, 'tests/js-tests') build_cmd = "%s compile -s %s -p android --ndk-mode %s --app-abi %s" % ( cmd_path, proj_path, self.mode, self.app_abi) if self.android_platform is not None: build_cmd += ' --ap %s' % self.android_platform self._run_cmd(build_cmd) # copy .a to prebuilt dir ANDROID_A_PATH = "proj.android/app/build/intermediates/ndkBuild/%s/obj/local/%s" % ( self.mode, self.app_abi) if self.language != 'cpp': ANDROID_A_PATH = 'project/' + ANDROID_A_PATH android_out_dir = os.path.join(self.lib_dir, "android") obj_dir = os.path.join(proj_path, ANDROID_A_PATH) copy_cfg = { "from": obj_dir, "to": android_out_dir, "include": ["*.a$"] } cocos.copy_files_with_config(copy_cfg, obj_dir, android_out_dir) if not self.disable_strip: # strip the android libs ndk_root = os.environ["NDK_ROOT"] if cocos.os_is_win32(): if cocos.os_is_32bit_windows(): check_bits = ["", "-x86_64"] else: check_bits = ["-x86_64", ""] sys_folder_name = "windows" for bit_str in check_bits: check_folder_name = "windows%s" % bit_str check_path = os.path.join( ndk_root, "toolchains/arm-linux-androideabi-4.9/prebuilt/%s" % check_folder_name) if os.path.isdir(check_path): sys_folder_name = check_folder_name break elif cocos.os_is_mac(): sys_folder_name = "darwin-x86_64" else: sys_folder_name = "linux-x86_64" # set strip execute file name if cocos.os_is_win32(): strip_execute_name = "strip.exe" else: strip_execute_name = "strip" # strip arm libs strip_cmd_path = os.path.join( ndk_root, "toolchains/arm-linux-androideabi-4.9/prebuilt/%s/arm-linux-androideabi/bin/%s" % (sys_folder_name, strip_execute_name)) if os.path.exists(strip_cmd_path): self.trip_libs(strip_cmd_path, os.path.join(android_out_dir, "armeabi-v7a")) # strip arm64-v8a libs strip_cmd_path = os.path.join( ndk_root, "toolchains/aarch64-linux-android-4.9/prebuilt/%s/aarch64-linux-android/bin/%s" % (sys_folder_name, strip_execute_name)) if os.path.exists(strip_cmd_path) and os.path.exists( os.path.join(android_out_dir, "arm64-v8a")): self.trip_libs(strip_cmd_path, os.path.join(android_out_dir, 'arm64-v8a')) # strip x86 libs strip_cmd_path = os.path.join( ndk_root, "toolchains/x86-4.8/prebuilt/%s/i686-linux-android/bin/%s" % (sys_folder_name, strip_execute_name)) if os.path.exists(strip_cmd_path) and os.path.exists( os.path.join(android_out_dir, "x86")): self.trip_libs(strip_cmd_path, os.path.join(android_out_dir, 'x86'))
def compile_android(self): # build .so for android CONSOLE_PATH = "tools/cocos2d-console/bin" ANDROID_A_PATH = "frameworks/runtime-src/proj.android/obj/local" android_out_dir = os.path.join(self.lib_dir, "android") engine_dir = self.repo_x console_dir = os.path.join(engine_dir, CONSOLE_PATH) if cocos.os_is_win32(): cmd_path = os.path.join(console_dir, "cocos.bat") else: cmd_path = os.path.join(console_dir, "cocos") # build the simulator project proj_path = os.path.join(engine_dir, 'tools/simulator') build_cmd = "%s compile -s %s -p android --ndk-mode %s --app-abi %s" % (cmd_path, proj_path, self.mode, self.app_abi) if self.android_platform is not None: build_cmd += ' --ap %s' % self.android_platform self._run_cmd(build_cmd) # copy .a to prebuilt dir obj_dir = os.path.join(proj_path, ANDROID_A_PATH) copy_cfg = { "from": obj_dir, "to": android_out_dir, "include": [ "*.a$" ] } cocos.copy_files_with_config(copy_cfg, obj_dir, android_out_dir) if not self.disable_strip: # strip the android libs ndk_root = os.environ["NDK_ROOT"] if cocos.os_is_win32(): if cocos.os_is_32bit_windows(): check_bits = [ "", "-x86_64" ] else: check_bits = [ "-x86_64", "" ] sys_folder_name = "windows" for bit_str in check_bits: check_folder_name = "windows%s" % bit_str check_path = os.path.join(ndk_root, "toolchains/arm-linux-androideabi-4.8/prebuilt/%s" % check_folder_name) if os.path.isdir(check_path): sys_folder_name = check_folder_name break elif cocos.os_is_mac(): sys_folder_name = "darwin-x86_64" else: sys_folder_name = "linux-x86_64" # set strip execute file name if cocos.os_is_win32(): strip_execute_name = "strip.exe" else: strip_execute_name = "strip" # strip arm libs strip_cmd_path = os.path.join(ndk_root, "toolchains/arm-linux-androideabi-4.8/prebuilt/%s/arm-linux-androideabi/bin/%s" % (sys_folder_name, strip_execute_name)) if os.path.exists(strip_cmd_path): armlibs = ["armeabi", "armeabi-v7a"] for fold in armlibs: self.trip_libs(strip_cmd_path, os.path.join(android_out_dir, fold)) # strip arm64-v8a libs strip_cmd_path = os.path.join(ndk_root, "toolchains/aarch64-linux-android-4.9/prebuilt/%s/aarch64-linux-android/bin/%s" % (sys_folder_name, strip_execute_name)) if os.path.exists(strip_cmd_path) and os.path.exists(os.path.join(android_out_dir, "arm64-v8a")): self.trip_libs(strip_cmd_path, os.path.join(android_out_dir, 'arm64-v8a')) # strip x86 libs strip_cmd_path = os.path.join(ndk_root, "toolchains/x86-4.8/prebuilt/%s/i686-linux-android/bin/%s" % (sys_folder_name, strip_execute_name)) if os.path.exists(strip_cmd_path) and os.path.exists(os.path.join(android_out_dir, "x86")): self.trip_libs(strip_cmd_path, os.path.join(android_out_dir, 'x86'))
def compile_android(self): # build .so for android CONSOLE_PATH = "tools/cocos2d-console/bin" ANDROID_A_PATH = "frameworks/runtime-src/proj.android/obj/local" android_out_dir = os.path.join(self.lib_dir, "android") engine_dir = self.repo_x console_dir = os.path.join(engine_dir, CONSOLE_PATH) if cocos.os_is_win32(): cmd_path = os.path.join(console_dir, "cocos.bat") else: cmd_path = os.path.join(console_dir, "cocos") # build the simulator project proj_path = os.path.join(engine_dir, 'tools/simulator') build_cmd = "%s compile -s %s -p android --ndk-mode %s --app-abi %s" % ( cmd_path, proj_path, self.mode, self.app_abi) env_param = ExtendEnv.get_extend_env_str() if env_param and len(env_param) > 0: build_cmd += (' --env "%s"' % env_param) self._run_cmd(build_cmd) # copy .a to prebuilt dir obj_dir = os.path.join(proj_path, ANDROID_A_PATH) copy_cfg = { "from": obj_dir, "to": android_out_dir, "include": ["*.a$"] } cocos.copy_files_with_config(copy_cfg, obj_dir, android_out_dir) if not self.disable_strip: # strip the android libs ndk_root = cocos.check_environment_variable("NDK_ROOT") if cocos.os_is_win32(): if cocos.os_is_32bit_windows(): check_bits = ["", "-x86_64"] else: check_bits = ["-x86_64", ""] sys_folder_name = "windows" for bit_str in check_bits: check_folder_name = "windows%s" % bit_str check_path = os.path.join( ndk_root, "toolchains/arm-linux-androideabi-4.8/prebuilt/%s" % check_folder_name) if os.path.isdir(check_path): sys_folder_name = check_folder_name break elif cocos.os_is_mac(): sys_folder_name = "darwin-x86_64" else: sys_folder_name = "linux-x86_64" # set strip execute file name if cocos.os_is_win32(): strip_execute_name = "strip.exe" else: strip_execute_name = "strip" # strip arm libs strip_cmd_path = os.path.join( ndk_root, "toolchains/arm-linux-androideabi-4.8/prebuilt/%s/arm-linux-androideabi/bin/%s" % (sys_folder_name, strip_execute_name)) if os.path.exists(strip_cmd_path): armlibs = ["armeabi", "armeabi-v7a"] for fold in armlibs: self.trip_libs(strip_cmd_path, os.path.join(android_out_dir, fold)) # strip x86 libs strip_cmd_path = os.path.join( ndk_root, "toolchains/x86-4.8/prebuilt/%s/i686-linux-android/bin/%s" % (sys_folder_name, strip_execute_name)) if os.path.exists(strip_cmd_path) and os.path.exists( os.path.join(android_out_dir, "x86")): self.trip_libs(strip_cmd_path, os.path.join(android_out_dir, 'x86'))
def build_win32(self): if not self._platforms.is_win32_active(): return if not cocos.os_is_win32(): raise cocos.CCPluginError("Please build on winodws") win32_projectdir = self._platforms.project_path() output_dir = self._output_dir cocos.Logging.info("building") # get the solution file & project name cfg_obj = self._platforms.get_current_config() if cfg_obj.sln_file is not None: sln_name = cfg_obj.sln_file if cfg_obj.project_name is None: raise cocos.CCPluginError("Must specified \"%s\" when \"%s\" is specified in file \"%s\"") % \ (cocos_project.Win32Config.KEY_PROJECT_NAME, cocos_project.Win32Config.KEY_SLN_FILE, cocos_project.Project.CONFIG) else: name = cfg_obj.project_name else: name, sln_name = self.checkFileByExtention(".sln", win32_projectdir) if not sln_name: message = "Can't find the \".sln\" file" raise cocos.CCPluginError(message) self.project_name = name projectPath = os.path.join(win32_projectdir, sln_name) # get the required VS version build_cfg_path = self._build_cfg_path() required_vs_version = self._get_required_vs_version(projectPath) if required_vs_version is None: raise cocos.CCPluginError("Can't parse the sln file to find required VS version") cocos.Logging.info("Required VS version : %s" % required_vs_version) # get the correct available VS path needUpgrade, vsPath = self._get_vs_path(required_vs_version) if vsPath is None: message = "Can't find correct Visual Studio's path in the regedit" raise cocos.CCPluginError(message) cocos.Logging.info("Find VS path : %s" % vsPath) commandPath = os.path.join(vsPath, "Common7", "IDE", "devenv") build_mode = 'Debug' if self._is_debug_mode() else 'Release' # upgrade projects if needUpgrade: commandUpgrade = ' '.join([ "\"%s\"" % commandPath, "\"%s\"" % projectPath, "/Upgrade" ]) self._run_cmd(commandUpgrade) # build the project commands = ' '.join([ "\"%s\"" % commandPath, "\"%s\"" % projectPath, "/Build \"%s|Win32\"" % build_mode, "/Project \"%s\"" % self.project_name ]) self._run_cmd(commands) cocos.Logging.info("build succeeded.") # copy files build_folder_name = "%s.win32" % build_mode build_folder_path = os.path.join(win32_projectdir, build_folder_name) if not os.path.isdir(build_folder_path): message = "Can not find the %s" % build_folder_path raise cocos.CCPluginError(message) # remove the files in output dir (keep the exe files) if os.path.exists(output_dir): output_files = os.listdir(output_dir) for element in output_files: ele_full_path = os.path.join(output_dir, element) if os.path.isfile(ele_full_path): base_name, file_ext = os.path.splitext(element) if not file_ext == ".exe": os.remove(ele_full_path) elif os.path.isdir(ele_full_path): shutil.rmtree(ele_full_path) # create output dir if it not existed if not os.path.exists(output_dir): os.makedirs(output_dir) # copy dll & exe files = os.listdir(build_folder_path) for filename in files: name, ext = os.path.splitext(filename) proj_exe_name = "%s.exe" % self.project_name if ext == '.dll' or filename == proj_exe_name: file_path = os.path.join(build_folder_path, filename) cocos.Logging.info("Copying %s" % filename) shutil.copy(file_path, output_dir) # copy lua files & res build_cfg = os.path.join(build_cfg_path, CCPluginCompile.BUILD_CONFIG_FILE) if not os.path.exists(build_cfg): message = "%s not found" % build_cfg raise cocos.CCPluginError(message) f = open(build_cfg) data = json.load(f) if data.has_key(CCPluginCompile.CFG_KEY_MUST_COPY_RESOURCES): if self._no_res: fileList = data[CCPluginCompile.CFG_KEY_MUST_COPY_RESOURCES] else: fileList = data[CCPluginCompile.CFG_KEY_COPY_RESOURCES] + data[CCPluginCompile.CFG_KEY_MUST_COPY_RESOURCES] else: fileList = data[CCPluginCompile.CFG_KEY_COPY_RESOURCES] for cfg in fileList: cocos.copy_files_with_config(cfg, build_cfg_path, output_dir) # check the project config & compile the script files self.compile_scripts(output_dir, output_dir) self.run_root = output_dir
def build_web(self): if not self._platforms.is_web_active(): return project_dir = self._platforms.project_path() # store env for run cfg_obj = self._platforms.get_current_config() if cfg_obj.run_root_dir is not None: self.run_root = cfg_obj.run_root_dir else: self.run_root = project_dir if cfg_obj.sub_url is not None: self.sub_url = cfg_obj.sub_url else: self.sub_url = '/' if self._is_debug_mode(): return else: self.sub_url = '%spublish/html5/' % self.sub_url f = open(os.path.join(project_dir, "project.json")) project_json = json.load(f) f.close() engine_dir = os.path.join(project_json["engineDir"]) realEngineDir = os.path.normpath(os.path.join(project_dir, engine_dir)) publish_dir = os.path.normpath(os.path.join(project_dir, "publish", "html5")) # need to config in options of command buildOpt = { "outputFileName" : "game.min.js", #"compilationLevel" : "simple", "compilationLevel" : "advanced", "sourceMapOpened" : True if self._has_sourcemap else False } if os.path.exists(publish_dir) == False: os.makedirs(publish_dir) # generate build.xml build_web.gen_buildxml(project_dir, project_json, publish_dir, buildOpt) outputJsPath = os.path.join(publish_dir, buildOpt["outputFileName"]) if os.path.exists(outputJsPath) == True: os.remove(outputJsPath) # call closure compiler ant_root = cocos.check_environment_variable('ANT_ROOT') ant_path = os.path.join(ant_root, 'ant') self._run_cmd("%s -f %s" % (ant_path, os.path.join(publish_dir, 'build.xml'))) # handle sourceMap sourceMapPath = os.path.join(publish_dir, "sourcemap") if os.path.exists(sourceMapPath): smFile = open(sourceMapPath) try: smContent = smFile.read() finally: smFile.close() dir_to_replace = project_dir if cocos.os_is_win32(): dir_to_replace = project_dir.replace('\\', '\\\\') smContent = smContent.replace(dir_to_replace, os.path.relpath(project_dir, publish_dir)) smContent = smContent.replace(realEngineDir, os.path.relpath(realEngineDir, publish_dir)) smContent = smContent.replace('\\\\', '/') smContent = smContent.replace('\\', '/') smFile = open(sourceMapPath, "w") smFile.write(smContent) smFile.close() # handle project.json del project_json["engineDir"] del project_json["modules"] del project_json["jsList"] project_json_output_file = open(os.path.join(publish_dir, "project.json"), "w") project_json_output_file.write(json.dumps(project_json)) project_json_output_file.close() # handle index.html indexHtmlFile = open(os.path.join(project_dir, "index.html")) try: indexContent = indexHtmlFile.read() finally: indexHtmlFile.close() reg1 = re.compile(r'<script\s+src\s*=\s*("|\')[^"\']*CCBoot\.js("|\')\s*><\/script>') indexContent = reg1.sub("", indexContent) mainJs = project_json.get("main") or "main.js" indexContent = indexContent.replace(mainJs, buildOpt["outputFileName"]) indexHtmlOutputFile = open(os.path.join(publish_dir, "index.html"), "w") indexHtmlOutputFile.write(indexContent) indexHtmlOutputFile.close() # copy res dir dst_dir = os.path.join(publish_dir, 'res') src_dir = os.path.join(project_dir, 'res') if os.path.exists(dst_dir): shutil.rmtree(dst_dir) shutil.copytree(src_dir, dst_dir)
def build_win32(self): if not self._platforms.is_win32_active(): return if not cocos.os_is_win32(): raise cocos.CCPluginError("Please build on winodws") win32_projectdir = self._platforms.project_path() output_dir = self._output_dir cocos.Logging.info("building") # get the solution file & project name cfg_obj = self._platforms.get_current_config() if cfg_obj.sln_file is not None: sln_name = cfg_obj.sln_file if cfg_obj.project_name is None: raise cocos.CCPluginError("Must specified \"%s\" when \"%s\" is specified in file \"%s\"") % \ (cocos_project.Win32Config.KEY_PROJECT_NAME, cocos_project.Win32Config.KEY_SLN_FILE, cocos_project.Project.CONFIG) else: name = cfg_obj.project_name else: name, sln_name = self.checkFileByExtention(".sln", win32_projectdir) if not sln_name: message = "Can't find the \".sln\" file" raise cocos.CCPluginError(message) self.project_name = name projectPath = os.path.join(win32_projectdir, sln_name) # get the required VS version build_cfg_path = self._build_cfg_path() required_vs_version = self._get_required_vs_version(projectPath) if required_vs_version is None: raise cocos.CCPluginError( "Can't parse the sln file to find required VS version") cocos.Logging.info("Required VS version : %s" % required_vs_version) # get the correct available VS path needUpgrade, vsPath = self._get_vs_path(required_vs_version) if vsPath is None: message = "Can't find correct Visual Studio's path in the regedit" raise cocos.CCPluginError(message) cocos.Logging.info("Find VS path : %s" % vsPath) commandPath = os.path.join(vsPath, "Common7", "IDE", "devenv") build_mode = 'Debug' if self._is_debug_mode() else 'Release' # upgrade projects if needUpgrade: commandUpgrade = ' '.join( ["\"%s\"" % commandPath, "\"%s\"" % projectPath, "/Upgrade"]) self._run_cmd(commandUpgrade) # build the project commands = ' '.join([ "\"%s\"" % commandPath, "\"%s\"" % projectPath, "/Build \"%s|Win32\"" % build_mode, "/Project \"%s\"" % self.project_name ]) self._run_cmd(commands) cocos.Logging.info("build succeeded.") # copy files build_folder_name = "%s.win32" % build_mode build_folder_path = os.path.join(win32_projectdir, build_folder_name) if not os.path.isdir(build_folder_path): message = "Can not find the %s" % build_folder_path raise cocos.CCPluginError(message) # remove the files in output dir (keep the exe files) if os.path.exists(output_dir): output_files = os.listdir(output_dir) for element in output_files: ele_full_path = os.path.join(output_dir, element) if os.path.isfile(ele_full_path): base_name, file_ext = os.path.splitext(element) if not file_ext == ".exe": os.remove(ele_full_path) elif os.path.isdir(ele_full_path): shutil.rmtree(ele_full_path) # create output dir if it not existed if not os.path.exists(output_dir): os.makedirs(output_dir) # copy dll & exe files = os.listdir(build_folder_path) for filename in files: name, ext = os.path.splitext(filename) proj_exe_name = "%s.exe" % self.project_name if ext == '.dll' or filename == proj_exe_name: file_path = os.path.join(build_folder_path, filename) cocos.Logging.info("Copying %s" % filename) shutil.copy(file_path, output_dir) # copy lua files & res build_cfg = os.path.join(build_cfg_path, CCPluginCompile.BUILD_CONFIG_FILE) if not os.path.exists(build_cfg): message = "%s not found" % build_cfg raise cocos.CCPluginError(message) f = open(build_cfg) data = json.load(f) if data.has_key(CCPluginCompile.CFG_KEY_MUST_COPY_RESOURCES): if self._no_res: fileList = data[CCPluginCompile.CFG_KEY_MUST_COPY_RESOURCES] else: fileList = data[CCPluginCompile.CFG_KEY_COPY_RESOURCES] + data[ CCPluginCompile.CFG_KEY_MUST_COPY_RESOURCES] else: fileList = data[CCPluginCompile.CFG_KEY_COPY_RESOURCES] for cfg in fileList: cocos.copy_files_with_config(cfg, build_cfg_path, output_dir) # check the project config & compile the script files self.compile_scripts(output_dir, output_dir) self.run_root = output_dir