blob: c9d2aaa11f764f4576716f5931eb70bf5b219608 [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
asharif0d3535a2013-02-15 04:50:33 +000018import setup_chromeos
raymes01959ae2013-02-15 04:50:07 +000019from utils import command_executer
bjanakiraman7f4a4852013-02-15 04:35:28 +000020from utils import utils
raymes01959ae2013-02-15 04:50:07 +000021from utils import logger
bjanakiraman7f4a4852013-02-15 04:35:28 +000022
asharif5a9bb462013-02-15 04:50:57 +000023
24cmd_executer = None
bjanakiraman7f4a4852013-02-15 04:35:28 +000025
bjanakiraman7f4a4852013-02-15 04:35:28 +000026
asharif0d3535a2013-02-15 04:50:33 +000027def Main(argv):
asharif19c73dd2013-02-15 04:35:37 +000028 """The main function."""
asharif5a9bb462013-02-15 04:50:57 +000029 # Common initializations
30 global cmd_executer
31 cmd_executer = command_executer.GetCommandExecuter()
asharif0d3535a2013-02-15 04:50:33 +000032 rootdir = utils.GetRoot(sys.argv[0])[0]
33
asharif19c73dd2013-02-15 04:35:37 +000034 parser = optparse.OptionParser()
35 parser.add_option("-c", "--chromeos_root", dest="chromeos_root",
asharif0d3535a2013-02-15 04:50:33 +000036 help=("ChromeOS root checkout directory" +
37 " uses ../.. if none given."))
asharif19c73dd2013-02-15 04:35:37 +000038 parser.add_option("-t", "--toolchain_root", dest="toolchain_root",
39 help="Toolchain root directory.")
asharifd751e252013-02-15 04:35:52 +000040 parser.add_option("-b", "--board", dest="board", default="x86-generic",
asharif19c73dd2013-02-15 04:35:37 +000041 help="board is the argument to the setup_board command.")
asharifd751e252013-02-15 04:35:52 +000042 parser.add_option("-C", "--clean", dest="clean", default=False,
43 action="store_true",
asharife2cca302013-02-15 04:35:42 +000044 help="Uninstall the toolchain.")
asharifd751e252013-02-15 04:35:52 +000045 parser.add_option("-f", "--force", dest="force", default=False,
46 action="store_true",
asharife2cca302013-02-15 04:35:42 +000047 help="Do an uninstall/install cycle.")
asharif19c73dd2013-02-15 04:35:37 +000048 parser.add_option("-i", "--incremental", dest="incremental",
49 help="The toolchain component that should be "
50 "incrementally compiled.")
asharife2cca302013-02-15 04:35:42 +000051 parser.add_option("-B", "--binary", dest="binary",
52 action="store_true", default=False,
53 help="The toolchain should use binaries stored in "
54 "the install/ directory.")
asharif0d3535a2013-02-15 04:50:33 +000055 parser.add_option("-s", "--setup-chromeos-options",
56 dest="setup_chromeos_options",
57 help="Additional options that should be passed on to"
58 "the setup_chromeos script.")
59 parser.add_option("-o", "--output", dest="output",
asharif1ba8a3b2013-02-15 04:56:50 +000060 help="The output directory where logs,pkgs, etc. go. "
61 "The default is the toolchain_root/output")
bjanakiraman7f4a4852013-02-15 04:35:28 +000062
asharif0d3535a2013-02-15 04:50:33 +000063 options = parser.parse_args(argv)[0]
asharif17621302013-02-15 04:46:35 +000064
asharif1755b432013-02-15 04:55:29 +000065 if (options.clean == False and
66 (options.toolchain_root is None or options.board is None)):
asharif19c73dd2013-02-15 04:35:37 +000067 parser.print_help()
68 sys.exit()
bjanakiraman7f4a4852013-02-15 04:35:28 +000069
asharif0d3535a2013-02-15 04:50:33 +000070 if options.chromeos_root is None:
71 if os.path.exists("enter_chroot.sh"):
72 options.chromeos_root = "../.."
73 else:
74 logger.GetLogger().LogError("--chromeos_root not given")
75 parser.print_help()
76 sys.exit()
77 else:
78 options.chromeos_root = os.path.expanduser(options.chromeos_root)
79
asharif88f01422013-02-15 04:50:35 +000080 if ((not os.path.exists(options.chromeos_root)) or
81 (not os.path.exists(options.chromeos_root +
82 "/src/scripts/enter_chroot.sh"))):
asharif0d3535a2013-02-15 04:50:33 +000083 logger.GetLogger().LogOutput("Creating a chromeos checkout at: %s" %
84 options.chromeos_root)
asharif88f01422013-02-15 04:50:35 +000085 sc_args = []
asharif6ba78c42013-02-15 04:50:40 +000086 sc_args.append("--minilayout")
asharif88f01422013-02-15 04:50:35 +000087 sc_args.append("--dir=%s" % options.chromeos_root)
88 if options.setup_chromeos_options:
89 sc_args.append(options.setup_chromeos_options)
asharif0d3535a2013-02-15 04:50:33 +000090 setup_chromeos.Main(sc_args)
91
asharif1ba8a3b2013-02-15 04:56:50 +000092 if options.output is None:
93 output = options.toolchain_root + "/output"
94 else:
95 output = options.output
asharif0d3535a2013-02-15 04:50:33 +000096
97 if output.startswith("/") == False:
98 output = os.getcwd() + "/" + output
99 else:
100 output = os.path.expanduser(output)
101
102 chroot_mount = "/usr/local/toolchain_root/"
103 chroot_base = utils.GetRoot(output)[1]
104 chroot_output = chroot_mount + chroot_base
105
106 tc_enter_chroot_options = []
asharif541b6392013-02-15 04:50:38 +0000107 output_mount = ("--output=" + output)
asharif0d3535a2013-02-15 04:50:33 +0000108 tc_enter_chroot_options.append(output_mount)
109
asharife3668f12013-02-15 04:46:29 +0000110 build_chromeos.MakeChroot(options.chromeos_root)
111
asharife2cca302013-02-15 04:35:42 +0000112 portage_flags = ""
113 if options.binary == True:
114 # FIXME(asharif): This should be using --usepkg but that was not working.
115 portage_flags = "--usepkgonly"
asharif1755b432013-02-15 04:55:29 +0000116 tc_enter_chroot_options.append("-s")
asharife2cca302013-02-15 04:35:42 +0000117
asharif19c73dd2013-02-15 04:35:37 +0000118 f = open(options.chromeos_root + "/src/overlays/overlay-" +
asharif1755b432013-02-15 04:55:29 +0000119 options.board.split("_")[0] + "/toolchain.conf", "r")
asharif19c73dd2013-02-15 04:35:37 +0000120 target = f.read()
121 f.close()
122 target = target.strip()
asharif2d815d02013-02-15 04:36:02 +0000123 features = "noclean userfetch userpriv usersandbox -strict"
asharif09bfb6f2013-02-15 04:35:44 +0000124 if options.incremental is not None and options.incremental:
125 features += " keepwork"
asharif19c73dd2013-02-15 04:35:37 +0000126 env = CreateEnvVarString(" FEATURES", features)
asharif80c6e552013-02-15 04:35:40 +0000127 env += CreateEnvVarString(" PORTAGE_USERNAME", getpass.getuser())
asharif0d3535a2013-02-15 04:50:33 +0000128 logdir = "/logs"
129 pkgdir = "/pkgs"
130 tmpdir = "/objects"
131 installdir = "/install"
132 package_dir = output + pkgdir
133 portage_logdir = chroot_output + logdir
134 portage_pkgdir = chroot_output + pkgdir
135 portage_tmpdir = chroot_output + tmpdir
136 env += CreateEnvVarString(" PORT_LOGDIR", portage_logdir)
137 env += CreateEnvVarString(" PKGDIR", portage_pkgdir)
138 env += CreateEnvVarString(" PORTAGE_BINHOST", portage_pkgdir)
asharif2dfbf512013-02-15 05:15:49 +0000139 if options.binary == False:
140 env += CreateEnvVarString(" PORTAGE_TMPDIR", portage_tmpdir)
asharif252df0f2013-02-15 04:46:28 +0000141 env += CreateEnvVarString(" USE", "mounted_sources")
asharif2d815d02013-02-15 04:36:02 +0000142
asharifd751e252013-02-15 04:35:52 +0000143 retval = 0
asharif80c6e552013-02-15 04:35:40 +0000144 if options.force == True:
asharifd751e252013-02-15 04:35:52 +0000145 retval = BuildTC(options.chromeos_root, options.toolchain_root, env,
asharif0d3535a2013-02-15 04:50:33 +0000146 target, True, options.incremental, portage_flags,
147 tc_enter_chroot_options)
asharifd751e252013-02-15 04:35:52 +0000148 retval = BuildTC(options.chromeos_root, options.toolchain_root, env,
asharif0d3535a2013-02-15 04:50:33 +0000149 target, options.clean, options.incremental, portage_flags,
150 tc_enter_chroot_options)
asharifd751e252013-02-15 04:35:52 +0000151 utils.AssertTrue(retval == 0, "Build toolchain failed!")
asharif0d3535a2013-02-15 04:50:33 +0000152 command = "sudo chown -R " + getpass.getuser() + " " + package_dir
asharifd751e252013-02-15 04:35:52 +0000153
154 if options.incremental is None and not options.clean:
asharif0d3535a2013-02-15 04:50:33 +0000155 install_dir = output + installdir
asharifd751e252013-02-15 04:35:52 +0000156 retval = InstallTC(package_dir, install_dir)
157 utils.AssertTrue(retval == 0, "Installation of the toolchain failed!")
158
159 return retval
asharife2cca302013-02-15 04:35:42 +0000160
161
162def CreateCrossdevPortageFlags(portage_flags):
asharif2d815d02013-02-15 04:36:02 +0000163 portage_flags = portage_flags.strip()
asharife2cca302013-02-15 04:35:42 +0000164 if not portage_flags:
165 return ""
166 crossdev_flags = " --portage "
167 crossdev_flags += " --portage ".join(portage_flags.split(" "))
168 return crossdev_flags
asharif19c73dd2013-02-15 04:35:37 +0000169
170
171def CreateEnvVarString(variable, value):
asharifc0f71932013-02-15 04:56:18 +0000172 return variable + "=\"" + value + "\""
asharif19c73dd2013-02-15 04:35:37 +0000173
174
175def EscapeQuoteString(string):
176 return "\\\"" + string + "\\\""
177
178
asharifd751e252013-02-15 04:35:52 +0000179def InstallTC(package_dir, install_dir):
asharif9994d292013-02-15 04:36:04 +0000180 command = ("mkdir -p " + install_dir)
181 command += ("&& for f in $(find " + package_dir +
ashariffcf8cfc2013-02-15 04:49:21 +0000182 " -name \\*.tbz2); do tar xf $f -C " +
183 install_dir + " ; done")
raymes01959ae2013-02-15 04:50:07 +0000184 retval = cmd_executer.RunCommand(command)
asharifd751e252013-02-15 04:35:52 +0000185 return retval
186
187
asharif19c73dd2013-02-15 04:35:37 +0000188def BuildTC(chromeos_root, toolchain_root, env, target, uninstall,
asharif0d3535a2013-02-15 04:50:33 +0000189 incremental_component, portage_flags, tc_enter_chroot_options):
asharif19c73dd2013-02-15 04:35:37 +0000190 """Build the toolchain."""
asharif2d815d02013-02-15 04:36:02 +0000191 portage_flags = portage_flags.strip()
192 portage_flags += " -b "
193
asharif28238cf2013-02-15 04:51:01 +0000194 binutils_version = "9999"
asharif19c73dd2013-02-15 04:35:37 +0000195 gcc_version = "9999"
asharif17621302013-02-15 04:46:35 +0000196 libc_version = "2.10.1-r2"
asharif19c73dd2013-02-15 04:35:37 +0000197 kernel_version = "2.6.30-r1"
asharif19c73dd2013-02-15 04:35:37 +0000198
raymes01959ae2013-02-15 04:50:07 +0000199 rootdir = utils.GetRoot(sys.argv[0])[0]
asharif2d815d02013-02-15 04:36:02 +0000200 env += " "
201
asharif19c73dd2013-02-15 04:35:37 +0000202 if uninstall == True:
203 tflag = " -C "
204 else:
205 tflag = " -t "
206
asharif0269d462013-02-15 04:46:31 +0000207 command = " -- sudo " + env
asharif2d815d02013-02-15 04:36:02 +0000208
209 if uninstall == True:
210 command += " crossdev " + tflag + target
asharif1755b432013-02-15 04:55:29 +0000211 enter_chroot = chromeos_root + "/src/scripts/enter_chroot.sh"
212 retval = cmd_executer.RunCommand(enter_chroot + command)
asharif2d815d02013-02-15 04:36:02 +0000213 return retval
asharif19c73dd2013-02-15 04:35:37 +0000214
215 if incremental_component == "binutils":
asharife2cca302013-02-15 04:35:42 +0000216 command += (" emerge =cross-" + target + "/binutils-" + binutils_version +
217 portage_flags)
asharif19c73dd2013-02-15 04:35:37 +0000218 elif incremental_component == "gcc":
asharife2cca302013-02-15 04:35:42 +0000219 command += (" emerge =cross-" + target + "/gcc-" + gcc_version +
220 portage_flags)
asharif19c73dd2013-02-15 04:35:37 +0000221 elif incremental_component == "libc" or incremental_component == "glibc":
asharife2cca302013-02-15 04:35:42 +0000222 command += (" emerge =cross-" + target + "/glibc-" + libc_version +
223 portage_flags)
asharif19c73dd2013-02-15 04:35:37 +0000224 else:
asharif2d815d02013-02-15 04:36:02 +0000225 crossdev_flags = CreateCrossdevPortageFlags(portage_flags)
asharif19c73dd2013-02-15 04:35:37 +0000226 command += (" crossdev -v " + tflag + target +
227 " --binutils " + binutils_version +
228 " --libc " + libc_version +
229 " --gcc " + gcc_version +
230 " --kernel " + kernel_version +
asharif2d815d02013-02-15 04:36:02 +0000231 crossdev_flags)
asharifc0f71932013-02-15 04:56:18 +0000232 command += ("&& sudo cp -r $(" + env + " portageq envvar PKGDIR)/*" +
233 " /var/lib/portage/pkgs/")
asharif19c73dd2013-02-15 04:35:37 +0000234
asharif1755b432013-02-15 04:55:29 +0000235 argv = [rootdir + "/tc_enter_chroot.py",
236 "--chromeos_root=" + chromeos_root,
237 "--toolchain_root=" + toolchain_root]
238 argv += tc_enter_chroot_options
239
asharife3668f12013-02-15 04:46:29 +0000240 argv.append(command)
241 retval = tc_enter_chroot.Main(argv)
asharif19c73dd2013-02-15 04:35:37 +0000242 return retval
243
244if __name__ == "__main__":
asharif0d3535a2013-02-15 04:50:33 +0000245 Main(sys.argv)
asharifd751e252013-02-15 04:35:52 +0000246