blob: 5734eb8dad80a7d2d9d0ae65bcd76196f1b09048 [file] [log] [blame]
Mike Frysingerc7f15932013-03-20 13:43:35 -04001#!/usr/bin/python
bjanakiraman6496e5f2013-02-15 04:50:58 +00002#
3# Copyright 2010 Google Inc. All Rights Reserved.
bjanakiraman6496e5f2013-02-15 04:50:58 +00004"""Script to build ChromeOS benchmarks
5
bjanakiraman229d6262013-02-15 04:56:46 +00006Inputs:
bjanakiraman6496e5f2013-02-15 04:50:58 +00007 chromeos_root
8 toolchain_root
9 board
10 [chromeos/cpu/<benchname>|chromeos/browser/[pagecycler|sunspider]|chromeos/startup]
bjanakiraman229d6262013-02-15 04:56:46 +000011
bjanakiraman6496e5f2013-02-15 04:50:58 +000012 This script assumes toolchain has already been built in toolchain_root.
13
14 chromeos/cpu/<benchname>
15 - Execute bench.py script within chroot to build benchmark
16 - Copy build results to perflab-bin
17
18 chromeos/startup
bjanakiraman229d6262013-02-15 04:56:46 +000019 - Call build_chromeos to build image.
bjanakiraman6496e5f2013-02-15 04:50:58 +000020 - Copy image to perflab-bin
bjanakiraman229d6262013-02-15 04:56:46 +000021
bjanakiraman6496e5f2013-02-15 04:50:58 +000022 chromeos/browser/*
23 - Call build_chromebrowser to build image with new browser
24 - Copy image to perflab-bin
25
26"""
27
Luis Lozanof2a3ef42015-12-15 13:49:30 -080028__author__ = 'bjanakiraman@google.com (Bhaskar Janakiraman)'
bjanakiraman6496e5f2013-02-15 04:50:58 +000029
30import optparse
31import os
32import sys
33import re
kbaclawski20082a02013-02-16 02:12:57 +000034
bjanakiraman6496e5f2013-02-15 04:50:58 +000035import build_chromeos
kbaclawski20082a02013-02-16 02:12:57 +000036import tc_enter_chroot
bjanakiraman6496e5f2013-02-15 04:50:58 +000037from utils import command_executer
38from utils import logger
bjanakiraman6496e5f2013-02-15 04:50:58 +000039
bjanakiraman6496e5f2013-02-15 04:50:58 +000040KNOWN_BENCHMARKS = [
Luis Lozanof2a3ef42015-12-15 13:49:30 -080041 'chromeos/startup', 'chromeos/browser/pagecycler',
42 'chromeos/browser/sunspider', 'chromeos/browser/v8bench',
43 'chromeos/cpu/bikjmp'
44]
bjanakiraman6496e5f2013-02-15 04:50:58 +000045
bjanakiraman229d6262013-02-15 04:56:46 +000046# Commands to build CPU benchmarks.
bjanakiramanaabd2d12013-02-15 04:55:30 +000047
Luis Lozanof2a3ef42015-12-15 13:49:30 -080048CPU_BUILDCMD_CLEAN = 'cd /usr/local/toolchain_root/third_party/android_bench/v2_0/CLOSED_SOURCE/%s;\
49python ../../scripts/bench.py --toolchain=/usr/bin --action=clean;'
bjanakiramanaabd2d12013-02-15 04:55:30 +000050
Luis Lozanof2a3ef42015-12-15 13:49:30 -080051CPU_BUILDCMD_BUILD = 'cd /usr/local/toolchain_root/third_party/android_bench/v2_0/CLOSED_SOURCE/%s;\
52python ../../scripts/bench.py --toolchain=/usr/bin --add_cflags=%s --add_ldflags=%s --makeopts=%s --action=build'
bjanakiraman6496e5f2013-02-15 04:50:58 +000053
bjanakiramanaabd2d12013-02-15 04:55:30 +000054# Common initializations
55cmd_executer = command_executer.GetCommandExecuter()
56
bjanakiraman6496e5f2013-02-15 04:50:58 +000057
58def Usage(parser, message):
Luis Lozanof2a3ef42015-12-15 13:49:30 -080059 print 'ERROR: ' + message
bjanakiraman6496e5f2013-02-15 04:50:58 +000060 parser.print_help()
61 sys.exit(0)
62
63
bjanakiraman229d6262013-02-15 04:56:46 +000064def CreateRunsh(destdir, benchmark):
Luis Lozanof2a3ef42015-12-15 13:49:30 -080065 """Create run.sh script to run benchmark.
66
67 Perflab needs a run.sh that runs the benchmark.
68 """
69 run_cmd = os.path.dirname(os.path.abspath(__file__)) + '/run_benchmarks.py'
70 contents = '#!/bin/sh\n%s $@ %s\n' % (run_cmd, benchmark)
bjanakiraman229d6262013-02-15 04:56:46 +000071 runshfile = destdir + '/run.sh'
72 f = open(runshfile, 'w')
73 f.write(contents)
74 f.close()
asharifef75f392013-02-15 10:22:49 +000075 retval = cmd_executer.RunCommand('chmod +x %s' % runshfile)
76 return retval
bjanakiraman229d6262013-02-15 04:56:46 +000077
78
Luis Lozanof2a3ef42015-12-15 13:49:30 -080079def CreateBinaryCopy(sourcedir, destdir, copy=None):
shenhan12089d42013-02-15 20:32:18 +000080 """Create links in perflab-bin/destdir/* to sourcedir/* for now, instead of copies
Luis Lozanof2a3ef42015-12-15 13:49:30 -080081
shenhan12089d42013-02-15 20:32:18 +000082 Args:
83 copy: when none, make soft links to everything under sourcedir, otherwise
84 copy all to destdir.
85 TODO: remove this parameter if it's determined that CopyFiles can use
86 rsync -L.
87 """
bjanakiramane7a379a2013-02-15 10:24:30 +000088 retval = 0
bjanakiramanaabd2d12013-02-15 04:55:30 +000089 # check if sourcedir exists
90 if not os.path.exists(sourcedir):
Luis Lozanof2a3ef42015-12-15 13:49:30 -080091 logger.GetLogger().LogError('benchmark results %s does not exist.' %
kbaclawski6999ada2013-02-15 19:57:09 +000092 sourcedir)
bjanakiraman229d6262013-02-15 04:56:46 +000093 return 1
bjanakiramanaabd2d12013-02-15 04:55:30 +000094
95 # Deal with old copies - save off old ones for now.
bjanakiraman229d6262013-02-15 04:56:46 +000096 # Note - if its a link, it doesn't save anything.
bjanakiramanaabd2d12013-02-15 04:55:30 +000097 if os.path.exists(destdir):
bjanakiraman229d6262013-02-15 04:56:46 +000098 command = 'rm -rf %s.old' % destdir
bjanakiramane7a379a2013-02-15 10:24:30 +000099 retval = cmd_executer.RunCommand(command)
100 if retval != 0:
101 return retval
bjanakiramanaabd2d12013-02-15 04:55:30 +0000102 command = 'mv %s %s.old' % (destdir, destdir)
bjanakiramane7a379a2013-02-15 10:24:30 +0000103 retval = cmd_executer.RunCommand(command)
104 if retval != 0:
105 return retval
bjanakiraman229d6262013-02-15 04:56:46 +0000106 os.makedirs(destdir)
bjanakiramanaabd2d12013-02-15 04:55:30 +0000107 sourcedir = os.path.abspath(sourcedir)
shenhan12089d42013-02-15 20:32:18 +0000108 if copy is None:
109 command = 'ln -s %s/* %s' % (sourcedir, destdir)
110 else:
111 command = 'cp -fr %s/* %s' % (sourcedir, destdir)
asharifef75f392013-02-15 10:22:49 +0000112 retval = cmd_executer.RunCommand(command)
113 return retval
bjanakiramanaabd2d12013-02-15 04:55:30 +0000114
115
bjanakiraman6496e5f2013-02-15 04:50:58 +0000116def Main(argv):
117 """Build ChromeOS."""
118 # Common initializations
119
120 parser = optparse.OptionParser()
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800121 parser.add_option('-c',
122 '--chromeos_root',
123 dest='chromeos_root',
124 help='Target directory for ChromeOS installation.')
125 parser.add_option('-t',
126 '--toolchain_root',
127 dest='toolchain_root',
128 help='This is obsolete. Do not use.')
129 parser.add_option('-r',
130 '--third_party',
131 dest='third_party',
132 help='The third_party dir containing android benchmarks.')
133 parser.add_option('-C',
134 '--clean',
135 dest='clean',
136 action='store_true',
137 default=False,
138 help='Clean up build.'),
139 parser.add_option('-B',
140 '--build',
141 dest='build',
142 action='store_true',
143 default=False,
144 help='Build benchmark.'),
145 parser.add_option('-O',
146 '--only_copy',
147 dest='only_copy',
148 action='store_true',
149 default=False,
150 help='Only copy to perflab-bin - no builds.'),
151 parser.add_option('--workdir',
152 dest='workdir',
153 default='.',
154 help='Work directory for perflab outputs.')
155 parser.add_option('--clobber_chroot',
156 dest='clobber_chroot',
157 action='store_true',
158 help='Delete the chroot and start fresh',
159 default=False)
160 parser.add_option('--clobber_board',
161 dest='clobber_board',
162 action='store_true',
163 help='Delete the board and start fresh',
164 default=False)
165 parser.add_option('--cflags',
166 dest='cflags',
167 default='',
168 help='CFLAGS for the ChromeOS packages')
169 parser.add_option('--cxxflags',
170 dest='cxxflags',
171 default='',
172 help='CXXFLAGS for the ChromeOS packages')
173 parser.add_option('--ldflags',
174 dest='ldflags',
175 default='',
176 help='LDFLAGS for the ChromeOS packages')
177 parser.add_option('--makeopts',
178 dest='makeopts',
179 default='',
180 help='Make options for the ChromeOS packages')
181 parser.add_option('--board',
182 dest='board',
183 help='ChromeOS target board, e.g. x86-generic')
bjanakiraman6496e5f2013-02-15 04:50:58 +0000184
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800185 (options, args) = parser.parse_args(argv[1:])
bjanakiraman6496e5f2013-02-15 04:50:58 +0000186
187 # validate args
188 for arg in args:
189 if arg not in KNOWN_BENCHMARKS:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800190 logger.GetLogger().LogFatal('Bad benchmark %s specified' % arg)
bjanakiraman229d6262013-02-15 04:56:46 +0000191
bjanakiraman6496e5f2013-02-15 04:50:58 +0000192 if options.chromeos_root is None:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800193 Usage(parser, '--chromeos_root must be set')
bjanakiraman6496e5f2013-02-15 04:50:58 +0000194
bjanakiraman6496e5f2013-02-15 04:50:58 +0000195 if options.board is None:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800196 Usage(parser, '--board must be set')
bjanakiraman6496e5f2013-02-15 04:50:58 +0000197
asharif8697d4e2013-02-15 09:18:09 +0000198 if options.toolchain_root:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800199 logger.GetLogger().LogWarning('--toolchain_root should not be set')
asharif8697d4e2013-02-15 09:18:09 +0000200
201 options.chromeos_root = os.path.expanduser(options.chromeos_root)
202 options.workdir = os.path.expanduser(options.workdir)
203
bjanakiramanaabd2d12013-02-15 04:55:30 +0000204 retval = 0
asharif2198c512013-02-15 09:21:35 +0000205 if options.third_party:
206 third_party = options.third_party
207 else:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800208 third_party = '%s/../../../third_party' % os.path.dirname(__file__)
asharif2198c512013-02-15 09:21:35 +0000209 third_party = os.path.realpath(third_party)
bjanakiraman6496e5f2013-02-15 04:50:58 +0000210 for arg in args:
211 # CPU benchmarks
212 if re.match('chromeos/cpu', arg):
213 comps = re.split('/', arg)
bjanakiramanaabd2d12013-02-15 04:55:30 +0000214 benchname = comps[2]
asharif8697d4e2013-02-15 09:18:09 +0000215
216 tec_options = []
asharif2198c512013-02-15 09:21:35 +0000217 if third_party:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800218 tec_options.append('--third_party=%s' % third_party)
bjanakiramanaabd2d12013-02-15 04:55:30 +0000219 if options.clean:
asharifca3c6c12013-02-15 23:17:54 +0000220 retval = cmd_executer.ChrootRunCommand(options.chromeos_root,
221 CPU_BUILDCMD_CLEAN % benchname,
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800222 tec_options=tec_options)
kbaclawski6999ada2013-02-15 19:57:09 +0000223 logger.GetLogger().LogErrorIf(retval,
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800224 'clean of benchmark %s failed.' % arg)
bjanakiramanaabd2d12013-02-15 04:55:30 +0000225 if options.build:
asharifca3c6c12013-02-15 23:17:54 +0000226 retval = cmd_executer.ChrootRunCommand(
227 options.chromeos_root,
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800228 CPU_BUILDCMD_BUILD % (benchname, options.cflags, options.ldflags,
229 options.makeopts),
asharifca3c6c12013-02-15 23:17:54 +0000230 tec_options=tec_options)
kbaclawski6999ada2013-02-15 19:57:09 +0000231 logger.GetLogger().LogErrorIf(retval,
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800232 'Build of benchmark %s failed.' % arg)
bjanakiramanaabd2d12013-02-15 04:55:30 +0000233 if retval == 0 and (options.build or options.only_copy):
asharif2198c512013-02-15 09:21:35 +0000234 benchdir = ('%s/android_bench/v2_0/CLOSED_SOURCE/%s' %
235 (third_party, benchname))
bjanakiraman229d6262013-02-15 04:56:46 +0000236 linkdir = '%s/perflab-bin/%s' % (options.workdir, arg)
asharif2198c512013-02-15 09:21:35 +0000237
shenhan12089d42013-02-15 20:32:18 +0000238 # For cpu/*, we need to copy (not symlinks) of all the contents,
239 # because they are part of the test fixutre.
240 retval = CreateBinaryCopy(benchdir, linkdir, True)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800241 if retval != 0:
242 return retval
asharif2198c512013-02-15 09:21:35 +0000243 retval = CreateRunsh(linkdir, arg)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800244 if retval != 0:
245 return retval
bjanakiraman6496e5f2013-02-15 04:50:58 +0000246 elif re.match('chromeos/startup', arg):
bjanakiramanaabd2d12013-02-15 04:55:30 +0000247 if options.build:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800248 # Clean for chromeos/browser and chromeos/startup is a Nop since builds are always from scratch.
249 build_args = [
250 os.path.dirname(os.path.abspath(__file__)) + '/build_chromeos.py',
251 '--chromeos_root=' + options.chromeos_root,
252 '--board=' + options.board, '--cflags=' + options.cflags,
253 '--cxxflags=' + options.cxxflags, '--ldflags=' + options.ldflags,
254 '--clobber_board'
255 ]
bjanakiramanaabd2d12013-02-15 04:55:30 +0000256 retval = build_chromeos.Main(build_args)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800257 logger.GetLogger().LogErrorIf(retval, 'Build of ChromeOS failed.')
bjanakiramanaabd2d12013-02-15 04:55:30 +0000258 if retval == 0 and (options.build or options.only_copy):
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800259 benchdir = '%s/src/build/images/%s/latest' % (options.chromeos_root,
260 options.board)
bjanakiraman4e8bdf22013-02-15 04:57:09 +0000261 linkdir = '%s/perflab-bin/%s' % (options.workdir, arg)
asharif2198c512013-02-15 09:21:35 +0000262 retval = CreateBinaryCopy(benchdir, linkdir)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800263 if retval != 0:
264 return retval
bjanakiraman229d6262013-02-15 04:56:46 +0000265 CreateRunsh(linkdir, arg)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800266 if retval != 0:
267 return retval
bjanakiraman6496e5f2013-02-15 04:50:58 +0000268 elif re.match('chromeos/browser', arg):
bjanakiramanaabd2d12013-02-15 04:55:30 +0000269 if options.build:
bjanakiraman229d6262013-02-15 04:56:46 +0000270 # For now, re-build os. TBD: Change to call build_browser
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800271 build_args = [os.path.dirname(os.path.abspath(__file__)) +
272 '/build_chrome_browser.py',
273 '--chromeos_root=' + options.chromeos_root,
274 '--board=' + options.board, '--cflags=' + options.cflags,
275 '--cxxflags=' + options.cxxflags,
276 '--ldflags=' + options.ldflags]
bjanakiramanaabd2d12013-02-15 04:55:30 +0000277 retval = build_chromeos.Main(build_args)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800278 logger.GetLogger().LogErrorIf(retval, 'Build of ChromeOS failed.')
bjanakiramanaabd2d12013-02-15 04:55:30 +0000279 if retval == 0 and (options.build or options.only_copy):
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800280 benchdir = '%s/src/build/images/%s/latest' % (options.chromeos_root,
281 options.board)
bjanakiraman4e8bdf22013-02-15 04:57:09 +0000282 linkdir = '%s/perflab-bin/%s' % (options.workdir, arg)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800283 retval = CreateBinaryCopy(benchdir, linkdir)
284 if retval != 0:
285 return retval
asharif2198c512013-02-15 09:21:35 +0000286 retval = CreateRunsh(linkdir, arg)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800287 if retval != 0:
288 return retval
bjanakiraman6496e5f2013-02-15 04:50:58 +0000289
asharif2198c512013-02-15 09:21:35 +0000290 return 0
bjanakiraman6496e5f2013-02-15 04:50:58 +0000291
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800292
293if __name__ == '__main__':
kbaclawski5fec5502013-02-15 19:55:56 +0000294 sys.exit(Main(sys.argv))