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. |
asharif | 8697d4e | 2013-02-15 09:18:09 +0000 | [diff] [blame] | 28 | def ExecuteCommandInChroot(chromeos_root, command, toolchain_root=None, |
| 29 | return_output=False, full_mount=False, |
| 30 | tec_options=[]): |
asharif | e3668f1 | 2013-02-15 04:46:29 +0000 | [diff] [blame] | 31 | """Executes a command in the chroot.""" |
raymes | 49fd5a3 | 2013-02-15 04:55:27 +0000 | [diff] [blame] | 32 | global cmd_executer |
| 33 | cmd_executer = command_executer.GetCommandExecuter() |
asharif | 01e29a5 | 2013-02-15 04:56:41 +0000 | [diff] [blame] | 34 | chromeos_root = os.path.expanduser(chromeos_root) |
raymes | 49fd5a3 | 2013-02-15 04:55:27 +0000 | [diff] [blame] | 35 | |
asharif | 8697d4e | 2013-02-15 09:18:09 +0000 | [diff] [blame] | 36 | argv = [os.path.dirname(os.path.abspath(__file__)) + "/tc_enter_chroot.py", |
| 37 | "--chromeos_root=" + chromeos_root, |
| 38 | command] |
| 39 | if toolchain_root: |
asharif | 01e29a5 | 2013-02-15 04:56:41 +0000 | [diff] [blame] | 40 | toolchain_root = os.path.expanduser(toolchain_root) |
asharif | 8697d4e | 2013-02-15 09:18:09 +0000 | [diff] [blame] | 41 | 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) |
asharif | e3668f1 | 2013-02-15 04:46:29 +0000 | [diff] [blame] | 46 | |
| 47 | |
| 48 | def 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) |
asharif | 967d700 | 2013-02-15 04:51:00 +0000 | [diff] [blame] | 58 | ret = command_executer.GetCommandExecuter().RunCommands(commands) |
asharif | e3668f1 | 2013-02-15 04:46:29 +0000 | [diff] [blame] | 59 | utils.AssertTrue(ret == 0, "make_chroot failed") |
| 60 | else: |
raymes | 01959ae | 2013-02-15 04:50:07 +0000 | [diff] [blame] | 61 | logger.GetLogger().LogOutput("Did not make_chroot because it already exists") |
raymes | 5154d7f | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 62 | |
| 63 | |
bjanakiraman | 6496e5f | 2013-02-15 04:50:58 +0000 | [diff] [blame] | 64 | def Main(argv): |
raymes | 5154d7f | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 65 | """Build ChromeOS.""" |
| 66 | # Common initializations |
asharif | 5a9bb46 | 2013-02-15 04:50:57 +0000 | [diff] [blame] | 67 | cmd_executer = command_executer.GetCommandExecuter() |
raymes | 5154d7f | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 68 | |
| 69 | parser = optparse.OptionParser() |
| 70 | parser.add_option("--chromeos_root", dest="chromeos_root", |
| 71 | help="Target directory for ChromeOS installation.") |
raymes | 5154d7f | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 72 | 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) |
asharif | 80b47dc | 2013-02-15 06:31:19 +0000 | [diff] [blame] | 78 | parser.add_option("--rebuild", dest="rebuild", |
| 79 | action="store_true", |
| 80 | help="Rebuild all board packages except the toolchain.", |
| 81 | default=False) |
raymes | 9b8305c | 2013-02-15 04:56:27 +0000 | [diff] [blame] | 82 | parser.add_option("--cflags", dest="cflags", default="", |
raymes | 5154d7f | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 83 | help="CFLAGS for the ChromeOS packages") |
raymes | 9b8305c | 2013-02-15 04:56:27 +0000 | [diff] [blame] | 84 | parser.add_option("--cxxflags", dest="cxxflags", default="", |
raymes | 5154d7f | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 85 | help="CXXFLAGS for the ChromeOS packages") |
raymes | 9b8305c | 2013-02-15 04:56:27 +0000 | [diff] [blame] | 86 | parser.add_option("--ldflags", dest="ldflags", default="", |
raymes | 5154d7f | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 87 | help="LDFLAGS for the ChromeOS packages") |
| 88 | parser.add_option("--board", dest="board", |
| 89 | help="ChromeOS target board, e.g. x86-generic") |
asharif | 8697d4e | 2013-02-15 09:18:09 +0000 | [diff] [blame] | 90 | parser.add_option("--label", dest="label", |
| 91 | help="Optional label symlink to point to build dir.") |
asharif | 0341d30 | 2013-02-15 04:56:38 +0000 | [diff] [blame] | 92 | parser.add_option("--vanilla", dest="vanilla", |
asharif | b1752c8 | 2013-02-15 04:56:37 +0000 | [diff] [blame] | 93 | default=False, |
| 94 | action="store_true", |
asharif | 0341d30 | 2013-02-15 04:56:38 +0000 | [diff] [blame] | 95 | help="Use default ChromeOS toolchain.") |
raymes | 5154d7f | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 96 | |
bjanakiraman | 6496e5f | 2013-02-15 04:50:58 +0000 | [diff] [blame] | 97 | options = parser.parse_args(argv[1:])[0] |
raymes | 5154d7f | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 98 | |
| 99 | if options.chromeos_root is None: |
| 100 | Usage(parser, "--chromeos_root must be set") |
| 101 | |
raymes | 5154d7f | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 102 | if options.board is None: |
| 103 | Usage(parser, "--board must be set") |
| 104 | |
asharif | 80b47dc | 2013-02-15 06:31:19 +0000 | [diff] [blame] | 105 | build_packages_env = "" |
| 106 | if options.rebuild == True: |
| 107 | build_packages_env = "EXTRA_BOARD_FLAGS=-e" |
| 108 | |
asharif | 01e29a5 | 2013-02-15 04:56:41 +0000 | [diff] [blame] | 109 | options.chromeos_root = os.path.expanduser(options.chromeos_root) |
asharif | 01e29a5 | 2013-02-15 04:56:41 +0000 | [diff] [blame] | 110 | |
asharif | e3668f1 | 2013-02-15 04:46:29 +0000 | [diff] [blame] | 111 | MakeChroot(options.chromeos_root, options.clobber_chroot) |
raymes | 5154d7f | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 112 | |
asharif | e0cc305 | 2013-02-15 05:20:48 +0000 | [diff] [blame] | 113 | build_packages_command = utils.GetBuildPackagesCommand(options.board) |
| 114 | build_image_command = utils.GetBuildImageCommand(options.board) |
| 115 | mod_image_command = utils.GetModImageForTestCommand(options.board) |
asharif | ca8c5ef | 2013-02-15 04:57:02 +0000 | [diff] [blame] | 116 | |
asharif | 0341d30 | 2013-02-15 04:56:38 +0000 | [diff] [blame] | 117 | if options.vanilla == True: |
asharif | e0cc305 | 2013-02-15 05:20:48 +0000 | [diff] [blame] | 118 | command = utils.GetSetupBoardCommand(options.board, |
| 119 | usepkg=False, |
| 120 | force=options.clobber_board) |
asharif | c380f61 | 2013-02-15 09:13:07 +0000 | [diff] [blame] | 121 | command += "; " + build_packages_env + " " + build_packages_command |
asharif | ca8c5ef | 2013-02-15 04:57:02 +0000 | [diff] [blame] | 122 | command += "&& " + build_image_command |
| 123 | command += "&& " + mod_image_command |
asharif | 8697d4e | 2013-02-15 09:18:09 +0000 | [diff] [blame] | 124 | ret = ExecuteCommandInChroot(options.chromeos_root, command) |
asharif | b1752c8 | 2013-02-15 04:56:37 +0000 | [diff] [blame] | 125 | return ret |
| 126 | |
raymes | 5154d7f | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 127 | # Setup board |
raymes | 04164a1 | 2013-02-15 04:36:03 +0000 | [diff] [blame] | 128 | if not os.path.isdir(options.chromeos_root + "/chroot/build/" |
| 129 | + options.board) or options.clobber_board: |
raymes | 04164a1 | 2013-02-15 04:36:03 +0000 | [diff] [blame] | 130 | # Run build_tc.py from binary package |
bjanakiraman | 6496e5f | 2013-02-15 04:50:58 +0000 | [diff] [blame] | 131 | rootdir = utils.GetRoot(argv[0])[0] |
raymes | 5f6be5f | 2013-02-15 04:36:13 +0000 | [diff] [blame] | 132 | version_number = utils.GetRoot(rootdir)[1] |
asharif | 8697d4e | 2013-02-15 09:18:09 +0000 | [diff] [blame] | 133 | ret = ExecuteCommandInChroot(options.chromeos_root, |
asharif | e0cc305 | 2013-02-15 05:20:48 +0000 | [diff] [blame] | 134 | utils.GetSetupBoardCommand(options.board, |
| 135 | gcc_version="9999", |
| 136 | binutils_version="9999", |
| 137 | force=options.clobber_board)) |
raymes | 5f35b92 | 2013-02-15 04:35:57 +0000 | [diff] [blame] | 138 | utils.AssertTrue(ret == 0, "setup_board failed") |
| 139 | else: |
raymes | 01959ae | 2013-02-15 04:50:07 +0000 | [diff] [blame] | 140 | logger.GetLogger().LogOutput("Did not setup_board " |
| 141 | "because it already exists") |
raymes | bfb5799 | 2013-02-15 04:35:45 +0000 | [diff] [blame] | 142 | |
raymes | 9b8305c | 2013-02-15 04:56:27 +0000 | [diff] [blame] | 143 | # Build packages |
asharif | 8697d4e | 2013-02-15 09:18:09 +0000 | [diff] [blame] | 144 | ret = ExecuteCommandInChroot(options.chromeos_root, |
raymes | aa35176 | 2013-02-15 04:56:51 +0000 | [diff] [blame] | 145 | "CFLAGS=\"$(portageq-%s envvar CFLAGS) %s\" " |
| 146 | "LDFLAGS=\"$(portageq-%s envvar LDFLAGS) %s\" " |
| 147 | "CXXFLAGS=\"$(portageq-%s envvar CXXFLAGS) %s\" " |
asharif | c0f7193 | 2013-02-15 04:56:18 +0000 | [diff] [blame] | 148 | "CHROME_ORIGIN=SERVER_SOURCE " |
asharif | 80b47dc | 2013-02-15 06:31:19 +0000 | [diff] [blame] | 149 | "%s " |
asharif | ca8c5ef | 2013-02-15 04:57:02 +0000 | [diff] [blame] | 150 | "%s" |
asharif | 253e88b | 2013-02-15 05:15:34 +0000 | [diff] [blame] | 151 | % (options.board, options.cflags, |
| 152 | options.board, options.cxxflags, |
| 153 | options.board, options.ldflags, |
asharif | 80b47dc | 2013-02-15 06:31:19 +0000 | [diff] [blame] | 154 | build_packages_env, |
asharif | 0e0e268 | 2013-02-15 05:15:29 +0000 | [diff] [blame] | 155 | build_packages_command)) |
raymes | bfb5799 | 2013-02-15 04:35:45 +0000 | [diff] [blame] | 156 | |
| 157 | utils.AssertTrue(ret == 0, "build_packages failed") |
raymes | 5154d7f | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 158 | |
| 159 | # Build image |
asharif | 8697d4e | 2013-02-15 09:18:09 +0000 | [diff] [blame] | 160 | ret = ExecuteCommandInChroot(options.chromeos_root, |
asharif | ca8c5ef | 2013-02-15 04:57:02 +0000 | [diff] [blame] | 161 | build_image_command) |
raymes | bfb5799 | 2013-02-15 04:35:45 +0000 | [diff] [blame] | 162 | |
| 163 | utils.AssertTrue(ret == 0, "build_image failed") |
raymes | 5154d7f | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 164 | |
| 165 | # Mod image for test |
asharif | 8697d4e | 2013-02-15 09:18:09 +0000 | [diff] [blame] | 166 | ret = ExecuteCommandInChroot(options.chromeos_root, |
asharif | ca8c5ef | 2013-02-15 04:57:02 +0000 | [diff] [blame] | 167 | mod_image_command) |
raymes | 5154d7f | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 168 | |
raymes | bfb5799 | 2013-02-15 04:35:45 +0000 | [diff] [blame] | 169 | utils.AssertTrue(ret == 0, "mod_image_for_test failed") |
asharif | 8697d4e | 2013-02-15 09:18:09 +0000 | [diff] [blame] | 170 | |
| 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 |
raymes | 5154d7f | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 197 | |
| 198 | if __name__ == "__main__": |
asharif | 2198c51 | 2013-02-15 09:21:35 +0000 | [diff] [blame] | 199 | retval = Main(sys.argv) |
| 200 | sys.exit(retval) |