OLD | NEW |
| (Empty) |
1 # Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
2 # Use of this source code is governed by a BSD-style license that can be | |
3 # found in the LICENSE file. | |
4 | |
5 import errno | |
6 import glob | |
7 import os | |
8 import re | |
9 import subprocess | |
10 import sys | |
11 | |
12 """ | |
13 This script searches for Visual Studio versions on the current system. | |
14 | |
15 Pass in the preferred VS version on the command line, or pass "auto" for | |
16 autodetect. | |
17 | |
18 This script prints a string containing the VS root directory. On failure it | |
19 returns the empty string. | |
20 """ | |
21 | |
22 def _ConvertToCygpath(path): | |
23 """Convert to cygwin path if we are using cygwin.""" | |
24 if sys.platform == 'cygwin': | |
25 p = subprocess.Popen(['cygpath', path], stdout=subprocess.PIPE) | |
26 path = p.communicate()[0].strip() | |
27 return path | |
28 | |
29 | |
30 def _RegistryQueryBase(sysdir, key, value): | |
31 """Use reg.exe to read a particular key. | |
32 | |
33 While ideally we might use the win32 module, we would like gyp to be | |
34 python neutral, so for instance cygwin python lacks this module. | |
35 | |
36 Arguments: | |
37 sysdir: The system subdirectory to attempt to launch reg.exe from. | |
38 key: The registry key to read from. | |
39 value: The particular value to read. | |
40 Return: | |
41 stdout from reg.exe, or None for failure. | |
42 """ | |
43 # Setup params to pass to and attempt to launch reg.exe | |
44 cmd = [os.path.join(os.environ.get('WINDIR', ''), sysdir, 'reg.exe'), | |
45 'query', key] | |
46 if value: | |
47 cmd.extend(['/v', value]) | |
48 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
49 # Obtain the stdout from reg.exe, reading to the end so p.returncode is valid | |
50 # Note that the error text may be in [1] in some cases | |
51 text = p.communicate()[0] | |
52 # Check return code from reg.exe; officially 0==success and 1==error | |
53 if p.returncode: | |
54 return None | |
55 return text | |
56 | |
57 | |
58 def _RegistryQuery(key, value=None): | |
59 """Use reg.exe to read a particular key through _RegistryQueryBase. | |
60 | |
61 First tries to launch from %WinDir%\Sysnative to avoid WoW64 redirection. If | |
62 that fails, it falls back to System32. Sysnative is available on Vista and | |
63 up and available on Windows Server 2003 and XP through KB patch 942589. Note | |
64 that Sysnative will always fail if using 64-bit python due to it being a | |
65 virtual directory and System32 will work correctly in the first place. | |
66 | |
67 KB 942589 - http://support.microsoft.com/kb/942589/en-us. | |
68 | |
69 Arguments: | |
70 key: The registry key. | |
71 value: The particular registry value to read (optional). | |
72 Return: | |
73 stdout from reg.exe, or None for failure. | |
74 """ | |
75 text = None | |
76 try: | |
77 text = _RegistryQueryBase('Sysnative', key, value) | |
78 except OSError, e: | |
79 if e.errno == errno.ENOENT: | |
80 text = _RegistryQueryBase('System32', key, value) | |
81 else: | |
82 raise | |
83 return text | |
84 | |
85 | |
86 def _RegistryGetValue(key, value): | |
87 """Use reg.exe to obtain the value of a registry key. | |
88 | |
89 Args: | |
90 key: The registry key. | |
91 value: The particular registry value to read. | |
92 Return: | |
93 contents of the registry key's value, or None on failure. | |
94 """ | |
95 text = _RegistryQuery(key, value) | |
96 if not text: | |
97 return None | |
98 # Extract value. | |
99 match = re.search(r'REG_\w+\s+([^\r]+)\r\n', text) | |
100 if not match: | |
101 return None | |
102 return match.group(1) | |
103 | |
104 | |
105 def _DetectVisualStudioVersion(versions_to_check, force_express): | |
106 """Gets the path of the preferred Visual Studio version. | |
107 | |
108 Returns: | |
109 The base path of Visual Studio based on the registry and a quick check if | |
110 devenv.exe exists. | |
111 | |
112 Possibilities are: | |
113 2010(e) - Visual Studio 2010 (10) | |
114 2012(e) - Visual Studio 2012 (11) | |
115 2013(e) - Visual Studio 2013 (12) | |
116 Where (e) is e for express editions of MSVS and blank otherwise. | |
117 """ | |
118 versions = [] | |
119 for version in versions_to_check: | |
120 # Old method of searching for which VS version is installed | |
121 # We don't use the 2010-encouraged-way because we also want to get the | |
122 # path to the binaries, which it doesn't offer. | |
123 keys = [r'HKLM\Software\Microsoft\VisualStudio\%s' % version, | |
124 r'HKLM\Software\Wow6432Node\Microsoft\VisualStudio\%s' % version, | |
125 r'HKLM\Software\Microsoft\VCExpress\%s' % version, | |
126 r'HKLM\Software\Wow6432Node\Microsoft\VCExpress\%s' % version] | |
127 for index in range(len(keys)): | |
128 path = _RegistryGetValue(keys[index], 'InstallDir') | |
129 if not path: | |
130 continue | |
131 path = _ConvertToCygpath(path) | |
132 # Check for full. | |
133 full_path = os.path.join(path, 'devenv.exe') | |
134 express_path = os.path.join(path, '*express.exe') | |
135 if not force_express and os.path.exists(full_path): | |
136 return os.path.normpath(os.path.join(path, '..', '..')) | |
137 # Check for express. | |
138 elif glob.glob(express_path): | |
139 return os.path.normpath(os.path.join(path, '..', '..')) | |
140 | |
141 # The old method above does not work when only SDK is installed. | |
142 keys = [r'HKLM\Software\Microsoft\VisualStudio\SxS\VC7', | |
143 r'HKLM\Software\Wow6432Node\Microsoft\VisualStudio\SxS\VC7'] | |
144 for index in range(len(keys)): | |
145 path = _RegistryGetValue(keys[index], version) | |
146 if not path: | |
147 continue | |
148 path = _ConvertToCygpath(path) | |
149 return os.path.normpath(os.path.join(path, '..')) | |
150 | |
151 return None | |
152 | |
153 if len(sys.argv) != 2: | |
154 print 'Usage: get_visual_studio_path.py <version>' | |
155 print 'Use "auto" for the version to autodetect.' | |
156 sys.exit(2) | |
157 version_map = { | |
158 'auto': ('10.0', '12.0', '11.0'), | |
159 '2010': ('10.0',), | |
160 '2010e': ('10.0',), | |
161 '2012': ('11.0',), | |
162 '2012e': ('11.0',), | |
163 '2013': ('12.0',), | |
164 '2013e': ('12.0',), | |
165 } | |
166 | |
167 requested_version = sys.argv[1] | |
168 vs_path = _DetectVisualStudioVersion(version_map[requested_version], | |
169 'e' in requested_version) | |
170 if not vs_path: | |
171 # No Visual Studio version detected. | |
172 print '""' # Return empty string to .gn file. | |
173 sys.exit(1); | |
174 | |
175 # Return Visual Studio path to the .gn file. | |
176 print '"%s"' % vs_path | |
OLD | NEW |