~ubuntu-branches/ubuntu/raring/qtwebkit-source/raring-proposed

« back to all changes in this revision

Viewing changes to Source/ThirdParty/gyp/pylib/gyp/MSVSVersion.py

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-02-18 14:24:18 UTC
  • Revision ID: package-import@ubuntu.com-20130218142418-eon0jmjg3nj438uy
Tags: upstream-2.3
ImportĀ upstreamĀ versionĀ 2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
 
 
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.
 
6
 
 
7
"""Handle version information related to Visual Stuio."""
 
8
 
 
9
import os
 
10
import re
 
11
import subprocess
 
12
import sys
 
13
 
 
14
 
 
15
class VisualStudioVersion:
 
16
  """Information regarding a version of Visual Studio."""
 
17
 
 
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
 
26
 
 
27
  def ShortName(self):
 
28
    return self.short_name
 
29
 
 
30
  def Description(self):
 
31
    """Get the full description of the version."""
 
32
    return self.description
 
33
 
 
34
  def SolutionVersion(self):
 
35
    """Get the version number of the sln files."""
 
36
    return self.solution_version
 
37
 
 
38
  def ProjectVersion(self):
 
39
    """Get the version number of the vcproj or vcxproj files."""
 
40
    return self.project_version
 
41
 
 
42
  def FlatSolution(self):
 
43
    return self.flat_sln
 
44
 
 
45
  def UsesVcxproj(self):
 
46
    """Returns true if this version uses a vcxproj file."""
 
47
    return self.uses_vcxproj
 
48
 
 
49
  def ProjectExtension(self):
 
50
    """Returns the file extension for the project."""
 
51
    return self.uses_vcxproj and '.vcxproj' or '.vcproj'
 
52
 
 
53
def _RegistryGetValue(key, value):
 
54
  """Use reg.exe to read a paricular key.
 
55
 
 
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.
 
58
 
 
59
  Arguments:
 
60
    key: The registry key to read from.
 
61
    value: The particular value to read.
 
62
  Return:
 
63
    The contents there, or None for failure.
 
64
  """
 
65
  # Skip if not on Windows.
 
66
  if sys.platform not in ('win32', 'cygwin'):
 
67
    return None
 
68
  # Run reg.exe.
 
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.
 
74
  if p.returncode:
 
75
    return None
 
76
  # Extract value.
 
77
  match = re.search(r'REG_\w+\s+([^\r]+)\r\n', text)
 
78
  if not match:
 
79
    return None
 
80
  return match.group(1)
 
81
 
 
82
 
 
83
def _RegistryKeyExists(key):
 
84
  """Use reg.exe to see if a key exists.
 
85
 
 
86
  Args:
 
87
    key: The registry key to check.
 
88
  Return:
 
89
    True if the key exists
 
90
  """
 
91
  # Skip if not on Windows.
 
92
  if sys.platform not in ('win32', 'cygwin'):
 
93
    return None
 
94
  # Run reg.exe.
 
95
  cmd = [os.path.join(os.environ.get('WINDIR', ''), 'System32', 'reg.exe'),
 
96
         'query', key]
 
97
  p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
 
98
  return p.returncode == 0
 
99
 
 
100
 
 
101
def _CreateVersion(name):
 
102
  versions = {
 
103
      '2010': VisualStudioVersion('2010',
 
104
                                  'Visual Studio 2010',
 
105
                                  solution_version='11.00',
 
106
                                  project_version='4.0',
 
107
                                  flat_sln=False,
 
108
                                  uses_vcxproj=True),
 
109
      '2008': VisualStudioVersion('2008',
 
110
                                  'Visual Studio 2008',
 
111
                                  solution_version='10.00',
 
112
                                  project_version='9.00',
 
113
                                  flat_sln=False,
 
114
                                  uses_vcxproj=False),
 
115
      '2008e': VisualStudioVersion('2008e',
 
116
                                   'Visual Studio 2008',
 
117
                                   solution_version='10.00',
 
118
                                   project_version='9.00',
 
119
                                   flat_sln=True,
 
120
                                   uses_vcxproj=False),
 
121
      '2005': VisualStudioVersion('2005',
 
122
                                  'Visual Studio 2005',
 
123
                                  solution_version='9.00',
 
124
                                  project_version='8.00',
 
125
                                  flat_sln=False,
 
126
                                  uses_vcxproj=False),
 
127
      '2005e': VisualStudioVersion('2005e',
 
128
                                   'Visual Studio 2005',
 
129
                                   solution_version='9.00',
 
130
                                   project_version='8.00',
 
131
                                   flat_sln=True,
 
132
                                   uses_vcxproj=False),
 
133
  }
 
134
  return versions[str(name)]
 
135
 
 
136
 
 
137
def _DetectVisualStudioVersions():
 
138
  """Collect the list of installed visual studio versions.
 
139
 
 
140
  Returns:
 
141
    A list of visual studio versions installed in descending order of
 
142
    usage preference.
 
143
    Base this on the registry and a quick check if devenv.exe exists.
 
144
    Only versions 8-10 are considered.
 
145
    Possibilities are:
 
146
      2005 - Visual Studio 2005 (8)
 
147
      2008 - Visual Studio 2008 (9)
 
148
      2010 - Visual Studio 2010 (10)
 
149
  """
 
150
  version_to_year = {'8.0': '2005', '9.0': '2008', '10.0': '2010'}
 
151
  versions = []
 
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' % (
 
158
         version)
 
159
    if _RegistryKeyExists(key32) or _RegistryKeyExists(key64):
 
160
      # Add this one.
 
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]))
 
164
      continue
 
165
    # Get the install dir for this version.
 
166
    key = r'HKLM\Software\Microsoft\VisualStudio\%s' % version
 
167
    path = _RegistryGetValue(key, 'InstallDir')
 
168
    if not path:
 
169
      continue
 
170
    # Check for full.
 
171
    if os.path.exists(os.path.join(path, 'devenv.exe')):
 
172
      # Add this one.
 
173
      versions.append(_CreateVersion(version_to_year[version]))
 
174
    # Check for express.
 
175
    elif os.path.exists(os.path.join(path, 'vcexpress.exe')):
 
176
      # Add this one.
 
177
      versions.append(_CreateVersion(version_to_year[version] + 'e'))
 
178
  return versions
 
179
 
 
180
 
 
181
def SelectVisualStudioVersion(version='auto'):
 
182
  """Select which version of Visual Studio projects to generate.
 
183
 
 
184
  Arguments:
 
185
    version: Hook to allow caller to force a particular version (vs auto).
 
186
  Returns:
 
187
    An object representing a visual studio project format version.
 
188
  """
 
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()
 
195
    if not versions:
 
196
      # Default to 2005.
 
197
      return _CreateVersion('2005')
 
198
    return versions[0]
 
199
  # Convert version string into a version object.
 
200
  return _CreateVersion(version)