factory.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. """Factory method to retrieve the appropriate port implementation."""
  29. import fnmatch
  30. import optparse
  31. import re
  32. from webkitpy.port import builders
  33. def platform_options(use_globs=False):
  34. return [
  35. optparse.make_option('--platform', action='store',
  36. help=('Glob-style list of platform/ports to use (e.g., "mac*")' if use_globs else 'Platform to use (e.g., "mac-lion")')),
  37. optparse.make_option('--efl', action='store_const', dest='platform',
  38. const=('efl*' if use_globs else 'efl'),
  39. help=('Alias for --platform=efl*' if use_globs else 'Alias for --platform=efl')),
  40. optparse.make_option('--gtk', action='store_const', dest='platform',
  41. const=('gtk*' if use_globs else 'gtk'),
  42. help=('Alias for --platform=gtk*' if use_globs else 'Alias for --platform=gtk')),
  43. optparse.make_option('--qt', action='store_const', dest="platform",
  44. const=('qt*' if use_globs else 'qt'),
  45. help=('Alias for --platform=qt' if use_globs else 'Alias for --platform=qt')),
  46. optparse.make_option('--orbis', action='store_const', dest='platform',
  47. const=('orbis*' if use_globs else 'orbis'),
  48. help=('Alias for --platform=orbis' if use_globs else 'Alias for --platform=orbis'))
  49. ]
  50. def configuration_options():
  51. return [
  52. optparse.make_option("-t", "--target", dest="configuration", help="(DEPRECATED)"),
  53. # FIXME: --help should display which configuration is default.
  54. optparse.make_option('--debug', action='store_const', const='Debug', dest="configuration",
  55. help='Set the configuration to Debug'),
  56. optparse.make_option('--release', action='store_const', const='Release', dest="configuration",
  57. help='Set the configuration to Release'),
  58. optparse.make_option('--32-bit', action='store_const', const='x86', default=None, dest="architecture",
  59. help='use 32-bit binaries by default (x86 instead of x86_64)'),
  60. ]
  61. def _builder_options(builder_name):
  62. configuration = "Debug" if re.search(r"[d|D](ebu|b)g", builder_name) else "Release"
  63. is_webkit2 = builder_name.find("WK2") != -1
  64. builder_name = builder_name
  65. return optparse.Values({'builder_name': builder_name, 'configuration': configuration, 'webkit_test_runner': is_webkit2})
  66. class PortFactory(object):
  67. PORT_CLASSES = (
  68. 'efl.EflPort',
  69. 'gtk.GtkPort',
  70. 'mac.MacPort',
  71. 'mock_drt.MockDRTPort',
  72. 'qt.QtPort',
  73. 'test.TestPort',
  74. 'win.WinPort',
  75. 'orbis.OrbisPort'
  76. )
  77. def __init__(self, host):
  78. self._host = host
  79. def _default_port(self, options):
  80. platform = self._host.platform
  81. if platform.is_linux() or platform.is_freebsd():
  82. return 'qt-linux'
  83. elif platform.is_mac():
  84. return 'mac'
  85. elif platform.is_win():
  86. return 'win'
  87. raise NotImplementedError('unknown platform: %s' % platform)
  88. def get(self, port_name=None, options=None, **kwargs):
  89. """Returns an object implementing the Port interface. If
  90. port_name is None, this routine attempts to guess at the most
  91. appropriate port on this platform."""
  92. port_name = port_name or self._default_port(options)
  93. for port_class in self.PORT_CLASSES:
  94. module_name, class_name = port_class.rsplit('.', 1)
  95. module = __import__(module_name, globals(), locals(), [], -1)
  96. cls = module.__dict__[class_name]
  97. if port_name.startswith(cls.port_name):
  98. port_name = cls.determine_full_port_name(self._host, options, port_name)
  99. return cls(self._host, port_name, options=options, **kwargs)
  100. raise NotImplementedError('unsupported platform: "%s"' % port_name)
  101. def all_port_names(self, platform=None):
  102. """Return a list of all valid, fully-specified, "real" port names.
  103. This is the list of directories that are used as actual baseline_paths()
  104. by real ports. This does not include any "fake" names like "test"
  105. or "mock-mac", and it does not include any directories that are not.
  106. If platform is not specified, we will glob-match all ports"""
  107. platform = platform or '*'
  108. return fnmatch.filter(builders.all_port_names(), platform)
  109. def get_from_builder_name(self, builder_name):
  110. port_name = builders.port_name_for_builder_name(builder_name)
  111. assert port_name, "unrecognized builder name '%s'" % builder_name
  112. return self.get(port_name, _builder_options(builder_name))