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, |
asharif | 1755b43 | 2013-02-15 04:55:29 +0000 | [diff] [blame] | 29 | return_output=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, |
asharif | 1755b43 | 2013-02-15 04:55:29 +0000 | [diff] [blame] | 50 | "-s", |
raymes | 49fd5a3 | 2013-02-15 04:55:27 +0000 | [diff] [blame] | 51 | "\n" + command] |
| 52 | return tc_enter_chroot.Main(argv) |
asharif | e3668f1 | 2013-02-15 04:46:29 +0000 | [diff] [blame] | 53 | |
| 54 | |
| 55 | def MakeChroot(chromeos_root, clobber_chroot=False): |
| 56 | """Make a chroot given a chromeos checkout.""" |
| 57 | if (not os.path.isdir(chromeos_root + "/chroot") |
| 58 | or clobber_chroot): |
| 59 | commands = [] |
| 60 | commands.append("cd " + chromeos_root + "/src/scripts") |
| 61 | clobber_chroot = "" |
| 62 | if clobber_chroot: |
| 63 | clobber_chroot = "--replace" |
| 64 | commands.append("./make_chroot --fast " + clobber_chroot) |
asharif | 967d700 | 2013-02-15 04:51:00 +0000 | [diff] [blame] | 65 | ret = command_executer.GetCommandExecuter().RunCommands(commands) |
asharif | e3668f1 | 2013-02-15 04:46:29 +0000 | [diff] [blame] | 66 | utils.AssertTrue(ret == 0, "make_chroot failed") |
| 67 | else: |
raymes | 01959ae | 2013-02-15 04:50:07 +0000 | [diff] [blame] | 68 | logger.GetLogger().LogOutput("Did not make_chroot because it already exists") |
raymes | 5154d7f | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 69 | |
| 70 | |
bjanakiraman | 6496e5f | 2013-02-15 04:50:58 +0000 | [diff] [blame] | 71 | def Main(argv): |
raymes | 5154d7f | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 72 | """Build ChromeOS.""" |
| 73 | # Common initializations |
asharif | 5a9bb46 | 2013-02-15 04:50:57 +0000 | [diff] [blame] | 74 | cmd_executer = command_executer.GetCommandExecuter() |
raymes | 5154d7f | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 75 | |
| 76 | parser = optparse.OptionParser() |
| 77 | parser.add_option("--chromeos_root", dest="chromeos_root", |
| 78 | help="Target directory for ChromeOS installation.") |
| 79 | parser.add_option("--toolchain_root", dest="toolchain_root", |
| 80 | help="The gcctools directory of your P4 checkout.") |
| 81 | parser.add_option("--clobber_chroot", dest="clobber_chroot", |
| 82 | action="store_true", help= |
| 83 | "Delete the chroot and start fresh", default=False) |
| 84 | parser.add_option("--clobber_board", dest="clobber_board", |
| 85 | action="store_true", |
| 86 | help="Delete the board and start fresh", default=False) |
| 87 | parser.add_option("--cflags", dest="cflags", |
| 88 | help="CFLAGS for the ChromeOS packages") |
| 89 | parser.add_option("--cxxflags", dest="cxxflags", |
| 90 | help="CXXFLAGS for the ChromeOS packages") |
| 91 | parser.add_option("--ldflags", dest="ldflags", |
| 92 | help="LDFLAGS for the ChromeOS packages") |
| 93 | parser.add_option("--board", dest="board", |
| 94 | help="ChromeOS target board, e.g. x86-generic") |
| 95 | |
bjanakiraman | 6496e5f | 2013-02-15 04:50:58 +0000 | [diff] [blame] | 96 | options = parser.parse_args(argv[1:])[0] |
raymes | 5154d7f | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 97 | |
| 98 | if options.chromeos_root is None: |
| 99 | Usage(parser, "--chromeos_root must be set") |
| 100 | |
| 101 | if options.toolchain_root is None: |
| 102 | Usage(parser, "--toolchain_root must be set") |
| 103 | |
| 104 | if options.board is None: |
| 105 | Usage(parser, "--board must be set") |
| 106 | |
asharif | e3668f1 | 2013-02-15 04:46:29 +0000 | [diff] [blame] | 107 | MakeChroot(options.chromeos_root, options.clobber_chroot) |
raymes | 5154d7f | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 108 | |
| 109 | # Setup board |
raymes | 04164a1 | 2013-02-15 04:36:03 +0000 | [diff] [blame] | 110 | if not os.path.isdir(options.chromeos_root + "/chroot/build/" |
| 111 | + options.board) or options.clobber_board: |
raymes | 5f35b92 | 2013-02-15 04:35:57 +0000 | [diff] [blame] | 112 | force = "" |
| 113 | if options.clobber_board: |
| 114 | force = "--force" |
raymes | 04164a1 | 2013-02-15 04:36:03 +0000 | [diff] [blame] | 115 | # Run build_tc.py from binary package |
bjanakiraman | 6496e5f | 2013-02-15 04:50:58 +0000 | [diff] [blame] | 116 | rootdir = utils.GetRoot(argv[0])[0] |
raymes | 01959ae | 2013-02-15 04:50:07 +0000 | [diff] [blame] | 117 | ret = cmd_executer.RunCommand(rootdir + "/build_tc.py --chromeos_root=%s " |
| 118 | "--toolchain_root=%s --board=%s -B" |
| 119 | % (options.chromeos_root, |
| 120 | options.toolchain_root, options.board)) |
raymes | 04164a1 | 2013-02-15 04:36:03 +0000 | [diff] [blame] | 121 | utils.AssertTrue(ret == 0, "build_tc.py failed") |
raymes | 5f6be5f | 2013-02-15 04:36:13 +0000 | [diff] [blame] | 122 | version_number = utils.GetRoot(rootdir)[1] |
asharif | 541b639 | 2013-02-15 04:50:38 +0000 | [diff] [blame] | 123 | pkgdir = "/usr/local/toolchain_root/" + version_number + "/output/pkgs" |
raymes | 5f35b92 | 2013-02-15 04:35:57 +0000 | [diff] [blame] | 124 | ret = ExecuteCommandInChroot(options.chromeos_root, options.toolchain_root, |
raymes | 5f6be5f | 2013-02-15 04:36:13 +0000 | [diff] [blame] | 125 | "PKGDIR=%s ./setup_board --board=%s " |
| 126 | " --gcc_version=9999 " |
asharif | 1755b43 | 2013-02-15 04:55:29 +0000 | [diff] [blame] | 127 | " --binutils_version=9999 " |
raymes | 5f6be5f | 2013-02-15 04:36:13 +0000 | [diff] [blame] | 128 | "%s" % (pkgdir, options.board, force)) |
raymes | 5f35b92 | 2013-02-15 04:35:57 +0000 | [diff] [blame] | 129 | utils.AssertTrue(ret == 0, "setup_board failed") |
| 130 | else: |
raymes | 01959ae | 2013-02-15 04:50:07 +0000 | [diff] [blame] | 131 | logger.GetLogger().LogOutput("Did not setup_board " |
| 132 | "because it already exists") |
raymes | bfb5799 | 2013-02-15 04:35:45 +0000 | [diff] [blame] | 133 | |
| 134 | # Modify make.conf to add CFLAGS/CXXFLAGS/LDFLAGS |
raymes | bfb5799 | 2013-02-15 04:35:45 +0000 | [diff] [blame] | 135 | ret1 = ExecuteCommandInChroot(options.chromeos_root, options.toolchain_root, |
| 136 | "[ -e /build/%s/etc/make.conf.orig ] || " |
| 137 | "sudo mv /build/%s/etc/make.conf " |
| 138 | "/build/%s/etc/make.conf.orig" |
| 139 | % (options.board, options.board, options.board)) |
asharif | 541b639 | 2013-02-15 04:50:38 +0000 | [diff] [blame] | 140 | makeconf = ("source make.conf.orig\n") |
raymes | bfb5799 | 2013-02-15 04:35:45 +0000 | [diff] [blame] | 141 | #"CFLAGS='%s'\\\nCXXFLAGS='%s'\\\nLDFLAGS='%s'\\\n" % |
| 142 | #(options.cflags, options.cxxflags, options.ldflags)) |
raymes | e91a6e6 | 2013-02-15 04:35:51 +0000 | [diff] [blame] | 143 | ret2 = ExecuteCommandInChroot(options.chromeos_root, options.toolchain_root, |
asharif | 541b639 | 2013-02-15 04:50:38 +0000 | [diff] [blame] | 144 | "if [ -e /build/%s/etc/make.conf.orig ] ; " |
| 145 | "then sudo echo -e \"%s\" | sudo tee " |
raymes | e91a6e6 | 2013-02-15 04:35:51 +0000 | [diff] [blame] | 146 | "/build/%s/etc/make.conf > /dev/null ;" |
asharif | 541b639 | 2013-02-15 04:50:38 +0000 | [diff] [blame] | 147 | "else exit 1 ; fi" |
raymes | 5f6be5f | 2013-02-15 04:36:13 +0000 | [diff] [blame] | 148 | % (options.board, makeconf, options.board)) |
raymes | 5f35b92 | 2013-02-15 04:35:57 +0000 | [diff] [blame] | 149 | |
raymes | bfb5799 | 2013-02-15 04:35:45 +0000 | [diff] [blame] | 150 | utils.AssertTrue(ret1 == 0 and ret2 == 0, "Could not modify make.conf") |
raymes | 5154d7f | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 151 | |
asharif | c0f7193 | 2013-02-15 04:56:18 +0000 | [diff] [blame] | 152 | # Build packages |
raymes | bfb5799 | 2013-02-15 04:35:45 +0000 | [diff] [blame] | 153 | ret = ExecuteCommandInChroot(options.chromeos_root, options.toolchain_root, |
asharif | c0f7193 | 2013-02-15 04:56:18 +0000 | [diff] [blame] | 154 | "CHROME_ORIGIN=SERVER_SOURCE " |
raymes | bfb5799 | 2013-02-15 04:35:45 +0000 | [diff] [blame] | 155 | "./build_packages --withdev " |
| 156 | "--board=%s --withtest --withautotest" |
asharif | c0f7193 | 2013-02-15 04:56:18 +0000 | [diff] [blame] | 157 | % (options.board)) |
raymes | bfb5799 | 2013-02-15 04:35:45 +0000 | [diff] [blame] | 158 | |
| 159 | utils.AssertTrue(ret == 0, "build_packages failed") |
raymes | 5154d7f | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 160 | |
| 161 | # Build image |
raymes | bfb5799 | 2013-02-15 04:35:45 +0000 | [diff] [blame] | 162 | ret = ExecuteCommandInChroot(options.chromeos_root, options.toolchain_root, |
| 163 | "./build_image --board=%s" % options.board) |
| 164 | |
| 165 | utils.AssertTrue(ret == 0, "build_image failed") |
raymes | 5154d7f | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 166 | |
| 167 | # Mod image for test |
raymes | bfb5799 | 2013-02-15 04:35:45 +0000 | [diff] [blame] | 168 | ret = ExecuteCommandInChroot(options.chromeos_root, options.toolchain_root, |
| 169 | "./mod_image_for_test.sh --board=%s" |
| 170 | % options.board) |
raymes | 5154d7f | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 171 | |
raymes | bfb5799 | 2013-02-15 04:35:45 +0000 | [diff] [blame] | 172 | utils.AssertTrue(ret == 0, "mod_image_for_test failed") |
raymes | 5154d7f | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 173 | |
| 174 | if __name__ == "__main__": |
bjanakiraman | 6496e5f | 2013-02-15 04:50:58 +0000 | [diff] [blame] | 175 | Main(sys.argv) |