config.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. # Copyright (C) 2010 Google Inc. All rights reserved.
  2. #
  3. # Redistribution and use in source and binary forms, with or without
  4. # modification, are permitted provided that the following conditions are
  5. # met:
  6. #
  7. # * Redistributions of source code must retain the above copyright
  8. # notice, this list of conditions and the following disclaimer.
  9. # * Redistributions in binary form must reproduce the above
  10. # copyright notice, this list of conditions and the following disclaimer
  11. # in the documentation and/or other materials provided with the
  12. # distribution.
  13. # * Neither the name of Google Inc. nor the names of its
  14. # contributors may be used to endorse or promote products derived from
  15. # this software without specific prior written permission.
  16. #
  17. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  20. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  21. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  22. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  23. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  24. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  25. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. """Wrapper objects for WebKit-specific utility routines."""
  29. # FIXME: This file needs to be unified with common/config/ports.py .
  30. import logging
  31. from webkitpy.common import webkit_finder
  32. _log = logging.getLogger(__name__)
  33. #
  34. # FIXME: This is used to record if we've already hit the filesystem to look
  35. # for a default configuration. We cache this to speed up the unit tests,
  36. # but this can be reset with clear_cached_configuration(). This should be
  37. # replaced with us consistently using MockConfigs() for tests that don't
  38. # hit the filesystem at all and provide a reliable value.
  39. #
  40. _have_determined_configuration = False
  41. _configuration = "Release"
  42. def clear_cached_configuration():
  43. global _have_determined_configuration, _configuration
  44. _have_determined_configuration = False
  45. _configuration = "Release"
  46. class Config(object):
  47. _FLAGS_FROM_CONFIGURATIONS = {
  48. "Debug": "--debug",
  49. "Release": "--release",
  50. }
  51. def __init__(self, executive, filesystem, port_implementation=None):
  52. self._executive = executive
  53. self._filesystem = filesystem
  54. self._webkit_finder = webkit_finder.WebKitFinder(self._filesystem)
  55. self._default_configuration = None
  56. self._build_directories = {}
  57. self._port_implementation = port_implementation
  58. def build_directory(self, configuration):
  59. """Returns the path to the build directory for the configuration."""
  60. if configuration:
  61. flags = ["--configuration", self.flag_for_configuration(configuration)]
  62. else:
  63. configuration = ""
  64. flags = []
  65. if self._port_implementation:
  66. flags.append('--' + self._port_implementation)
  67. if not self._build_directories.get(configuration):
  68. args = ["perl", self._webkit_finder.path_to_script("webkit-build-directory")] + flags
  69. output = self._executive.run_command(args, cwd=self._webkit_finder.webkit_base(), return_stderr=False).rstrip()
  70. parts = output.split("\n")
  71. self._build_directories[configuration] = parts[0]
  72. if len(parts) == 2:
  73. default_configuration = parts[1][len(parts[0]):]
  74. if default_configuration.startswith("/"):
  75. default_configuration = default_configuration[1:]
  76. self._build_directories[default_configuration] = parts[1]
  77. return self._build_directories[configuration]
  78. def flag_for_configuration(self, configuration):
  79. return self._FLAGS_FROM_CONFIGURATIONS[configuration]
  80. def default_configuration(self):
  81. """Returns the default configuration for the user.
  82. Returns the value set by 'set-webkit-configuration', or "Release"
  83. if that has not been set. This mirrors the logic in webkitdirs.pm."""
  84. if not self._default_configuration:
  85. self._default_configuration = self._determine_configuration()
  86. if not self._default_configuration:
  87. self._default_configuration = 'Release'
  88. if self._default_configuration not in self._FLAGS_FROM_CONFIGURATIONS:
  89. _log.warn("Configuration \"%s\" is not a recognized value.\n" % self._default_configuration)
  90. _log.warn("Scripts may fail. See 'set-webkit-configuration --help'.")
  91. return self._default_configuration
  92. def _determine_configuration(self):
  93. # This mirrors the logic in webkitdirs.pm:determineConfiguration().
  94. #
  95. # FIXME: See the comment at the top of the file regarding unit tests
  96. # and our use of global mutable static variables.
  97. # FIXME: We should just @memoize this method and then this will only
  98. # be read once per object lifetime (which should be sufficiently fast).
  99. global _have_determined_configuration, _configuration
  100. if not _have_determined_configuration:
  101. contents = self._read_configuration()
  102. if not contents:
  103. contents = "Release"
  104. if contents == "Deployment":
  105. contents = "Release"
  106. if contents == "Development":
  107. contents = "Debug"
  108. _configuration = contents
  109. _have_determined_configuration = True
  110. return _configuration
  111. def _read_configuration(self):
  112. try:
  113. configuration_path = self._filesystem.join(self.build_directory(None), "Configuration")
  114. if not self._filesystem.exists(configuration_path):
  115. return None
  116. except:
  117. return None
  118. return self._filesystem.read_text_file(configuration_path).rstrip()