blob: d38e9a4020a893b312863d6526f4f126c4f82ee6 [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
raymes01959ae2013-02-15 04:50:07 +000018from utils import command_executer
bjanakiraman7f4a4852013-02-15 04:35:28 +000019from utils import utils
raymes01959ae2013-02-15 04:50:07 +000020from utils import logger
bjanakiraman7f4a4852013-02-15 04:35:28 +000021
22# Common initializations
raymes01959ae2013-02-15 04:50:07 +000023logger.InitLogger(sys.argv[0])
24cmd_executer = command_executer.GetCommandExecuter()
bjanakiraman7f4a4852013-02-15 04:35:28 +000025
bjanakiraman7f4a4852013-02-15 04:35:28 +000026
asharif19c73dd2013-02-15 04:35:37 +000027def Main():
28 """The main function."""
29 parser = optparse.OptionParser()
30 parser.add_option("-c", "--chromeos_root", dest="chromeos_root",
asharifd751e252013-02-15 04:35:52 +000031 default="../..",
asharif19c73dd2013-02-15 04:35:37 +000032 help="ChromeOS root checkout directory.")
33 parser.add_option("-t", "--toolchain_root", dest="toolchain_root",
34 help="Toolchain root directory.")
asharifd751e252013-02-15 04:35:52 +000035 parser.add_option("-b", "--board", dest="board", default="x86-generic",
asharif19c73dd2013-02-15 04:35:37 +000036 help="board is the argument to the setup_board command.")
asharifd751e252013-02-15 04:35:52 +000037 parser.add_option("-C", "--clean", dest="clean", default=False,
38 action="store_true",
asharife2cca302013-02-15 04:35:42 +000039 help="Uninstall the toolchain.")
asharifd751e252013-02-15 04:35:52 +000040 parser.add_option("-f", "--force", dest="force", default=False,
41 action="store_true",
asharife2cca302013-02-15 04:35:42 +000042 help="Do an uninstall/install cycle.")
asharif19c73dd2013-02-15 04:35:37 +000043 parser.add_option("-i", "--incremental", dest="incremental",
44 help="The toolchain component that should be "
45 "incrementally compiled.")
asharife2cca302013-02-15 04:35:42 +000046 parser.add_option("-B", "--binary", dest="binary",
47 action="store_true", default=False,
48 help="The toolchain should use binaries stored in "
49 "the install/ directory.")
bjanakiraman7f4a4852013-02-15 04:35:28 +000050
asharif19c73dd2013-02-15 04:35:37 +000051 options = parser.parse_args()[0]
bjanakiraman7f4a4852013-02-15 04:35:28 +000052
asharif17621302013-02-15 04:46:35 +000053 options.chromeos_root = os.path.expanduser(options.chromeos_root)
54
asharif19c73dd2013-02-15 04:35:37 +000055 if options.toolchain_root is None or options.board is None:
56 parser.print_help()
57 sys.exit()
bjanakiraman7f4a4852013-02-15 04:35:28 +000058
asharife3668f12013-02-15 04:46:29 +000059 build_chromeos.MakeChroot(options.chromeos_root)
60
asharife2cca302013-02-15 04:35:42 +000061 portage_flags = ""
62 if options.binary == True:
63 # FIXME(asharif): This should be using --usepkg but that was not working.
64 portage_flags = "--usepkgonly"
65
asharif19c73dd2013-02-15 04:35:37 +000066 f = open(options.chromeos_root + "/src/overlays/overlay-" +
67 options.board + "/toolchain.conf", "r")
68 target = f.read()
69 f.close()
70 target = target.strip()
asharif2d815d02013-02-15 04:36:02 +000071 features = "noclean userfetch userpriv usersandbox -strict"
asharif09bfb6f2013-02-15 04:35:44 +000072 if options.incremental is not None and options.incremental:
73 features += " keepwork"
asharif19c73dd2013-02-15 04:35:37 +000074 env = CreateEnvVarString(" FEATURES", features)
asharif80c6e552013-02-15 04:35:40 +000075 env += CreateEnvVarString(" PORTAGE_USERNAME", getpass.getuser())
raymes01959ae2013-02-15 04:50:07 +000076 rootdir = utils.GetRoot(sys.argv[0])[0]
asharif80c6e552013-02-15 04:35:40 +000077 version_number = utils.GetRoot(rootdir)[1]
asharif252df0f2013-02-15 04:46:28 +000078 version_dir = "/usr/local/toolchain_root/" + version_number
asharif80c6e552013-02-15 04:35:40 +000079 env += CreateEnvVarString(" PORT_LOGDIR", version_dir + "/logs")
asharifd751e252013-02-15 04:35:52 +000080 env += CreateEnvVarString(" PKGDIR", version_dir + "/pkgs")
asharif2d815d02013-02-15 04:36:02 +000081 env += CreateEnvVarString(" PORTAGE_BINHOST", version_dir + "/pkgs")
asharif80c6e552013-02-15 04:35:40 +000082 env += CreateEnvVarString(" PORTAGE_TMPDIR", version_dir + "/objects")
asharif252df0f2013-02-15 04:46:28 +000083 env += CreateEnvVarString(" USE", "mounted_sources")
asharif2d815d02013-02-15 04:36:02 +000084
asharifd751e252013-02-15 04:35:52 +000085 retval = 0
asharif80c6e552013-02-15 04:35:40 +000086 if options.force == True:
asharifd751e252013-02-15 04:35:52 +000087 retval = BuildTC(options.chromeos_root, options.toolchain_root, env,
88 target, True, options.incremental, portage_flags)
asharifd751e252013-02-15 04:35:52 +000089 retval = BuildTC(options.chromeos_root, options.toolchain_root, env,
90 target, options.clean, options.incremental, portage_flags)
91 utils.AssertTrue(retval == 0, "Build toolchain failed!")
92
93 if options.incremental is None and not options.clean:
94 install_dir = rootdir + "/install"
asharif9994d292013-02-15 04:36:04 +000095 package_dir = (rootdir + "/pkgs/")
asharifd751e252013-02-15 04:35:52 +000096 retval = InstallTC(package_dir, install_dir)
97 utils.AssertTrue(retval == 0, "Installation of the toolchain failed!")
98
99 return retval
asharife2cca302013-02-15 04:35:42 +0000100
101
102def CreateCrossdevPortageFlags(portage_flags):
asharif2d815d02013-02-15 04:36:02 +0000103 portage_flags = portage_flags.strip()
asharife2cca302013-02-15 04:35:42 +0000104 if not portage_flags:
105 return ""
106 crossdev_flags = " --portage "
107 crossdev_flags += " --portage ".join(portage_flags.split(" "))
108 return crossdev_flags
asharif19c73dd2013-02-15 04:35:37 +0000109
110
111def CreateEnvVarString(variable, value):
112 return variable + "=" + EscapeQuoteString(value)
113
114
115def EscapeQuoteString(string):
116 return "\\\"" + string + "\\\""
117
118
asharifd751e252013-02-15 04:35:52 +0000119def InstallTC(package_dir, install_dir):
asharif9994d292013-02-15 04:36:04 +0000120 command = ("mkdir -p " + install_dir)
121 command += ("&& for f in $(find " + package_dir +
ashariffcf8cfc2013-02-15 04:49:21 +0000122 " -name \\*.tbz2); do tar xf $f -C " +
123 install_dir + " ; done")
raymes01959ae2013-02-15 04:50:07 +0000124 retval = cmd_executer.RunCommand(command)
asharifd751e252013-02-15 04:35:52 +0000125 return retval
126
127
asharif19c73dd2013-02-15 04:35:37 +0000128def BuildTC(chromeos_root, toolchain_root, env, target, uninstall,
asharife2cca302013-02-15 04:35:42 +0000129 incremental_component, portage_flags):
asharif19c73dd2013-02-15 04:35:37 +0000130 """Build the toolchain."""
asharif2d815d02013-02-15 04:36:02 +0000131 portage_flags = portage_flags.strip()
132 portage_flags += " -b "
133
asharif19c73dd2013-02-15 04:35:37 +0000134 binutils_version = "2.20.1-r1"
135 gcc_version = "9999"
asharif17621302013-02-15 04:46:35 +0000136 libc_version = "2.10.1-r2"
asharif19c73dd2013-02-15 04:35:37 +0000137 kernel_version = "2.6.30-r1"
asharif19c73dd2013-02-15 04:35:37 +0000138
raymes01959ae2013-02-15 04:50:07 +0000139 rootdir = utils.GetRoot(sys.argv[0])[0]
asharife3668f12013-02-15 04:46:29 +0000140 argv = [rootdir + "tc_enter_chroot.py",
141 "--chromeos_root=" + chromeos_root,
142 "--toolchain_root=" + toolchain_root]
asharif252df0f2013-02-15 04:46:28 +0000143
asharif2d815d02013-02-15 04:36:02 +0000144 env += " "
145
asharif19c73dd2013-02-15 04:35:37 +0000146 if uninstall == True:
147 tflag = " -C "
148 else:
149 tflag = " -t "
150
asharif0269d462013-02-15 04:46:31 +0000151 command = " -- sudo " + env
asharif2d815d02013-02-15 04:36:02 +0000152
153 if uninstall == True:
154 command += " crossdev " + tflag + target
asharife3668f12013-02-15 04:46:29 +0000155 argv.append(command)
156 retval = tc_enter_chroot.Main(argv)
asharif2d815d02013-02-15 04:36:02 +0000157 return retval
asharif19c73dd2013-02-15 04:35:37 +0000158
159 if incremental_component == "binutils":
asharife2cca302013-02-15 04:35:42 +0000160 command += (" emerge =cross-" + target + "/binutils-" + binutils_version +
161 portage_flags)
asharif19c73dd2013-02-15 04:35:37 +0000162 elif incremental_component == "gcc":
asharife2cca302013-02-15 04:35:42 +0000163 command += (" emerge =cross-" + target + "/gcc-" + gcc_version +
164 portage_flags)
asharif19c73dd2013-02-15 04:35:37 +0000165 elif incremental_component == "libc" or incremental_component == "glibc":
asharife2cca302013-02-15 04:35:42 +0000166 command += (" emerge =cross-" + target + "/glibc-" + libc_version +
167 portage_flags)
asharif19c73dd2013-02-15 04:35:37 +0000168 else:
asharif2d815d02013-02-15 04:36:02 +0000169 crossdev_flags = CreateCrossdevPortageFlags(portage_flags)
asharif19c73dd2013-02-15 04:35:37 +0000170 command += (" crossdev -v " + tflag + target +
171 " --binutils " + binutils_version +
172 " --libc " + libc_version +
173 " --gcc " + gcc_version +
174 " --kernel " + kernel_version +
asharif2d815d02013-02-15 04:36:02 +0000175 crossdev_flags)
asharif19c73dd2013-02-15 04:35:37 +0000176
asharife3668f12013-02-15 04:46:29 +0000177 argv.append(command)
178 retval = tc_enter_chroot.Main(argv)
asharif19c73dd2013-02-15 04:35:37 +0000179 return retval
180
181if __name__ == "__main__":
182 Main()
asharifd751e252013-02-15 04:35:52 +0000183