1#!/usr/bin/env python
2# Copyright 2013 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6import os
7import sys
8
9import bb_utils
10import bb_annotations
11
12sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
13from pylib import constants
14
15
16SLAVE_SCRIPTS_DIR = os.path.join(bb_utils.BB_BUILD_DIR, 'scripts', 'slave')
17VALID_HOST_TESTS = set(['check_webview_licenses', 'findbugs'])
18
19DIR_BUILD_ROOT = os.path.dirname(constants.DIR_SOURCE_ROOT)
20
21# Short hand for RunCmd which is used extensively in this file.
22RunCmd = bb_utils.RunCmd
23
24
25def SrcPath(*path):
26  return os.path.join(constants.DIR_SOURCE_ROOT, *path)
27
28
29def CheckWebViewLicenses(_):
30  bb_annotations.PrintNamedStep('check_licenses')
31  RunCmd([SrcPath('android_webview', 'tools', 'webview_licenses.py'), 'scan'],
32         warning_code=1)
33
34
35def RunHooks(build_type):
36  RunCmd([SrcPath('build', 'landmines.py')])
37  build_path = SrcPath('out', build_type)
38  landmine_path = os.path.join(build_path, '.landmines_triggered')
39  clobber_env = os.environ.get('BUILDBOT_CLOBBER')
40  if clobber_env or os.path.isfile(landmine_path):
41    bb_annotations.PrintNamedStep('Clobber')
42    if not clobber_env:
43      print 'Clobbering due to triggered landmines:'
44      with open(landmine_path) as f:
45        print f.read()
46    RunCmd(['rm', '-rf', build_path])
47
48  bb_annotations.PrintNamedStep('runhooks')
49  RunCmd(['gclient', 'runhooks'], halt_on_failure=True)
50
51
52def Compile(options):
53  RunHooks(options.target)
54  cmd = [os.path.join(SLAVE_SCRIPTS_DIR, 'compile.py'),
55         '--build-tool=ninja',
56         '--compiler=goma',
57         '--target=%s' % options.target,
58         '--goma-dir=%s' % bb_utils.GOMA_DIR]
59  bb_annotations.PrintNamedStep('compile')
60  if options.build_targets:
61    build_targets = options.build_targets.split(',')
62    cmd += ['--build-args', ' '.join(build_targets)]
63  RunCmd(cmd, halt_on_failure=True, cwd=DIR_BUILD_ROOT)
64
65
66def ZipBuild(options):
67  bb_annotations.PrintNamedStep('zip_build')
68  RunCmd([
69      os.path.join(SLAVE_SCRIPTS_DIR, 'zip_build.py'),
70      '--src-dir', constants.DIR_SOURCE_ROOT,
71      '--exclude-files', 'lib.target,gen,android_webview,jingle_unittests']
72      + bb_utils.EncodeProperties(options), cwd=DIR_BUILD_ROOT)
73
74
75def ExtractBuild(options):
76  bb_annotations.PrintNamedStep('extract_build')
77  RunCmd([os.path.join(SLAVE_SCRIPTS_DIR, 'extract_build.py')]
78         + bb_utils.EncodeProperties(options), cwd=DIR_BUILD_ROOT)
79
80
81def FindBugs(options):
82  bb_annotations.PrintNamedStep('findbugs')
83  build_type = []
84  if options.target == 'Release':
85    build_type = ['--release-build']
86  RunCmd([SrcPath('build', 'android', 'findbugs_diff.py')] + build_type)
87  RunCmd([SrcPath(
88      'tools', 'android', 'findbugs_plugin', 'test',
89      'run_findbugs_plugin_tests.py')] + build_type)
90
91
92def BisectPerfRegression(options):
93  args = []
94  if options.extra_src:
95    args = ['--extra_src', options.extra_src]
96  RunCmd([SrcPath('tools', 'prepare-bisect-perf-regression.py'),
97          '-w', os.path.join(constants.DIR_SOURCE_ROOT, os.pardir)])
98  RunCmd([SrcPath('tools', 'run-bisect-perf-regression.py'),
99          '-w', os.path.join(constants.DIR_SOURCE_ROOT, os.pardir)] + args)
100
101
102def GetHostStepCmds():
103  return [
104      ('compile', Compile),
105      ('extract_build', ExtractBuild),
106      ('check_webview_licenses', CheckWebViewLicenses),
107      ('bisect_perf_regression', BisectPerfRegression),
108      ('findbugs', FindBugs),
109      ('zip_build', ZipBuild)
110  ]
111
112
113def GetHostStepsOptParser():
114  parser = bb_utils.GetParser()
115  parser.add_option('--steps', help='Comma separated list of host tests.')
116  parser.add_option('--build-targets', default='',
117                    help='Comma separated list of build targets.')
118  parser.add_option('--experimental', action='store_true',
119                    help='Indicate whether to compile experimental targets.')
120  parser.add_option('--extra_src', default='',
121                    help='Path to extra source file. If this is supplied, '
122                    'bisect script will use it to override default behavior.')
123
124  return parser
125
126
127def main(argv):
128  parser = GetHostStepsOptParser()
129  options, args = parser.parse_args(argv[1:])
130  if args:
131    return sys.exit('Unused args %s' % args)
132
133  setattr(options, 'target', options.factory_properties.get('target', 'Debug'))
134  setattr(options, 'extra_src',
135          options.factory_properties.get('extra_src', ''))
136
137  if options.steps:
138    bb_utils.RunSteps(options.steps.split(','), GetHostStepCmds(), options)
139
140
141if __name__ == '__main__':
142  sys.exit(main(sys.argv))
143