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