1# Copyright (c) 2015 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import os
6import shutil
7
8from autotest_lib.client.common_lib import error
9from autotest_lib.server import utils
10
11from native_Benchmarks_common import *
12
13class v8(object):
14    """Build and copy the v8 engine to client."""
15
16    def __init__(self, scratch_srv, scratch_cli, client, flags_additional):
17        self.src = "%s/v8" % scratch_srv
18
19        # unpack
20        cmd = 'tar jxf %s/v8.tar.bz2 -C %s' % (SERVER_TEST_ROOT, scratch_srv)
21        run_check(utils, cmd, 'Error occurred while unpacking v8')
22
23        # build
24        arch = client.get_arch()
25        flags = {}
26        def_flag(flags, 'LDFLAGS', '-static')
27        options = '-C %s i18nsupport=off snapshot=off -j40' % self.src
28        if arch == 'armv7l':
29            def_flag(flags, 'CXX', 'armv7a-cros-linux-gnueabi-g++')
30            def_flag(flags, 'LINK', 'armv7a-cros-linux-gnueabi-g++')
31            options += ' arm.release'
32            d8src = '%s/out/arm.release/d8' % self.src
33        elif arch == 'x86_64':
34            def_flag(flags, 'CXX', 'x86_64-cros-linux-gnu-g++')
35            def_flag(flags, 'LINK', 'x86_64-cros-linux-gnu-g++')
36            options += ' x64.release'
37            d8src = '%s/out/x64.release/d8' % self.src
38        else:
39            raise error.TestFail('Unknown cpu architecture: %s' % arch)
40        for f, v in flags_additional.iteritems():
41            def_flag(flags, f, v)
42        envs = ' '.join('%s=%s' % (k, v) for k, v in flags.iteritems())
43        cmd = '%s make %s' % (envs, options)
44
45        run_check(utils, cmd, 'Error occurred building v8')
46        if not os.path.isfile(d8src):
47            raise error.TestFail('Unknown error when building v8')
48
49        # copy
50        d8dst = '%s/d8' % scratch_cli
51        rcp_check(client, d8src, d8dst,
52                  'Error occurred while sending d8 to client.\n')
53        self.executable = d8dst
54
55    def __del__(self):
56        if os.path.isdir(self.src):
57            shutil.rmtree(self.src)
58