 Chromium Code Reviews
 Chromium Code Reviews Issue 1556993002:
  [gn] Detect location of Visual Studio in the registry.  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master
    
  
    Issue 1556993002:
  [gn] Detect location of Visual Studio in the registry.  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master| Index: build/vs_toolchain.py | 
| diff --git a/build/vs_toolchain.py b/build/vs_toolchain.py | 
| index 7f535ea9bb41c317b55af45eb804116f38324f9d..1c36d52fe94e0b14ffb01d85bc11f8db02dca96e 100755 | 
| --- a/build/vs_toolchain.py | 
| +++ b/build/vs_toolchain.py | 
| @@ -64,9 +64,71 @@ def SetEnvironmentAndGetRuntimeDllDirs(): | 
| # Include the VS runtime in the PATH in case it's not machine-installed. | 
| runtime_path = ';'.join(vs_runtime_dll_dirs) | 
| os.environ['PATH'] = runtime_path + ';' + os.environ['PATH'] | 
| + elif sys.platform in ('win32', 'cygwin') and not depot_tools_win_toolchain: | 
| 
scottmg
2016/01/05 23:30:32
Did you test on cygwin? Otherwise drop that; I thi
 
Daniel Bratell
2016/01/07 16:10:48
Quite likely, and yes, I did not test in cygwin. R
 | 
| + if not 'GYP_MSVS_OVERRIDE_PATH' in os.environ: | 
| + os.environ['GYP_MSVS_OVERRIDE_PATH'] = DetectVisualStudioPath() | 
| + | 
| return vs_runtime_dll_dirs | 
| +def _RegistryGetValueUsingWinReg(key, value): | 
| + """Use the _winreg module to obtain the value of a registry key. | 
| + | 
| + Args: | 
| + key: The registry key. | 
| + value: The particular registry value to read. | 
| + Return: | 
| + contents of the registry key's value, or None on failure. Throws | 
| + ImportError if _winreg is unavailable. | 
| + """ | 
| + import _winreg | 
| + try: | 
| + root, subkey = key.split('\\', 1) | 
| + assert root == 'HKLM' # Only need HKLM for now. | 
| + with _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, subkey) as hkey: | 
| + return _winreg.QueryValueEx(hkey, value)[0] | 
| + except WindowsError: | 
| + return None | 
| + | 
| + | 
| +def _RegistryGetValue(key, value): | 
| + try: | 
| 
scottmg
2016/01/05 23:30:32
Indent is wrong in this function.
 
Daniel Bratell
2016/01/07 16:10:48
Done.
 | 
| + return _RegistryGetValueUsingWinReg(key, value) | 
| + except ImportError: | 
| + raise Exception('The python library _winreg not found.') | 
| + | 
| + | 
| +def DetectVisualStudioPath(): | 
| + """Return path to the GYP_MSVS_VERSION of Visual Studio | 
| 
scottmg
2016/01/05 23:30:32
nit; End with '.'.
 | 
| + """ | 
| + | 
| + # Note that this code is used from | 
| + # build/toolchain/win/setup_toolchain.py as well. | 
| + | 
| + # Default to Visual Studio 2013 for now. | 
| + version_as_year = os.environ.get('GYP_MSVS_VERSION', '2013') | 
| + year_to_version = { | 
| + '2013': '12.0', | 
| + '2015': '14.0', | 
| + } | 
| + if not version_as_year in year_to_version: | 
| 
scottmg
2016/01/05 23:30:32
"if version_as_year not in year_to_version" is mor
 
Daniel Bratell
2016/01/07 16:10:48
Done.
 | 
| + raise Exception(('Visual Studio Version %s (from GYP_MSVS_VERSION)' | 
| 
scottmg
2016/01/05 23:30:32
nit; lower case v on "version".
 
Daniel Bratell
2016/01/07 16:10:48
Done.
 | 
| + ' not supported. Supported versions are: %s') % ( | 
| + version_as_year, ', '.join(year_to_version.keys()))) | 
| + version = year_to_version[version_as_year] | 
| + keys = [r'HKLM\Software\Microsoft\VisualStudio\%s' % version, | 
| + r'HKLM\Software\Wow6432Node\Microsoft\VisualStudio\%s' % version] | 
| + for key in keys: | 
| + path = _RegistryGetValue(key, 'InstallDir') | 
| + if not path: | 
| + continue | 
| + path = os.path.normpath(os.path.join(path, '..', '..')) | 
| + return path | 
| + | 
| + raise Exception(('Visual Studio Version %s (from GYP_MSVS_VERSION)' | 
| + ' not found.') % (version_as_year)) | 
| + | 
| + | 
| def _VersionNumber(): | 
| """Gets the standard version number ('120', '140', etc.) based on | 
| GYP_MSVS_VERSION.""" |