blob: c3b7887071c6d7cddfd7aeb397dbe428215aeffe [file] [log] [blame]
Caroline Ticef6ef4392017-04-06 17:16:05 -07001#!/usr/bin/env python2
raymes49fd5a32013-02-15 04:55:27 +00002#
3# Copyright 2010 Google Inc. All Rights Reserved.
raymes49fd5a32013-02-15 04:55:27 +00004"""Script to checkout the ChromeOS source.
5
6This script sets up the ChromeOS source in the given directory, matching a
7particular release of ChromeOS.
8"""
9
Caroline Tice88272d42016-01-13 09:48:29 -080010from __future__ import print_function
11
Luis Lozanof2a3ef42015-12-15 13:49:30 -080012__author__ = 'raymes@google.com (Raymes Khoury)'
raymes49fd5a32013-02-15 04:55:27 +000013
Caroline Tice88272d42016-01-13 09:48:29 -080014import argparse
asharif8697d4e2013-02-15 09:18:09 +000015import os
raymes49fd5a32013-02-15 04:55:27 +000016import sys
kbaclawski20082a02013-02-16 02:12:57 +000017
Caroline Tice88272d42016-01-13 09:48:29 -080018from cros_utils import command_executer
19from cros_utils import logger
20from cros_utils import misc
raymes49fd5a32013-02-15 04:55:27 +000021
22
23def Usage(parser, message):
Caroline Tice88272d42016-01-13 09:48:29 -080024 print('ERROR: %s' % message)
raymes49fd5a32013-02-15 04:55:27 +000025 parser.print_help()
26 sys.exit(0)
27
28
asharif8697d4e2013-02-15 09:18:09 +000029def Main(argv):
raymes49fd5a32013-02-15 04:55:27 +000030 """Build Chrome browser."""
Caroline Tice88272d42016-01-13 09:48:29 -080031
raymes49fd5a32013-02-15 04:55:27 +000032 cmd_executer = command_executer.GetCommandExecuter()
33
Caroline Tice88272d42016-01-13 09:48:29 -080034 parser = argparse.ArgumentParser()
Caroline Ticef6ef4392017-04-06 17:16:05 -070035 parser.add_argument(
36 '--chromeos_root',
37 dest='chromeos_root',
38 help='Target directory for ChromeOS installation.')
Caroline Tice88272d42016-01-13 09:48:29 -080039 parser.add_argument('--version', dest='version')
Caroline Ticef6ef4392017-04-06 17:16:05 -070040 parser.add_argument(
41 '--clean',
42 dest='clean',
43 default=False,
44 action='store_true',
45 help=('Clean the /var/cache/chromeos-chrome/'
46 'chrome-src/src/out_$board dir'))
47 parser.add_argument(
48 '--env', dest='env', default='', help='Use the following env')
49 parser.add_argument(
50 '--ebuild_version',
51 dest='ebuild_version',
52 help='Use this ebuild instead of the default one.')
53 parser.add_argument(
54 '--cflags',
55 dest='cflags',
56 default='',
57 help='CFLAGS for the ChromeOS packages')
58 parser.add_argument(
59 '--cxxflags',
60 dest='cxxflags',
61 default='',
62 help='CXXFLAGS for the ChromeOS packages')
63 parser.add_argument(
64 '--ldflags',
65 dest='ldflags',
66 default='',
67 help='LDFLAGS for the ChromeOS packages')
68 parser.add_argument(
69 '--board', dest='board', help='ChromeOS target board, e.g. x86-generic')
70 parser.add_argument(
71 '--no_build_image',
72 dest='no_build_image',
73 default=False,
74 action='store_true',
75 help=('Skip build image after building browser.'
76 'Defaults to False.'))
77 parser.add_argument(
78 '--label',
79 dest='label',
80 help='Optional label to apply to the ChromeOS image.')
81 parser.add_argument(
82 '--build_image_args',
83 default='',
84 dest='build_image_args',
85 help='Optional arguments to build_image.')
86 parser.add_argument(
87 '--cros_workon',
88 dest='cros_workon',
89 help='Build using external source tree.')
90 parser.add_argument(
91 '--dev',
92 dest='dev',
93 default=False,
94 action='store_true',
95 help=('Build a dev (eg. writable/large) image. '
96 'Defaults to False.'))
97 parser.add_argument(
98 '--debug',
99 dest='debug',
100 default=False,
101 action='store_true',
102 help=('Build chrome browser using debug mode. '
103 'This option implies --dev. Defaults to false.'))
104 parser.add_argument(
105 '--verbose',
106 dest='verbose',
107 default=False,
108 action='store_true',
109 help='Build with verbose information.')
raymes49fd5a32013-02-15 04:55:27 +0000110
Caroline Tice88272d42016-01-13 09:48:29 -0800111 options = parser.parse_args(argv)
raymes49fd5a32013-02-15 04:55:27 +0000112
113 if options.chromeos_root is None:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800114 Usage(parser, '--chromeos_root must be set')
raymes49fd5a32013-02-15 04:55:27 +0000115
116 if options.board is None:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800117 Usage(parser, '--board must be set')
raymes49fd5a32013-02-15 04:55:27 +0000118
raymes49fd5a32013-02-15 04:55:27 +0000119 if options.version is None:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800120 logger.GetLogger().LogOutput('No Chrome version given so '
121 'using the default checked in version.')
122 chrome_version = ''
raymes49fd5a32013-02-15 04:55:27 +0000123 else:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800124 chrome_version = 'CHROME_VERSION=%s' % options.version
raymes49fd5a32013-02-15 04:55:27 +0000125
shenhan52e96222013-02-19 22:45:41 +0000126 if options.dev and options.no_build_image:
127 logger.GetLogger().LogOutput(
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800128 "\"--dev\" is meaningless if \"--no_build_image\" is given.")
shenhan52e96222013-02-19 22:45:41 +0000129
130 if options.debug:
131 options.dev = True
132
asharif52ff3072013-02-16 02:13:00 +0000133 options.chromeos_root = misc.CanonicalizePath(options.chromeos_root)
134
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800135 unmask_env = 'ACCEPT_KEYWORDS=~*'
asharif52ff3072013-02-16 02:13:00 +0000136 if options.ebuild_version:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800137 ebuild_version = '=%s' % options.ebuild_version
138 options.env = '%s %s' % (options.env, unmask_env)
asharif52ff3072013-02-16 02:13:00 +0000139 else:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800140 ebuild_version = 'chromeos-chrome'
asharif52ff3072013-02-16 02:13:00 +0000141
shenhan9213b832013-02-19 20:42:47 +0000142 if options.cros_workon and not (
Caroline Ticef6ef4392017-04-06 17:16:05 -0700143 os.path.isdir(options.cros_workon) and os.path.exists(
144 os.path.join(options.cros_workon, 'src/chromeos/BUILD.gn'))):
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800145 Usage(parser, '--cros_workon must be a valid chromium browser checkout.')
shenhan9213b832013-02-19 20:42:47 +0000146
shenhan52e96222013-02-19 22:45:41 +0000147 if options.verbose:
148 options.env = misc.MergeEnvStringWithDict(
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800149 options.env, {'USE': 'chrome_internal verbose'})
shenhan52e96222013-02-19 22:45:41 +0000150 else:
151 options.env = misc.MergeEnvStringWithDict(options.env,
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800152 {'USE': 'chrome_internal'})
shenhan52e96222013-02-19 22:45:41 +0000153 if options.debug:
154 options.env = misc.MergeEnvStringWithDict(options.env,
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800155 {'BUILDTYPE': 'Debug'})
shenhan52e96222013-02-19 22:45:41 +0000156
ashariffc4c06f2013-02-19 19:43:10 +0000157 if options.clean:
llozano3a428922013-02-19 21:36:47 +0000158 misc.RemoveChromeBrowserObjectFiles(options.chromeos_root, options.board)
ashariffc4c06f2013-02-19 19:43:10 +0000159
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800160 chrome_origin = 'SERVER_SOURCE'
shenhan9213b832013-02-19 20:42:47 +0000161 if options.cros_workon:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800162 chrome_origin = 'LOCAL_SOURCE'
shenhan9213b832013-02-19 20:42:47 +0000163 command = 'cros_workon --board={0} start chromeos-chrome'.format(
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800164 options.board)
Luis Lozano036c9232015-12-10 10:47:01 -0800165 ret = cmd_executer.ChrootRunCommandWOutput(options.chromeos_root, command)
shenhan9213b832013-02-19 20:42:47 +0000166
167 # cros_workon start returns non-zero if chromeos-chrome is already a
168 # cros_workon package.
169 if ret[0] and ret[2].find(
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800170 'WARNING : Already working on chromeos-base/chromeos-chrome') == -1:
171 logger.GetLogger().LogFatal('cros_workon chromeos-chrome failed.')
shenhan9213b832013-02-19 20:42:47 +0000172
173 # Return value is non-zero means we do find the "Already working on..."
174 # message, keep the information, so later on we do not revert the
175 # cros_workon status.
176 cros_workon_keep = (ret[0] != 0)
177
raymes49fd5a32013-02-15 04:55:27 +0000178 # Emerge the browser
Caroline Tice88272d42016-01-13 09:48:29 -0800179 emerge_browser_command = ('CHROME_ORIGIN={0} {1} '
180 'CFLAGS="$(portageq-{2} envvar CFLAGS) {3}" '
181 'LDFLAGS="$(portageq-{2} envvar LDFLAGS) {4}" '
182 'CXXFLAGS="$(portageq-{2} envvar CXXFLAGS) {5}" '
183 '{6} emerge-{2} --buildpkg {7}').format(
184 chrome_origin, chrome_version, options.board,
185 options.cflags, options.ldflags,
186 options.cxxflags, options.env, ebuild_version)
shenhan9213b832013-02-19 20:42:47 +0000187
188 cros_sdk_options = ''
189 if options.cros_workon:
shenhan52e96222013-02-19 22:45:41 +0000190 cros_sdk_options = '--chrome_root={0}'.format(options.cros_workon)
shenhan9213b832013-02-19 20:42:47 +0000191
Caroline Ticef6ef4392017-04-06 17:16:05 -0700192 ret = cmd_executer.ChrootRunCommand(
193 options.chromeos_root,
194 emerge_browser_command,
195 cros_sdk_options=cros_sdk_options)
raymes49fd5a32013-02-15 04:55:27 +0000196
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800197 logger.GetLogger().LogFatalIf(ret, 'build_packages failed')
raymes49fd5a32013-02-15 04:55:27 +0000198
shenhan9213b832013-02-19 20:42:47 +0000199 if options.cros_workon and not cros_workon_keep:
200 command = 'cros_workon --board={0} stop chromeos-chrome'.format(
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800201 options.board)
shenhan9213b832013-02-19 20:42:47 +0000202 ret = cmd_executer.ChrootRunCommand(options.chromeos_root, command)
203 # cros_workon failed, not a fatal one, just report it.
204 if ret:
Caroline Tice88272d42016-01-13 09:48:29 -0800205 print('cros_workon stop chromeos-chrome failed.')
shenhan9213b832013-02-19 20:42:47 +0000206
shenhan52e96222013-02-19 22:45:41 +0000207 if options.no_build_image:
208 return ret
209
210 # Finally build the image
Caroline Ticef6ef4392017-04-06 17:16:05 -0700211 ret = cmd_executer.ChrootRunCommand(options.chromeos_root,
212 '{0} {1} {2} {3}'.format(
213 unmask_env, options.env,
214 misc.GetBuildImageCommand(
215 options.board, dev=options.dev),
216 options.build_image_args))
raymes49fd5a32013-02-15 04:55:27 +0000217
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800218 logger.GetLogger().LogFatalIf(ret, 'build_image failed')
raymes49fd5a32013-02-15 04:55:27 +0000219
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800220 flags_file_name = 'chrome_flags.txt'
221 flags_file_path = '{0}/src/build/images/{1}/latest/{2}'.format(
222 options.chromeos_root, options.board, flags_file_name)
223 flags_file = open(flags_file_path, 'wb')
224 flags_file.write('CFLAGS={0}\n'.format(options.cflags))
225 flags_file.write('CXXFLAGS={0}\n'.format(options.cxxflags))
226 flags_file.write('LDFLAGS={0}\n'.format(options.ldflags))
asharif8697d4e2013-02-15 09:18:09 +0000227 flags_file.close()
228
asharif8697d4e2013-02-15 09:18:09 +0000229 if options.label:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800230 image_dir_path = '{0}/src/build/images/{1}/latest'.format(
231 options.chromeos_root, options.board)
asharif8697d4e2013-02-15 09:18:09 +0000232 real_image_dir_path = os.path.realpath(image_dir_path)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800233 command = 'ln -sf -T {0} {1}/{2}'.format(
Caroline Tice88272d42016-01-13 09:48:29 -0800234 os.path.basename(real_image_dir_path),\
shenhan52e96222013-02-19 22:45:41 +0000235 os.path.dirname(real_image_dir_path),\
236 options.label)
asharif8697d4e2013-02-15 09:18:09 +0000237
238 ret = cmd_executer.RunCommand(command)
Caroline Ticef6ef4392017-04-06 17:16:05 -0700239 logger.GetLogger().LogFatalIf(
240 ret, 'Failed to apply symlink label %s' % options.label)
asharif8697d4e2013-02-15 09:18:09 +0000241
242 return ret
243
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800244
245if __name__ == '__main__':
Caroline Tice88272d42016-01-13 09:48:29 -0800246 retval = Main(sys.argv[1:])
asharif2198c512013-02-15 09:21:35 +0000247 sys.exit(retval)