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