blob: 97770819e9573be098524d6a258e56f0fb2dfbc3 [file] [log] [blame]
bjanakiraman7f4a4852013-02-15 04:35:28 +00001#!/usr/bin/python2.6
2#
3# Copyright 2010 Google Inc. All Rights Reserved.
4
5"""Script to build the ChromeOS toolchain.
6
7This script sets up the toolchain if you give it the gcctools directory.
8"""
9
10__author__ = "asharif@google.com (Ahmad Sharif)"
11
asharif80c6e552013-02-15 04:35:40 +000012import getpass
bjanakiraman7f4a4852013-02-15 04:35:28 +000013import optparse
14import sys
15from utils import utils
16
17# Common initializations
18(rootdir, basename) = utils.GetRoot(sys.argv[0])
19utils.InitLogger(rootdir, basename)
20
bjanakiraman7f4a4852013-02-15 04:35:28 +000021
asharif19c73dd2013-02-15 04:35:37 +000022def 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",
asharife2cca302013-02-15 04:35:42 +000032 action="store_true", default=False,
33 help="Uninstall the toolchain.")
asharif80c6e552013-02-15 04:35:40 +000034 parser.add_option("-f", "--force", dest="force",
asharife2cca302013-02-15 04:35:42 +000035 action="store_true", default=False,
36 help="Do an uninstall/install cycle.")
asharif19c73dd2013-02-15 04:35:37 +000037 parser.add_option("-i", "--incremental", dest="incremental",
38 help="The toolchain component that should be "
39 "incrementally compiled.")
asharife2cca302013-02-15 04:35:42 +000040 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.")
bjanakiraman7f4a4852013-02-15 04:35:28 +000044
asharif19c73dd2013-02-15 04:35:37 +000045 options = parser.parse_args()[0]
bjanakiraman7f4a4852013-02-15 04:35:28 +000046
asharif19c73dd2013-02-15 04:35:37 +000047 if options.toolchain_root is None or options.board is None:
48 parser.print_help()
49 sys.exit()
bjanakiraman7f4a4852013-02-15 04:35:28 +000050
asharif19c73dd2013-02-15 04:35:37 +000051 if options.chromeos_root is None:
52 options.chromeos_root = "../.."
53
asharife2cca302013-02-15 04:35:42 +000054 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
asharif19c73dd2013-02-15 04:35:37 +000059 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()
asharif80c6e552013-02-15 04:35:40 +000064 features = "noclean userfetch userpriv usersandbox"
asharif19c73dd2013-02-15 04:35:37 +000065 env = CreateEnvVarString(" FEATURES", features)
asharif80c6e552013-02-15 04:35:40 +000066 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")
70 env += CreateEnvVarString(" PKGDIR", version_dir + "/install")
asharife2cca302013-02-15 04:35:42 +000071 env += CreateEnvVarString(" PORTAGE_BINHOST", version_dir +
72 "/cross/" + target)
asharif80c6e552013-02-15 04:35:40 +000073 env += CreateEnvVarString(" PORTAGE_TMPDIR", version_dir + "/objects")
74 if options.force == True:
75 BuildTC(options.chromeos_root, options.toolchain_root, env, target,
asharife2cca302013-02-15 04:35:42 +000076 True, options.incremental, portage_flags)
asharif19c73dd2013-02-15 04:35:37 +000077 BuildTC(options.chromeos_root, options.toolchain_root, env, target,
asharife2cca302013-02-15 04:35:42 +000078 options.clean, options.incremental, portage_flags)
79
80
81def CreateCrossdevPortageFlags(portage_flags):
82 if not portage_flags:
83 return ""
84 crossdev_flags = " --portage "
85 crossdev_flags += " --portage ".join(portage_flags.split(" "))
86 return crossdev_flags
asharif19c73dd2013-02-15 04:35:37 +000087
88
89def CreateEnvVarString(variable, value):
90 return variable + "=" + EscapeQuoteString(value)
91
92
93def EscapeQuoteString(string):
94 return "\\\"" + string + "\\\""
95
96
97def BuildTC(chromeos_root, toolchain_root, env, target, uninstall,
asharife2cca302013-02-15 04:35:42 +000098 incremental_component, portage_flags):
asharif19c73dd2013-02-15 04:35:37 +000099 """Build the toolchain."""
100 binutils_version = "2.20.1-r1"
101 gcc_version = "9999"
102 libc_version = "2.10.1-r1"
103 kernel_version = "2.6.30-r1"
asharif80c6e552013-02-15 04:35:40 +0000104 if incremental_component is not None and incremental_component:
asharife2cca302013-02-15 04:35:42 +0000105 env += " FEATURES+=" + EscapeQuoteString("keepwork")
asharif19c73dd2013-02-15 04:35:37 +0000106
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":
asharife2cca302013-02-15 04:35:42 +0000120 command += (" emerge =cross-" + target + "/binutils-" + binutils_version +
121 portage_flags)
asharif19c73dd2013-02-15 04:35:37 +0000122 elif incremental_component == "gcc":
asharife2cca302013-02-15 04:35:42 +0000123 command += (" emerge =cross-" + target + "/gcc-" + gcc_version +
124 portage_flags)
asharif19c73dd2013-02-15 04:35:37 +0000125 elif incremental_component == "libc" or incremental_component == "glibc":
asharife2cca302013-02-15 04:35:42 +0000126 command += (" emerge =cross-" + target + "/glibc-" + libc_version +
127 portage_flags)
asharif19c73dd2013-02-15 04:35:37 +0000128 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")
asharife2cca302013-02-15 04:35:42 +0000135 crossdev_flags = CreateCrossdevPortageFlags(portage_flags)
136 command += crossdev_flags
asharif19c73dd2013-02-15 04:35:37 +0000137
138 retval = utils.RunCommand(command)
139 return retval
140
141if __name__ == "__main__":
142 Main()