[M73 Dev][EFL] Fix errors to generate ninja files
[platform/framework/web/chromium-efl.git] / build / vs_toolchain.py
1 #!/usr/bin/env python
2 # Copyright 2014 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
5
6 import glob
7 import json
8 import os
9 import pipes
10 import platform
11 import re
12 import shutil
13 import stat
14 import subprocess
15 import sys
16
17 from gn_helpers import ToGNString
18
19
20 script_dir = os.path.dirname(os.path.realpath(__file__))
21 json_data_file = os.path.join(script_dir, 'win_toolchain.json')
22
23
24 # Use MSVS2017 as the default toolchain.
25 CURRENT_DEFAULT_TOOLCHAIN_VERSION = '2017'
26
27
28 def SetEnvironmentAndGetRuntimeDllDirs():
29   """Sets up os.environ to use the depot_tools VS toolchain with gyp, and
30   returns the location of the VC runtime DLLs so they can be copied into
31   the output directory after gyp generation.
32
33   Return value is [x64path, x86path, 'Arm64Unused'] or None. arm64path is
34   generated separately because there are multiple folders for the arm64 VC
35   runtime.
36   """
37   vs_runtime_dll_dirs = None
38   depot_tools_win_toolchain = \
39       bool(int(os.environ.get('DEPOT_TOOLS_WIN_TOOLCHAIN', '1')))
40   # When running on a non-Windows host, only do this if the SDK has explicitly
41   # been downloaded before (in which case json_data_file will exist).
42   if ((sys.platform in ('win32', 'cygwin') or os.path.exists(json_data_file))
43       and depot_tools_win_toolchain):
44     if ShouldUpdateToolchain():
45       update_result = Update()
46       if update_result != 0:
47         raise Exception('Failed to update, error code %d.' % update_result)
48     with open(json_data_file, 'r') as tempf:
49       toolchain_data = json.load(tempf)
50
51     toolchain = toolchain_data['path']
52     version = toolchain_data['version']
53     win_sdk = toolchain_data.get('win_sdk')
54     if not win_sdk:
55       win_sdk = toolchain_data['win8sdk']
56     wdk = toolchain_data['wdk']
57     # TODO(scottmg): The order unfortunately matters in these. They should be
58     # split into separate keys for x64/x86/arm64. (See CopyDlls call below).
59     # http://crbug.com/345992
60     vs_runtime_dll_dirs = toolchain_data['runtime_dirs']
61     # The number of runtime_dirs in the toolchain_data was two (x64/x86) but
62     # changed to three (x64/x86/arm64) and this code needs to handle both
63     # possibilities, which can change independently from this code.
64     if len(vs_runtime_dll_dirs) == 2:
65       vs_runtime_dll_dirs.append('Arm64Unused')
66
67     os.environ['GYP_MSVS_OVERRIDE_PATH'] = toolchain
68     os.environ['GYP_MSVS_VERSION'] = version
69
70     os.environ['WINDOWSSDKDIR'] = win_sdk
71     os.environ['WDK_DIR'] = wdk
72     # Include the VS runtime in the PATH in case it's not machine-installed.
73     runtime_path = os.path.pathsep.join(vs_runtime_dll_dirs)
74     os.environ['PATH'] = runtime_path + os.path.pathsep + os.environ['PATH']
75   elif sys.platform == 'win32' and not depot_tools_win_toolchain:
76     if not 'GYP_MSVS_OVERRIDE_PATH' in os.environ:
77       os.environ['GYP_MSVS_OVERRIDE_PATH'] = DetectVisualStudioPath()
78     if not 'GYP_MSVS_VERSION' in os.environ:
79       os.environ['GYP_MSVS_VERSION'] = GetVisualStudioVersion()
80
81     # When using an installed toolchain these files aren't needed in the output
82     # directory in order to run binaries locally, but they are needed in order
83     # to create isolates or the mini_installer. Copying them to the output
84     # directory ensures that they are available when needed.
85     bitness = platform.architecture()[0]
86     # When running 64-bit python the x64 DLLs will be in System32
87     # ARM64 binaries will not be available in the system directories because we
88     # don't build on ARM64 machines.
89     x64_path = 'System32' if bitness == '64bit' else 'Sysnative'
90     x64_path = os.path.join(os.path.expandvars('%windir%'), x64_path)
91     vs_runtime_dll_dirs = [x64_path,
92                            os.path.join(os.path.expandvars('%windir%'),
93                                         'SysWOW64'),
94                            'Arm64Unused']
95
96   return vs_runtime_dll_dirs
97
98
99 def _RegistryGetValueUsingWinReg(key, value):
100   """Use the _winreg module to obtain the value of a registry key.
101
102   Args:
103     key: The registry key.
104     value: The particular registry value to read.
105   Return:
106     contents of the registry key's value, or None on failure.  Throws
107     ImportError if _winreg is unavailable.
108   """
109   import _winreg
110   try:
111     root, subkey = key.split('\\', 1)
112     assert root == 'HKLM'  # Only need HKLM for now.
113     with _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, subkey) as hkey:
114       return _winreg.QueryValueEx(hkey, value)[0]
115   except WindowsError:
116     return None
117
118
119 def _RegistryGetValue(key, value):
120   try:
121     return _RegistryGetValueUsingWinReg(key, value)
122   except ImportError:
123     raise Exception('The python library _winreg not found.')
124
125
126 def GetVisualStudioVersion():
127   """Return GYP_MSVS_VERSION of Visual Studio.
128   """
129   return os.environ.get('GYP_MSVS_VERSION', CURRENT_DEFAULT_TOOLCHAIN_VERSION)
130
131
132 def DetectVisualStudioPath():
133   """Return path to the GYP_MSVS_VERSION of Visual Studio.
134   """
135
136   # Note that this code is used from
137   # build/toolchain/win/setup_toolchain.py as well.
138   version_as_year = GetVisualStudioVersion()
139   year_to_version = {
140       '2017': '15.0',
141       '2019': '16.0',
142   }
143   if version_as_year not in year_to_version:
144     raise Exception(('Visual Studio version %s (from GYP_MSVS_VERSION)'
145                      ' not supported. Supported versions are: %s') % (
146                        version_as_year, ', '.join(year_to_version.keys())))
147
148   # The VC++ >=2017 install location needs to be located using COM instead of
149   # the registry. For details see:
150   # https://blogs.msdn.microsoft.com/heaths/2016/09/15/changes-to-visual-studio-15-setup/
151   # For now we use a hardcoded default with an environment variable override.
152   for path in (
153       os.environ.get('vs%s_install' % version_as_year),
154       os.path.expandvars('%ProgramFiles(x86)%' +
155                          '/Microsoft Visual Studio/%s/Enterprise' %
156                          version_as_year),
157       os.path.expandvars('%ProgramFiles(x86)%' +
158                          '/Microsoft Visual Studio/%s/Professional' %
159                          version_as_year),
160       os.path.expandvars('%ProgramFiles(x86)%' +
161                          '/Microsoft Visual Studio/%s/Community' %
162                          version_as_year),
163       os.path.expandvars('%ProgramFiles(x86)%' +
164                          '/Microsoft Visual Studio/%s/Preview' %
165                          version_as_year)):
166     if path and os.path.exists(path):
167       return path
168
169   raise Exception(('Visual Studio Version %s (from GYP_MSVS_VERSION)'
170                    ' not found.') % (version_as_year))
171
172
173 def _CopyRuntimeImpl(target, source, verbose=True):
174   """Copy |source| to |target| if it doesn't already exist or if it needs to be
175   updated (comparing last modified time as an approximate float match as for
176   some reason the values tend to differ by ~1e-07 despite being copies of the
177   same file... https://crbug.com/603603).
178   """
179   if (os.path.isdir(os.path.dirname(target)) and
180       (not os.path.isfile(target) or
181        abs(os.stat(target).st_mtime - os.stat(source).st_mtime) >= 0.01)):
182     if verbose:
183       print 'Copying %s to %s...' % (source, target)
184     if os.path.exists(target):
185       # Make the file writable so that we can delete it now, and keep it
186       # readable.
187       os.chmod(target, stat.S_IWRITE | stat.S_IREAD)
188       os.unlink(target)
189     shutil.copy2(source, target)
190     # Make the file writable so that we can overwrite or delete it later,
191     # keep it readable.
192     os.chmod(target, stat.S_IWRITE | stat.S_IREAD)
193
194
195 def _CopyUCRTRuntime(target_dir, source_dir, target_cpu, dll_pattern, suffix):
196   """Copy both the msvcp and vccorlib runtime DLLs, only if the target doesn't
197   exist, but the target directory does exist."""
198   if target_cpu == 'arm64':
199     # Windows ARM64 VCRuntime is located at {toolchain_root}/VC/Redist/MSVC/
200     # {x.y.z}/[debug_nonredist/]arm64/Microsoft.VC141.CRT/.
201     vc_redist_root = FindVCRedistRoot()
202     if suffix.startswith('.'):
203       source_dir = os.path.join(vc_redist_root,
204                                 'arm64', 'Microsoft.VC141.CRT')
205     else:
206       source_dir = os.path.join(vc_redist_root, 'debug_nonredist',
207                                 'arm64', 'Microsoft.VC141.DebugCRT')
208   for file_part in ('msvcp', 'vccorlib', 'vcruntime'):
209     dll = dll_pattern % file_part
210     target = os.path.join(target_dir, dll)
211     source = os.path.join(source_dir, dll)
212     _CopyRuntimeImpl(target, source)
213   # Copy the UCRT files from the Windows SDK. This location includes the
214   # api-ms-win-crt-*.dll files that are not found in the Windows directory.
215   # These files are needed for component builds. If WINDOWSSDKDIR is not set
216   # use the default SDK path. This will be the case when
217   # DEPOT_TOOLS_WIN_TOOLCHAIN=0 and vcvarsall.bat has not been run.
218   win_sdk_dir = os.path.normpath(
219       os.environ.get('WINDOWSSDKDIR',
220                      os.path.expandvars('%ProgramFiles(x86)%'
221                                         '\\Windows Kits\\10')))
222   # ARM64 doesn't have a redist for the ucrt DLLs because they are always
223   # present in the OS.
224   if target_cpu != 'arm64':
225     # Starting with the 10.0.17763 SDK the ucrt files are in a version-named
226     # directory - this handles both cases.
227     redist_dir = os.path.join(win_sdk_dir, 'Redist')
228     version_dirs = glob.glob(os.path.join(redist_dir, '10.*'))
229     if len(version_dirs) > 0:
230       version_dirs.sort(reverse=True)
231       redist_dir = version_dirs[0]
232     ucrt_dll_dirs = os.path.join(redist_dir, 'ucrt', 'DLLs', target_cpu)
233     ucrt_files = glob.glob(os.path.join(ucrt_dll_dirs, 'api-ms-win-*.dll'))
234     assert len(ucrt_files) > 0
235     for ucrt_src_file in ucrt_files:
236       file_part = os.path.basename(ucrt_src_file)
237       ucrt_dst_file = os.path.join(target_dir, file_part)
238       _CopyRuntimeImpl(ucrt_dst_file, ucrt_src_file, False)
239   # We must copy ucrtbase.dll for x64/x86, and ucrtbased.dll for all CPU types.
240   if target_cpu != 'arm64' or not suffix.startswith('.'):
241     if not suffix.startswith('.'):
242       # ucrtbased.dll is located at {win_sdk_dir}/bin/{a.b.c.d}/{target_cpu}/
243       # ucrt/.
244       sdk_redist_root = os.path.join(win_sdk_dir, 'bin')
245       sdk_bin_sub_dirs = os.listdir(sdk_redist_root)
246       # Select the most recent SDK if there are multiple versions installed.
247       sdk_bin_sub_dirs.sort(reverse=True)
248       for directory in sdk_bin_sub_dirs:
249         sdk_redist_root_version = os.path.join(sdk_redist_root, directory)
250         if not os.path.isdir(sdk_redist_root_version):
251           continue
252         if re.match('10\.\d+\.\d+\.\d+', directory):
253           source_dir = os.path.join(sdk_redist_root_version, target_cpu, 'ucrt')
254           break
255     _CopyRuntimeImpl(os.path.join(target_dir, 'ucrtbase' + suffix),
256                      os.path.join(source_dir, 'ucrtbase' + suffix))
257
258
259 def FindVCComponentRoot(component):
260   """Find the most recent Tools or Redist or other directory in an MSVC install.
261   Typical results are {toolchain_root}/VC/{component}/MSVC/{x.y.z}. The {x.y.z}
262   version number part changes frequently so the highest version number found is
263   used.
264   """
265   assert GetVisualStudioVersion() in ['2017', '2019']
266   SetEnvironmentAndGetRuntimeDllDirs()
267   assert ('GYP_MSVS_OVERRIDE_PATH' in os.environ)
268   vc_component_msvc_root = os.path.join(os.environ['GYP_MSVS_OVERRIDE_PATH'],
269       'VC', component, 'MSVC')
270   vc_component_msvc_contents = os.listdir(vc_component_msvc_root)
271   # Select the most recent toolchain if there are several.
272   vc_component_msvc_contents.sort(reverse=True)
273   for directory in vc_component_msvc_contents:
274     if not os.path.isdir(os.path.join(vc_component_msvc_root, directory)):
275       continue
276     if re.match('14\.\d+\.\d+', directory):
277       return os.path.join(vc_component_msvc_root, directory)
278   raise Exception('Unable to find the VC %s directory.' % component)
279
280
281 def FindVCRedistRoot():
282   """In >=VS2017, Redist binaries are located in
283   {toolchain_root}/VC/Redist/MSVC/{x.y.z}/{target_cpu}/.
284
285   This returns the '{toolchain_root}/VC/Redist/MSVC/{x.y.z}/' path.
286   """
287   return FindVCComponentRoot('Redist')
288
289
290 def _CopyRuntime(target_dir, source_dir, target_cpu, debug):
291   """Copy the VS runtime DLLs, only if the target doesn't exist, but the target
292   directory does exist. Handles VS 2015, 2017 and 2019."""
293   suffix = 'd.dll' if debug else '.dll'
294   # VS 2015, 2017 and 2019 use the same CRT DLLs.
295   _CopyUCRTRuntime(target_dir, source_dir, target_cpu, '%s140' + suffix,
296                     suffix)
297
298
299 def CopyDlls(target_dir, configuration, target_cpu):
300   """Copy the VS runtime DLLs into the requested directory as needed.
301
302   configuration is one of 'Debug' or 'Release'.
303   target_cpu is one of 'x86', 'x64' or 'arm64'.
304
305   The debug configuration gets both the debug and release DLLs; the
306   release config only the latter.
307   """
308   vs_runtime_dll_dirs = SetEnvironmentAndGetRuntimeDllDirs()
309   if not vs_runtime_dll_dirs:
310     return
311
312   x64_runtime, x86_runtime, arm64_runtime = vs_runtime_dll_dirs
313   if target_cpu == 'x64':
314     runtime_dir = x64_runtime
315   elif target_cpu == 'x86':
316     runtime_dir = x86_runtime
317   elif target_cpu == 'arm64':
318     runtime_dir = arm64_runtime
319   else:
320     raise Exception('Unknown target_cpu: ' + target_cpu)
321   _CopyRuntime(target_dir, runtime_dir, target_cpu, debug=False)
322   if configuration == 'Debug':
323     _CopyRuntime(target_dir, runtime_dir, target_cpu, debug=True)
324   _CopyDebugger(target_dir, target_cpu)
325
326
327 def _CopyDebugger(target_dir, target_cpu):
328   """Copy dbghelp.dll and dbgcore.dll into the requested directory as needed.
329
330   target_cpu is one of 'x86', 'x64' or 'arm64'.
331
332   dbghelp.dll is used when Chrome needs to symbolize stacks. Copying this file
333   from the SDK directory avoids using the system copy of dbghelp.dll which then
334   ensures compatibility with recent debug information formats, such as VS
335   2017 /debug:fastlink PDBs.
336
337   dbgcore.dll is needed when using some functions from dbghelp.dll (like
338   MinidumpWriteDump).
339   """
340   win_sdk_dir = SetEnvironmentAndGetSDKDir()
341   if not win_sdk_dir:
342     return
343
344   # List of debug files that should be copied, the first element of the tuple is
345   # the name of the file and the second indicates if it's optional.
346   debug_files = [('dbghelp.dll', False), ('dbgcore.dll', True)]
347   for debug_file, is_optional in debug_files:
348     full_path = os.path.join(win_sdk_dir, 'Debuggers', target_cpu, debug_file)
349     if not os.path.exists(full_path):
350       if is_optional:
351         continue
352       else:
353         # TODO(crbug.com/773476): remove version requirement.
354         raise Exception('%s not found in "%s"\r\nYou must install the '
355                         '"Debugging Tools for Windows" feature from the Windows'
356                         ' 10 SDK.'
357                         % (debug_file, full_path))
358     target_path = os.path.join(target_dir, debug_file)
359     _CopyRuntimeImpl(target_path, full_path)
360
361
362 def _GetDesiredVsToolchainHashes():
363   """Load a list of SHA1s corresponding to the toolchains that we want installed
364   to build with."""
365   env_version = GetVisualStudioVersion()
366   if env_version == '2017':
367     # VS 2017 Update 9 (15.9.3) with 10.0.17763.132 SDK, 10.0.17134 version of
368     # d3dcompiler_47.dll, with ARM64 libraries.
369     toolchain_hash = '818a152b3f1da991c1725d85be19a0f27af6bab4'
370     # Third parties that do not have access to the canonical toolchain can map
371     # canonical toolchain version to their own toolchain versions.
372     toolchain_hash_mapping_key = 'GYP_MSVS_HASH_%s' % toolchain_hash
373     return [os.environ.get(toolchain_hash_mapping_key, toolchain_hash)]
374   raise Exception('Unsupported VS version %s' % env_version)
375
376
377 def ShouldUpdateToolchain():
378   """Check if the toolchain should be upgraded."""
379   if not os.path.exists(json_data_file):
380     return True
381   with open(json_data_file, 'r') as tempf:
382     toolchain_data = json.load(tempf)
383   version = toolchain_data['version']
384   env_version = GetVisualStudioVersion()
385   # If there's a mismatch between the version set in the environment and the one
386   # in the json file then the toolchain should be updated.
387   return version != env_version
388
389
390 def Update(force=False):
391   """Requests an update of the toolchain to the specific hashes we have at
392   this revision. The update outputs a .json of the various configuration
393   information required to pass to gyp which we use in |GetToolchainDir()|.
394   """
395   if force != False and force != '--force':
396     print >>sys.stderr, 'Unknown parameter "%s"' % force
397     return 1
398   if force == '--force' or os.path.exists(json_data_file):
399     force = True
400
401   depot_tools_win_toolchain = \
402       bool(int(os.environ.get('DEPOT_TOOLS_WIN_TOOLCHAIN', '1')))
403   if ((sys.platform in ('win32', 'cygwin') or force) and
404         depot_tools_win_toolchain):
405     import find_depot_tools
406     depot_tools_path = find_depot_tools.add_depot_tools_to_path()
407
408     # On Linux, the file system is usually case-sensitive while the Windows
409     # SDK only works on case-insensitive file systems.  If it doesn't already
410     # exist, set up a ciopfs fuse mount to put the SDK in a case-insensitive
411     # part of the file system.
412     toolchain_dir = os.path.join(depot_tools_path, 'win_toolchain', 'vs_files')
413     # For testing this block, unmount existing mounts with
414     # fusermount -u third_party/depot_tools/win_toolchain/vs_files
415     if sys.platform.startswith('linux') and not os.path.ismount(toolchain_dir):
416       import distutils.spawn
417       ciopfs = distutils.spawn.find_executable('ciopfs')
418       if not ciopfs:
419         # ciopfs not found in PATH; try the one downloaded from the DEPS hook.
420         ciopfs = os.path.join(script_dir, 'ciopfs')
421       if not os.path.isdir(toolchain_dir):
422         os.mkdir(toolchain_dir)
423       if not os.path.isdir(toolchain_dir + '.ciopfs'):
424         os.mkdir(toolchain_dir + '.ciopfs')
425       # Without use_ino, clang's #pragma once and Wnonportable-include-path
426       # both don't work right, see https://llvm.org/PR34931
427       # use_ino doesn't slow down builds, so it seems there's no drawback to
428       # just using it always.
429       subprocess.check_call([
430           ciopfs, '-o', 'use_ino', toolchain_dir + '.ciopfs', toolchain_dir])
431
432     # Necessary so that get_toolchain_if_necessary.py will put the VS toolkit
433     # in the correct directory.
434     os.environ['GYP_MSVS_VERSION'] = GetVisualStudioVersion()
435     get_toolchain_args = [
436         sys.executable,
437         os.path.join(depot_tools_path,
438                     'win_toolchain',
439                     'get_toolchain_if_necessary.py'),
440         '--output-json', json_data_file,
441       ] + _GetDesiredVsToolchainHashes()
442     if force:
443       get_toolchain_args.append('--force')
444     subprocess.check_call(get_toolchain_args)
445
446   return 0
447
448
449 def NormalizePath(path):
450   while path.endswith('\\'):
451     path = path[:-1]
452   return path
453
454
455 def SetEnvironmentAndGetSDKDir():
456   """Gets location information about the current sdk (must have been
457   previously updated by 'update'). This is used for the GN build."""
458   SetEnvironmentAndGetRuntimeDllDirs()
459
460   # If WINDOWSSDKDIR is not set, search the default SDK path and set it.
461   if not 'WINDOWSSDKDIR' in os.environ:
462     default_sdk_path = os.path.expandvars('%ProgramFiles(x86)%'
463                                           '\\Windows Kits\\10')
464     if os.path.isdir(default_sdk_path):
465       os.environ['WINDOWSSDKDIR'] = default_sdk_path
466
467   return NormalizePath(os.environ['WINDOWSSDKDIR'])
468
469
470 def GetToolchainDir():
471   """Gets location information about the current toolchain (must have been
472   previously updated by 'update'). This is used for the GN build."""
473   runtime_dll_dirs = SetEnvironmentAndGetRuntimeDllDirs()
474   win_sdk_dir = SetEnvironmentAndGetSDKDir()
475
476   print '''vs_path = %s
477 sdk_path = %s
478 vs_version = %s
479 wdk_dir = %s
480 runtime_dirs = %s
481 ''' % (
482       ToGNString(NormalizePath(os.environ['GYP_MSVS_OVERRIDE_PATH'])),
483       ToGNString(win_sdk_dir),
484       ToGNString(GetVisualStudioVersion()),
485       ToGNString(NormalizePath(os.environ.get('WDK_DIR', ''))),
486       ToGNString(os.path.pathsep.join(runtime_dll_dirs or ['None'])))
487
488
489 def main():
490   commands = {
491       'update': Update,
492       'get_toolchain_dir': GetToolchainDir,
493       'copy_dlls': CopyDlls,
494   }
495   if len(sys.argv) < 2 or sys.argv[1] not in commands:
496     print >>sys.stderr, 'Expected one of: %s' % ', '.join(commands)
497     return 1
498   return commands[sys.argv[1]](*sys.argv[2:])
499
500
501 if __name__ == '__main__':
502   sys.exit(main())