blob: 0b0676d1a26ac974e072cc781e247b0fba032301 [file] [log] [blame]
Caroline Ticef6ef4392017-04-06 17:16:05 -07001#!/usr/bin/env python2
raymes5154d7f2013-02-15 04:35:37 +00002#
3# Copyright 2010 Google Inc. All Rights Reserved.
raymes5154d7f2013-02-15 04:35:37 +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__ = ('asharif@google.com (Ahmad Sharif) '
13 'llozano@google.com (Luis Lozano) '
14 'raymes@google.com (Raymes Khoury) '
15 'shenhan@google.com (Han Shen)')
raymes5154d7f2013-02-15 04:35:37 +000016
Caroline Tice88272d42016-01-13 09:48:29 -080017import argparse
raymes5154d7f2013-02-15 04:35:37 +000018import os
19import sys
kbaclawski20082a02013-02-16 02:12:57 +000020
Caroline Tice88272d42016-01-13 09:48:29 -080021from cros_utils import command_executer
22from cros_utils import logger
23from cros_utils import misc
raymes5154d7f2013-02-15 04:35:37 +000024
asharife3668f12013-02-15 04:46:29 +000025
raymes5154d7f2013-02-15 04:35:37 +000026def Usage(parser, message):
Caroline Tice88272d42016-01-13 09:48:29 -080027 print('ERROR: %s' % message)
raymes5154d7f2013-02-15 04:35:37 +000028 parser.print_help()
29 sys.exit(0)
30
asharife3668f12013-02-15 04:46:29 +000031
bjanakiraman6496e5f2013-02-15 04:50:58 +000032def Main(argv):
raymes5154d7f2013-02-15 04:35:37 +000033 """Build ChromeOS."""
34 # Common initializations
asharif5a9bb462013-02-15 04:50:57 +000035 cmd_executer = command_executer.GetCommandExecuter()
raymes5154d7f2013-02-15 04:35:37 +000036
Caroline Tice88272d42016-01-13 09:48:29 -080037 parser = argparse.ArgumentParser()
Caroline Ticef6ef4392017-04-06 17:16:05 -070038 parser.add_argument(
39 '--chromeos_root',
40 dest='chromeos_root',
41 help='Target directory for ChromeOS installation.')
42 parser.add_argument(
43 '--clobber_chroot',
44 dest='clobber_chroot',
45 action='store_true',
46 help='Delete the chroot and start fresh',
47 default=False)
48 parser.add_argument(
49 '--clobber_board',
50 dest='clobber_board',
51 action='store_true',
52 help='Delete the board and start fresh',
53 default=False)
54 parser.add_argument(
55 '--rebuild',
56 dest='rebuild',
57 action='store_true',
58 help='Rebuild all board packages except the toolchain.',
59 default=False)
60 parser.add_argument(
61 '--cflags',
62 dest='cflags',
63 default='',
64 help='CFLAGS for the ChromeOS packages')
65 parser.add_argument(
66 '--cxxflags',
67 dest='cxxflags',
68 default='',
69 help='CXXFLAGS for the ChromeOS packages')
70 parser.add_argument(
71 '--ldflags',
72 dest='ldflags',
73 default='',
74 help='LDFLAGS for the ChromeOS packages')
75 parser.add_argument(
76 '--board', dest='board', help='ChromeOS target board, e.g. x86-generic')
77 parser.add_argument(
78 '--package', dest='package', help='The package needs to be built')
79 parser.add_argument(
80 '--label',
81 dest='label',
82 help='Optional label symlink to point to build dir.')
83 parser.add_argument(
84 '--dev',
85 dest='dev',
86 default=False,
87 action='store_true',
88 help=('Make the final image in dev mode (eg writable, '
89 'more space on image). Defaults to False.'))
90 parser.add_argument(
91 '--debug',
92 dest='debug',
93 default=False,
94 action='store_true',
95 help=("Optional. Build chrome browser with \"-g -O0\". "
96 "Notice, this also turns on \'--dev\'. "
97 'Defaults to False.'))
98 parser.add_argument(
99 '--env', dest='env', default='', help='Env to pass to build_packages.')
100 parser.add_argument(
101 '--vanilla',
102 dest='vanilla',
103 default=False,
104 action='store_true',
105 help='Use default ChromeOS toolchain.')
106 parser.add_argument(
107 '--vanilla_image',
108 dest='vanilla_image',
109 default=False,
110 action='store_true',
111 help=('Use prebuild packages for building the image. '
112 'It also implies the --vanilla option is set.'))
raymes5154d7f2013-02-15 04:35:37 +0000113
Caroline Tice88272d42016-01-13 09:48:29 -0800114 options = parser.parse_args(argv[1:])
raymes5154d7f2013-02-15 04:35:37 +0000115
116 if options.chromeos_root is None:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800117 Usage(parser, '--chromeos_root must be set')
Luis Lozano09b027f2015-03-30 13:29:49 -0700118 options.chromeos_root = os.path.expanduser(options.chromeos_root)
119 scripts_dir = os.path.join(options.chromeos_root, 'src', 'scripts')
120 if not os.path.isdir(scripts_dir):
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800121 Usage(parser, '--chromeos_root must be set up first. Use setup_chromeos.py')
raymes5154d7f2013-02-15 04:35:37 +0000122
raymes5154d7f2013-02-15 04:35:37 +0000123 if options.board is None:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800124 Usage(parser, '--board must be set')
raymes5154d7f2013-02-15 04:35:37 +0000125
shenhan48738582013-02-19 22:45:41 +0000126 if options.debug:
127 options.dev = True
128
asharif44473782013-02-19 19:58:15 +0000129 build_packages_env = options.env
shenhan48738582013-02-19 22:45:41 +0000130 if build_packages_env.find('EXTRA_BOARD_FLAGS=') != -1:
131 logger.GetLogger().LogFatal(
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800132 ('Passing "EXTRA_BOARD_FLAGS" in "--env" is not supported. '
133 'This flags is used internally by this script. '
134 'Contact the author for more detail.'))
shenhan48738582013-02-19 22:45:41 +0000135
asharif80b47dc2013-02-15 06:31:19 +0000136 if options.rebuild == True:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800137 build_packages_env += ' EXTRA_BOARD_FLAGS=-e'
llozano3a428922013-02-19 21:36:47 +0000138 # EXTRA_BOARD_FLAGS=-e should clean up the object files for the chrome
139 # browser but it doesn't. So do it here.
140 misc.RemoveChromeBrowserObjectFiles(options.chromeos_root, options.board)
asharif80b47dc2013-02-15 06:31:19 +0000141
Luis Lozano09b027f2015-03-30 13:29:49 -0700142 # Build with afdo_use by default.
143 # To change the default use --env="USE=-afdo_use".
144 build_packages_env = misc.MergeEnvStringWithDict(
Luis A. Lozano09ce67b2017-06-15 17:57:49 -0700145 build_packages_env, {'USE': 'chrome_internal afdo_use -cros-debug'})
asharif01e29a52013-02-15 04:56:41 +0000146
shenhan48738582013-02-19 22:45:41 +0000147 build_packages_command = misc.GetBuildPackagesCommand(
Caroline Ticef6ef4392017-04-06 17:16:05 -0700148 board=options.board, usepkg=options.vanilla_image, debug=options.debug)
yunlian5acba6e2013-02-19 22:34:37 +0000149
150 if options.package:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800151 build_packages_command += ' {0}'.format(options.package)
yunlian5acba6e2013-02-19 22:34:37 +0000152
shenhan48738582013-02-19 22:45:41 +0000153 build_image_command = misc.GetBuildImageCommand(options.board, options.dev)
asharifca8c5ef2013-02-15 04:57:02 +0000154
Yunlian Jiangd145a582013-08-19 13:59:34 -0700155 if options.vanilla or options.vanilla_image:
Caroline Ticef6ef4392017-04-06 17:16:05 -0700156 command = misc.GetSetupBoardCommand(
157 options.board,
158 usepkg=options.vanilla_image,
159 force=options.clobber_board)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800160 command += '; ' + build_packages_env + ' ' + build_packages_command
161 command += '&& ' + build_packages_env + ' ' + build_image_command
asharifca3c6c12013-02-15 23:17:54 +0000162 ret = cmd_executer.ChrootRunCommand(options.chromeos_root, command)
asharifb1752c82013-02-15 04:56:37 +0000163 return ret
164
raymes5154d7f2013-02-15 04:35:37 +0000165 # Setup board
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800166 if not os.path.isdir(options.chromeos_root + '/chroot/build/' +
167 options.board) or options.clobber_board:
raymes04164a12013-02-15 04:36:03 +0000168 # Run build_tc.py from binary package
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800169 ret = cmd_executer.ChrootRunCommand(options.chromeos_root,
170 misc.GetSetupBoardCommand(
171 options.board,
172 force=options.clobber_board))
173 logger.GetLogger().LogFatalIf(ret, 'setup_board failed')
raymes5f35b922013-02-15 04:35:57 +0000174 else:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800175 logger.GetLogger().LogOutput('Did not setup_board '
176 'because it already exists')
raymesbfb57992013-02-15 04:35:45 +0000177
shenhan48738582013-02-19 22:45:41 +0000178 if options.debug:
179 # Perform 2-step build_packages to build a debug chrome browser.
180
181 # Firstly, build everything that chromeos-chrome depends on normally.
182 if options.rebuild == True:
183 # Give warning about "--rebuild" and "--debug". Under this combination,
184 # only dependencies of "chromeos-chrome" get rebuilt.
185 logger.GetLogger().LogWarning(
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800186 "\"--rebuild\" does not correctly re-build every package when "
187 "\"--debug\" is enabled. ")
shenhan48738582013-02-19 22:45:41 +0000188
189 # Replace EXTRA_BOARD_FLAGS=-e with "-e --onlydeps"
190 build_packages_env = build_packages_env.replace(
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800191 'EXTRA_BOARD_FLAGS=-e', 'EXTRA_BOARD_FLAGS=\"-e --onlydeps\"')
shenhan48738582013-02-19 22:45:41 +0000192 else:
193 build_packages_env += ' EXTRA_BOARD_FLAGS=--onlydeps'
194
195 ret = cmd_executer.ChrootRunCommand(
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800196 options.chromeos_root, "CFLAGS=\"$(portageq-%s envvar CFLAGS) %s\" "
197 "CXXFLAGS=\"$(portageq-%s envvar CXXFLAGS) %s\" "
198 "LDFLAGS=\"$(portageq-%s envvar LDFLAGS) %s\" "
199 'CHROME_ORIGIN=SERVER_SOURCE '
200 '%s '
201 '%s --skip_chroot_upgrade'
202 'chromeos-chrome' % (options.board, options.cflags, options.board,
203 options.cxxflags, options.board, options.ldflags,
204 build_packages_env, build_packages_command))
shenhan48738582013-02-19 22:45:41 +0000205
206 logger.GetLogger().LogFatalIf(\
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800207 ret, 'build_packages failed while trying to build chromeos-chrome deps.')
shenhan48738582013-02-19 22:45:41 +0000208
209 # Secondly, build chromeos-chrome using debug mode.
210 # Replace '--onlydeps' with '--nodeps'.
211 if options.rebuild == True:
212 build_packages_env = build_packages_env.replace(
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800213 'EXTRA_BOARD_FLAGS=\"-e --onlydeps\"', 'EXTRA_BOARD_FLAGS=--nodeps')
shenhan48738582013-02-19 22:45:41 +0000214 else:
215 build_packages_env = build_packages_env.replace(
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800216 'EXTRA_BOARD_FLAGS=--onlydeps', 'EXTRA_BOARD_FLAGS=--nodeps')
shenhan48738582013-02-19 22:45:41 +0000217 ret = cmd_executer.ChrootRunCommand(
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800218 options.chromeos_root, "CFLAGS=\"$(portageq-%s envvar CFLAGS) %s\" "
219 "CXXFLAGS=\"$(portageq-%s envvar CXXFLAGS) %s\" "
220 "LDFLAGS=\"$(portageq-%s envvar LDFLAGS) %s\" "
221 'CHROME_ORIGIN=SERVER_SOURCE BUILDTYPE=Debug '
222 '%s '
223 '%s --skip_chroot_upgrade'
224 'chromeos-chrome' % (options.board, options.cflags, options.board,
225 options.cxxflags, options.board, options.ldflags,
226 build_packages_env, build_packages_command))
shenhan48738582013-02-19 22:45:41 +0000227 logger.GetLogger().LogFatalIf(
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800228 ret,
229 'build_packages failed while trying to build debug chromeos-chrome.')
shenhan48738582013-02-19 22:45:41 +0000230
231 # Now, we have built chromeos-chrome and all dependencies.
232 # Finally, remove '-e' from EXTRA_BOARD_FLAGS,
233 # otherwise, chromeos-chrome gets rebuilt.
234 build_packages_env = build_packages_env.replace(\
235 'EXTRA_BOARD_FLAGS=--nodeps', '')
236
237 # Up to now, we have a debug built chromos-chrome browser.
238 # Fall through to build the rest of the world.
239
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800240 # Build packages
asharifca3c6c12013-02-15 23:17:54 +0000241 ret = cmd_executer.ChrootRunCommand(
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800242 options.chromeos_root, "CFLAGS=\"$(portageq-%s envvar CFLAGS) %s\" "
asharifca3c6c12013-02-15 23:17:54 +0000243 "CXXFLAGS=\"$(portageq-%s envvar CXXFLAGS) %s\" "
llozano109ac9f2013-02-19 19:58:27 +0000244 "LDFLAGS=\"$(portageq-%s envvar LDFLAGS) %s\" "
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800245 'CHROME_ORIGIN=SERVER_SOURCE '
246 '%s '
Caroline Ticef6ef4392017-04-06 17:16:05 -0700247 '%s --skip_chroot_upgrade' %
248 (options.board, options.cflags, options.board, options.cxxflags,
249 options.board, options.ldflags, build_packages_env,
250 build_packages_command))
raymesbfb57992013-02-15 04:35:45 +0000251
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800252 logger.GetLogger().LogFatalIf(ret, 'build_packages failed')
yunlian5acba6e2013-02-19 22:34:37 +0000253 if options.package:
254 return 0
raymes5154d7f2013-02-15 04:35:37 +0000255 # Build image
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800256 ret = cmd_executer.ChrootRunCommand(
257 options.chromeos_root, build_packages_env + ' ' + build_image_command)
raymesbfb57992013-02-15 04:35:45 +0000258
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800259 logger.GetLogger().LogFatalIf(ret, 'build_image failed')
raymes5154d7f2013-02-15 04:35:37 +0000260
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800261 flags_file_name = 'flags.txt'
262 flags_file_path = ('%s/src/build/images/%s/latest/%s' %
263 (options.chromeos_root, options.board, flags_file_name))
264 flags_file = open(flags_file_path, 'wb')
265 flags_file.write('CFLAGS=%s\n' % options.cflags)
266 flags_file.write('CXXFLAGS=%s\n' % options.cxxflags)
267 flags_file.write('LDFLAGS=%s\n' % options.ldflags)
asharif8697d4e2013-02-15 09:18:09 +0000268 flags_file.close()
269
270 if options.label:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800271 image_dir_path = ('%s/src/build/images/%s/latest' % (options.chromeos_root,
272 options.board))
asharif8697d4e2013-02-15 09:18:09 +0000273 real_image_dir_path = os.path.realpath(image_dir_path)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800274 command = ('ln -sf -T %s %s/%s' %
asharif8697d4e2013-02-15 09:18:09 +0000275 (os.path.basename(real_image_dir_path),
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800276 os.path.dirname(real_image_dir_path), options.label))
asharif8697d4e2013-02-15 09:18:09 +0000277
278 ret = cmd_executer.RunCommand(command)
Caroline Ticef6ef4392017-04-06 17:16:05 -0700279 logger.GetLogger().LogFatalIf(
280 ret, 'Failed to apply symlink label %s' % options.label)
asharif8697d4e2013-02-15 09:18:09 +0000281
282 return ret
raymes5154d7f2013-02-15 04:35:37 +0000283
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800284
285if __name__ == '__main__':
asharif2198c512013-02-15 09:21:35 +0000286 retval = Main(sys.argv)
287 sys.exit(retval)