/src/3rdparty/webkit/Source/ThirdParty/gyp/pylib/gyp/MSVSVersion.py

https://bitbucket.org/ultra_iter/qt-vtl · Python · 200 lines · 108 code · 22 blank · 70 comment · 16 complexity · a1d75a096c651a5d3dd8319897ce122d MD5 · raw file

  1. #!/usr/bin/python
  2. # Copyright (c) 2009 Google Inc. 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. """Handle version information related to Visual Stuio."""
  6. import os
  7. import re
  8. import subprocess
  9. import sys
  10. class VisualStudioVersion:
  11. """Information regarding a version of Visual Studio."""
  12. def __init__(self, short_name, description,
  13. solution_version, project_version, flat_sln, uses_vcxproj):
  14. self.short_name = short_name
  15. self.description = description
  16. self.solution_version = solution_version
  17. self.project_version = project_version
  18. self.flat_sln = flat_sln
  19. self.uses_vcxproj = uses_vcxproj
  20. def ShortName(self):
  21. return self.short_name
  22. def Description(self):
  23. """Get the full description of the version."""
  24. return self.description
  25. def SolutionVersion(self):
  26. """Get the version number of the sln files."""
  27. return self.solution_version
  28. def ProjectVersion(self):
  29. """Get the version number of the vcproj or vcxproj files."""
  30. return self.project_version
  31. def FlatSolution(self):
  32. return self.flat_sln
  33. def UsesVcxproj(self):
  34. """Returns true if this version uses a vcxproj file."""
  35. return self.uses_vcxproj
  36. def ProjectExtension(self):
  37. """Returns the file extension for the project."""
  38. return self.uses_vcxproj and '.vcxproj' or '.vcproj'
  39. def _RegistryGetValue(key, value):
  40. """Use reg.exe to read a paricular key.
  41. While ideally we might use the win32 module, we would like gyp to be
  42. python neutral, so for instance cygwin python lacks this module.
  43. Arguments:
  44. key: The registry key to read from.
  45. value: The particular value to read.
  46. Return:
  47. The contents there, or None for failure.
  48. """
  49. # Skip if not on Windows.
  50. if sys.platform not in ('win32', 'cygwin'):
  51. return None
  52. # Run reg.exe.
  53. cmd = [os.path.join(os.environ.get('WINDIR', ''), 'System32', 'reg.exe'),
  54. 'query', key, '/v', value]
  55. p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  56. text = p.communicate()[0]
  57. # Require a successful return value.
  58. if p.returncode:
  59. return None
  60. # Extract value.
  61. match = re.search(r'REG_\w+\s+([^\r]+)\r\n', text)
  62. if not match:
  63. return None
  64. return match.group(1)
  65. def _RegistryKeyExists(key):
  66. """Use reg.exe to see if a key exists.
  67. Args:
  68. key: The registry key to check.
  69. Return:
  70. True if the key exists
  71. """
  72. # Skip if not on Windows.
  73. if sys.platform not in ('win32', 'cygwin'):
  74. return None
  75. # Run reg.exe.
  76. cmd = [os.path.join(os.environ.get('WINDIR', ''), 'System32', 'reg.exe'),
  77. 'query', key]
  78. p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  79. return p.returncode == 0
  80. def _CreateVersion(name):
  81. versions = {
  82. '2010': VisualStudioVersion('2010',
  83. 'Visual Studio 2010',
  84. solution_version='11.00',
  85. project_version='4.0',
  86. flat_sln=False,
  87. uses_vcxproj=True),
  88. '2008': VisualStudioVersion('2008',
  89. 'Visual Studio 2008',
  90. solution_version='10.00',
  91. project_version='9.00',
  92. flat_sln=False,
  93. uses_vcxproj=False),
  94. '2008e': VisualStudioVersion('2008e',
  95. 'Visual Studio 2008',
  96. solution_version='10.00',
  97. project_version='9.00',
  98. flat_sln=True,
  99. uses_vcxproj=False),
  100. '2005': VisualStudioVersion('2005',
  101. 'Visual Studio 2005',
  102. solution_version='9.00',
  103. project_version='8.00',
  104. flat_sln=False,
  105. uses_vcxproj=False),
  106. '2005e': VisualStudioVersion('2005e',
  107. 'Visual Studio 2005',
  108. solution_version='9.00',
  109. project_version='8.00',
  110. flat_sln=True,
  111. uses_vcxproj=False),
  112. }
  113. return versions[str(name)]
  114. def _DetectVisualStudioVersions():
  115. """Collect the list of installed visual studio versions.
  116. Returns:
  117. A list of visual studio versions installed in descending order of
  118. usage preference.
  119. Base this on the registry and a quick check if devenv.exe exists.
  120. Only versions 8-10 are considered.
  121. Possibilities are:
  122. 2005 - Visual Studio 2005 (8)
  123. 2008 - Visual Studio 2008 (9)
  124. 2010 - Visual Studio 2010 (10)
  125. """
  126. version_to_year = {'8.0': '2005', '9.0': '2008', '10.0': '2010'}
  127. versions = []
  128. # For now, prefer versions before VS2010
  129. for version in ('9.0', '8.0', '10.0'):
  130. # Check if VS2010 and later is installed as specified by
  131. # http://msdn.microsoft.com/en-us/library/bb164659.aspx
  132. key32 = r'HKLM\SOFTWARE\Microsoft\DevDiv\VS\Servicing\%s' % version
  133. key64 = r'HKLM\SOFTWARE\Wow6432Node\Microsoft\DevDiv\VS\Servicing\%sD' % (
  134. version)
  135. if _RegistryKeyExists(key32) or _RegistryKeyExists(key64):
  136. # Add this one.
  137. # TODO(jeanluc) This does not check for an express version.
  138. # TODO(jeanluc) Uncomment this line when ready to support VS2010:
  139. # versions.append(_CreateVersion(version_to_year[version]))
  140. continue
  141. # Get the install dir for this version.
  142. key = r'HKLM\Software\Microsoft\VisualStudio\%s' % version
  143. path = _RegistryGetValue(key, 'InstallDir')
  144. if not path:
  145. continue
  146. # Check for full.
  147. if os.path.exists(os.path.join(path, 'devenv.exe')):
  148. # Add this one.
  149. versions.append(_CreateVersion(version_to_year[version]))
  150. # Check for express.
  151. elif os.path.exists(os.path.join(path, 'vcexpress.exe')):
  152. # Add this one.
  153. versions.append(_CreateVersion(version_to_year[version] + 'e'))
  154. return versions
  155. def SelectVisualStudioVersion(version='auto'):
  156. """Select which version of Visual Studio projects to generate.
  157. Arguments:
  158. version: Hook to allow caller to force a particular version (vs auto).
  159. Returns:
  160. An object representing a visual studio project format version.
  161. """
  162. # In auto mode, check environment variable for override.
  163. if version == 'auto':
  164. version = os.environ.get('GYP_MSVS_VERSION', 'auto')
  165. # In auto mode, pick the most preferred version present.
  166. if version == 'auto':
  167. versions = _DetectVisualStudioVersions()
  168. if not versions:
  169. # Default to 2005.
  170. return _CreateVersion('2005')
  171. return versions[0]
  172. # Convert version string into a version object.
  173. return _CreateVersion(version)