| bjanakiraman | 7f4a485 | 2013-02-15 04:35:28 +0000 | [diff] [blame] | 1 | #!/usr/bin/python2.6 | 
|  | 2 | # | 
|  | 3 | # Copyright 2010 Google Inc. All Rights Reserved. | 
|  | 4 |  | 
|  | 5 | """Script to build the ChromeOS toolchain. | 
|  | 6 |  | 
|  | 7 | This script sets up the toolchain if you give it the gcctools directory. | 
|  | 8 | """ | 
|  | 9 |  | 
|  | 10 | __author__ = "asharif@google.com (Ahmad Sharif)" | 
|  | 11 |  | 
| asharif | 80c6e55 | 2013-02-15 04:35:40 +0000 | [diff] [blame] | 12 | import getpass | 
| bjanakiraman | 7f4a485 | 2013-02-15 04:35:28 +0000 | [diff] [blame] | 13 | import optparse | 
| asharif | 1762130 | 2013-02-15 04:46:35 +0000 | [diff] [blame] | 14 | import os | 
| bjanakiraman | 7f4a485 | 2013-02-15 04:35:28 +0000 | [diff] [blame] | 15 | import sys | 
| asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 16 | import tc_enter_chroot | 
| asharif | e3668f1 | 2013-02-15 04:46:29 +0000 | [diff] [blame] | 17 | import build_chromeos | 
| asharif | 0d3535a | 2013-02-15 04:50:33 +0000 | [diff] [blame] | 18 | import setup_chromeos | 
| raymes | 01959ae | 2013-02-15 04:50:07 +0000 | [diff] [blame] | 19 | from utils import command_executer | 
| bjanakiraman | 7f4a485 | 2013-02-15 04:35:28 +0000 | [diff] [blame] | 20 | from utils import utils | 
| raymes | 01959ae | 2013-02-15 04:50:07 +0000 | [diff] [blame] | 21 | from utils import logger | 
| bjanakiraman | 7f4a485 | 2013-02-15 04:35:28 +0000 | [diff] [blame] | 22 |  | 
| asharif | 5a9bb46 | 2013-02-15 04:50:57 +0000 | [diff] [blame] | 23 |  | 
|  | 24 | cmd_executer = None | 
| bjanakiraman | 7f4a485 | 2013-02-15 04:35:28 +0000 | [diff] [blame] | 25 |  | 
| bjanakiraman | 7f4a485 | 2013-02-15 04:35:28 +0000 | [diff] [blame] | 26 |  | 
| asharif | 0d3535a | 2013-02-15 04:50:33 +0000 | [diff] [blame] | 27 | def Main(argv): | 
| asharif | 19c73dd | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 28 | """The main function.""" | 
| asharif | 5a9bb46 | 2013-02-15 04:50:57 +0000 | [diff] [blame] | 29 | # Common initializations | 
|  | 30 | global cmd_executer | 
|  | 31 | cmd_executer = command_executer.GetCommandExecuter() | 
| asharif | 0d3535a | 2013-02-15 04:50:33 +0000 | [diff] [blame] | 32 | rootdir = utils.GetRoot(sys.argv[0])[0] | 
|  | 33 |  | 
| asharif | 19c73dd | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 34 | parser = optparse.OptionParser() | 
|  | 35 | parser.add_option("-c", "--chromeos_root", dest="chromeos_root", | 
| asharif | 0d3535a | 2013-02-15 04:50:33 +0000 | [diff] [blame] | 36 | help=("ChromeOS root checkout directory" + | 
|  | 37 | " uses ../.. if none given.")) | 
| asharif | 19c73dd | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 38 | parser.add_option("-t", "--toolchain_root", dest="toolchain_root", | 
|  | 39 | help="Toolchain root directory.") | 
| asharif | d751e25 | 2013-02-15 04:35:52 +0000 | [diff] [blame] | 40 | parser.add_option("-b", "--board", dest="board", default="x86-generic", | 
| asharif | 19c73dd | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 41 | help="board is the argument to the setup_board command.") | 
| asharif | d751e25 | 2013-02-15 04:35:52 +0000 | [diff] [blame] | 42 | parser.add_option("-C", "--clean", dest="clean", default=False, | 
|  | 43 | action="store_true", | 
| asharif | e2cca30 | 2013-02-15 04:35:42 +0000 | [diff] [blame] | 44 | help="Uninstall the toolchain.") | 
| asharif | d751e25 | 2013-02-15 04:35:52 +0000 | [diff] [blame] | 45 | parser.add_option("-f", "--force", dest="force", default=False, | 
|  | 46 | action="store_true", | 
| asharif | e2cca30 | 2013-02-15 04:35:42 +0000 | [diff] [blame] | 47 | help="Do an uninstall/install cycle.") | 
| asharif | 19c73dd | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 48 | parser.add_option("-i", "--incremental", dest="incremental", | 
|  | 49 | help="The toolchain component that should be " | 
|  | 50 | "incrementally compiled.") | 
| asharif | e2cca30 | 2013-02-15 04:35:42 +0000 | [diff] [blame] | 51 | parser.add_option("-B", "--binary", dest="binary", | 
|  | 52 | action="store_true", default=False, | 
|  | 53 | help="The toolchain should use binaries stored in " | 
|  | 54 | "the install/ directory.") | 
| asharif | 0d3535a | 2013-02-15 04:50:33 +0000 | [diff] [blame] | 55 | parser.add_option("-s", "--setup-chromeos-options", | 
|  | 56 | dest="setup_chromeos_options", | 
|  | 57 | help="Additional options that should be passed on to" | 
|  | 58 | "the setup_chromeos script.") | 
|  | 59 | parser.add_option("-o", "--output", dest="output", | 
| asharif | 1ba8a3b | 2013-02-15 04:56:50 +0000 | [diff] [blame] | 60 | help="The output directory where logs,pkgs, etc. go. " | 
|  | 61 | "The default is the toolchain_root/output") | 
| bjanakiraman | 7f4a485 | 2013-02-15 04:35:28 +0000 | [diff] [blame] | 62 |  | 
| asharif | 0d3535a | 2013-02-15 04:50:33 +0000 | [diff] [blame] | 63 | options = parser.parse_args(argv)[0] | 
| asharif | 1762130 | 2013-02-15 04:46:35 +0000 | [diff] [blame] | 64 |  | 
| asharif | 1755b43 | 2013-02-15 04:55:29 +0000 | [diff] [blame] | 65 | if (options.clean == False and | 
|  | 66 | (options.toolchain_root is None or options.board is None)): | 
| asharif | 19c73dd | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 67 | parser.print_help() | 
|  | 68 | sys.exit() | 
| bjanakiraman | 7f4a485 | 2013-02-15 04:35:28 +0000 | [diff] [blame] | 69 |  | 
| asharif | 0d3535a | 2013-02-15 04:50:33 +0000 | [diff] [blame] | 70 | if options.chromeos_root is None: | 
|  | 71 | if os.path.exists("enter_chroot.sh"): | 
|  | 72 | options.chromeos_root = "../.." | 
|  | 73 | else: | 
|  | 74 | logger.GetLogger().LogError("--chromeos_root not given") | 
|  | 75 | parser.print_help() | 
|  | 76 | sys.exit() | 
|  | 77 | else: | 
|  | 78 | options.chromeos_root = os.path.expanduser(options.chromeos_root) | 
|  | 79 |  | 
| asharif | 88f0142 | 2013-02-15 04:50:35 +0000 | [diff] [blame] | 80 | if ((not os.path.exists(options.chromeos_root)) or | 
|  | 81 | (not os.path.exists(options.chromeos_root + | 
|  | 82 | "/src/scripts/enter_chroot.sh"))): | 
| asharif | 0d3535a | 2013-02-15 04:50:33 +0000 | [diff] [blame] | 83 | logger.GetLogger().LogOutput("Creating a chromeos checkout at: %s" % | 
|  | 84 | options.chromeos_root) | 
| asharif | 88f0142 | 2013-02-15 04:50:35 +0000 | [diff] [blame] | 85 | sc_args = [] | 
| asharif | 6ba78c4 | 2013-02-15 04:50:40 +0000 | [diff] [blame] | 86 | sc_args.append("--minilayout") | 
| asharif | 88f0142 | 2013-02-15 04:50:35 +0000 | [diff] [blame] | 87 | sc_args.append("--dir=%s" % options.chromeos_root) | 
|  | 88 | if options.setup_chromeos_options: | 
|  | 89 | sc_args.append(options.setup_chromeos_options) | 
| asharif | 0d3535a | 2013-02-15 04:50:33 +0000 | [diff] [blame] | 90 | setup_chromeos.Main(sc_args) | 
|  | 91 |  | 
| asharif | 1ba8a3b | 2013-02-15 04:56:50 +0000 | [diff] [blame] | 92 | if options.output is None: | 
|  | 93 | output = options.toolchain_root + "/output" | 
|  | 94 | else: | 
|  | 95 | output = options.output | 
| asharif | 0d3535a | 2013-02-15 04:50:33 +0000 | [diff] [blame] | 96 |  | 
|  | 97 | if output.startswith("/") == False: | 
|  | 98 | output = os.getcwd() + "/" + output | 
|  | 99 | else: | 
|  | 100 | output = os.path.expanduser(output) | 
|  | 101 |  | 
|  | 102 | chroot_mount = "/usr/local/toolchain_root/" | 
|  | 103 | chroot_base = utils.GetRoot(output)[1] | 
|  | 104 | chroot_output = chroot_mount + chroot_base | 
|  | 105 |  | 
|  | 106 | tc_enter_chroot_options = [] | 
| asharif | 541b639 | 2013-02-15 04:50:38 +0000 | [diff] [blame] | 107 | output_mount = ("--output=" + output) | 
| asharif | 0d3535a | 2013-02-15 04:50:33 +0000 | [diff] [blame] | 108 | tc_enter_chroot_options.append(output_mount) | 
|  | 109 |  | 
| asharif | e3668f1 | 2013-02-15 04:46:29 +0000 | [diff] [blame] | 110 | build_chromeos.MakeChroot(options.chromeos_root) | 
|  | 111 |  | 
| asharif | e2cca30 | 2013-02-15 04:35:42 +0000 | [diff] [blame] | 112 | portage_flags = "" | 
|  | 113 | if options.binary == True: | 
|  | 114 | # FIXME(asharif): This should be using --usepkg but that was not working. | 
|  | 115 | portage_flags = "--usepkgonly" | 
| asharif | 1755b43 | 2013-02-15 04:55:29 +0000 | [diff] [blame] | 116 | tc_enter_chroot_options.append("-s") | 
| asharif | e2cca30 | 2013-02-15 04:35:42 +0000 | [diff] [blame] | 117 |  | 
| asharif | 19c73dd | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 118 | f = open(options.chromeos_root + "/src/overlays/overlay-" + | 
| asharif | 1755b43 | 2013-02-15 04:55:29 +0000 | [diff] [blame] | 119 | options.board.split("_")[0] + "/toolchain.conf", "r") | 
| asharif | 19c73dd | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 120 | target = f.read() | 
|  | 121 | f.close() | 
|  | 122 | target = target.strip() | 
| asharif | 2d815d0 | 2013-02-15 04:36:02 +0000 | [diff] [blame] | 123 | features = "noclean userfetch userpriv usersandbox -strict" | 
| asharif | 09bfb6f | 2013-02-15 04:35:44 +0000 | [diff] [blame] | 124 | if options.incremental is not None and options.incremental: | 
|  | 125 | features += " keepwork" | 
| asharif | 19c73dd | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 126 | env = CreateEnvVarString(" FEATURES", features) | 
| asharif | 80c6e55 | 2013-02-15 04:35:40 +0000 | [diff] [blame] | 127 | env += CreateEnvVarString(" PORTAGE_USERNAME", getpass.getuser()) | 
| asharif | 0d3535a | 2013-02-15 04:50:33 +0000 | [diff] [blame] | 128 | logdir = "/logs" | 
|  | 129 | pkgdir = "/pkgs" | 
|  | 130 | tmpdir = "/objects" | 
|  | 131 | installdir = "/install" | 
|  | 132 | package_dir = output + pkgdir | 
|  | 133 | portage_logdir = chroot_output + logdir | 
|  | 134 | portage_pkgdir = chroot_output + pkgdir | 
|  | 135 | portage_tmpdir = chroot_output + tmpdir | 
|  | 136 | env += CreateEnvVarString(" PORT_LOGDIR", portage_logdir) | 
|  | 137 | env += CreateEnvVarString(" PKGDIR", portage_pkgdir) | 
|  | 138 | env += CreateEnvVarString(" PORTAGE_BINHOST", portage_pkgdir) | 
| asharif | 2dfbf51 | 2013-02-15 05:15:49 +0000 | [diff] [blame^] | 139 | if options.binary == False: | 
|  | 140 | env += CreateEnvVarString(" PORTAGE_TMPDIR", portage_tmpdir) | 
| asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 141 | env += CreateEnvVarString(" USE", "mounted_sources") | 
| asharif | 2d815d0 | 2013-02-15 04:36:02 +0000 | [diff] [blame] | 142 |  | 
| asharif | d751e25 | 2013-02-15 04:35:52 +0000 | [diff] [blame] | 143 | retval = 0 | 
| asharif | 80c6e55 | 2013-02-15 04:35:40 +0000 | [diff] [blame] | 144 | if options.force == True: | 
| asharif | d751e25 | 2013-02-15 04:35:52 +0000 | [diff] [blame] | 145 | retval = BuildTC(options.chromeos_root, options.toolchain_root, env, | 
| asharif | 0d3535a | 2013-02-15 04:50:33 +0000 | [diff] [blame] | 146 | target, True, options.incremental, portage_flags, | 
|  | 147 | tc_enter_chroot_options) | 
| asharif | d751e25 | 2013-02-15 04:35:52 +0000 | [diff] [blame] | 148 | retval = BuildTC(options.chromeos_root, options.toolchain_root, env, | 
| asharif | 0d3535a | 2013-02-15 04:50:33 +0000 | [diff] [blame] | 149 | target, options.clean, options.incremental, portage_flags, | 
|  | 150 | tc_enter_chroot_options) | 
| asharif | d751e25 | 2013-02-15 04:35:52 +0000 | [diff] [blame] | 151 | utils.AssertTrue(retval == 0, "Build toolchain failed!") | 
| asharif | 0d3535a | 2013-02-15 04:50:33 +0000 | [diff] [blame] | 152 | command = "sudo chown -R " + getpass.getuser() + " " + package_dir | 
| asharif | d751e25 | 2013-02-15 04:35:52 +0000 | [diff] [blame] | 153 |  | 
|  | 154 | if options.incremental is None and not options.clean: | 
| asharif | 0d3535a | 2013-02-15 04:50:33 +0000 | [diff] [blame] | 155 | install_dir = output + installdir | 
| asharif | d751e25 | 2013-02-15 04:35:52 +0000 | [diff] [blame] | 156 | retval = InstallTC(package_dir, install_dir) | 
|  | 157 | utils.AssertTrue(retval == 0, "Installation of the toolchain failed!") | 
|  | 158 |  | 
|  | 159 | return retval | 
| asharif | e2cca30 | 2013-02-15 04:35:42 +0000 | [diff] [blame] | 160 |  | 
|  | 161 |  | 
|  | 162 | def CreateCrossdevPortageFlags(portage_flags): | 
| asharif | 2d815d0 | 2013-02-15 04:36:02 +0000 | [diff] [blame] | 163 | portage_flags = portage_flags.strip() | 
| asharif | e2cca30 | 2013-02-15 04:35:42 +0000 | [diff] [blame] | 164 | if not portage_flags: | 
|  | 165 | return "" | 
|  | 166 | crossdev_flags = " --portage " | 
|  | 167 | crossdev_flags += " --portage ".join(portage_flags.split(" ")) | 
|  | 168 | return crossdev_flags | 
| asharif | 19c73dd | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 169 |  | 
|  | 170 |  | 
|  | 171 | def CreateEnvVarString(variable, value): | 
| asharif | c0f7193 | 2013-02-15 04:56:18 +0000 | [diff] [blame] | 172 | return variable + "=\"" + value + "\"" | 
| asharif | 19c73dd | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 173 |  | 
|  | 174 |  | 
|  | 175 | def EscapeQuoteString(string): | 
|  | 176 | return "\\\"" + string + "\\\"" | 
|  | 177 |  | 
|  | 178 |  | 
| asharif | d751e25 | 2013-02-15 04:35:52 +0000 | [diff] [blame] | 179 | def InstallTC(package_dir, install_dir): | 
| asharif | 9994d29 | 2013-02-15 04:36:04 +0000 | [diff] [blame] | 180 | command = ("mkdir -p " + install_dir) | 
|  | 181 | command += ("&& for f in $(find " + package_dir + | 
| asharif | fcf8cfc | 2013-02-15 04:49:21 +0000 | [diff] [blame] | 182 | " -name \\*.tbz2); do tar xf $f -C " + | 
|  | 183 | install_dir + " ; done") | 
| raymes | 01959ae | 2013-02-15 04:50:07 +0000 | [diff] [blame] | 184 | retval = cmd_executer.RunCommand(command) | 
| asharif | d751e25 | 2013-02-15 04:35:52 +0000 | [diff] [blame] | 185 | return retval | 
|  | 186 |  | 
|  | 187 |  | 
| asharif | 19c73dd | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 188 | def BuildTC(chromeos_root, toolchain_root, env, target, uninstall, | 
| asharif | 0d3535a | 2013-02-15 04:50:33 +0000 | [diff] [blame] | 189 | incremental_component, portage_flags, tc_enter_chroot_options): | 
| asharif | 19c73dd | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 190 | """Build the toolchain.""" | 
| asharif | 2d815d0 | 2013-02-15 04:36:02 +0000 | [diff] [blame] | 191 | portage_flags = portage_flags.strip() | 
|  | 192 | portage_flags += " -b " | 
|  | 193 |  | 
| asharif | 28238cf | 2013-02-15 04:51:01 +0000 | [diff] [blame] | 194 | binutils_version = "9999" | 
| asharif | 19c73dd | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 195 | gcc_version = "9999" | 
| asharif | 1762130 | 2013-02-15 04:46:35 +0000 | [diff] [blame] | 196 | libc_version = "2.10.1-r2" | 
| asharif | 19c73dd | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 197 | kernel_version = "2.6.30-r1" | 
| asharif | 19c73dd | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 198 |  | 
| raymes | 01959ae | 2013-02-15 04:50:07 +0000 | [diff] [blame] | 199 | rootdir = utils.GetRoot(sys.argv[0])[0] | 
| asharif | 2d815d0 | 2013-02-15 04:36:02 +0000 | [diff] [blame] | 200 | env += " " | 
|  | 201 |  | 
| asharif | 19c73dd | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 202 | if uninstall == True: | 
|  | 203 | tflag = " -C " | 
|  | 204 | else: | 
|  | 205 | tflag = " -t " | 
|  | 206 |  | 
| asharif | 0269d46 | 2013-02-15 04:46:31 +0000 | [diff] [blame] | 207 | command = " -- sudo " + env | 
| asharif | 2d815d0 | 2013-02-15 04:36:02 +0000 | [diff] [blame] | 208 |  | 
|  | 209 | if uninstall == True: | 
|  | 210 | command += " crossdev " + tflag + target | 
| asharif | 1755b43 | 2013-02-15 04:55:29 +0000 | [diff] [blame] | 211 | enter_chroot = chromeos_root + "/src/scripts/enter_chroot.sh" | 
|  | 212 | retval = cmd_executer.RunCommand(enter_chroot + command) | 
| asharif | 2d815d0 | 2013-02-15 04:36:02 +0000 | [diff] [blame] | 213 | return retval | 
| asharif | 19c73dd | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 214 |  | 
|  | 215 | if incremental_component == "binutils": | 
| asharif | e2cca30 | 2013-02-15 04:35:42 +0000 | [diff] [blame] | 216 | command += (" emerge =cross-" + target + "/binutils-" + binutils_version + | 
|  | 217 | portage_flags) | 
| asharif | 19c73dd | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 218 | elif incremental_component == "gcc": | 
| asharif | e2cca30 | 2013-02-15 04:35:42 +0000 | [diff] [blame] | 219 | command += (" emerge =cross-" + target + "/gcc-" + gcc_version + | 
|  | 220 | portage_flags) | 
| asharif | 19c73dd | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 221 | elif incremental_component == "libc" or incremental_component == "glibc": | 
| asharif | e2cca30 | 2013-02-15 04:35:42 +0000 | [diff] [blame] | 222 | command += (" emerge =cross-" + target + "/glibc-" + libc_version + | 
|  | 223 | portage_flags) | 
| asharif | 19c73dd | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 224 | else: | 
| asharif | 2d815d0 | 2013-02-15 04:36:02 +0000 | [diff] [blame] | 225 | crossdev_flags = CreateCrossdevPortageFlags(portage_flags) | 
| asharif | 19c73dd | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 226 | command += (" crossdev -v " + tflag + target + | 
|  | 227 | " --binutils " + binutils_version + | 
|  | 228 | " --libc " + libc_version + | 
|  | 229 | " --gcc " + gcc_version + | 
|  | 230 | " --kernel " + kernel_version + | 
| asharif | 2d815d0 | 2013-02-15 04:36:02 +0000 | [diff] [blame] | 231 | crossdev_flags) | 
| asharif | c0f7193 | 2013-02-15 04:56:18 +0000 | [diff] [blame] | 232 | command += ("&& sudo cp -r $(" + env + " portageq envvar PKGDIR)/*" + | 
|  | 233 | " /var/lib/portage/pkgs/") | 
| asharif | 19c73dd | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 234 |  | 
| asharif | 1755b43 | 2013-02-15 04:55:29 +0000 | [diff] [blame] | 235 | argv = [rootdir + "/tc_enter_chroot.py", | 
|  | 236 | "--chromeos_root=" + chromeos_root, | 
|  | 237 | "--toolchain_root=" + toolchain_root] | 
|  | 238 | argv += tc_enter_chroot_options | 
|  | 239 |  | 
| asharif | e3668f1 | 2013-02-15 04:46:29 +0000 | [diff] [blame] | 240 | argv.append(command) | 
|  | 241 | retval = tc_enter_chroot.Main(argv) | 
| asharif | 19c73dd | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 242 | return retval | 
|  | 243 |  | 
|  | 244 | if __name__ == "__main__": | 
| asharif | 0d3535a | 2013-02-15 04:50:33 +0000 | [diff] [blame] | 245 | Main(sys.argv) | 
| asharif | d751e25 | 2013-02-15 04:35:52 +0000 | [diff] [blame] | 246 |  |