summaryrefslogtreecommitdiff
path: root/buildbot/buildbot_run.py
blob: 10460b3ea5f410ff21e5decdcf36b57144f4400f (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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#!/usr/bin/env python
# Copyright (c) 2012 Google Inc. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.


"""Argument-less script to select what to run on the buildbots."""


import filecmp
import os
import shutil
import subprocess
import sys


if sys.platform in ['win32', 'cygwin']:
  EXE_SUFFIX = '.exe'
else:
  EXE_SUFFIX = ''


BUILDBOT_DIR = os.path.dirname(os.path.abspath(__file__))
TRUNK_DIR = os.path.dirname(BUILDBOT_DIR)
ROOT_DIR = os.path.dirname(TRUNK_DIR)
ANDROID_DIR = os.path.join(ROOT_DIR, 'android')
CMAKE_DIR = os.path.join(ROOT_DIR, 'cmake')
CMAKE_BIN_DIR = os.path.join(CMAKE_DIR, 'bin')
OUT_DIR = os.path.join(TRUNK_DIR, 'out')


def CallSubProcess(*args, **kwargs):
  """Wrapper around subprocess.call which treats errors as build exceptions."""
  with open(os.devnull) as devnull_fd:
    retcode = subprocess.call(stdin=devnull_fd, *args, **kwargs)
  if retcode != 0:
    print '@@@STEP_EXCEPTION@@@'
    sys.exit(1)


def PrepareCmake():
  """Build CMake 2.8.8 since the version in Precise is 2.8.7."""
  if os.environ['BUILDBOT_CLOBBER'] == '1':
    print '@@@BUILD_STEP Clobber CMake checkout@@@'
    shutil.rmtree(CMAKE_DIR)

  # We always build CMake 2.8.8, so no need to do anything
  # if the directory already exists.
  if os.path.isdir(CMAKE_DIR):
    return

  print '@@@BUILD_STEP Initialize CMake checkout@@@'
  os.mkdir(CMAKE_DIR)

  print '@@@BUILD_STEP Sync CMake@@@'
  CallSubProcess(
      ['git', 'clone',
       '--depth', '1',
       '--single-branch',
       '--branch', 'v2.8.8',
       '--',
       'git://cmake.org/cmake.git',
       CMAKE_DIR],
      cwd=CMAKE_DIR)

  print '@@@BUILD_STEP Build CMake@@@'
  CallSubProcess(
      ['/bin/bash', 'bootstrap', '--prefix=%s' % CMAKE_DIR],
      cwd=CMAKE_DIR)

  CallSubProcess( ['make', 'cmake'], cwd=CMAKE_DIR)


_ANDROID_SETUP = 'source build/envsetup.sh && lunch full-eng'


def PrepareAndroidTree():
  """Prepare an Android tree to run 'android' format tests."""
  if os.environ['BUILDBOT_CLOBBER'] == '1':
    print '@@@BUILD_STEP Clobber Android checkout@@@'
    shutil.rmtree(ANDROID_DIR)

  # (Re)create the directory so that the following steps will succeed.
  if not os.path.isdir(ANDROID_DIR):
    os.mkdir(ANDROID_DIR)

  # We use a manifest from the gyp project listing pinned revisions of AOSP to
  # use, to ensure that we test against a stable target. This needs to be
  # updated to pick up new build system changes sometimes, so we must test if
  # it has changed.
  manifest_filename = 'aosp_manifest.xml'
  gyp_manifest = os.path.join(BUILDBOT_DIR, manifest_filename)
  android_manifest = os.path.join(ANDROID_DIR, '.repo', 'manifests',
                                  manifest_filename)
  manifest_is_current = (os.path.isfile(android_manifest) and
                         filecmp.cmp(gyp_manifest, android_manifest))
  if not manifest_is_current:
    # It's safe to repeat these steps, so just do them again to make sure we are
    # in a good state.
    print '@@@BUILD_STEP Initialize Android checkout@@@'
    CallSubProcess(
        ['repo', 'init',
         '-u', 'https://android.googlesource.com/platform/manifest',
         '-b', 'master',
         '-g', 'all,-notdefault,-device,-darwin,-mips,-x86'],
        cwd=ANDROID_DIR)
    shutil.copy(gyp_manifest, android_manifest)

    print '@@@BUILD_STEP Sync Android@@@'
    CallSubProcess(['repo', 'sync', '-j4', '-m', manifest_filename],
                   cwd=ANDROID_DIR)

  # If we already built the system image successfully and didn't sync to a new
  # version of the source, skip running the build again as it's expensive even
  # when there's nothing to do.
  system_img = os.path.join(ANDROID_DIR, 'out', 'target', 'product', 'generic',
                            'system.img')
  if manifest_is_current and os.path.isfile(system_img):
    return

  print '@@@BUILD_STEP Build Android@@@'
  CallSubProcess(
      ['/bin/bash',
       '-c', '%s && make -j4' % _ANDROID_SETUP],
      cwd=ANDROID_DIR)


def StartAndroidEmulator():
  """Start an android emulator from the built android tree."""
  print '@@@BUILD_STEP Start Android emulator@@@'

  CallSubProcess(['/bin/bash', '-c',
      '%s && adb kill-server ' % _ANDROID_SETUP],
      cwd=ANDROID_DIR)

  # If taskset is available, use it to force adbd to run only on one core, as,
  # sadly, it improves its reliability (see crbug.com/268450).
  adbd_wrapper = ''
  with open(os.devnull, 'w') as devnull_fd:
    if subprocess.call(['which', 'taskset'], stdout=devnull_fd) == 0:
      adbd_wrapper = 'taskset -c 0'
  CallSubProcess(['/bin/bash', '-c',
      '%s && %s adb start-server ' % (_ANDROID_SETUP, adbd_wrapper)],
      cwd=ANDROID_DIR)

  subprocess.Popen(
      ['/bin/bash', '-c',
       '%s && emulator -no-window' % _ANDROID_SETUP],
      cwd=ANDROID_DIR)
  CallSubProcess(
      ['/bin/bash', '-c',
       '%s && adb wait-for-device' % _ANDROID_SETUP],
      cwd=ANDROID_DIR)


def StopAndroidEmulator():
  """Stop all android emulators."""
  print '@@@BUILD_STEP Stop Android emulator@@@'
  # If this fails, it's because there is no emulator running.
  subprocess.call(['pkill', 'emulator.*'])


def GypTestFormat(title, format=None, msvs_version=None, tests=[]):
  """Run the gyp tests for a given format, emitting annotator tags.

  See annotator docs at:
    https://sites.google.com/a/chromium.org/dev/developers/testing/chromium-build-infrastructure/buildbot-annotations
  Args:
    format: gyp format to test.
  Returns:
    0 for sucesss, 1 for failure.
  """
  if not format:
    format = title

  print '@@@BUILD_STEP ' + title + '@@@'
  sys.stdout.flush()
  env = os.environ.copy()
  if msvs_version:
    env['GYP_MSVS_VERSION'] = msvs_version
  command = ' '.join(
      [sys.executable, 'trunk/gyptest.py',
       '--all',
       '--passed',
       '--format', format,
       '--path', CMAKE_BIN_DIR,
       '--chdir', 'trunk'] + tests)
  if format == 'android':
    # gyptest needs the environment setup from envsetup/lunch in order to build
    # using the 'android' backend, so this is done in a single shell.
    retcode = subprocess.call(
        ['/bin/bash',
         '-c', '%s && cd %s && %s' % (_ANDROID_SETUP, ROOT_DIR, command)],
        cwd=ANDROID_DIR, env=env)
  else:
    retcode = subprocess.call(command, cwd=ROOT_DIR, env=env, shell=True)
  if retcode:
    # Emit failure tag, and keep going.
    print '@@@STEP_FAILURE@@@'
    return 1
  return 0


def GypBuild():
  # Dump out/ directory.
  print '@@@BUILD_STEP cleanup@@@'
  print 'Removing %s...' % OUT_DIR
  shutil.rmtree(OUT_DIR, ignore_errors=True)
  print 'Done.'

  retcode = 0
  # The Android gyp bot runs on linux so this must be tested first.
  if os.environ['BUILDBOT_BUILDERNAME'] == 'gyp-android':
    PrepareAndroidTree()
    StartAndroidEmulator()
    try:
      retcode += GypTestFormat('android')
    finally:
      StopAndroidEmulator()
  elif sys.platform.startswith('linux'):
    retcode += GypTestFormat('ninja')
    retcode += GypTestFormat('make')
    PrepareCmake()
    retcode += GypTestFormat('cmake')
  elif sys.platform == 'darwin':
    retcode += GypTestFormat('ninja')
    retcode += GypTestFormat('xcode')
    retcode += GypTestFormat('make')
  elif sys.platform == 'win32':
    retcode += GypTestFormat('ninja')
    if os.environ['BUILDBOT_BUILDERNAME'] == 'gyp-win64':
      retcode += GypTestFormat('msvs-ninja-2013', format='msvs-ninja',
                               msvs_version='2013',
                               tests=[
                                   'test\generator-output\gyptest-actions.py',
                                   'test\generator-output\gyptest-relocate.py',
                                   'test\generator-output\gyptest-rules.py'])
      retcode += GypTestFormat('msvs-2013', format='msvs', msvs_version='2013')
  else:
    raise Exception('Unknown platform')
  if retcode:
    # TODO(bradnelson): once the annotator supports a postscript (section for
    #     after the build proper that could be used for cumulative failures),
    #     use that instead of this. This isolates the final return value so
    #     that it isn't misattributed to the last stage.
    print '@@@BUILD_STEP failures@@@'
    sys.exit(retcode)


if __name__ == '__main__':
  GypBuild()