更新了最新的vs2019 16.7版本后 chromium 编译不通过
chromium在最新的vs2019 16.7.1编译报错。
方法一:同时安装以前版本:
msvs vs2019 安装较早期版本,同时安装vs2019的不同版本参考:
https://docs.microsoft.com/zh-cn/visualstudio/install/install-visual-studio-versions-side-by-side?view=vs-2019
https://docs.microsoft.com/zh-cn/visualstudio/releases/2019/history#installing-an-earlier-release
查看chromium编译脚本,在D:\dev\electron7\src\build\vs_toolchain.py中有查找vs版本的方法:
指定查找的版本顺序,自定义优先:
def DetectVisualStudioPath(): """Return path to the GYP_MSVS_VERSION of Visual Studio. """ # Note that this code is used from # build/toolchain/win/setup_toolchain.py as well. version_as_year = GetVisualStudioVersion() # The VC++ >=2017 install location needs to be located using COM instead of # the registry. For details see: # https://blogs.msdn.microsoft.com/heaths/2016/09/15/changes-to-visual-studio-15-setup/ # For now we use a hardcoded default with an environment variable override. for path in ( os.environ.get('vs%s_install' % version_as_year), os.path.expandvars('%ProgramFiles(x86)%' + '/Microsoft Visual Studio/%s/Community' % version_as_year), os.path.expandvars('%ProgramFiles(x86)%' + '/Microsoft Visual Studio/%s/Enterprise' % version_as_year), os.path.expandvars('%ProgramFiles(x86)%' + '/Microsoft Visual Studio/%s/Professional' % version_as_year), os.path.expandvars('%ProgramFiles(x86)%' + '/Microsoft Visual Studio/%s/Preview' % version_as_year)): if path and os.path.exists(path): return path raise Exception('Visual Studio Version %s (from GYP_MSVS_VERSION)' ' not found.' % version_as_year)
方法二、
D:\dev\electron7\src\build\toolchain\win\setup_toolchain.py 这个是调用方,构造vs环境路径:
def _LoadToolchainEnv(cpu, sdk_dir, target_store): """Returns a dictionary with environment variables that must be set while running binaries from the toolchain (e.g. INCLUDE and PATH for cl.exe).""" # Check if we are running in the SDK command line environment and use # the setup script from the SDK if so. |cpu| should be either # 'x86' or 'x64' or 'arm' or 'arm64'. assert cpu in ('x86', 'x64', 'arm', 'arm64') if bool(int(os.environ.get('DEPOT_TOOLS_WIN_TOOLCHAIN', 1))) and sdk_dir: # Load environment from json file. env = os.path.normpath(os.path.join(sdk_dir, 'bin/SetEnv.%s.json' % cpu)) env = json.load(open(env))['env'] for k in env: entries = [os.path.join(*([os.path.join(sdk_dir, 'bin')] + e)) for e in env[k]] # clang-cl wants INCLUDE to be ;-separated even on non-Windows, # lld-link wants LIB to be ;-separated even on non-Windows. Path gets :. # The separator for INCLUDE here must match the one used in main() below. sep = os.pathsep if k == 'PATH' else ';' env[k] = sep.join(entries) # PATH is a bit of a special case, it's in addition to the current PATH. env['PATH'] = env['PATH'] + os.pathsep + os.environ['PATH'] # Augment with the current env to pick up TEMP and friends. for k in os.environ: if k not in env: env[k] = os.environ[k] varlines = [] for k in sorted(env.keys()): varlines.append('%s=%s' % (str(k), str(env[k]))) variables = '\n'.join(varlines) # Check that the json file contained the same environment as the .cmd file. if sys.platform in ('win32', 'cygwin'): script = os.path.normpath(os.path.join(sdk_dir, 'Bin/SetEnv.cmd')) arg = '/' + cpu json_env = _ExtractImportantEnvironment(variables) cmd_env = _ExtractImportantEnvironment(_LoadEnvFromBat([script, arg])) assert _LowercaseDict(json_env) == _LowercaseDict(cmd_env) else: if 'GYP_MSVS_OVERRIDE_PATH' not in os.environ: os.environ['GYP_MSVS_OVERRIDE_PATH'] = _DetectVisualStudioPath() # We only support x64-hosted tools. script_path = os.path.normpath(os.path.join( os.environ['GYP_MSVS_OVERRIDE_PATH'], 'VC/vcvarsall.bat')) print('vs path: %s ' % (script_path), file=sys.stderr) if not os.path.exists(script_path): # vcvarsall.bat for VS 2017 fails if run after running vcvarsall.bat from # VS 2013 or VS 2015. Fix this by clearing the vsinstalldir environment # variable. if 'VSINSTALLDIR' in os.environ: del os.environ['VSINSTALLDIR'] other_path = os.path.normpath(os.path.join( os.environ['GYP_MSVS_OVERRIDE_PATH'], 'VC/Auxiliary/Build/vcvarsall.bat')) print('other_path path: %s ' % (other_path), file=sys.stderr) if not os.path.exists(other_path): raise Exception('%s is missing - make sure VC++ tools are installed.' % script_path) script_path = other_path cpu_arg = "amd64" if (cpu != 'x64'): # x64 is default target CPU thus any other CPU requires a target set cpu_arg += '_' + cpu args = [script_path, cpu_arg] # Store target must come before any SDK version declaration if (target_store): args.append(['store']) variables = _LoadEnvFromBat(args) print(args, file=sys.stderr) return _ExtractImportantEnvironment(variables)
解决方法:
调用到了加载环境变量,根据命令行D:\>"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvarsall.bat" amd64 -vcvars_ver=14.25 是使用指定的编译环境。
在python中加一句: args.append('-vcvars_ver=14.25')
def _LoadEnvFromBat(args): """Given a bat command, runs it and returns env vars set by it.""" args = args[:]
args.append('-vcvars_ver=14.25') //加了一句指定版本 args.extend(('&&', 'set')) popen = subprocess.Popen( args, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) variables, _ = popen.communicate() if popen.returncode != 0: raise Exception('"%s" failed with error %d' % (args, popen.returncode)) return variables.decode(errors='ignore')
记得必须安装alt mfc的生成工具:
从最外层调用:
def main(): if len(sys.argv) != 7: print('Usage setup_toolchain.py ' '<visual studio path> <win sdk path> ' '<runtime dirs> <target_os> <target_cpu> ' '<environment block name|none>') sys.exit(2) win_sdk_path = sys.argv[2] runtime_dirs = sys.argv[3] target_os = sys.argv[4] target_cpu = sys.argv[5] environment_block_name = sys.argv[6] if (environment_block_name == 'none'): environment_block_name = '' if (target_os == 'winuwp'): target_store = True else: target_store = False cpus = ('x86', 'x64', 'arm', 'arm64') assert target_cpu in cpus vc_bin_dir = '' vc_lib_path = '' vc_lib_atlmfc_path = '' vc_lib_um_path = '' include = '' lib = '' # TODO(scottmg|goma): Do we need an equivalent of # ninja_use_custom_environment_files? for cpu in cpus: if cpu == target_cpu: # Extract environment variables for subprocesses. env = _LoadToolchainEnv(cpu, win_sdk_path, target_store) print(env, file=sys.stderr) env['PATH'] = runtime_dirs + os.pathsep + env['PATH'] for path in env['PATH'].split(os.pathsep): print('vs path: %s ' % (path), file=sys.stderr) if os.path.exists(os.path.join(path, 'cl.exe')): vc_bin_dir = os.path.realpath(path) break
方法三、使用新的clang 11 替换旧的clang 10.
但会爆出一系列警告当错误。
在编译选项中添加忽略:
d:\dev\electron7\src\build\config\compiler\BUILD.gn
cflags = [ "-Wno-incompatible-pointer-types" ]
cflags += [ "-Wno-range-loop-construct" ]
cflags += [ "-Wno-void-pointer-to-int-cast" ]
cflags += [ "-Wno-pointer-to-int-cast" ]
cflags += [ "-Wno-bool-operation" ]
还要忽略多少参数不知道,不推荐。
----------------------------------
结论是升级clang从10到11.这个不现实。替换chromium内部的编译工具带来更多麻烦,因为electron 7对应的版本就是clang 10的。
感觉微软环境不稳定,还是linux好。
最近使用regex_search也会内存溢出。而linux上gcc没问题。