aboutsummaryrefslogtreecommitdiffhomepage
path: root/infra/bots/recipe_modules/flavor/gn_chromebook_flavor.py
blob: c004d9971e4ee4e9eb6726d0c97bb926022b28c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# Copyright 2016 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.


from recipe_engine import recipe_api

import default_flavor
import gn_flavor
import json
import subprocess


"""
  GN Chromebook flavor utils, used for building and testing Skia for ARM
  Chromebooks with GN
"""
class GNChromebookFlavorUtils(gn_flavor.GNFlavorUtils):

  def __init__(self, m):
    super(GNChromebookFlavorUtils, self).__init__(m)
    self._user_ip = ''

    self.device_dirs = default_flavor.DeviceDirs(
      dm_dir        = self.m.vars.chromeos_homedir + 'dm_out',
      perf_data_dir = self.m.vars.chromeos_homedir + 'perf',
      resource_dir  = self.m.vars.chromeos_homedir + 'resources',
      images_dir    = self.m.vars.chromeos_homedir + 'images',
      skp_dir       = self.m.vars.chromeos_homedir + 'skps',
      svg_dir       = self.m.vars.chromeos_homedir + 'svgs',
      tmp_dir       = self.m.vars.chromeos_homedir)

    self._bin_dir = self.m.vars.chromeos_homedir + 'bin'

  @property
  def user_ip(self):
    if not self._user_ip:
      ssh_info = self.m.run(self.m.python.inline, 'read chromeos ip',
                            program="""
      import os
      SSH_MACHINE_FILE = os.path.expanduser('~/ssh_machine.json')
      with open(SSH_MACHINE_FILE, 'r') as f:
        print f.read()
      """,
      stdout=self.m.raw_io.output(),
      infra_step=True).stdout

      self._user_ip = json.loads(ssh_info).get(u'user_ip', 'ERROR')
    return self._user_ip

  def _ssh(self, title, *cmd, **kwargs):
    if 'infra_step' not in kwargs:
      kwargs['infra_step'] = True

    ssh_cmd = ['ssh', '-oConnectTimeout=15', '-oBatchMode=yes',
               '-t', '-t', self.user_ip] + list(cmd)

    return self._run(title, ssh_cmd, **kwargs)

  def install(self):
    self._ssh('mkdir %s' % self.device_dirs.resource_dir, 'mkdir', '-p',
              self.device_dirs.resource_dir)

    # Ensure the home dir is marked executable
    self._ssh('remount %s as exec' % self.m.vars.chromeos_homedir,
              'sudo', 'mount', '-i', '-o', 'remount,exec', '/home/chronos')

    self.create_clean_device_dir(self._bin_dir)

  def compile(self, unused_target):
    configuration = self.m.vars.builder_cfg.get('configuration')
    os            = self.m.vars.builder_cfg.get('os')
    target_arch   = self.m.vars.builder_cfg.get('target_arch')

    clang_linux = self.m.vars.slave_dir.join('clang_linux')
    # This is a pretty typical arm-linux-gnueabihf sysroot
    sysroot_dir = self.m.vars.slave_dir.join('armhf_sysroot')
    # This is the extra things needed to link against for the chromebook.
    #  For example, the Mali GL drivers.
    gl_dir   = self.m.vars.slave_dir.join('chromebook_arm_gles')

    extra_asmflags = [
      '--target=armv7a-linux-gnueabihf',
      '--sysroot=%s' % sysroot_dir,
      '-march=armv7-a',
      '-mfpu=neon',
      '-mthumb',
    ]

    extra_cflags = [
      '--target=armv7a-linux-gnueabihf',
      '--sysroot=%s' % sysroot_dir,
      '-I%s' % gl_dir.join('include'),
      '-I%s' % sysroot_dir.join('include'),
      '-I%s' % sysroot_dir.join('include', 'c++', '4.8.4'),
      '-I%s' % sysroot_dir.join('include', 'c++', '4.8.4',
                                'arm-linux-gnueabihf'),
      '-DMESA_EGL_NO_X11_HEADERS',
    ]

    extra_ldflags = [
      '--target=armv7a-linux-gnueabihf',
      '--sysroot=%s' % sysroot_dir,
      # use sysroot's ld which can properly link things.
      '-B%s' % sysroot_dir.join('bin'),
      # helps locate crt*.o
      '-B%s' % sysroot_dir.join('gcc-cross'),
      # helps locate libgcc*.so
      '-L%s' % sysroot_dir.join('gcc-cross'),
      '-L%s' % sysroot_dir.join('lib'),
      '-L%s' % gl_dir.join('lib'),
      # Explicitly do not use lld for cross compiling like this - I observed
      # failures like "Unrecognized reloc 41" and couldn't find out why.
    ]

    quote = lambda x: '"%s"' % x
    args = {
      'cc': quote(clang_linux.join('bin','clang')),
      'cxx': quote(clang_linux.join('bin','clang++')),
      'target_cpu': quote(target_arch),
      'skia_use_fontconfig': 'false',
      'skia_use_system_freetype2': 'false',
      'skia_use_egl': 'true',
    }

    if configuration != 'Debug':
      args['is_debug'] = 'false'
    args['extra_asmflags'] = repr(extra_asmflags).replace("'", '"')
    args['extra_cflags'] = repr(extra_cflags).replace("'", '"')
    args['extra_ldflags'] = repr(extra_ldflags).replace("'", '"')

    gn_args = ' '.join('%s=%s' % (k,v) for (k,v) in sorted(args.iteritems()))

    gn    = 'gn.exe'    if 'Win' in os else 'gn'
    ninja = 'ninja.exe' if 'Win' in os else 'ninja'
    gn = self.m.vars.skia_dir.join('bin', gn)

    context = {
      'cwd': self.m.vars.skia_dir,
      'env': {
          'LD_LIBRARY_PATH': sysroot_dir.join('lib'),
      }
    }
    with self.m.step.context(context):
      self._py('fetch-gn', self.m.vars.skia_dir.join('bin', 'fetch-gn'))
      self._run('gn gen', [gn, 'gen', self.out_dir, '--args=' + gn_args])
      self._run('ninja', [ninja, '-C', self.out_dir, 'nanobench', 'dm'])

  def create_clean_device_dir(self, path):
    # use -f to silently return if path doesn't exist
    self._ssh('rm %s' % path, 'rm', '-rf', path)
    self._ssh('mkdir %s' % path, 'mkdir', '-p', path)

  def read_file_on_device(self, path, **kwargs):
    rv = self._ssh('read %s' % path,
                   'cat', path, stdout=self.m.raw_io.output(),
                   **kwargs)
    return rv.stdout.rstrip() if rv and rv.stdout else None

  def remove_file_on_device(self, path):
    # use -f to silently return if path doesn't exist
    self._ssh('rm %s' % path, 'rm', '-f', path)

  def _prefix_device_path(self, device_path):
    return '%s:%s' % (self.user_ip, device_path)

  def copy_file_to_device(self, host_path, device_path):
    device_path = self._prefix_device_path(device_path)
    # Recipe
    self.m.python.inline(str('scp %s %s' % (host_path, device_path)),
    """
    import subprocess
    import sys
    host = sys.argv[1]
    device   = sys.argv[2]
    print subprocess.check_output(['scp', host, device])
    """, args=[host_path, device_path], infra_step=True)

  def _copy_dir(self, src, dest):
    # We can't use rsync to communicate with the chromebooks because the
    # chromebooks don't have rsync installed on them.
    self.m.python.inline(str('scp -r %s %s' % (src, dest)),
    """
    import subprocess
    import sys
    src = sys.argv[1] + '/*'
    dest   = sys.argv[2]
    print subprocess.check_output('scp -r %s %s' % (src, dest), shell=True)
    """, args=[src, dest], infra_step=True)

  def copy_directory_contents_to_device(self, host_path, device_path):
    self._copy_dir(host_path, self._prefix_device_path(device_path))

  def copy_directory_contents_to_host(self, device_path, host_path):
    self._copy_dir(self._prefix_device_path(device_path), host_path)

  def step(self, name, cmd, **kwargs):
    # Push and run either dm or nanobench

    name = cmd[0]
    app = self.m.vars.skia_out.join(self.m.vars.configuration, cmd[0])

    cmd[0] = '%s/%s' % (self._bin_dir, cmd[0])
    self.copy_file_to_device(app, cmd[0])

    self._ssh('chmod %s' % name, 'chmod', '+x', cmd[0])
    self._ssh(str(name), *cmd)