blob: 51fde50c7a9c988345fd26055c894240b8143381 [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
asharif252df0f2013-02-15 04:46:28 +000015import tc_enter_chroot
asharife3668f12013-02-15 04:46:29 +000016import build_chromeos
bjanakiraman7f4a4852013-02-15 04:35:28 +000017from utils import utils
18
19# Common initializations
20(rootdir, basename) = utils.GetRoot(sys.argv[0])
21utils.InitLogger(rootdir, basename)
22
bjanakiraman7f4a4852013-02-15 04:35:28 +000023
asharif19c73dd2013-02-15 04:35:37 +000024def Main():
25 """The main function."""
26 parser = optparse.OptionParser()
27 parser.add_option("-c", "--chromeos_root", dest="chromeos_root",
asharifd751e252013-02-15 04:35:52 +000028 default="../..",
asharif19c73dd2013-02-15 04:35:37 +000029 help="ChromeOS root checkout directory.")
30 parser.add_option("-t", "--toolchain_root", dest="toolchain_root",
31 help="Toolchain root directory.")
asharifd751e252013-02-15 04:35:52 +000032 parser.add_option("-b", "--board", dest="board", default="x86-generic",
asharif19c73dd2013-02-15 04:35:37 +000033 help="board is the argument to the setup_board command.")
asharifd751e252013-02-15 04:35:52 +000034 parser.add_option("-C", "--clean", dest="clean", default=False,
35 action="store_true",
asharife2cca302013-02-15 04:35:42 +000036 help="Uninstall the toolchain.")
asharifd751e252013-02-15 04:35:52 +000037 parser.add_option("-f", "--force", dest="force", default=False,
38 action="store_true",
asharife2cca302013-02-15 04:35:42 +000039 help="Do an uninstall/install cycle.")
asharif19c73dd2013-02-15 04:35:37 +000040 parser.add_option("-i", "--incremental", dest="incremental",
41 help="The toolchain component that should be "
42 "incrementally compiled.")
asharife2cca302013-02-15 04:35:42 +000043 parser.add_option("-B", "--binary", dest="binary",
44 action="store_true", default=False,
45 help="The toolchain should use binaries stored in "
46 "the install/ directory.")
bjanakiraman7f4a4852013-02-15 04:35:28 +000047
asharif19c73dd2013-02-15 04:35:37 +000048 options = parser.parse_args()[0]
bjanakiraman7f4a4852013-02-15 04:35:28 +000049
asharif19c73dd2013-02-15 04:35:37 +000050 if options.toolchain_root is None or options.board is None:
51 parser.print_help()
52 sys.exit()
bjanakiraman7f4a4852013-02-15 04:35:28 +000053
asharife3668f12013-02-15 04:46:29 +000054 build_chromeos.MakeChroot(options.chromeos_root)
55
asharife2cca302013-02-15 04:35:42 +000056 portage_flags = ""
57 if options.binary == True:
58 # FIXME(asharif): This should be using --usepkg but that was not working.
59 portage_flags = "--usepkgonly"
60
asharif19c73dd2013-02-15 04:35:37 +000061 f = open(options.chromeos_root + "/src/overlays/overlay-" +
62 options.board + "/toolchain.conf", "r")
63 target = f.read()
64 f.close()
65 target = target.strip()
asharif2d815d02013-02-15 04:36:02 +000066 features = "noclean userfetch userpriv usersandbox -strict"
asharif09bfb6f2013-02-15 04:35:44 +000067 if options.incremental is not None and options.incremental:
68 features += " keepwork"
asharif19c73dd2013-02-15 04:35:37 +000069 env = CreateEnvVarString(" FEATURES", features)
asharif80c6e552013-02-15 04:35:40 +000070 env += CreateEnvVarString(" PORTAGE_USERNAME", getpass.getuser())
71 version_number = utils.GetRoot(rootdir)[1]
asharif252df0f2013-02-15 04:46:28 +000072 version_dir = "/usr/local/toolchain_root/" + version_number
asharif80c6e552013-02-15 04:35:40 +000073 env += CreateEnvVarString(" PORT_LOGDIR", version_dir + "/logs")
asharifd751e252013-02-15 04:35:52 +000074 env += CreateEnvVarString(" PKGDIR", version_dir + "/pkgs")
asharif2d815d02013-02-15 04:36:02 +000075 env += CreateEnvVarString(" PORTAGE_BINHOST", version_dir + "/pkgs")
asharif80c6e552013-02-15 04:35:40 +000076 env += CreateEnvVarString(" PORTAGE_TMPDIR", version_dir + "/objects")
asharif252df0f2013-02-15 04:46:28 +000077 env += CreateEnvVarString(" USE", "mounted_sources")
asharif2d815d02013-02-15 04:36:02 +000078
asharifd751e252013-02-15 04:35:52 +000079 retval = 0
asharif80c6e552013-02-15 04:35:40 +000080 if options.force == True:
asharifd751e252013-02-15 04:35:52 +000081 retval = BuildTC(options.chromeos_root, options.toolchain_root, env,
82 target, True, options.incremental, portage_flags)
asharifd751e252013-02-15 04:35:52 +000083 retval = BuildTC(options.chromeos_root, options.toolchain_root, env,
84 target, options.clean, options.incremental, portage_flags)
85 utils.AssertTrue(retval == 0, "Build toolchain failed!")
86
87 if options.incremental is None and not options.clean:
88 install_dir = rootdir + "/install"
asharif9994d292013-02-15 04:36:04 +000089 package_dir = (rootdir + "/pkgs/")
asharifd751e252013-02-15 04:35:52 +000090 retval = InstallTC(package_dir, install_dir)
91 utils.AssertTrue(retval == 0, "Installation of the toolchain failed!")
92
93 return retval
asharife2cca302013-02-15 04:35:42 +000094
95
96def CreateCrossdevPortageFlags(portage_flags):
asharif2d815d02013-02-15 04:36:02 +000097 portage_flags = portage_flags.strip()
asharife2cca302013-02-15 04:35:42 +000098 if not portage_flags:
99 return ""
100 crossdev_flags = " --portage "
101 crossdev_flags += " --portage ".join(portage_flags.split(" "))
102 return crossdev_flags
asharif19c73dd2013-02-15 04:35:37 +0000103
104
105def CreateEnvVarString(variable, value):
106 return variable + "=" + EscapeQuoteString(value)
107
108
109def EscapeQuoteString(string):
110 return "\\\"" + string + "\\\""
111
112
asharifd751e252013-02-15 04:35:52 +0000113def InstallTC(package_dir, install_dir):
asharif9994d292013-02-15 04:36:04 +0000114 command = ("mkdir -p " + install_dir)
115 command += ("&& for f in $(find " + package_dir +
116 " -name \\*.tbz2); do tar xvf $f -C " +
117 install_dir + "; done")
asharifd751e252013-02-15 04:35:52 +0000118 retval = utils.RunCommand(command)
119 return retval
120
121
asharif19c73dd2013-02-15 04:35:37 +0000122def BuildTC(chromeos_root, toolchain_root, env, target, uninstall,
asharife2cca302013-02-15 04:35:42 +0000123 incremental_component, portage_flags):
asharif19c73dd2013-02-15 04:35:37 +0000124 """Build the toolchain."""
asharif2d815d02013-02-15 04:36:02 +0000125 portage_flags = portage_flags.strip()
126 portage_flags += " -b "
127
asharif19c73dd2013-02-15 04:35:37 +0000128 binutils_version = "2.20.1-r1"
129 gcc_version = "9999"
130 libc_version = "2.10.1-r1"
131 kernel_version = "2.6.30-r1"
asharif19c73dd2013-02-15 04:35:37 +0000132
asharife3668f12013-02-15 04:46:29 +0000133 argv = [rootdir + "tc_enter_chroot.py",
134 "--chromeos_root=" + chromeos_root,
135 "--toolchain_root=" + toolchain_root]
asharif252df0f2013-02-15 04:46:28 +0000136
asharif2d815d02013-02-15 04:36:02 +0000137 env += " "
138
asharif19c73dd2013-02-15 04:35:37 +0000139 if uninstall == True:
140 tflag = " -C "
141 else:
142 tflag = " -t "
143
asharif0269d462013-02-15 04:46:31 +0000144 command = " -- sudo " + env
asharif2d815d02013-02-15 04:36:02 +0000145
146 if uninstall == True:
147 command += " crossdev " + tflag + target
asharife3668f12013-02-15 04:46:29 +0000148 argv.append(command)
149 retval = tc_enter_chroot.Main(argv)
asharif2d815d02013-02-15 04:36:02 +0000150 return retval
asharif19c73dd2013-02-15 04:35:37 +0000151
152 if incremental_component == "binutils":
asharife2cca302013-02-15 04:35:42 +0000153 command += (" emerge =cross-" + target + "/binutils-" + binutils_version +
154 portage_flags)
asharif19c73dd2013-02-15 04:35:37 +0000155 elif incremental_component == "gcc":
asharife2cca302013-02-15 04:35:42 +0000156 command += (" emerge =cross-" + target + "/gcc-" + gcc_version +
157 portage_flags)
asharif19c73dd2013-02-15 04:35:37 +0000158 elif incremental_component == "libc" or incremental_component == "glibc":
asharife2cca302013-02-15 04:35:42 +0000159 command += (" emerge =cross-" + target + "/glibc-" + libc_version +
160 portage_flags)
asharif19c73dd2013-02-15 04:35:37 +0000161 else:
asharif2d815d02013-02-15 04:36:02 +0000162 crossdev_flags = CreateCrossdevPortageFlags(portage_flags)
asharif19c73dd2013-02-15 04:35:37 +0000163 command += (" crossdev -v " + tflag + target +
164 " --binutils " + binutils_version +
165 " --libc " + libc_version +
166 " --gcc " + gcc_version +
167 " --kernel " + kernel_version +
asharif2d815d02013-02-15 04:36:02 +0000168 crossdev_flags)
asharif19c73dd2013-02-15 04:35:37 +0000169
asharife3668f12013-02-15 04:46:29 +0000170 argv.append(command)
171 retval = tc_enter_chroot.Main(argv)
asharif19c73dd2013-02-15 04:35:37 +0000172 return retval
173
174if __name__ == "__main__":
175 Main()
asharifd751e252013-02-15 04:35:52 +0000176