blob: e4f6426863ceb161cb868bd7ec955d4fc587830f [file] [log] [blame]
yunlian5acba6e2013-02-19 22:34:37 +00001#!/usr/bin/python
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
Luis Lozanof2a3ef42015-12-15 13:49:30 -080010__author__ = ('asharif@google.com (Ahmad Sharif) '
11 'llozano@google.com (Luis Lozano) '
12 'raymes@google.com (Raymes Khoury) '
13 'shenhan@google.com (Han Shen)')
raymes5154d7f2013-02-15 04:35:37 +000014
15import optparse
16import os
17import sys
kbaclawski20082a02013-02-16 02:12:57 +000018
asharife3668f12013-02-15 04:46:29 +000019import tc_enter_chroot
raymes01959ae2013-02-15 04:50:07 +000020from utils import command_executer
21from utils import logger
kbaclawski20082a02013-02-16 02:12:57 +000022from utils import misc
raymes5154d7f2013-02-15 04:35:37 +000023
asharife3668f12013-02-15 04:46:29 +000024
raymes5154d7f2013-02-15 04:35:37 +000025def Usage(parser, message):
Luis Lozanof2a3ef42015-12-15 13:49:30 -080026 print 'ERROR: ' + message
raymes5154d7f2013-02-15 04:35:37 +000027 parser.print_help()
28 sys.exit(0)
29
asharife3668f12013-02-15 04:46:29 +000030
bjanakiraman6496e5f2013-02-15 04:50:58 +000031def Main(argv):
raymes5154d7f2013-02-15 04:35:37 +000032 """Build ChromeOS."""
33 # Common initializations
asharif5a9bb462013-02-15 04:50:57 +000034 cmd_executer = command_executer.GetCommandExecuter()
raymes5154d7f2013-02-15 04:35:37 +000035
36 parser = optparse.OptionParser()
Luis Lozanof2a3ef42015-12-15 13:49:30 -080037 parser.add_option('--chromeos_root',
38 dest='chromeos_root',
39 help='Target directory for ChromeOS installation.')
40 parser.add_option('--clobber_chroot',
41 dest='clobber_chroot',
42 action='store_true',
43 help='Delete the chroot and start fresh',
asharif80b47dc2013-02-15 06:31:19 +000044 default=False)
Luis Lozanof2a3ef42015-12-15 13:49:30 -080045 parser.add_option('--clobber_board',
46 dest='clobber_board',
47 action='store_true',
48 help='Delete the board and start fresh',
49 default=False)
50 parser.add_option('--rebuild',
51 dest='rebuild',
52 action='store_true',
53 help='Rebuild all board packages except the toolchain.',
54 default=False)
55 parser.add_option('--cflags',
56 dest='cflags',
57 default='',
58 help='CFLAGS for the ChromeOS packages')
59 parser.add_option('--cxxflags',
60 dest='cxxflags',
61 default='',
62 help='CXXFLAGS for the ChromeOS packages')
63 parser.add_option('--ldflags',
64 dest='ldflags',
65 default='',
66 help='LDFLAGS for the ChromeOS packages')
67 parser.add_option('--board',
68 dest='board',
69 help='ChromeOS target board, e.g. x86-generic')
70 parser.add_option('--package',
71 dest='package',
72 help='The package needs to be built')
73 parser.add_option('--label',
74 dest='label',
75 help='Optional label symlink to point to build dir.')
76 parser.add_option('--dev',
77 dest='dev',
78 default=False,
79 action='store_true',
80 help=('Make the final image in dev mode (eg writable, '
81 'more space on image). Defaults to False.'))
82 parser.add_option('--debug',
83 dest='debug',
84 default=False,
85 action='store_true',
shenhan48738582013-02-19 22:45:41 +000086 help=("Optional. Build chrome browser with \"-g -O0\". "
87 "Notice, this also turns on \'--dev\'. "
Luis Lozanof2a3ef42015-12-15 13:49:30 -080088 'Defaults to False.'))
89 parser.add_option('--env',
90 dest='env',
91 default='',
92 help='Env to pass to build_packages.')
93 parser.add_option('--vanilla',
94 dest='vanilla',
asharifb1752c82013-02-15 04:56:37 +000095 default=False,
Luis Lozanof2a3ef42015-12-15 13:49:30 -080096 action='store_true',
97 help='Use default ChromeOS toolchain.')
98 parser.add_option('--vanilla_image',
99 dest='vanilla_image',
Yunlian Jiangd145a582013-08-19 13:59:34 -0700100 default=False,
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800101 action='store_true',
102 help=('Use prebuild packages for building the image. '
103 'It also implies the --vanilla option is set.'))
raymes5154d7f2013-02-15 04:35:37 +0000104
bjanakiraman6496e5f2013-02-15 04:50:58 +0000105 options = parser.parse_args(argv[1:])[0]
raymes5154d7f2013-02-15 04:35:37 +0000106
107 if options.chromeos_root is None:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800108 Usage(parser, '--chromeos_root must be set')
Luis Lozano09b027f2015-03-30 13:29:49 -0700109 options.chromeos_root = os.path.expanduser(options.chromeos_root)
110 scripts_dir = os.path.join(options.chromeos_root, 'src', 'scripts')
111 if not os.path.isdir(scripts_dir):
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800112 Usage(parser, '--chromeos_root must be set up first. Use setup_chromeos.py')
raymes5154d7f2013-02-15 04:35:37 +0000113
raymes5154d7f2013-02-15 04:35:37 +0000114 if options.board is None:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800115 Usage(parser, '--board must be set')
raymes5154d7f2013-02-15 04:35:37 +0000116
shenhan48738582013-02-19 22:45:41 +0000117 if options.debug:
118 options.dev = True
119
asharif44473782013-02-19 19:58:15 +0000120 build_packages_env = options.env
shenhan48738582013-02-19 22:45:41 +0000121 if build_packages_env.find('EXTRA_BOARD_FLAGS=') != -1:
122 logger.GetLogger().LogFatal(
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800123 ('Passing "EXTRA_BOARD_FLAGS" in "--env" is not supported. '
124 'This flags is used internally by this script. '
125 'Contact the author for more detail.'))
shenhan48738582013-02-19 22:45:41 +0000126
asharif80b47dc2013-02-15 06:31:19 +0000127 if options.rebuild == True:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800128 build_packages_env += ' EXTRA_BOARD_FLAGS=-e'
llozano3a428922013-02-19 21:36:47 +0000129 # EXTRA_BOARD_FLAGS=-e should clean up the object files for the chrome
130 # browser but it doesn't. So do it here.
131 misc.RemoveChromeBrowserObjectFiles(options.chromeos_root, options.board)
asharif80b47dc2013-02-15 06:31:19 +0000132
Luis Lozano09b027f2015-03-30 13:29:49 -0700133 # Build with afdo_use by default.
134 # To change the default use --env="USE=-afdo_use".
135 build_packages_env = misc.MergeEnvStringWithDict(
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800136 build_packages_env, {'USE': 'chrome_internal afdo_use'})
asharif01e29a52013-02-15 04:56:41 +0000137
shenhan48738582013-02-19 22:45:41 +0000138 build_packages_command = misc.GetBuildPackagesCommand(
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800139 board=options.board,
140 usepkg=options.vanilla_image,
141 debug=options.debug)
yunlian5acba6e2013-02-19 22:34:37 +0000142
143 if options.package:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800144 build_packages_command += ' {0}'.format(options.package)
yunlian5acba6e2013-02-19 22:34:37 +0000145
shenhan48738582013-02-19 22:45:41 +0000146 build_image_command = misc.GetBuildImageCommand(options.board, options.dev)
asharifca8c5ef2013-02-15 04:57:02 +0000147
Yunlian Jiangd145a582013-08-19 13:59:34 -0700148 if options.vanilla or options.vanilla_image:
kbaclawski20082a02013-02-16 02:12:57 +0000149 command = misc.GetSetupBoardCommand(options.board,
Yunlian Jiangd145a582013-08-19 13:59:34 -0700150 usepkg=options.vanilla_image,
llozano3a428922013-02-19 21:36:47 +0000151 force=options.clobber_board)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800152 command += '; ' + build_packages_env + ' ' + build_packages_command
153 command += '&& ' + build_packages_env + ' ' + build_image_command
asharifca3c6c12013-02-15 23:17:54 +0000154 ret = cmd_executer.ChrootRunCommand(options.chromeos_root, command)
asharifb1752c82013-02-15 04:56:37 +0000155 return ret
156
raymes5154d7f2013-02-15 04:35:37 +0000157 # Setup board
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800158 if not os.path.isdir(options.chromeos_root + '/chroot/build/' +
159 options.board) or options.clobber_board:
raymes04164a12013-02-15 04:36:03 +0000160 # Run build_tc.py from binary package
kbaclawski20082a02013-02-16 02:12:57 +0000161 rootdir = misc.GetRoot(argv[0])[0]
162 version_number = misc.GetRoot(rootdir)[1]
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800163 ret = cmd_executer.ChrootRunCommand(options.chromeos_root,
164 misc.GetSetupBoardCommand(
165 options.board,
166 force=options.clobber_board))
167 logger.GetLogger().LogFatalIf(ret, 'setup_board failed')
raymes5f35b922013-02-15 04:35:57 +0000168 else:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800169 logger.GetLogger().LogOutput('Did not setup_board '
170 'because it already exists')
raymesbfb57992013-02-15 04:35:45 +0000171
shenhan48738582013-02-19 22:45:41 +0000172 if options.debug:
173 # Perform 2-step build_packages to build a debug chrome browser.
174
175 # Firstly, build everything that chromeos-chrome depends on normally.
176 if options.rebuild == True:
177 # Give warning about "--rebuild" and "--debug". Under this combination,
178 # only dependencies of "chromeos-chrome" get rebuilt.
179 logger.GetLogger().LogWarning(
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800180 "\"--rebuild\" does not correctly re-build every package when "
181 "\"--debug\" is enabled. ")
shenhan48738582013-02-19 22:45:41 +0000182
183 # Replace EXTRA_BOARD_FLAGS=-e with "-e --onlydeps"
184 build_packages_env = build_packages_env.replace(
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800185 'EXTRA_BOARD_FLAGS=-e', 'EXTRA_BOARD_FLAGS=\"-e --onlydeps\"')
shenhan48738582013-02-19 22:45:41 +0000186 else:
187 build_packages_env += ' EXTRA_BOARD_FLAGS=--onlydeps'
188
189 ret = cmd_executer.ChrootRunCommand(
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800190 options.chromeos_root, "CFLAGS=\"$(portageq-%s envvar CFLAGS) %s\" "
191 "CXXFLAGS=\"$(portageq-%s envvar CXXFLAGS) %s\" "
192 "LDFLAGS=\"$(portageq-%s envvar LDFLAGS) %s\" "
193 'CHROME_ORIGIN=SERVER_SOURCE '
194 '%s '
195 '%s --skip_chroot_upgrade'
196 'chromeos-chrome' % (options.board, options.cflags, options.board,
197 options.cxxflags, options.board, options.ldflags,
198 build_packages_env, build_packages_command))
shenhan48738582013-02-19 22:45:41 +0000199
200 logger.GetLogger().LogFatalIf(\
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800201 ret, 'build_packages failed while trying to build chromeos-chrome deps.')
shenhan48738582013-02-19 22:45:41 +0000202
203 # Secondly, build chromeos-chrome using debug mode.
204 # Replace '--onlydeps' with '--nodeps'.
205 if options.rebuild == True:
206 build_packages_env = build_packages_env.replace(
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800207 'EXTRA_BOARD_FLAGS=\"-e --onlydeps\"', 'EXTRA_BOARD_FLAGS=--nodeps')
shenhan48738582013-02-19 22:45:41 +0000208 else:
209 build_packages_env = build_packages_env.replace(
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800210 'EXTRA_BOARD_FLAGS=--onlydeps', 'EXTRA_BOARD_FLAGS=--nodeps')
shenhan48738582013-02-19 22:45:41 +0000211 ret = cmd_executer.ChrootRunCommand(
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800212 options.chromeos_root, "CFLAGS=\"$(portageq-%s envvar CFLAGS) %s\" "
213 "CXXFLAGS=\"$(portageq-%s envvar CXXFLAGS) %s\" "
214 "LDFLAGS=\"$(portageq-%s envvar LDFLAGS) %s\" "
215 'CHROME_ORIGIN=SERVER_SOURCE BUILDTYPE=Debug '
216 '%s '
217 '%s --skip_chroot_upgrade'
218 'chromeos-chrome' % (options.board, options.cflags, options.board,
219 options.cxxflags, options.board, options.ldflags,
220 build_packages_env, build_packages_command))
shenhan48738582013-02-19 22:45:41 +0000221 logger.GetLogger().LogFatalIf(
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800222 ret,
223 'build_packages failed while trying to build debug chromeos-chrome.')
shenhan48738582013-02-19 22:45:41 +0000224
225 # Now, we have built chromeos-chrome and all dependencies.
226 # Finally, remove '-e' from EXTRA_BOARD_FLAGS,
227 # otherwise, chromeos-chrome gets rebuilt.
228 build_packages_env = build_packages_env.replace(\
229 'EXTRA_BOARD_FLAGS=--nodeps', '')
230
231 # Up to now, we have a debug built chromos-chrome browser.
232 # Fall through to build the rest of the world.
233
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800234 # Build packages
asharifca3c6c12013-02-15 23:17:54 +0000235 ret = cmd_executer.ChrootRunCommand(
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800236 options.chromeos_root, "CFLAGS=\"$(portageq-%s envvar CFLAGS) %s\" "
asharifca3c6c12013-02-15 23:17:54 +0000237 "CXXFLAGS=\"$(portageq-%s envvar CXXFLAGS) %s\" "
llozano109ac9f2013-02-19 19:58:27 +0000238 "LDFLAGS=\"$(portageq-%s envvar LDFLAGS) %s\" "
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800239 'CHROME_ORIGIN=SERVER_SOURCE '
240 '%s '
241 '%s --skip_chroot_upgrade' % (options.board, options.cflags,
242 options.board, options.cxxflags,
243 options.board, options.ldflags,
244 build_packages_env, build_packages_command))
raymesbfb57992013-02-15 04:35:45 +0000245
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800246 logger.GetLogger().LogFatalIf(ret, 'build_packages failed')
yunlian5acba6e2013-02-19 22:34:37 +0000247 if options.package:
248 return 0
raymes5154d7f2013-02-15 04:35:37 +0000249 # Build image
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800250 ret = cmd_executer.ChrootRunCommand(
251 options.chromeos_root, build_packages_env + ' ' + build_image_command)
raymesbfb57992013-02-15 04:35:45 +0000252
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800253 logger.GetLogger().LogFatalIf(ret, 'build_image failed')
raymes5154d7f2013-02-15 04:35:37 +0000254
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800255 flags_file_name = 'flags.txt'
256 flags_file_path = ('%s/src/build/images/%s/latest/%s' %
257 (options.chromeos_root, options.board, flags_file_name))
258 flags_file = open(flags_file_path, 'wb')
259 flags_file.write('CFLAGS=%s\n' % options.cflags)
260 flags_file.write('CXXFLAGS=%s\n' % options.cxxflags)
261 flags_file.write('LDFLAGS=%s\n' % options.ldflags)
asharif8697d4e2013-02-15 09:18:09 +0000262 flags_file.close()
263
264 if options.label:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800265 image_dir_path = ('%s/src/build/images/%s/latest' % (options.chromeos_root,
266 options.board))
asharif8697d4e2013-02-15 09:18:09 +0000267 real_image_dir_path = os.path.realpath(image_dir_path)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800268 command = ('ln -sf -T %s %s/%s' %
asharif8697d4e2013-02-15 09:18:09 +0000269 (os.path.basename(real_image_dir_path),
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800270 os.path.dirname(real_image_dir_path), options.label))
asharif8697d4e2013-02-15 09:18:09 +0000271
272 ret = cmd_executer.RunCommand(command)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800273 logger.GetLogger().LogFatalIf(ret, 'Failed to apply symlink label %s' %
kbaclawski6999ada2013-02-15 19:57:09 +0000274 options.label)
asharif8697d4e2013-02-15 09:18:09 +0000275
276 return ret
raymes5154d7f2013-02-15 04:35:37 +0000277
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800278
279if __name__ == '__main__':
asharif2198c512013-02-15 09:21:35 +0000280 retval = Main(sys.argv)
281 sys.exit(retval)