blob: cacd9e35934f32faf6965e92585e091962f706a1 [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",
asharifd751e252013-02-15 04:35:52 +000026 default="../..",
asharif19c73dd2013-02-15 04:35:37 +000027 help="ChromeOS root checkout directory.")
28 parser.add_option("-t", "--toolchain_root", dest="toolchain_root",
29 help="Toolchain root directory.")
asharifd751e252013-02-15 04:35:52 +000030 parser.add_option("-b", "--board", dest="board", default="x86-generic",
asharif19c73dd2013-02-15 04:35:37 +000031 help="board is the argument to the setup_board command.")
asharifd751e252013-02-15 04:35:52 +000032 parser.add_option("-C", "--clean", dest="clean", default=False,
33 action="store_true",
asharife2cca302013-02-15 04:35:42 +000034 help="Uninstall the toolchain.")
asharifd751e252013-02-15 04:35:52 +000035 parser.add_option("-f", "--force", dest="force", default=False,
36 action="store_true",
asharife2cca302013-02-15 04:35:42 +000037 help="Do an uninstall/install cycle.")
asharif19c73dd2013-02-15 04:35:37 +000038 parser.add_option("-i", "--incremental", dest="incremental",
39 help="The toolchain component that should be "
40 "incrementally compiled.")
asharife2cca302013-02-15 04:35:42 +000041 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.")
bjanakiraman7f4a4852013-02-15 04:35:28 +000045
asharif19c73dd2013-02-15 04:35:37 +000046 options = parser.parse_args()[0]
bjanakiraman7f4a4852013-02-15 04:35:28 +000047
asharif19c73dd2013-02-15 04:35:37 +000048 if options.toolchain_root is None or options.board is None:
49 parser.print_help()
50 sys.exit()
bjanakiraman7f4a4852013-02-15 04:35:28 +000051
asharife2cca302013-02-15 04:35:42 +000052 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
asharif19c73dd2013-02-15 04:35:37 +000057 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()
asharif80c6e552013-02-15 04:35:40 +000062 features = "noclean userfetch userpriv usersandbox"
asharif09bfb6f2013-02-15 04:35:44 +000063 if options.incremental is not None and options.incremental:
64 features += " keepwork"
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")
asharifd751e252013-02-15 04:35:52 +000070 env += CreateEnvVarString(" PKGDIR", version_dir + "/pkgs")
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")
asharifd751e252013-02-15 04:35:52 +000074 retval = 0
asharif80c6e552013-02-15 04:35:40 +000075 if options.force == True:
asharifd751e252013-02-15 04:35:52 +000076 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
asharife2cca302013-02-15 04:35:42 +000091
92
93def 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
asharif19c73dd2013-02-15 04:35:37 +000099
100
101def CreateEnvVarString(variable, value):
102 return variable + "=" + EscapeQuoteString(value)
103
104
105def EscapeQuoteString(string):
106 return "\\\"" + string + "\\\""
107
108
asharifd751e252013-02-15 04:35:52 +0000109def 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
asharif19c73dd2013-02-15 04:35:37 +0000117def BuildTC(chromeos_root, toolchain_root, env, target, uninstall,
asharife2cca302013-02-15 04:35:42 +0000118 incremental_component, portage_flags):
asharif19c73dd2013-02-15 04:35:37 +0000119 """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"
asharif19c73dd2013-02-15 04:35:37 +0000124
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":
asharife2cca302013-02-15 04:35:42 +0000138 command += (" emerge =cross-" + target + "/binutils-" + binutils_version +
139 portage_flags)
asharif19c73dd2013-02-15 04:35:37 +0000140 elif incremental_component == "gcc":
asharife2cca302013-02-15 04:35:42 +0000141 command += (" emerge =cross-" + target + "/gcc-" + gcc_version +
142 portage_flags)
asharif19c73dd2013-02-15 04:35:37 +0000143 elif incremental_component == "libc" or incremental_component == "glibc":
asharife2cca302013-02-15 04:35:42 +0000144 command += (" emerge =cross-" + target + "/glibc-" + libc_version +
145 portage_flags)
asharif19c73dd2013-02-15 04:35:37 +0000146 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")
asharife2cca302013-02-15 04:35:42 +0000153 crossdev_flags = CreateCrossdevPortageFlags(portage_flags)
154 command += crossdev_flags
asharif19c73dd2013-02-15 04:35:37 +0000155
156 retval = utils.RunCommand(command)
157 return retval
158
159if __name__ == "__main__":
160 Main()
asharifd751e252013-02-15 04:35:52 +0000161