3
# Copyright (c) 2009 Google Inc. All rights reserved.
4
# Use of this source code is governed by a BSD-style license that can be
5
# found in the LICENSE file.
7
"""Handle version information related to Visual Stuio."""
15
class VisualStudioVersion:
16
"""Information regarding a version of Visual Studio."""
18
def __init__(self, short_name, description,
19
solution_version, project_version, flat_sln, uses_vcxproj):
20
self.short_name = short_name
21
self.description = description
22
self.solution_version = solution_version
23
self.project_version = project_version
24
self.flat_sln = flat_sln
25
self.uses_vcxproj = uses_vcxproj
28
return self.short_name
30
def Description(self):
31
"""Get the full description of the version."""
32
return self.description
34
def SolutionVersion(self):
35
"""Get the version number of the sln files."""
36
return self.solution_version
38
def ProjectVersion(self):
39
"""Get the version number of the vcproj or vcxproj files."""
40
return self.project_version
42
def FlatSolution(self):
45
def UsesVcxproj(self):
46
"""Returns true if this version uses a vcxproj file."""
47
return self.uses_vcxproj
49
def ProjectExtension(self):
50
"""Returns the file extension for the project."""
51
return self.uses_vcxproj and '.vcxproj' or '.vcproj'
53
def _RegistryGetValue(key, value):
54
"""Use reg.exe to read a paricular key.
56
While ideally we might use the win32 module, we would like gyp to be
57
python neutral, so for instance cygwin python lacks this module.
60
key: The registry key to read from.
61
value: The particular value to read.
63
The contents there, or None for failure.
65
# Skip if not on Windows.
66
if sys.platform not in ('win32', 'cygwin'):
69
cmd = [os.path.join(os.environ.get('WINDIR', ''), 'System32', 'reg.exe'),
70
'query', key, '/v', value]
71
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
72
text = p.communicate()[0]
73
# Require a successful return value.
77
match = re.search(r'REG_\w+\s+([^\r]+)\r\n', text)
83
def _RegistryKeyExists(key):
84
"""Use reg.exe to see if a key exists.
87
key: The registry key to check.
89
True if the key exists
91
# Skip if not on Windows.
92
if sys.platform not in ('win32', 'cygwin'):
95
cmd = [os.path.join(os.environ.get('WINDIR', ''), 'System32', 'reg.exe'),
97
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
98
return p.returncode == 0
101
def _CreateVersion(name):
103
'2010': VisualStudioVersion('2010',
104
'Visual Studio 2010',
105
solution_version='11.00',
106
project_version='4.0',
109
'2008': VisualStudioVersion('2008',
110
'Visual Studio 2008',
111
solution_version='10.00',
112
project_version='9.00',
115
'2008e': VisualStudioVersion('2008e',
116
'Visual Studio 2008',
117
solution_version='10.00',
118
project_version='9.00',
121
'2005': VisualStudioVersion('2005',
122
'Visual Studio 2005',
123
solution_version='9.00',
124
project_version='8.00',
127
'2005e': VisualStudioVersion('2005e',
128
'Visual Studio 2005',
129
solution_version='9.00',
130
project_version='8.00',
134
return versions[str(name)]
137
def _DetectVisualStudioVersions():
138
"""Collect the list of installed visual studio versions.
141
A list of visual studio versions installed in descending order of
143
Base this on the registry and a quick check if devenv.exe exists.
144
Only versions 8-10 are considered.
146
2005 - Visual Studio 2005 (8)
147
2008 - Visual Studio 2008 (9)
148
2010 - Visual Studio 2010 (10)
150
version_to_year = {'8.0': '2005', '9.0': '2008', '10.0': '2010'}
152
# For now, prefer versions before VS2010
153
for version in ('9.0', '8.0', '10.0'):
154
# Check if VS2010 and later is installed as specified by
155
# http://msdn.microsoft.com/en-us/library/bb164659.aspx
156
key32 = r'HKLM\SOFTWARE\Microsoft\DevDiv\VS\Servicing\%s' % version
157
key64 = r'HKLM\SOFTWARE\Wow6432Node\Microsoft\DevDiv\VS\Servicing\%sD' % (
159
if _RegistryKeyExists(key32) or _RegistryKeyExists(key64):
161
# TODO(jeanluc) This does not check for an express version.
162
# TODO(jeanluc) Uncomment this line when ready to support VS2010:
163
# versions.append(_CreateVersion(version_to_year[version]))
165
# Get the install dir for this version.
166
key = r'HKLM\Software\Microsoft\VisualStudio\%s' % version
167
path = _RegistryGetValue(key, 'InstallDir')
171
if os.path.exists(os.path.join(path, 'devenv.exe')):
173
versions.append(_CreateVersion(version_to_year[version]))
175
elif os.path.exists(os.path.join(path, 'vcexpress.exe')):
177
versions.append(_CreateVersion(version_to_year[version] + 'e'))
181
def SelectVisualStudioVersion(version='auto'):
182
"""Select which version of Visual Studio projects to generate.
185
version: Hook to allow caller to force a particular version (vs auto).
187
An object representing a visual studio project format version.
189
# In auto mode, check environment variable for override.
190
if version == 'auto':
191
version = os.environ.get('GYP_MSVS_VERSION', 'auto')
192
# In auto mode, pick the most preferred version present.
193
if version == 'auto':
194
versions = _DetectVisualStudioVersions()
197
return _CreateVersion('2005')
199
# Convert version string into a version object.
200
return _CreateVersion(version)