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