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
29import logging
30import re
31
32from webkitpy.common.webkit_finder import WebKitFinder
33from webkitpy.layout_tests.breakpad.dump_reader_multipart import DumpReaderLinux
34from webkitpy.layout_tests.models import test_run_results
35from webkitpy.layout_tests.port import base
36from webkitpy.layout_tests.port import win
37from webkitpy.layout_tests.port import config
38
39
40_log = logging.getLogger(__name__)
41
42
43class LinuxPort(base.Port):
44    port_name = 'linux'
45
46    SUPPORTED_VERSIONS = ('x86', 'x86_64')
47
48    FALLBACK_PATHS = { 'x86_64': [ 'linux' ] + win.WinPort.latest_platform_fallback_path() }
49    FALLBACK_PATHS['x86'] = ['linux-x86'] + FALLBACK_PATHS['x86_64']
50
51    DEFAULT_BUILD_DIRECTORIES = ('out',)
52
53    @classmethod
54    def _determine_driver_path_statically(cls, host, options):
55        config_object = config.Config(host.executive, host.filesystem)
56        build_directory = getattr(options, 'build_directory', None)
57        finder = WebKitFinder(host.filesystem)
58        webkit_base = finder.webkit_base()
59        chromium_base = finder.chromium_base()
60        driver_name = getattr(options, 'driver_name', None)
61        if driver_name is None:
62            driver_name = cls.CONTENT_SHELL_NAME
63        if hasattr(options, 'configuration') and options.configuration:
64            configuration = options.configuration
65        else:
66            configuration = config_object.default_configuration()
67        return cls._static_build_path(host.filesystem, build_directory, chromium_base, configuration, [driver_name])
68
69    @staticmethod
70    def _determine_architecture(filesystem, executive, driver_path):
71        file_output = ''
72        if filesystem.isfile(driver_path):
73            # The --dereference flag tells file to follow symlinks
74            file_output = executive.run_command(['file', '--brief', '--dereference', driver_path], return_stderr=True)
75
76        if re.match(r'ELF 32-bit LSB\s+executable', file_output):
77            return 'x86'
78        if re.match(r'ELF 64-bit LSB\s+executable', file_output):
79            return 'x86_64'
80        if file_output:
81            _log.warning('Could not determine architecture from "file" output: %s' % file_output)
82
83        # We don't know what the architecture is; default to 'x86' because
84        # maybe we're rebaselining and the binary doesn't actually exist,
85        # or something else weird is going on. It's okay to do this because
86        # if we actually try to use the binary, check_build() should fail.
87        return 'x86_64'
88
89    @classmethod
90    def determine_full_port_name(cls, host, options, port_name):
91        if port_name.endswith('linux'):
92            return port_name + '-' + cls._determine_architecture(host.filesystem, host.executive, cls._determine_driver_path_statically(host, options))
93        return port_name
94
95    def __init__(self, host, port_name, **kwargs):
96        super(LinuxPort, self).__init__(host, port_name, **kwargs)
97        (base, arch) = port_name.rsplit('-', 1)
98        assert base == 'linux'
99        assert arch in self.SUPPORTED_VERSIONS
100        assert port_name in ('linux', 'linux-x86', 'linux-x86_64')
101        self._version = 'lucid'  # We only support lucid right now.
102        self._architecture = arch
103        if not self.get_option('disable_breakpad'):
104            self._dump_reader = DumpReaderLinux(host, self._build_path())
105
106    def additional_drt_flag(self):
107        flags = super(LinuxPort, self).additional_drt_flag()
108        if not self.get_option('disable_breakpad'):
109            flags += ['--enable-crash-reporter', '--crash-dumps-dir=%s' % self._dump_reader.crash_dumps_directory()]
110        return flags
111
112    def default_baseline_search_path(self):
113        port_names = self.FALLBACK_PATHS[self._architecture]
114        return map(self._webkit_baseline_path, port_names)
115
116    def _modules_to_search_for_symbols(self):
117        return [self._build_path('libffmpegsumo.so')]
118
119    def check_build(self, needs_http, printer):
120        result = super(LinuxPort, self).check_build(needs_http, printer)
121
122        if result:
123            _log.error('For complete Linux build requirements, please see:')
124            _log.error('')
125            _log.error('    http://code.google.com/p/chromium/wiki/LinuxBuildInstructions')
126        return result
127
128    def look_for_new_crash_logs(self, crashed_processes, start_time):
129        if self.get_option('disable_breakpad'):
130            return None
131        return self._dump_reader.look_for_new_crash_logs(crashed_processes, start_time)
132
133    def clobber_old_port_specific_results(self):
134        if not self.get_option('disable_breakpad'):
135            self._dump_reader.clobber_old_results()
136
137    def operating_system(self):
138        return 'linux'
139
140    def virtual_test_suites(self):
141        result = super(LinuxPort, self).virtual_test_suites()
142        result.extend([
143            base.VirtualTestSuite('linux-subpixel',
144                                  'platform/linux/fast/text/subpixel',
145                                  ['--enable-webkit-text-subpixel-positioning']),
146        ])
147        return result
148
149    #
150    # PROTECTED METHODS
151    #
152
153    def _check_apache_install(self):
154        result = self._check_file_exists(self._path_to_apache(), "apache2")
155        result = self._check_file_exists(self._path_to_apache_config_file(), "apache2 config file") and result
156        if not result:
157            _log.error('    Please install using: "sudo apt-get install apache2 libapache2-mod-php5"')
158            _log.error('')
159        return result
160
161    def _check_lighttpd_install(self):
162        result = self._check_file_exists(
163            self._path_to_lighttpd(), "LigHTTPd executable")
164        result = self._check_file_exists(self._path_to_lighttpd_php(), "PHP CGI executable") and result
165        result = self._check_file_exists(self._path_to_lighttpd_modules(), "LigHTTPd modules") and result
166        if not result:
167            _log.error('    Please install using: "sudo apt-get install lighttpd php5-cgi"')
168            _log.error('')
169        return result
170
171    def _wdiff_missing_message(self):
172        return 'wdiff is not installed; please install using "sudo apt-get install wdiff"'
173
174    def _path_to_apache(self):
175        # The Apache binary path can vary depending on OS and distribution
176        # See http://wiki.apache.org/httpd/DistrosDefaultLayout
177        for path in ["/usr/sbin/httpd", "/usr/sbin/apache2"]:
178            if self._filesystem.exists(path):
179                return path
180        _log.error("Could not find apache. Not installed or unknown path.")
181        return None
182
183    def _path_to_lighttpd(self):
184        return "/usr/sbin/lighttpd"
185
186    def _path_to_lighttpd_modules(self):
187        return "/usr/lib/lighttpd"
188
189    def _path_to_lighttpd_php(self):
190        return "/usr/bin/php-cgi"
191
192    def _path_to_driver(self, configuration=None):
193        binary_name = self.driver_name()
194        return self._build_path_with_configuration(configuration, binary_name)
195
196    def _path_to_helper(self):
197        return None
198