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() |
| 33 | |
raymes | 49fd5a3 | 2013-02-15 04:55:27 +0000 | [diff] [blame] | 34 | if toolchain_root is None: |
asharif | 708d57c | 2013-02-15 04:56:23 +0000 | [diff] [blame] | 35 | cmd_file = "enter_chroot.cmd" |
| 36 | cmd_file_path = chromeos_root + "/src/scripts/" + cmd_file |
| 37 | f = open(cmd_file_path, "w") |
| 38 | f.write(command) |
| 39 | logger.GetLogger().LogCmd(command) |
| 40 | f.close() |
| 41 | retval = cmd_executer.RunCommand("chmod +x " + cmd_file_path) |
| 42 | utils.AssertTrue(retval == 0, "chmod +x failed!") |
raymes | 49fd5a3 | 2013-02-15 04:55:27 +0000 | [diff] [blame] | 43 | return cmd_executer.RunCommand(chromeos_root + |
asharif | 708d57c | 2013-02-15 04:56:23 +0000 | [diff] [blame] | 44 | "/src/scripts/enter_chroot.sh -- ./%s" |
| 45 | % cmd_file) |
raymes | 49fd5a3 | 2013-02-15 04:55:27 +0000 | [diff] [blame] | 46 | else: |
| 47 | argv = [os.path.dirname(os.path.abspath(__file__)) + "/tc_enter_chroot.py", |
| 48 | "--chromeos_root=" + chromeos_root, |
| 49 | "--toolchain_root=" + toolchain_root, |
raymes | 49fd5a3 | 2013-02-15 04:55:27 +0000 | [diff] [blame] | 50 | "\n" + command] |
raymes | 81d8896 | 2013-02-15 04:56:26 +0000 | [diff] [blame] | 51 | if not full_mount: |
| 52 | argv.append("-s") |
raymes | 9b8305c | 2013-02-15 04:56:27 +0000 | [diff] [blame] | 53 | return tc_enter_chroot.Main(argv, return_output) |
asharif | e3668f1 | 2013-02-15 04:46:29 +0000 | [diff] [blame] | 54 | |
| 55 | |
| 56 | def MakeChroot(chromeos_root, clobber_chroot=False): |
| 57 | """Make a chroot given a chromeos checkout.""" |
| 58 | if (not os.path.isdir(chromeos_root + "/chroot") |
| 59 | or clobber_chroot): |
| 60 | commands = [] |
| 61 | commands.append("cd " + chromeos_root + "/src/scripts") |
| 62 | clobber_chroot = "" |
| 63 | if clobber_chroot: |
| 64 | clobber_chroot = "--replace" |
| 65 | commands.append("./make_chroot --fast " + clobber_chroot) |
asharif | 967d700 | 2013-02-15 04:51:00 +0000 | [diff] [blame] | 66 | ret = command_executer.GetCommandExecuter().RunCommands(commands) |
asharif | e3668f1 | 2013-02-15 04:46:29 +0000 | [diff] [blame] | 67 | utils.AssertTrue(ret == 0, "make_chroot failed") |
| 68 | else: |
raymes | 01959ae | 2013-02-15 04:50:07 +0000 | [diff] [blame] | 69 | logger.GetLogger().LogOutput("Did not make_chroot because it already exists") |
raymes | 5154d7f | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 70 | |
| 71 | |
bjanakiraman | 6496e5f | 2013-02-15 04:50:58 +0000 | [diff] [blame] | 72 | def Main(argv): |
raymes | 5154d7f | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 73 | """Build ChromeOS.""" |
| 74 | # Common initializations |
asharif | 5a9bb46 | 2013-02-15 04:50:57 +0000 | [diff] [blame] | 75 | cmd_executer = command_executer.GetCommandExecuter() |
raymes | 5154d7f | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 76 | |
| 77 | parser = optparse.OptionParser() |
| 78 | parser.add_option("--chromeos_root", dest="chromeos_root", |
| 79 | help="Target directory for ChromeOS installation.") |
| 80 | parser.add_option("--toolchain_root", dest="toolchain_root", |
| 81 | help="The gcctools directory of your P4 checkout.") |
| 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) |
raymes | 9b8305c | 2013-02-15 04:56:27 +0000 | [diff] [blame] | 88 | parser.add_option("--cflags", dest="cflags", default="", |
raymes | 5154d7f | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 89 | help="CFLAGS for the ChromeOS packages") |
raymes | 9b8305c | 2013-02-15 04:56:27 +0000 | [diff] [blame] | 90 | parser.add_option("--cxxflags", dest="cxxflags", default="", |
raymes | 5154d7f | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 91 | help="CXXFLAGS for the ChromeOS packages") |
raymes | 9b8305c | 2013-02-15 04:56:27 +0000 | [diff] [blame] | 92 | parser.add_option("--ldflags", dest="ldflags", default="", |
raymes | 5154d7f | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 93 | help="LDFLAGS for the ChromeOS packages") |
| 94 | parser.add_option("--board", dest="board", |
| 95 | help="ChromeOS target board, e.g. x86-generic") |
asharif | 0341d30 | 2013-02-15 04:56:38 +0000 | [diff] [blame^] | 96 | parser.add_option("--vanilla", dest="vanilla", |
asharif | b1752c8 | 2013-02-15 04:56:37 +0000 | [diff] [blame] | 97 | default=False, |
| 98 | action="store_true", |
asharif | 0341d30 | 2013-02-15 04:56:38 +0000 | [diff] [blame^] | 99 | help="Use default ChromeOS toolchain.") |
raymes | 5154d7f | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 100 | |
bjanakiraman | 6496e5f | 2013-02-15 04:50:58 +0000 | [diff] [blame] | 101 | options = parser.parse_args(argv[1:])[0] |
raymes | 5154d7f | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 102 | |
| 103 | if options.chromeos_root is None: |
| 104 | Usage(parser, "--chromeos_root must be set") |
| 105 | |
asharif | 0341d30 | 2013-02-15 04:56:38 +0000 | [diff] [blame^] | 106 | if options.toolchain_root is None and options.vanilla == False: |
raymes | 5154d7f | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 107 | Usage(parser, "--toolchain_root must be set") |
| 108 | |
| 109 | if options.board is None: |
| 110 | Usage(parser, "--board must be set") |
| 111 | |
asharif | e3668f1 | 2013-02-15 04:46:29 +0000 | [diff] [blame] | 112 | MakeChroot(options.chromeos_root, options.clobber_chroot) |
raymes | 5154d7f | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 113 | |
asharif | 0341d30 | 2013-02-15 04:56:38 +0000 | [diff] [blame^] | 114 | if options.vanilla == True: |
| 115 | command = "./setup_board --nousepkg --board=" + options.board |
| 116 | command += "&& ./build_packages --nousepkg --board=" + options.board |
| 117 | command += "&& ./build_image --nousepkg --board=" + options.board |
asharif | b1752c8 | 2013-02-15 04:56:37 +0000 | [diff] [blame] | 118 | command += "&& ./mod_image_for_test.sh --board=" + options.board |
| 119 | ret = ExecuteCommandInChroot(options.chromeos_root, None, command) |
| 120 | return ret |
| 121 | |
raymes | 5154d7f | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 122 | # Setup board |
raymes | 04164a1 | 2013-02-15 04:36:03 +0000 | [diff] [blame] | 123 | if not os.path.isdir(options.chromeos_root + "/chroot/build/" |
| 124 | + options.board) or options.clobber_board: |
raymes | 5f35b92 | 2013-02-15 04:35:57 +0000 | [diff] [blame] | 125 | force = "" |
| 126 | if options.clobber_board: |
| 127 | force = "--force" |
raymes | 04164a1 | 2013-02-15 04:36:03 +0000 | [diff] [blame] | 128 | # Run build_tc.py from binary package |
bjanakiraman | 6496e5f | 2013-02-15 04:50:58 +0000 | [diff] [blame] | 129 | rootdir = utils.GetRoot(argv[0])[0] |
raymes | 01959ae | 2013-02-15 04:50:07 +0000 | [diff] [blame] | 130 | ret = cmd_executer.RunCommand(rootdir + "/build_tc.py --chromeos_root=%s " |
| 131 | "--toolchain_root=%s --board=%s -B" |
| 132 | % (options.chromeos_root, |
| 133 | options.toolchain_root, options.board)) |
raymes | 04164a1 | 2013-02-15 04:36:03 +0000 | [diff] [blame] | 134 | utils.AssertTrue(ret == 0, "build_tc.py failed") |
raymes | 5f6be5f | 2013-02-15 04:36:13 +0000 | [diff] [blame] | 135 | version_number = utils.GetRoot(rootdir)[1] |
asharif | 541b639 | 2013-02-15 04:50:38 +0000 | [diff] [blame] | 136 | pkgdir = "/usr/local/toolchain_root/" + version_number + "/output/pkgs" |
raymes | 5f35b92 | 2013-02-15 04:35:57 +0000 | [diff] [blame] | 137 | ret = ExecuteCommandInChroot(options.chromeos_root, options.toolchain_root, |
raymes | 5f6be5f | 2013-02-15 04:36:13 +0000 | [diff] [blame] | 138 | "PKGDIR=%s ./setup_board --board=%s " |
| 139 | " --gcc_version=9999 " |
asharif | 1755b43 | 2013-02-15 04:55:29 +0000 | [diff] [blame] | 140 | " --binutils_version=9999 " |
raymes | 5f6be5f | 2013-02-15 04:36:13 +0000 | [diff] [blame] | 141 | "%s" % (pkgdir, options.board, force)) |
raymes | 5f35b92 | 2013-02-15 04:35:57 +0000 | [diff] [blame] | 142 | utils.AssertTrue(ret == 0, "setup_board failed") |
| 143 | else: |
raymes | 01959ae | 2013-02-15 04:50:07 +0000 | [diff] [blame] | 144 | logger.GetLogger().LogOutput("Did not setup_board " |
| 145 | "because it already exists") |
raymes | bfb5799 | 2013-02-15 04:35:45 +0000 | [diff] [blame] | 146 | |
raymes | 9b8305c | 2013-02-15 04:56:27 +0000 | [diff] [blame] | 147 | # Build packages |
raymes | bfb5799 | 2013-02-15 04:35:45 +0000 | [diff] [blame] | 148 | ret = ExecuteCommandInChroot(options.chromeos_root, options.toolchain_root, |
raymes | 9b8305c | 2013-02-15 04:56:27 +0000 | [diff] [blame] | 149 | "CFLAGS='%s' CXXFLAGS='%s' LDFLAGS='%s' " |
asharif | c0f7193 | 2013-02-15 04:56:18 +0000 | [diff] [blame] | 150 | "CHROME_ORIGIN=SERVER_SOURCE " |
asharif | b1752c8 | 2013-02-15 04:56:37 +0000 | [diff] [blame] | 151 | "./build_packages --withdev --nousepkg " |
raymes | bfb5799 | 2013-02-15 04:35:45 +0000 | [diff] [blame] | 152 | "--board=%s --withtest --withautotest" |
raymes | 9b8305c | 2013-02-15 04:56:27 +0000 | [diff] [blame] | 153 | % (options.cflags, options.cxxflags, |
| 154 | options.ldflags, options.board)) |
raymes | bfb5799 | 2013-02-15 04:35:45 +0000 | [diff] [blame] | 155 | |
| 156 | utils.AssertTrue(ret == 0, "build_packages failed") |
raymes | 5154d7f | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 157 | |
| 158 | # Build image |
raymes | bfb5799 | 2013-02-15 04:35:45 +0000 | [diff] [blame] | 159 | ret = ExecuteCommandInChroot(options.chromeos_root, options.toolchain_root, |
raymes | 81d8896 | 2013-02-15 04:56:26 +0000 | [diff] [blame] | 160 | "./build_image --yes --board=%s" % options.board) |
raymes | bfb5799 | 2013-02-15 04:35:45 +0000 | [diff] [blame] | 161 | |
| 162 | utils.AssertTrue(ret == 0, "build_image failed") |
raymes | 5154d7f | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 163 | |
| 164 | # Mod image for test |
raymes | bfb5799 | 2013-02-15 04:35:45 +0000 | [diff] [blame] | 165 | ret = ExecuteCommandInChroot(options.chromeos_root, options.toolchain_root, |
| 166 | "./mod_image_for_test.sh --board=%s" |
| 167 | % options.board) |
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") |
raymes | 5154d7f | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 170 | |
| 171 | if __name__ == "__main__": |
bjanakiraman | 6496e5f | 2013-02-15 04:50:58 +0000 | [diff] [blame] | 172 | Main(sys.argv) |