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 |
raymes | 01959ae | 2013-02-15 04:50:07 +0000 | [diff] [blame^] | 18 | from utils import command_executer |
bjanakiraman | 7f4a485 | 2013-02-15 04:35:28 +0000 | [diff] [blame] | 19 | from utils import utils |
raymes | 01959ae | 2013-02-15 04:50:07 +0000 | [diff] [blame^] | 20 | from utils import logger |
bjanakiraman | 7f4a485 | 2013-02-15 04:35:28 +0000 | [diff] [blame] | 21 | |
| 22 | # Common initializations |
raymes | 01959ae | 2013-02-15 04:50:07 +0000 | [diff] [blame^] | 23 | logger.InitLogger(sys.argv[0]) |
| 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 | 19c73dd | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 27 | def Main(): |
| 28 | """The main function.""" |
| 29 | parser = optparse.OptionParser() |
| 30 | parser.add_option("-c", "--chromeos_root", dest="chromeos_root", |
asharif | d751e25 | 2013-02-15 04:35:52 +0000 | [diff] [blame] | 31 | default="../..", |
asharif | 19c73dd | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 32 | help="ChromeOS root checkout directory.") |
| 33 | parser.add_option("-t", "--toolchain_root", dest="toolchain_root", |
| 34 | help="Toolchain root directory.") |
asharif | d751e25 | 2013-02-15 04:35:52 +0000 | [diff] [blame] | 35 | parser.add_option("-b", "--board", dest="board", default="x86-generic", |
asharif | 19c73dd | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 36 | help="board is the argument to the setup_board command.") |
asharif | d751e25 | 2013-02-15 04:35:52 +0000 | [diff] [blame] | 37 | parser.add_option("-C", "--clean", dest="clean", default=False, |
| 38 | action="store_true", |
asharif | e2cca30 | 2013-02-15 04:35:42 +0000 | [diff] [blame] | 39 | help="Uninstall the toolchain.") |
asharif | d751e25 | 2013-02-15 04:35:52 +0000 | [diff] [blame] | 40 | parser.add_option("-f", "--force", dest="force", default=False, |
| 41 | action="store_true", |
asharif | e2cca30 | 2013-02-15 04:35:42 +0000 | [diff] [blame] | 42 | help="Do an uninstall/install cycle.") |
asharif | 19c73dd | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 43 | parser.add_option("-i", "--incremental", dest="incremental", |
| 44 | help="The toolchain component that should be " |
| 45 | "incrementally compiled.") |
asharif | e2cca30 | 2013-02-15 04:35:42 +0000 | [diff] [blame] | 46 | parser.add_option("-B", "--binary", dest="binary", |
| 47 | action="store_true", default=False, |
| 48 | help="The toolchain should use binaries stored in " |
| 49 | "the install/ directory.") |
bjanakiraman | 7f4a485 | 2013-02-15 04:35:28 +0000 | [diff] [blame] | 50 | |
asharif | 19c73dd | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 51 | options = parser.parse_args()[0] |
bjanakiraman | 7f4a485 | 2013-02-15 04:35:28 +0000 | [diff] [blame] | 52 | |
asharif | 1762130 | 2013-02-15 04:46:35 +0000 | [diff] [blame] | 53 | options.chromeos_root = os.path.expanduser(options.chromeos_root) |
| 54 | |
asharif | 19c73dd | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 55 | if options.toolchain_root is None or options.board is None: |
| 56 | parser.print_help() |
| 57 | sys.exit() |
bjanakiraman | 7f4a485 | 2013-02-15 04:35:28 +0000 | [diff] [blame] | 58 | |
asharif | e3668f1 | 2013-02-15 04:46:29 +0000 | [diff] [blame] | 59 | build_chromeos.MakeChroot(options.chromeos_root) |
| 60 | |
asharif | e2cca30 | 2013-02-15 04:35:42 +0000 | [diff] [blame] | 61 | portage_flags = "" |
| 62 | if options.binary == True: |
| 63 | # FIXME(asharif): This should be using --usepkg but that was not working. |
| 64 | portage_flags = "--usepkgonly" |
| 65 | |
asharif | 19c73dd | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 66 | f = open(options.chromeos_root + "/src/overlays/overlay-" + |
| 67 | options.board + "/toolchain.conf", "r") |
| 68 | target = f.read() |
| 69 | f.close() |
| 70 | target = target.strip() |
asharif | 2d815d0 | 2013-02-15 04:36:02 +0000 | [diff] [blame] | 71 | features = "noclean userfetch userpriv usersandbox -strict" |
asharif | 09bfb6f | 2013-02-15 04:35:44 +0000 | [diff] [blame] | 72 | if options.incremental is not None and options.incremental: |
| 73 | features += " keepwork" |
asharif | 19c73dd | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 74 | env = CreateEnvVarString(" FEATURES", features) |
asharif | 80c6e55 | 2013-02-15 04:35:40 +0000 | [diff] [blame] | 75 | env += CreateEnvVarString(" PORTAGE_USERNAME", getpass.getuser()) |
raymes | 01959ae | 2013-02-15 04:50:07 +0000 | [diff] [blame^] | 76 | rootdir = utils.GetRoot(sys.argv[0])[0] |
asharif | 80c6e55 | 2013-02-15 04:35:40 +0000 | [diff] [blame] | 77 | version_number = utils.GetRoot(rootdir)[1] |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 78 | version_dir = "/usr/local/toolchain_root/" + version_number |
asharif | 80c6e55 | 2013-02-15 04:35:40 +0000 | [diff] [blame] | 79 | env += CreateEnvVarString(" PORT_LOGDIR", version_dir + "/logs") |
asharif | d751e25 | 2013-02-15 04:35:52 +0000 | [diff] [blame] | 80 | env += CreateEnvVarString(" PKGDIR", version_dir + "/pkgs") |
asharif | 2d815d0 | 2013-02-15 04:36:02 +0000 | [diff] [blame] | 81 | env += CreateEnvVarString(" PORTAGE_BINHOST", version_dir + "/pkgs") |
asharif | 80c6e55 | 2013-02-15 04:35:40 +0000 | [diff] [blame] | 82 | env += CreateEnvVarString(" PORTAGE_TMPDIR", version_dir + "/objects") |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 83 | env += CreateEnvVarString(" USE", "mounted_sources") |
asharif | 2d815d0 | 2013-02-15 04:36:02 +0000 | [diff] [blame] | 84 | |
asharif | d751e25 | 2013-02-15 04:35:52 +0000 | [diff] [blame] | 85 | retval = 0 |
asharif | 80c6e55 | 2013-02-15 04:35:40 +0000 | [diff] [blame] | 86 | if options.force == True: |
asharif | d751e25 | 2013-02-15 04:35:52 +0000 | [diff] [blame] | 87 | retval = BuildTC(options.chromeos_root, options.toolchain_root, env, |
| 88 | target, True, options.incremental, portage_flags) |
asharif | d751e25 | 2013-02-15 04:35:52 +0000 | [diff] [blame] | 89 | retval = BuildTC(options.chromeos_root, options.toolchain_root, env, |
| 90 | target, options.clean, options.incremental, portage_flags) |
| 91 | utils.AssertTrue(retval == 0, "Build toolchain failed!") |
| 92 | |
| 93 | if options.incremental is None and not options.clean: |
| 94 | install_dir = rootdir + "/install" |
asharif | 9994d29 | 2013-02-15 04:36:04 +0000 | [diff] [blame] | 95 | package_dir = (rootdir + "/pkgs/") |
asharif | d751e25 | 2013-02-15 04:35:52 +0000 | [diff] [blame] | 96 | retval = InstallTC(package_dir, install_dir) |
| 97 | utils.AssertTrue(retval == 0, "Installation of the toolchain failed!") |
| 98 | |
| 99 | return retval |
asharif | e2cca30 | 2013-02-15 04:35:42 +0000 | [diff] [blame] | 100 | |
| 101 | |
| 102 | def CreateCrossdevPortageFlags(portage_flags): |
asharif | 2d815d0 | 2013-02-15 04:36:02 +0000 | [diff] [blame] | 103 | portage_flags = portage_flags.strip() |
asharif | e2cca30 | 2013-02-15 04:35:42 +0000 | [diff] [blame] | 104 | if not portage_flags: |
| 105 | return "" |
| 106 | crossdev_flags = " --portage " |
| 107 | crossdev_flags += " --portage ".join(portage_flags.split(" ")) |
| 108 | return crossdev_flags |
asharif | 19c73dd | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 109 | |
| 110 | |
| 111 | def CreateEnvVarString(variable, value): |
| 112 | return variable + "=" + EscapeQuoteString(value) |
| 113 | |
| 114 | |
| 115 | def EscapeQuoteString(string): |
| 116 | return "\\\"" + string + "\\\"" |
| 117 | |
| 118 | |
asharif | d751e25 | 2013-02-15 04:35:52 +0000 | [diff] [blame] | 119 | def InstallTC(package_dir, install_dir): |
asharif | 9994d29 | 2013-02-15 04:36:04 +0000 | [diff] [blame] | 120 | command = ("mkdir -p " + install_dir) |
| 121 | command += ("&& for f in $(find " + package_dir + |
asharif | fcf8cfc | 2013-02-15 04:49:21 +0000 | [diff] [blame] | 122 | " -name \\*.tbz2); do tar xf $f -C " + |
| 123 | install_dir + " ; done") |
raymes | 01959ae | 2013-02-15 04:50:07 +0000 | [diff] [blame^] | 124 | retval = cmd_executer.RunCommand(command) |
asharif | d751e25 | 2013-02-15 04:35:52 +0000 | [diff] [blame] | 125 | return retval |
| 126 | |
| 127 | |
asharif | 19c73dd | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 128 | def BuildTC(chromeos_root, toolchain_root, env, target, uninstall, |
asharif | e2cca30 | 2013-02-15 04:35:42 +0000 | [diff] [blame] | 129 | incremental_component, portage_flags): |
asharif | 19c73dd | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 130 | """Build the toolchain.""" |
asharif | 2d815d0 | 2013-02-15 04:36:02 +0000 | [diff] [blame] | 131 | portage_flags = portage_flags.strip() |
| 132 | portage_flags += " -b " |
| 133 | |
asharif | 19c73dd | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 134 | binutils_version = "2.20.1-r1" |
| 135 | gcc_version = "9999" |
asharif | 1762130 | 2013-02-15 04:46:35 +0000 | [diff] [blame] | 136 | libc_version = "2.10.1-r2" |
asharif | 19c73dd | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 137 | kernel_version = "2.6.30-r1" |
asharif | 19c73dd | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 138 | |
raymes | 01959ae | 2013-02-15 04:50:07 +0000 | [diff] [blame^] | 139 | rootdir = utils.GetRoot(sys.argv[0])[0] |
asharif | e3668f1 | 2013-02-15 04:46:29 +0000 | [diff] [blame] | 140 | argv = [rootdir + "tc_enter_chroot.py", |
| 141 | "--chromeos_root=" + chromeos_root, |
| 142 | "--toolchain_root=" + toolchain_root] |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 143 | |
asharif | 2d815d0 | 2013-02-15 04:36:02 +0000 | [diff] [blame] | 144 | env += " " |
| 145 | |
asharif | 19c73dd | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 146 | if uninstall == True: |
| 147 | tflag = " -C " |
| 148 | else: |
| 149 | tflag = " -t " |
| 150 | |
asharif | 0269d46 | 2013-02-15 04:46:31 +0000 | [diff] [blame] | 151 | command = " -- sudo " + env |
asharif | 2d815d0 | 2013-02-15 04:36:02 +0000 | [diff] [blame] | 152 | |
| 153 | if uninstall == True: |
| 154 | command += " crossdev " + tflag + target |
asharif | e3668f1 | 2013-02-15 04:46:29 +0000 | [diff] [blame] | 155 | argv.append(command) |
| 156 | retval = tc_enter_chroot.Main(argv) |
asharif | 2d815d0 | 2013-02-15 04:36:02 +0000 | [diff] [blame] | 157 | return retval |
asharif | 19c73dd | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 158 | |
| 159 | if incremental_component == "binutils": |
asharif | e2cca30 | 2013-02-15 04:35:42 +0000 | [diff] [blame] | 160 | command += (" emerge =cross-" + target + "/binutils-" + binutils_version + |
| 161 | portage_flags) |
asharif | 19c73dd | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 162 | elif incremental_component == "gcc": |
asharif | e2cca30 | 2013-02-15 04:35:42 +0000 | [diff] [blame] | 163 | command += (" emerge =cross-" + target + "/gcc-" + gcc_version + |
| 164 | portage_flags) |
asharif | 19c73dd | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 165 | elif incremental_component == "libc" or incremental_component == "glibc": |
asharif | e2cca30 | 2013-02-15 04:35:42 +0000 | [diff] [blame] | 166 | command += (" emerge =cross-" + target + "/glibc-" + libc_version + |
| 167 | portage_flags) |
asharif | 19c73dd | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 168 | else: |
asharif | 2d815d0 | 2013-02-15 04:36:02 +0000 | [diff] [blame] | 169 | crossdev_flags = CreateCrossdevPortageFlags(portage_flags) |
asharif | 19c73dd | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 170 | command += (" crossdev -v " + tflag + target + |
| 171 | " --binutils " + binutils_version + |
| 172 | " --libc " + libc_version + |
| 173 | " --gcc " + gcc_version + |
| 174 | " --kernel " + kernel_version + |
asharif | 2d815d0 | 2013-02-15 04:36:02 +0000 | [diff] [blame] | 175 | crossdev_flags) |
asharif | 19c73dd | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 176 | |
asharif | e3668f1 | 2013-02-15 04:46:29 +0000 | [diff] [blame] | 177 | argv.append(command) |
| 178 | retval = tc_enter_chroot.Main(argv) |
asharif | 19c73dd | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 179 | return retval |
| 180 | |
| 181 | if __name__ == "__main__": |
| 182 | Main() |
asharif | d751e25 | 2013-02-15 04:35:52 +0000 | [diff] [blame] | 183 | |