blob: 756c60cc5c3bb5a19f37b110b11575b99b0fbebf [file] [log] [blame]
raymes5154d7f2013-02-15 04:35:37 +00001#!/usr/bin/python2.6
2#
3# Copyright 2010 Google Inc. All Rights Reserved.
4
5"""Script to checkout the ChromeOS source.
6
7This script sets up the ChromeOS source in the given directory, matching a
8particular release of ChromeOS.
9"""
10
11__author__ = "raymes@google.com (Raymes Khoury)"
12
13import optparse
14import os
15import sys
asharife3668f12013-02-15 04:46:29 +000016import tc_enter_chroot
raymes01959ae2013-02-15 04:50:07 +000017from utils import command_executer
18from utils import logger
raymes5154d7f2013-02-15 04:35:37 +000019from utils import utils
20
asharife3668f12013-02-15 04:46:29 +000021
raymes5154d7f2013-02-15 04:35:37 +000022def Usage(parser, message):
23 print "ERROR: " + message
24 parser.print_help()
25 sys.exit(0)
26
raymes49fd5a32013-02-15 04:55:27 +000027#TODO(raymes): move this to a common utils file.
asharif8697d4e2013-02-15 09:18:09 +000028def ExecuteCommandInChroot(chromeos_root, command, toolchain_root=None,
29 return_output=False, full_mount=False,
30 tec_options=[]):
asharife3668f12013-02-15 04:46:29 +000031 """Executes a command in the chroot."""
raymes49fd5a32013-02-15 04:55:27 +000032 global cmd_executer
33 cmd_executer = command_executer.GetCommandExecuter()
asharif01e29a52013-02-15 04:56:41 +000034 chromeos_root = os.path.expanduser(chromeos_root)
raymes49fd5a32013-02-15 04:55:27 +000035
asharif8697d4e2013-02-15 09:18:09 +000036 argv = [os.path.dirname(os.path.abspath(__file__)) + "/tc_enter_chroot.py",
37 "--chromeos_root=" + chromeos_root,
38 command]
39 if toolchain_root:
asharif01e29a52013-02-15 04:56:41 +000040 toolchain_root = os.path.expanduser(toolchain_root)
asharif8697d4e2013-02-15 09:18:09 +000041 argv.append("--toolchain_root=" + toolchain_root)
42 if not full_mount:
43 argv.append("-s")
44 argv += tec_options
45 return tc_enter_chroot.Main(argv, return_output)
asharife3668f12013-02-15 04:46:29 +000046
47
48def MakeChroot(chromeos_root, clobber_chroot=False):
49 """Make a chroot given a chromeos checkout."""
50 if (not os.path.isdir(chromeos_root + "/chroot")
51 or clobber_chroot):
52 commands = []
53 commands.append("cd " + chromeos_root + "/src/scripts")
54 clobber_chroot = ""
55 if clobber_chroot:
56 clobber_chroot = "--replace"
57 commands.append("./make_chroot --fast " + clobber_chroot)
asharif967d7002013-02-15 04:51:00 +000058 ret = command_executer.GetCommandExecuter().RunCommands(commands)
asharife3668f12013-02-15 04:46:29 +000059 utils.AssertTrue(ret == 0, "make_chroot failed")
60 else:
raymes01959ae2013-02-15 04:50:07 +000061 logger.GetLogger().LogOutput("Did not make_chroot because it already exists")
raymes5154d7f2013-02-15 04:35:37 +000062
63
bjanakiraman6496e5f2013-02-15 04:50:58 +000064def Main(argv):
raymes5154d7f2013-02-15 04:35:37 +000065 """Build ChromeOS."""
66 # Common initializations
asharif5a9bb462013-02-15 04:50:57 +000067 cmd_executer = command_executer.GetCommandExecuter()
raymes5154d7f2013-02-15 04:35:37 +000068
69 parser = optparse.OptionParser()
70 parser.add_option("--chromeos_root", dest="chromeos_root",
71 help="Target directory for ChromeOS installation.")
raymes5154d7f2013-02-15 04:35:37 +000072 parser.add_option("--clobber_chroot", dest="clobber_chroot",
73 action="store_true", help=
74 "Delete the chroot and start fresh", default=False)
75 parser.add_option("--clobber_board", dest="clobber_board",
76 action="store_true",
77 help="Delete the board and start fresh", default=False)
asharif80b47dc2013-02-15 06:31:19 +000078 parser.add_option("--rebuild", dest="rebuild",
79 action="store_true",
80 help="Rebuild all board packages except the toolchain.",
81 default=False)
raymes9b8305c2013-02-15 04:56:27 +000082 parser.add_option("--cflags", dest="cflags", default="",
raymes5154d7f2013-02-15 04:35:37 +000083 help="CFLAGS for the ChromeOS packages")
raymes9b8305c2013-02-15 04:56:27 +000084 parser.add_option("--cxxflags", dest="cxxflags", default="",
raymes5154d7f2013-02-15 04:35:37 +000085 help="CXXFLAGS for the ChromeOS packages")
raymes9b8305c2013-02-15 04:56:27 +000086 parser.add_option("--ldflags", dest="ldflags", default="",
raymes5154d7f2013-02-15 04:35:37 +000087 help="LDFLAGS for the ChromeOS packages")
88 parser.add_option("--board", dest="board",
89 help="ChromeOS target board, e.g. x86-generic")
asharif8697d4e2013-02-15 09:18:09 +000090 parser.add_option("--label", dest="label",
91 help="Optional label symlink to point to build dir.")
asharif0341d302013-02-15 04:56:38 +000092 parser.add_option("--vanilla", dest="vanilla",
asharifb1752c82013-02-15 04:56:37 +000093 default=False,
94 action="store_true",
asharif0341d302013-02-15 04:56:38 +000095 help="Use default ChromeOS toolchain.")
raymes5154d7f2013-02-15 04:35:37 +000096
bjanakiraman6496e5f2013-02-15 04:50:58 +000097 options = parser.parse_args(argv[1:])[0]
raymes5154d7f2013-02-15 04:35:37 +000098
99 if options.chromeos_root is None:
100 Usage(parser, "--chromeos_root must be set")
101
raymes5154d7f2013-02-15 04:35:37 +0000102 if options.board is None:
103 Usage(parser, "--board must be set")
104
asharif80b47dc2013-02-15 06:31:19 +0000105 build_packages_env = ""
106 if options.rebuild == True:
107 build_packages_env = "EXTRA_BOARD_FLAGS=-e"
108
asharif01e29a52013-02-15 04:56:41 +0000109 options.chromeos_root = os.path.expanduser(options.chromeos_root)
asharif01e29a52013-02-15 04:56:41 +0000110
asharife3668f12013-02-15 04:46:29 +0000111 MakeChroot(options.chromeos_root, options.clobber_chroot)
raymes5154d7f2013-02-15 04:35:37 +0000112
asharife0cc3052013-02-15 05:20:48 +0000113 build_packages_command = utils.GetBuildPackagesCommand(options.board)
114 build_image_command = utils.GetBuildImageCommand(options.board)
115 mod_image_command = utils.GetModImageForTestCommand(options.board)
asharifca8c5ef2013-02-15 04:57:02 +0000116
asharif0341d302013-02-15 04:56:38 +0000117 if options.vanilla == True:
asharife0cc3052013-02-15 05:20:48 +0000118 command = utils.GetSetupBoardCommand(options.board,
119 usepkg=False,
120 force=options.clobber_board)
asharifc380f612013-02-15 09:13:07 +0000121 command += "; " + build_packages_env + " " + build_packages_command
asharifca8c5ef2013-02-15 04:57:02 +0000122 command += "&& " + build_image_command
123 command += "&& " + mod_image_command
asharif8697d4e2013-02-15 09:18:09 +0000124 ret = ExecuteCommandInChroot(options.chromeos_root, command)
asharifb1752c82013-02-15 04:56:37 +0000125 return ret
126
raymes5154d7f2013-02-15 04:35:37 +0000127 # Setup board
raymes04164a12013-02-15 04:36:03 +0000128 if not os.path.isdir(options.chromeos_root + "/chroot/build/"
129 + options.board) or options.clobber_board:
raymes04164a12013-02-15 04:36:03 +0000130 # Run build_tc.py from binary package
bjanakiraman6496e5f2013-02-15 04:50:58 +0000131 rootdir = utils.GetRoot(argv[0])[0]
raymes5f6be5f2013-02-15 04:36:13 +0000132 version_number = utils.GetRoot(rootdir)[1]
asharif8697d4e2013-02-15 09:18:09 +0000133 ret = ExecuteCommandInChroot(options.chromeos_root,
asharife0cc3052013-02-15 05:20:48 +0000134 utils.GetSetupBoardCommand(options.board,
135 gcc_version="9999",
136 binutils_version="9999",
137 force=options.clobber_board))
raymes5f35b922013-02-15 04:35:57 +0000138 utils.AssertTrue(ret == 0, "setup_board failed")
139 else:
raymes01959ae2013-02-15 04:50:07 +0000140 logger.GetLogger().LogOutput("Did not setup_board "
141 "because it already exists")
raymesbfb57992013-02-15 04:35:45 +0000142
raymes9b8305c2013-02-15 04:56:27 +0000143 # Build packages
asharif8697d4e2013-02-15 09:18:09 +0000144 ret = ExecuteCommandInChroot(options.chromeos_root,
raymesaa351762013-02-15 04:56:51 +0000145 "CFLAGS=\"$(portageq-%s envvar CFLAGS) %s\" "
146 "LDFLAGS=\"$(portageq-%s envvar LDFLAGS) %s\" "
147 "CXXFLAGS=\"$(portageq-%s envvar CXXFLAGS) %s\" "
asharifc0f71932013-02-15 04:56:18 +0000148 "CHROME_ORIGIN=SERVER_SOURCE "
asharif80b47dc2013-02-15 06:31:19 +0000149 "%s "
asharifca8c5ef2013-02-15 04:57:02 +0000150 "%s"
asharif253e88b2013-02-15 05:15:34 +0000151 % (options.board, options.cflags,
152 options.board, options.cxxflags,
153 options.board, options.ldflags,
asharif80b47dc2013-02-15 06:31:19 +0000154 build_packages_env,
asharif0e0e2682013-02-15 05:15:29 +0000155 build_packages_command))
raymesbfb57992013-02-15 04:35:45 +0000156
157 utils.AssertTrue(ret == 0, "build_packages failed")
raymes5154d7f2013-02-15 04:35:37 +0000158
159 # Build image
asharif8697d4e2013-02-15 09:18:09 +0000160 ret = ExecuteCommandInChroot(options.chromeos_root,
asharifca8c5ef2013-02-15 04:57:02 +0000161 build_image_command)
raymesbfb57992013-02-15 04:35:45 +0000162
163 utils.AssertTrue(ret == 0, "build_image failed")
raymes5154d7f2013-02-15 04:35:37 +0000164
165 # Mod image for test
asharif8697d4e2013-02-15 09:18:09 +0000166 ret = ExecuteCommandInChroot(options.chromeos_root,
asharifca8c5ef2013-02-15 04:57:02 +0000167 mod_image_command)
raymes5154d7f2013-02-15 04:35:37 +0000168
raymesbfb57992013-02-15 04:35:45 +0000169 utils.AssertTrue(ret == 0, "mod_image_for_test failed")
asharif8697d4e2013-02-15 09:18:09 +0000170
171 flags_file_name = "flags.txt"
172 flags_file_path = ("%s/src/build/images/%s/latest/%s" %
173 (options.chromeos_root,
174 options.board,
175 flags_file_name))
176 flags_file = open(flags_file_path, "wb")
177 flags_file.write("CFLAGS=%s\n" % options.cflags)
178 flags_file.write("CXXFLAGS=%s\n" % options.cxxflags)
179 flags_file.write("LDFLAGS=%s\n" % options.ldflags)
180 flags_file.close()
181
182 if options.label:
183 image_dir_path = ("%s/src/build/images/%s/latest" %
184 (options.chromeos_root,
185 options.board))
186 real_image_dir_path = os.path.realpath(image_dir_path)
187 command = ("ln -sf -T %s %s/%s" %
188 (os.path.basename(real_image_dir_path),
189 os.path.dirname(real_image_dir_path),
190 options.label))
191
192 ret = cmd_executer.RunCommand(command)
193 utils.AssertExit(ret == 0,
194 "Failed to apply symlink label %s" % options.label)
195
196 return ret
raymes5154d7f2013-02-15 04:35:37 +0000197
198if __name__ == "__main__":
asharif2198c512013-02-15 09:21:35 +0000199 retval = Main(sys.argv)
200 sys.exit(retval)