blob: 594ed3687080431960934443a776c62467a6aa1d [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
asharif17621302013-02-15 04:46:35 +000014import os
bjanakiraman7f4a4852013-02-15 04:35:28 +000015import sys
asharif252df0f2013-02-15 04:46:28 +000016import tc_enter_chroot
asharife3668f12013-02-15 04:46:29 +000017import build_chromeos
bjanakiraman7f4a4852013-02-15 04:35:28 +000018from utils import utils
19
20# Common initializations
21(rootdir, basename) = utils.GetRoot(sys.argv[0])
22utils.InitLogger(rootdir, basename)
23
bjanakiraman7f4a4852013-02-15 04:35:28 +000024
asharif19c73dd2013-02-15 04:35:37 +000025def Main():
26 """The main function."""
27 parser = optparse.OptionParser()
28 parser.add_option("-c", "--chromeos_root", dest="chromeos_root",
asharifd751e252013-02-15 04:35:52 +000029 default="../..",
asharif19c73dd2013-02-15 04:35:37 +000030 help="ChromeOS root checkout directory.")
31 parser.add_option("-t", "--toolchain_root", dest="toolchain_root",
32 help="Toolchain root directory.")
asharifd751e252013-02-15 04:35:52 +000033 parser.add_option("-b", "--board", dest="board", default="x86-generic",
asharif19c73dd2013-02-15 04:35:37 +000034 help="board is the argument to the setup_board command.")
asharifd751e252013-02-15 04:35:52 +000035 parser.add_option("-C", "--clean", dest="clean", default=False,
36 action="store_true",
asharife2cca302013-02-15 04:35:42 +000037 help="Uninstall the toolchain.")
asharifd751e252013-02-15 04:35:52 +000038 parser.add_option("-f", "--force", dest="force", default=False,
39 action="store_true",
asharife2cca302013-02-15 04:35:42 +000040 help="Do an uninstall/install cycle.")
asharif19c73dd2013-02-15 04:35:37 +000041 parser.add_option("-i", "--incremental", dest="incremental",
42 help="The toolchain component that should be "
43 "incrementally compiled.")
asharife2cca302013-02-15 04:35:42 +000044 parser.add_option("-B", "--binary", dest="binary",
45 action="store_true", default=False,
46 help="The toolchain should use binaries stored in "
47 "the install/ directory.")
bjanakiraman7f4a4852013-02-15 04:35:28 +000048
asharif19c73dd2013-02-15 04:35:37 +000049 options = parser.parse_args()[0]
bjanakiraman7f4a4852013-02-15 04:35:28 +000050
asharif17621302013-02-15 04:46:35 +000051 options.chromeos_root = os.path.expanduser(options.chromeos_root)
52
asharif19c73dd2013-02-15 04:35:37 +000053 if options.toolchain_root is None or options.board is None:
54 parser.print_help()
55 sys.exit()
bjanakiraman7f4a4852013-02-15 04:35:28 +000056
asharife3668f12013-02-15 04:46:29 +000057 build_chromeos.MakeChroot(options.chromeos_root)
58
asharife2cca302013-02-15 04:35:42 +000059 portage_flags = ""
60 if options.binary == True:
61 # FIXME(asharif): This should be using --usepkg but that was not working.
62 portage_flags = "--usepkgonly"
63
asharif19c73dd2013-02-15 04:35:37 +000064 f = open(options.chromeos_root + "/src/overlays/overlay-" +
65 options.board + "/toolchain.conf", "r")
66 target = f.read()
67 f.close()
68 target = target.strip()
asharif2d815d02013-02-15 04:36:02 +000069 features = "noclean userfetch userpriv usersandbox -strict"
asharif09bfb6f2013-02-15 04:35:44 +000070 if options.incremental is not None and options.incremental:
71 features += " keepwork"
asharif19c73dd2013-02-15 04:35:37 +000072 env = CreateEnvVarString(" FEATURES", features)
asharif80c6e552013-02-15 04:35:40 +000073 env += CreateEnvVarString(" PORTAGE_USERNAME", getpass.getuser())
74 version_number = utils.GetRoot(rootdir)[1]
asharif252df0f2013-02-15 04:46:28 +000075 version_dir = "/usr/local/toolchain_root/" + version_number
asharif80c6e552013-02-15 04:35:40 +000076 env += CreateEnvVarString(" PORT_LOGDIR", version_dir + "/logs")
asharifd751e252013-02-15 04:35:52 +000077 env += CreateEnvVarString(" PKGDIR", version_dir + "/pkgs")
asharif2d815d02013-02-15 04:36:02 +000078 env += CreateEnvVarString(" PORTAGE_BINHOST", version_dir + "/pkgs")
asharif80c6e552013-02-15 04:35:40 +000079 env += CreateEnvVarString(" PORTAGE_TMPDIR", version_dir + "/objects")
asharif252df0f2013-02-15 04:46:28 +000080 env += CreateEnvVarString(" USE", "mounted_sources")
asharif2d815d02013-02-15 04:36:02 +000081
asharifd751e252013-02-15 04:35:52 +000082 retval = 0
asharif80c6e552013-02-15 04:35:40 +000083 if options.force == True:
asharifd751e252013-02-15 04:35:52 +000084 retval = BuildTC(options.chromeos_root, options.toolchain_root, env,
85 target, True, options.incremental, portage_flags)
asharifd751e252013-02-15 04:35:52 +000086 retval = BuildTC(options.chromeos_root, options.toolchain_root, env,
87 target, options.clean, options.incremental, portage_flags)
88 utils.AssertTrue(retval == 0, "Build toolchain failed!")
89
90 if options.incremental is None and not options.clean:
91 install_dir = rootdir + "/install"
asharif9994d292013-02-15 04:36:04 +000092 package_dir = (rootdir + "/pkgs/")
asharifd751e252013-02-15 04:35:52 +000093 retval = InstallTC(package_dir, install_dir)
94 utils.AssertTrue(retval == 0, "Installation of the toolchain failed!")
95
96 return retval
asharife2cca302013-02-15 04:35:42 +000097
98
99def CreateCrossdevPortageFlags(portage_flags):
asharif2d815d02013-02-15 04:36:02 +0000100 portage_flags = portage_flags.strip()
asharife2cca302013-02-15 04:35:42 +0000101 if not portage_flags:
102 return ""
103 crossdev_flags = " --portage "
104 crossdev_flags += " --portage ".join(portage_flags.split(" "))
105 return crossdev_flags
asharif19c73dd2013-02-15 04:35:37 +0000106
107
108def CreateEnvVarString(variable, value):
109 return variable + "=" + EscapeQuoteString(value)
110
111
112def EscapeQuoteString(string):
113 return "\\\"" + string + "\\\""
114
115
asharifd751e252013-02-15 04:35:52 +0000116def InstallTC(package_dir, install_dir):
asharif9994d292013-02-15 04:36:04 +0000117 command = ("mkdir -p " + install_dir)
118 command += ("&& for f in $(find " + package_dir +
ashariffcf8cfc2013-02-15 04:49:21 +0000119 " -name \\*.tbz2); do tar xf $f -C " +
120 install_dir + " ; done")
asharifd751e252013-02-15 04:35:52 +0000121 retval = utils.RunCommand(command)
122 return retval
123
124
asharif19c73dd2013-02-15 04:35:37 +0000125def BuildTC(chromeos_root, toolchain_root, env, target, uninstall,
asharife2cca302013-02-15 04:35:42 +0000126 incremental_component, portage_flags):
asharif19c73dd2013-02-15 04:35:37 +0000127 """Build the toolchain."""
asharif2d815d02013-02-15 04:36:02 +0000128 portage_flags = portage_flags.strip()
129 portage_flags += " -b "
130
asharif19c73dd2013-02-15 04:35:37 +0000131 binutils_version = "2.20.1-r1"
132 gcc_version = "9999"
asharif17621302013-02-15 04:46:35 +0000133 libc_version = "2.10.1-r2"
asharif19c73dd2013-02-15 04:35:37 +0000134 kernel_version = "2.6.30-r1"
asharif19c73dd2013-02-15 04:35:37 +0000135
asharife3668f12013-02-15 04:46:29 +0000136 argv = [rootdir + "tc_enter_chroot.py",
137 "--chromeos_root=" + chromeos_root,
138 "--toolchain_root=" + toolchain_root]
asharif252df0f2013-02-15 04:46:28 +0000139
asharif2d815d02013-02-15 04:36:02 +0000140 env += " "
141
asharif19c73dd2013-02-15 04:35:37 +0000142 if uninstall == True:
143 tflag = " -C "
144 else:
145 tflag = " -t "
146
asharif0269d462013-02-15 04:46:31 +0000147 command = " -- sudo " + env
asharif2d815d02013-02-15 04:36:02 +0000148
149 if uninstall == True:
150 command += " crossdev " + tflag + target
asharife3668f12013-02-15 04:46:29 +0000151 argv.append(command)
152 retval = tc_enter_chroot.Main(argv)
asharif2d815d02013-02-15 04:36:02 +0000153 return retval
asharif19c73dd2013-02-15 04:35:37 +0000154
155 if incremental_component == "binutils":
asharife2cca302013-02-15 04:35:42 +0000156 command += (" emerge =cross-" + target + "/binutils-" + binutils_version +
157 portage_flags)
asharif19c73dd2013-02-15 04:35:37 +0000158 elif incremental_component == "gcc":
asharife2cca302013-02-15 04:35:42 +0000159 command += (" emerge =cross-" + target + "/gcc-" + gcc_version +
160 portage_flags)
asharif19c73dd2013-02-15 04:35:37 +0000161 elif incremental_component == "libc" or incremental_component == "glibc":
asharife2cca302013-02-15 04:35:42 +0000162 command += (" emerge =cross-" + target + "/glibc-" + libc_version +
163 portage_flags)
asharif19c73dd2013-02-15 04:35:37 +0000164 else:
asharif2d815d02013-02-15 04:36:02 +0000165 crossdev_flags = CreateCrossdevPortageFlags(portage_flags)
asharif19c73dd2013-02-15 04:35:37 +0000166 command += (" crossdev -v " + tflag + target +
167 " --binutils " + binutils_version +
168 " --libc " + libc_version +
169 " --gcc " + gcc_version +
170 " --kernel " + kernel_version +
asharif2d815d02013-02-15 04:36:02 +0000171 crossdev_flags)
asharif19c73dd2013-02-15 04:35:37 +0000172
asharife3668f12013-02-15 04:46:29 +0000173 argv.append(command)
174 retval = tc_enter_chroot.Main(argv)
asharif19c73dd2013-02-15 04:35:37 +0000175 return retval
176
177if __name__ == "__main__":
178 Main()
asharifd751e252013-02-15 04:35:52 +0000179