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    BUILD_REQUIREMENTS_URL = 'https://code.google.com/p/chromium/wiki/LinuxBuildInstructions'
54
55    @classmethod
56    def _determine_driver_path_statically(cls, host, options):
57        config_object = config.Config(host.executive, host.filesystem)
58        build_directory = getattr(options, 'build_directory', None)
59        finder = WebKitFinder(host.filesystem)
60        webkit_base = finder.webkit_base()
61        chromium_base = finder.chromium_base()
62        driver_name = getattr(options, 'driver_name', None)
63        if driver_name is None:
64            driver_name = cls.CONTENT_SHELL_NAME
65        if hasattr(options, 'configuration') and options.configuration:
66            configuration = options.configuration
67        else:
68            configuration = config_object.default_configuration()
69        return cls._static_build_path(host.filesystem, build_directory, chromium_base, configuration, [driver_name])
70
71    @staticmethod
72    def _determine_architecture(filesystem, executive, driver_path):
73        file_output = ''
74        if filesystem.isfile(driver_path):
75            # The --dereference flag tells file to follow symlinks
76            file_output = executive.run_command(['file', '--brief', '--dereference', driver_path], return_stderr=True)
77
78        if re.match(r'ELF 32-bit LSB\s+executable', file_output):
79            return 'x86'
80        if re.match(r'ELF 64-bit LSB\s+executable', file_output):
81            return 'x86_64'
82        if file_output:
83            _log.warning('Could not determine architecture from "file" output: %s' % file_output)
84
85        # We don't know what the architecture is; default to 'x86' because
86        # maybe we're rebaselining and the binary doesn't actually exist,
87        # or something else weird is going on. It's okay to do this because
88        # if we actually try to use the binary, check_build() should fail.
89        return 'x86_64'
90
91    @classmethod
92    def determine_full_port_name(cls, host, options, port_name):
93        if port_name.endswith('linux'):
94            return port_name + '-' + cls._determine_architecture(host.filesystem, host.executive, cls._determine_driver_path_statically(host, options))
95        return port_name
96
97    def __init__(self, host, port_name, **kwargs):
98        super(LinuxPort, self).__init__(host, port_name, **kwargs)
99        (base, arch) = port_name.rsplit('-', 1)
100        assert base == 'linux'
101        assert arch in self.SUPPORTED_VERSIONS
102        assert port_name in ('linux', 'linux-x86', 'linux-x86_64')
103        self._version = 'lucid'  # We only support lucid right now.
104        self._architecture = arch
105        if not self.get_option('disable_breakpad'):
106            self._dump_reader = DumpReaderLinux(host, self._build_path())
107
108    def additional_drt_flag(self):
109        flags = super(LinuxPort, self).additional_drt_flag()
110        if not self.get_option('disable_breakpad'):
111            flags += ['--enable-crash-reporter', '--crash-dumps-dir=%s' % self._dump_reader.crash_dumps_directory()]
112        return flags
113
114    def default_baseline_search_path(self):
115        port_names = self.FALLBACK_PATHS[self._architecture]
116        return map(self._webkit_baseline_path, port_names)
117
118    def _modules_to_search_for_symbols(self):
119        return [self._build_path('libffmpegsumo.so')]
120
121    def check_build(self, needs_http, printer):
122        result = super(LinuxPort, self).check_build(needs_http, printer)
123
124        if result:
125            _log.error('For complete Linux build requirements, please see:')
126            _log.error('')
127            _log.error('    http://code.google.com/p/chromium/wiki/LinuxBuildInstructions')
128        return result
129
130    def look_for_new_crash_logs(self, crashed_processes, start_time):
131        if self.get_option('disable_breakpad'):
132            return None
133        return self._dump_reader.look_for_new_crash_logs(crashed_processes, start_time)
134
135    def clobber_old_port_specific_results(self):
136        if not self.get_option('disable_breakpad'):
137            self._dump_reader.clobber_old_results()
138
139    def operating_system(self):
140        return 'linux'
141
142    #
143    # PROTECTED METHODS
144    #
145
146    def _check_apache_install(self):
147        result = self._check_file_exists(self.path_to_apache(), "apache2")
148        result = self._check_file_exists(self.path_to_apache_config_file(), "apache2 config file") and result
149        if not result:
150            _log.error('    Please install using: "sudo apt-get install apache2 libapache2-mod-php5"')
151            _log.error('')
152        return result
153
154    def _wdiff_missing_message(self):
155        return 'wdiff is not installed; please install using "sudo apt-get install wdiff"'
156
157    def path_to_apache(self):
158        # The Apache binary path can vary depending on OS and distribution
159        # See http://wiki.apache.org/httpd/DistrosDefaultLayout
160        for path in ["/usr/sbin/httpd", "/usr/sbin/apache2"]:
161            if self._filesystem.exists(path):
162                return path
163        _log.error("Could not find apache. Not installed or unknown path.")
164        return None
165
166    def _path_to_driver(self, configuration=None):
167        binary_name = self.driver_name()
168        return self._build_path_with_configuration(configuration, binary_name)
169
170    def _path_to_helper(self):
171        return None
172