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