| raymes | 5154d7f | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 1 | #!/usr/bin/python2.6 | 
 | 2 | # | 
 | 3 | # Copyright 2010 Google Inc. All Rights Reserved. | 
 | 4 |  | 
 | 5 | """Script to checkout the ChromeOS source. | 
 | 6 |  | 
 | 7 | This script sets up the ChromeOS source in the given directory, matching a | 
 | 8 | particular release of ChromeOS. | 
 | 9 | """ | 
 | 10 |  | 
 | 11 | __author__ = "raymes@google.com (Raymes Khoury)" | 
 | 12 |  | 
 | 13 | import optparse | 
 | 14 | import os | 
 | 15 | import sys | 
| asharif | e3668f1 | 2013-02-15 04:46:29 +0000 | [diff] [blame] | 16 | import tc_enter_chroot | 
| raymes | 01959ae | 2013-02-15 04:50:07 +0000 | [diff] [blame] | 17 | from utils import command_executer | 
 | 18 | from utils import logger | 
| raymes | 5154d7f | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 19 | from utils import utils | 
 | 20 |  | 
| asharif | e3668f1 | 2013-02-15 04:46:29 +0000 | [diff] [blame] | 21 |  | 
| raymes | 5154d7f | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 22 | def Usage(parser, message): | 
 | 23 |   print "ERROR: " + message | 
 | 24 |   parser.print_help() | 
 | 25 |   sys.exit(0) | 
 | 26 |  | 
| raymes | 49fd5a3 | 2013-02-15 04:55:27 +0000 | [diff] [blame] | 27 | #TODO(raymes): move this to a common utils file. | 
| raymes | 5154d7f | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 28 | def ExecuteCommandInChroot(chromeos_root, toolchain_root, command, | 
| raymes | 81d8896 | 2013-02-15 04:56:26 +0000 | [diff] [blame] | 29 |                            return_output=False, full_mount=False): | 
| asharif | e3668f1 | 2013-02-15 04:46:29 +0000 | [diff] [blame] | 30 |   """Executes a command in the chroot.""" | 
| raymes | 49fd5a3 | 2013-02-15 04:55:27 +0000 | [diff] [blame] | 31 |   global cmd_executer | 
 | 32 |   cmd_executer = command_executer.GetCommandExecuter() | 
| asharif | 01e29a5 | 2013-02-15 04:56:41 +0000 | [diff] [blame] | 33 |   chromeos_root = os.path.expanduser(chromeos_root) | 
| raymes | 49fd5a3 | 2013-02-15 04:55:27 +0000 | [diff] [blame] | 34 |  | 
| raymes | 49fd5a3 | 2013-02-15 04:55:27 +0000 | [diff] [blame] | 35 |   if toolchain_root is None: | 
| asharif | 708d57c | 2013-02-15 04:56:23 +0000 | [diff] [blame] | 36 |     cmd_file = "enter_chroot.cmd" | 
 | 37 |     cmd_file_path = chromeos_root + "/src/scripts/" + cmd_file | 
 | 38 |     f = open(cmd_file_path, "w") | 
 | 39 |     f.write(command) | 
 | 40 |     logger.GetLogger().LogCmd(command) | 
 | 41 |     f.close() | 
 | 42 |     retval = cmd_executer.RunCommand("chmod +x " + cmd_file_path) | 
 | 43 |     utils.AssertTrue(retval == 0, "chmod +x failed!") | 
| raymes | 49fd5a3 | 2013-02-15 04:55:27 +0000 | [diff] [blame] | 44 |     return cmd_executer.RunCommand(chromeos_root + | 
| asharif | 708d57c | 2013-02-15 04:56:23 +0000 | [diff] [blame] | 45 |                                    "/src/scripts/enter_chroot.sh -- ./%s" | 
 | 46 |                                       % cmd_file) | 
| raymes | 49fd5a3 | 2013-02-15 04:55:27 +0000 | [diff] [blame] | 47 |   else: | 
| asharif | 01e29a5 | 2013-02-15 04:56:41 +0000 | [diff] [blame] | 48 |     toolchain_root = os.path.expanduser(toolchain_root) | 
| raymes | 49fd5a3 | 2013-02-15 04:55:27 +0000 | [diff] [blame] | 49 |     argv = [os.path.dirname(os.path.abspath(__file__)) + "/tc_enter_chroot.py", | 
 | 50 |             "--chromeos_root=" + chromeos_root, | 
 | 51 |             "--toolchain_root=" + toolchain_root, | 
| raymes | 49fd5a3 | 2013-02-15 04:55:27 +0000 | [diff] [blame] | 52 |             "\n" + command] | 
| raymes | 81d8896 | 2013-02-15 04:56:26 +0000 | [diff] [blame] | 53 |     if not full_mount: | 
 | 54 |       argv.append("-s") | 
| raymes | 9b8305c | 2013-02-15 04:56:27 +0000 | [diff] [blame] | 55 |     return tc_enter_chroot.Main(argv, return_output) | 
| asharif | e3668f1 | 2013-02-15 04:46:29 +0000 | [diff] [blame] | 56 |  | 
 | 57 |  | 
 | 58 | def MakeChroot(chromeos_root, clobber_chroot=False): | 
 | 59 |   """Make a chroot given a chromeos checkout.""" | 
 | 60 |   if (not os.path.isdir(chromeos_root + "/chroot") | 
 | 61 |       or clobber_chroot): | 
 | 62 |     commands = [] | 
 | 63 |     commands.append("cd " + chromeos_root + "/src/scripts") | 
 | 64 |     clobber_chroot = "" | 
 | 65 |     if clobber_chroot: | 
 | 66 |       clobber_chroot = "--replace" | 
 | 67 |     commands.append("./make_chroot --fast " + clobber_chroot) | 
| asharif | 967d700 | 2013-02-15 04:51:00 +0000 | [diff] [blame] | 68 |     ret = command_executer.GetCommandExecuter().RunCommands(commands) | 
| asharif | e3668f1 | 2013-02-15 04:46:29 +0000 | [diff] [blame] | 69 |     utils.AssertTrue(ret == 0, "make_chroot failed") | 
 | 70 |   else: | 
| raymes | 01959ae | 2013-02-15 04:50:07 +0000 | [diff] [blame] | 71 |     logger.GetLogger().LogOutput("Did not make_chroot because it already exists") | 
| raymes | 5154d7f | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 72 |  | 
 | 73 |  | 
| bjanakiraman | 6496e5f | 2013-02-15 04:50:58 +0000 | [diff] [blame] | 74 | def Main(argv): | 
| raymes | 5154d7f | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 75 |   """Build ChromeOS.""" | 
 | 76 |   # Common initializations | 
| asharif | 5a9bb46 | 2013-02-15 04:50:57 +0000 | [diff] [blame] | 77 |   cmd_executer = command_executer.GetCommandExecuter() | 
| raymes | 5154d7f | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 78 |  | 
 | 79 |   parser = optparse.OptionParser() | 
 | 80 |   parser.add_option("--chromeos_root", dest="chromeos_root", | 
 | 81 |                     help="Target directory for ChromeOS installation.") | 
| raymes | 5154d7f | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 82 |   parser.add_option("--clobber_chroot", dest="clobber_chroot", | 
 | 83 |                     action="store_true", help= | 
 | 84 |                     "Delete the chroot and start fresh", default=False) | 
 | 85 |   parser.add_option("--clobber_board", dest="clobber_board", | 
 | 86 |                     action="store_true", | 
 | 87 |                     help="Delete the board and start fresh", default=False) | 
| asharif | 80b47dc | 2013-02-15 06:31:19 +0000 | [diff] [blame] | 88 |   parser.add_option("--rebuild", dest="rebuild", | 
 | 89 |                     action="store_true", | 
 | 90 |                     help="Rebuild all board packages except the toolchain.", | 
 | 91 |                     default=False) | 
| raymes | 9b8305c | 2013-02-15 04:56:27 +0000 | [diff] [blame] | 92 |   parser.add_option("--cflags", dest="cflags", default="", | 
| raymes | 5154d7f | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 93 |                     help="CFLAGS for the ChromeOS packages") | 
| raymes | 9b8305c | 2013-02-15 04:56:27 +0000 | [diff] [blame] | 94 |   parser.add_option("--cxxflags", dest="cxxflags", default="", | 
| raymes | 5154d7f | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 95 |                     help="CXXFLAGS for the ChromeOS packages") | 
| raymes | 9b8305c | 2013-02-15 04:56:27 +0000 | [diff] [blame] | 96 |   parser.add_option("--ldflags", dest="ldflags", default="", | 
| raymes | 5154d7f | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 97 |                     help="LDFLAGS for the ChromeOS packages") | 
 | 98 |   parser.add_option("--board", dest="board", | 
 | 99 |                     help="ChromeOS target board, e.g. x86-generic") | 
| asharif | 0341d30 | 2013-02-15 04:56:38 +0000 | [diff] [blame] | 100 |   parser.add_option("--vanilla", dest="vanilla", | 
| asharif | b1752c8 | 2013-02-15 04:56:37 +0000 | [diff] [blame] | 101 |                     default=False, | 
 | 102 |                     action="store_true", | 
| asharif | 0341d30 | 2013-02-15 04:56:38 +0000 | [diff] [blame] | 103 |                     help="Use default ChromeOS toolchain.") | 
| raymes | 5154d7f | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 104 |  | 
| bjanakiraman | 6496e5f | 2013-02-15 04:50:58 +0000 | [diff] [blame] | 105 |   options = parser.parse_args(argv[1:])[0] | 
| raymes | 5154d7f | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 106 |  | 
 | 107 |   if options.chromeos_root is None: | 
 | 108 |     Usage(parser, "--chromeos_root must be set") | 
 | 109 |  | 
| raymes | 5154d7f | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 110 |   if options.board is None: | 
 | 111 |     Usage(parser, "--board must be set") | 
 | 112 |  | 
| asharif | 80b47dc | 2013-02-15 06:31:19 +0000 | [diff] [blame] | 113 |   build_packages_env = "" | 
 | 114 |   if options.rebuild == True: | 
 | 115 |     build_packages_env = "EXTRA_BOARD_FLAGS=-e" | 
 | 116 |  | 
| asharif | 01e29a5 | 2013-02-15 04:56:41 +0000 | [diff] [blame] | 117 |   options.chromeos_root = os.path.expanduser(options.chromeos_root) | 
| asharif | 01e29a5 | 2013-02-15 04:56:41 +0000 | [diff] [blame] | 118 |  | 
| asharif | e3668f1 | 2013-02-15 04:46:29 +0000 | [diff] [blame] | 119 |   MakeChroot(options.chromeos_root, options.clobber_chroot) | 
| raymes | 5154d7f | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 120 |  | 
| asharif | e0cc305 | 2013-02-15 05:20:48 +0000 | [diff] [blame] | 121 |   build_packages_command = utils.GetBuildPackagesCommand(options.board) | 
 | 122 |   build_image_command = utils.GetBuildImageCommand(options.board) | 
 | 123 |   mod_image_command = utils.GetModImageForTestCommand(options.board) | 
| asharif | ca8c5ef | 2013-02-15 04:57:02 +0000 | [diff] [blame] | 124 |  | 
| asharif | 0341d30 | 2013-02-15 04:56:38 +0000 | [diff] [blame] | 125 |   if options.vanilla == True: | 
| asharif | e0cc305 | 2013-02-15 05:20:48 +0000 | [diff] [blame] | 126 |     command = utils.GetSetupBoardCommand(options.board, | 
 | 127 |                                          usepkg=False, | 
 | 128 |                                          force=options.clobber_board) | 
| asharif | c380f61 | 2013-02-15 09:13:07 +0000 | [diff] [blame] | 129 |     command += "; " + build_packages_env + " " + build_packages_command | 
| asharif | ca8c5ef | 2013-02-15 04:57:02 +0000 | [diff] [blame] | 130 |     command += "&& " + build_image_command | 
 | 131 |     command += "&& " + mod_image_command | 
| asharif | b1752c8 | 2013-02-15 04:56:37 +0000 | [diff] [blame] | 132 |     ret = ExecuteCommandInChroot(options.chromeos_root, None, command) | 
 | 133 |     return ret | 
 | 134 |  | 
| raymes | 5154d7f | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 135 |   # Setup board | 
| raymes | 04164a1 | 2013-02-15 04:36:03 +0000 | [diff] [blame] | 136 |   if not os.path.isdir(options.chromeos_root + "/chroot/build/" | 
 | 137 |                        + options.board) or options.clobber_board: | 
| raymes | 04164a1 | 2013-02-15 04:36:03 +0000 | [diff] [blame] | 138 |     # Run build_tc.py from binary package | 
| bjanakiraman | 6496e5f | 2013-02-15 04:50:58 +0000 | [diff] [blame] | 139 |     rootdir = utils.GetRoot(argv[0])[0] | 
| raymes | 5f6be5f | 2013-02-15 04:36:13 +0000 | [diff] [blame] | 140 |     version_number = utils.GetRoot(rootdir)[1] | 
| asharif | 977cd6b | 2013-02-15 04:56:49 +0000 | [diff] [blame] | 141 |     ret = ExecuteCommandInChroot(options.chromeos_root, None, | 
| asharif | e0cc305 | 2013-02-15 05:20:48 +0000 | [diff] [blame] | 142 |                                  utils.GetSetupBoardCommand(options.board, | 
 | 143 |                                    gcc_version="9999", | 
 | 144 |                                    binutils_version="9999", | 
 | 145 |                                    force=options.clobber_board)) | 
| raymes | 5f35b92 | 2013-02-15 04:35:57 +0000 | [diff] [blame] | 146 |     utils.AssertTrue(ret == 0, "setup_board failed") | 
 | 147 |   else: | 
| raymes | 01959ae | 2013-02-15 04:50:07 +0000 | [diff] [blame] | 148 |     logger.GetLogger().LogOutput("Did not setup_board " | 
 | 149 |                                  "because it already exists") | 
| raymes | bfb5799 | 2013-02-15 04:35:45 +0000 | [diff] [blame] | 150 |  | 
| raymes | 9b8305c | 2013-02-15 04:56:27 +0000 | [diff] [blame] | 151 |   # Build packages | 
| asharif | 977cd6b | 2013-02-15 04:56:49 +0000 | [diff] [blame] | 152 |   ret = ExecuteCommandInChroot(options.chromeos_root, None, | 
| raymes | aa35176 | 2013-02-15 04:56:51 +0000 | [diff] [blame] | 153 |                                "CFLAGS=\"$(portageq-%s envvar CFLAGS) %s\" " | 
 | 154 |                                "LDFLAGS=\"$(portageq-%s envvar LDFLAGS) %s\" " | 
 | 155 |                                "CXXFLAGS=\"$(portageq-%s envvar CXXFLAGS) %s\" " | 
| asharif | c0f7193 | 2013-02-15 04:56:18 +0000 | [diff] [blame] | 156 |                                "CHROME_ORIGIN=SERVER_SOURCE " | 
| asharif | 80b47dc | 2013-02-15 06:31:19 +0000 | [diff] [blame] | 157 |                                "%s " | 
| asharif | ca8c5ef | 2013-02-15 04:57:02 +0000 | [diff] [blame] | 158 |                                "%s" | 
| asharif | 253e88b | 2013-02-15 05:15:34 +0000 | [diff] [blame] | 159 |                                % (options.board, options.cflags, | 
 | 160 |                                   options.board, options.cxxflags, | 
 | 161 |                                   options.board, options.ldflags, | 
| asharif | 80b47dc | 2013-02-15 06:31:19 +0000 | [diff] [blame] | 162 |                                   build_packages_env, | 
| asharif | 0e0e268 | 2013-02-15 05:15:29 +0000 | [diff] [blame] | 163 |                                   build_packages_command)) | 
| raymes | bfb5799 | 2013-02-15 04:35:45 +0000 | [diff] [blame] | 164 |  | 
 | 165 |   utils.AssertTrue(ret == 0, "build_packages failed") | 
| raymes | 5154d7f | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 166 |  | 
 | 167 |   # Build image | 
| asharif | 977cd6b | 2013-02-15 04:56:49 +0000 | [diff] [blame] | 168 |   ret = ExecuteCommandInChroot(options.chromeos_root, None, | 
| asharif | ca8c5ef | 2013-02-15 04:57:02 +0000 | [diff] [blame] | 169 |                                build_image_command) | 
| raymes | bfb5799 | 2013-02-15 04:35:45 +0000 | [diff] [blame] | 170 |  | 
 | 171 |   utils.AssertTrue(ret == 0, "build_image failed") | 
| raymes | 5154d7f | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 172 |  | 
 | 173 |   # Mod image for test | 
| asharif | 977cd6b | 2013-02-15 04:56:49 +0000 | [diff] [blame] | 174 |   ret = ExecuteCommandInChroot(options.chromeos_root, None, | 
| asharif | ca8c5ef | 2013-02-15 04:57:02 +0000 | [diff] [blame] | 175 |                                mod_image_command) | 
| raymes | 5154d7f | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 176 |  | 
| raymes | bfb5799 | 2013-02-15 04:35:45 +0000 | [diff] [blame] | 177 |   utils.AssertTrue(ret == 0, "mod_image_for_test failed") | 
| asharif | 253e88b | 2013-02-15 05:15:34 +0000 | [diff] [blame] | 178 |   return 0 | 
| raymes | 5154d7f | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 179 |  | 
 | 180 | if __name__ == "__main__": | 
| bjanakiraman | 6496e5f | 2013-02-15 04:50:58 +0000 | [diff] [blame] | 181 |   Main(sys.argv) |