blob: ef48f365f92ccb8a826b48e5612f235385f70712 [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
raymes85ef5db2013-02-15 05:20:49 +000065 if (options.clean == False and
asharif1755b432013-02-15 04:55:29 +000066 (options.toolchain_root is None or options.board is None)):
asharif19c73dd2013-02-15 04:35:37 +000067 parser.print_help()
68 sys.exit()
asharif28875842013-02-15 05:15:52 +000069 else:
70 options.toolchain_root = os.path.expanduser(options.toolchain_root)
bjanakiraman7f4a4852013-02-15 04:35:28 +000071
asharif0d3535a2013-02-15 04:50:33 +000072 if options.chromeos_root is None:
73 if os.path.exists("enter_chroot.sh"):
74 options.chromeos_root = "../.."
75 else:
76 logger.GetLogger().LogError("--chromeos_root not given")
77 parser.print_help()
78 sys.exit()
79 else:
80 options.chromeos_root = os.path.expanduser(options.chromeos_root)
81
asharif88f01422013-02-15 04:50:35 +000082 if ((not os.path.exists(options.chromeos_root)) or
83 (not os.path.exists(options.chromeos_root +
84 "/src/scripts/enter_chroot.sh"))):
asharif0d3535a2013-02-15 04:50:33 +000085 logger.GetLogger().LogOutput("Creating a chromeos checkout at: %s" %
86 options.chromeos_root)
asharif88f01422013-02-15 04:50:35 +000087 sc_args = []
asharif6ba78c42013-02-15 04:50:40 +000088 sc_args.append("--minilayout")
asharif88f01422013-02-15 04:50:35 +000089 sc_args.append("--dir=%s" % options.chromeos_root)
90 if options.setup_chromeos_options:
91 sc_args.append(options.setup_chromeos_options)
asharif0d3535a2013-02-15 04:50:33 +000092 setup_chromeos.Main(sc_args)
93
asharif1ba8a3b2013-02-15 04:56:50 +000094 if options.output is None:
95 output = options.toolchain_root + "/output"
96 else:
97 output = options.output
asharif0d3535a2013-02-15 04:50:33 +000098
99 if output.startswith("/") == False:
100 output = os.getcwd() + "/" + output
101 else:
102 output = os.path.expanduser(output)
103
104 chroot_mount = "/usr/local/toolchain_root/"
105 chroot_base = utils.GetRoot(output)[1]
106 chroot_output = chroot_mount + chroot_base
107
108 tc_enter_chroot_options = []
asharif541b6392013-02-15 04:50:38 +0000109 output_mount = ("--output=" + output)
asharif0d3535a2013-02-15 04:50:33 +0000110 tc_enter_chroot_options.append(output_mount)
111
asharife3668f12013-02-15 04:46:29 +0000112 build_chromeos.MakeChroot(options.chromeos_root)
113
asharif8697d4e2013-02-15 09:18:09 +0000114 portage_flags = "--oneshot"
asharife2cca302013-02-15 04:35:42 +0000115 if options.binary == True:
116 # FIXME(asharif): This should be using --usepkg but that was not working.
117 portage_flags = "--usepkgonly"
asharif1755b432013-02-15 04:55:29 +0000118 tc_enter_chroot_options.append("-s")
asharife2cca302013-02-15 04:35:42 +0000119
asharif19c73dd2013-02-15 04:35:37 +0000120 f = open(options.chromeos_root + "/src/overlays/overlay-" +
asharif1755b432013-02-15 04:55:29 +0000121 options.board.split("_")[0] + "/toolchain.conf", "r")
asharif19c73dd2013-02-15 04:35:37 +0000122 target = f.read()
123 f.close()
124 target = target.strip()
asharif2d815d02013-02-15 04:36:02 +0000125 features = "noclean userfetch userpriv usersandbox -strict"
asharif09bfb6f2013-02-15 04:35:44 +0000126 if options.incremental is not None and options.incremental:
127 features += " keepwork"
asharif19c73dd2013-02-15 04:35:37 +0000128 env = CreateEnvVarString(" FEATURES", features)
asharif80c6e552013-02-15 04:35:40 +0000129 env += CreateEnvVarString(" PORTAGE_USERNAME", getpass.getuser())
asharif0d3535a2013-02-15 04:50:33 +0000130 logdir = "/logs"
131 pkgdir = "/pkgs"
132 tmpdir = "/objects"
133 installdir = "/install"
asharif415e6632013-02-15 09:04:05 +0000134 for out_dir in [logdir, pkgdir, tmpdir, installdir]:
135 out_dir_path = output + "/" + out_dir
136 if os.path.isdir(out_dir_path):
137 continue
138 else:
139 os.makedirs(out_dir_path)
asharif0d3535a2013-02-15 04:50:33 +0000140 package_dir = output + pkgdir
141 portage_logdir = chroot_output + logdir
142 portage_pkgdir = chroot_output + pkgdir
143 portage_tmpdir = chroot_output + tmpdir
144 env += CreateEnvVarString(" PORT_LOGDIR", portage_logdir)
145 env += CreateEnvVarString(" PKGDIR", portage_pkgdir)
146 env += CreateEnvVarString(" PORTAGE_BINHOST", portage_pkgdir)
asharif2dfbf512013-02-15 05:15:49 +0000147 if options.binary == False:
148 env += CreateEnvVarString(" PORTAGE_TMPDIR", portage_tmpdir)
asharif252df0f2013-02-15 04:46:28 +0000149 env += CreateEnvVarString(" USE", "mounted_sources")
asharif2d815d02013-02-15 04:36:02 +0000150
asharifd751e252013-02-15 04:35:52 +0000151 retval = 0
asharif80c6e552013-02-15 04:35:40 +0000152 if options.force == True:
asharifd751e252013-02-15 04:35:52 +0000153 retval = BuildTC(options.chromeos_root, options.toolchain_root, env,
asharif0d3535a2013-02-15 04:50:33 +0000154 target, True, options.incremental, portage_flags,
155 tc_enter_chroot_options)
asharifd751e252013-02-15 04:35:52 +0000156 retval = BuildTC(options.chromeos_root, options.toolchain_root, env,
asharif0d3535a2013-02-15 04:50:33 +0000157 target, options.clean, options.incremental, portage_flags,
158 tc_enter_chroot_options)
asharifd751e252013-02-15 04:35:52 +0000159 utils.AssertTrue(retval == 0, "Build toolchain failed!")
asharif0d3535a2013-02-15 04:50:33 +0000160 command = "sudo chown -R " + getpass.getuser() + " " + package_dir
asharifd751e252013-02-15 04:35:52 +0000161
162 if options.incremental is None and not options.clean:
asharif0d3535a2013-02-15 04:50:33 +0000163 install_dir = output + installdir
asharifd751e252013-02-15 04:35:52 +0000164 retval = InstallTC(package_dir, install_dir)
165 utils.AssertTrue(retval == 0, "Installation of the toolchain failed!")
166
167 return retval
asharife2cca302013-02-15 04:35:42 +0000168
169
170def CreateCrossdevPortageFlags(portage_flags):
asharif2d815d02013-02-15 04:36:02 +0000171 portage_flags = portage_flags.strip()
asharife2cca302013-02-15 04:35:42 +0000172 if not portage_flags:
173 return ""
174 crossdev_flags = " --portage "
175 crossdev_flags += " --portage ".join(portage_flags.split(" "))
176 return crossdev_flags
asharif19c73dd2013-02-15 04:35:37 +0000177
178
179def CreateEnvVarString(variable, value):
asharifc0f71932013-02-15 04:56:18 +0000180 return variable + "=\"" + value + "\""
asharif19c73dd2013-02-15 04:35:37 +0000181
182
183def EscapeQuoteString(string):
184 return "\\\"" + string + "\\\""
185
186
asharifd751e252013-02-15 04:35:52 +0000187def InstallTC(package_dir, install_dir):
asharif9994d292013-02-15 04:36:04 +0000188 command = ("mkdir -p " + install_dir)
189 command += ("&& for f in $(find " + package_dir +
ashariffcf8cfc2013-02-15 04:49:21 +0000190 " -name \\*.tbz2); do tar xf $f -C " +
191 install_dir + " ; done")
raymes01959ae2013-02-15 04:50:07 +0000192 retval = cmd_executer.RunCommand(command)
asharifd751e252013-02-15 04:35:52 +0000193 return retval
194
195
asharif19c73dd2013-02-15 04:35:37 +0000196def BuildTC(chromeos_root, toolchain_root, env, target, uninstall,
asharif0d3535a2013-02-15 04:50:33 +0000197 incremental_component, portage_flags, tc_enter_chroot_options):
asharif19c73dd2013-02-15 04:35:37 +0000198 """Build the toolchain."""
asharif2d815d02013-02-15 04:36:02 +0000199 portage_flags = portage_flags.strip()
200 portage_flags += " -b "
201
asharif28238cf2013-02-15 04:51:01 +0000202 binutils_version = "9999"
asharif19c73dd2013-02-15 04:35:37 +0000203 gcc_version = "9999"
asharif17621302013-02-15 04:46:35 +0000204 libc_version = "2.10.1-r2"
asharif19c73dd2013-02-15 04:35:37 +0000205 kernel_version = "2.6.30-r1"
asharif19c73dd2013-02-15 04:35:37 +0000206
raymes01959ae2013-02-15 04:50:07 +0000207 rootdir = utils.GetRoot(sys.argv[0])[0]
asharif2d815d02013-02-15 04:36:02 +0000208 env += " "
209
asharif19c73dd2013-02-15 04:35:37 +0000210 if uninstall == True:
211 tflag = " -C "
212 else:
213 tflag = " -t "
214
asharif0269d462013-02-15 04:46:31 +0000215 command = " -- sudo " + env
asharif2d815d02013-02-15 04:36:02 +0000216
217 if uninstall == True:
asharif8697d4e2013-02-15 09:18:09 +0000218 command += "yes | crossdev " + tflag + target
raymes85ef5db2013-02-15 05:20:49 +0000219 retval = build_chromeos.ExecuteCommandInChroot(chromeos_root,
asharif8697d4e2013-02-15 09:18:09 +0000220 command,
221 toolchain_root)
asharif2d815d02013-02-15 04:36:02 +0000222 return retval
asharif19c73dd2013-02-15 04:35:37 +0000223
224 if incremental_component == "binutils":
asharife2cca302013-02-15 04:35:42 +0000225 command += (" emerge =cross-" + target + "/binutils-" + binutils_version +
226 portage_flags)
asharif19c73dd2013-02-15 04:35:37 +0000227 elif incremental_component == "gcc":
asharife2cca302013-02-15 04:35:42 +0000228 command += (" emerge =cross-" + target + "/gcc-" + gcc_version +
229 portage_flags)
asharif19c73dd2013-02-15 04:35:37 +0000230 elif incremental_component == "libc" or incremental_component == "glibc":
asharife2cca302013-02-15 04:35:42 +0000231 command += (" emerge =cross-" + target + "/glibc-" + libc_version +
232 portage_flags)
asharif19c73dd2013-02-15 04:35:37 +0000233 else:
asharif2d815d02013-02-15 04:36:02 +0000234 crossdev_flags = CreateCrossdevPortageFlags(portage_flags)
asharif19c73dd2013-02-15 04:35:37 +0000235 command += (" crossdev -v " + tflag + target +
236 " --binutils " + binutils_version +
237 " --libc " + libc_version +
238 " --gcc " + gcc_version +
239 " --kernel " + kernel_version +
asharif2d815d02013-02-15 04:36:02 +0000240 crossdev_flags)
asharifc0f71932013-02-15 04:56:18 +0000241 command += ("&& sudo cp -r $(" + env + " portageq envvar PKGDIR)/*" +
242 " /var/lib/portage/pkgs/")
asharif19c73dd2013-02-15 04:35:37 +0000243
asharif1755b432013-02-15 04:55:29 +0000244 argv = [rootdir + "/tc_enter_chroot.py",
245 "--chromeos_root=" + chromeos_root,
246 "--toolchain_root=" + toolchain_root]
247 argv += tc_enter_chroot_options
248
asharife3668f12013-02-15 04:46:29 +0000249 argv.append(command)
250 retval = tc_enter_chroot.Main(argv)
asharif19c73dd2013-02-15 04:35:37 +0000251 return retval
252
253if __name__ == "__main__":
asharif0d3535a2013-02-15 04:50:33 +0000254 Main(sys.argv)
asharifd751e252013-02-15 04:35:52 +0000255