blob: 6cc9ab3723bf1247576f9ce4513aa63f6e155df0 [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
asharife2cca302013-02-15 04:35:42 +0000114 portage_flags = ""
115 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"
134 package_dir = output + pkgdir
135 portage_logdir = chroot_output + logdir
136 portage_pkgdir = chroot_output + pkgdir
137 portage_tmpdir = chroot_output + tmpdir
138 env += CreateEnvVarString(" PORT_LOGDIR", portage_logdir)
139 env += CreateEnvVarString(" PKGDIR", portage_pkgdir)
140 env += CreateEnvVarString(" PORTAGE_BINHOST", portage_pkgdir)
asharif2dfbf512013-02-15 05:15:49 +0000141 if options.binary == False:
142 env += CreateEnvVarString(" PORTAGE_TMPDIR", portage_tmpdir)
asharif252df0f2013-02-15 04:46:28 +0000143 env += CreateEnvVarString(" USE", "mounted_sources")
asharif2d815d02013-02-15 04:36:02 +0000144
asharifd751e252013-02-15 04:35:52 +0000145 retval = 0
asharif80c6e552013-02-15 04:35:40 +0000146 if options.force == True:
asharifd751e252013-02-15 04:35:52 +0000147 retval = BuildTC(options.chromeos_root, options.toolchain_root, env,
asharif0d3535a2013-02-15 04:50:33 +0000148 target, True, options.incremental, portage_flags,
149 tc_enter_chroot_options)
asharifd751e252013-02-15 04:35:52 +0000150 retval = BuildTC(options.chromeos_root, options.toolchain_root, env,
asharif0d3535a2013-02-15 04:50:33 +0000151 target, options.clean, options.incremental, portage_flags,
152 tc_enter_chroot_options)
asharifd751e252013-02-15 04:35:52 +0000153 utils.AssertTrue(retval == 0, "Build toolchain failed!")
asharif0d3535a2013-02-15 04:50:33 +0000154 command = "sudo chown -R " + getpass.getuser() + " " + package_dir
asharifd751e252013-02-15 04:35:52 +0000155
156 if options.incremental is None and not options.clean:
asharif0d3535a2013-02-15 04:50:33 +0000157 install_dir = output + installdir
asharifd751e252013-02-15 04:35:52 +0000158 retval = InstallTC(package_dir, install_dir)
159 utils.AssertTrue(retval == 0, "Installation of the toolchain failed!")
160
161 return retval
asharife2cca302013-02-15 04:35:42 +0000162
163
164def CreateCrossdevPortageFlags(portage_flags):
asharif2d815d02013-02-15 04:36:02 +0000165 portage_flags = portage_flags.strip()
asharife2cca302013-02-15 04:35:42 +0000166 if not portage_flags:
167 return ""
168 crossdev_flags = " --portage "
169 crossdev_flags += " --portage ".join(portage_flags.split(" "))
170 return crossdev_flags
asharif19c73dd2013-02-15 04:35:37 +0000171
172
173def CreateEnvVarString(variable, value):
asharifc0f71932013-02-15 04:56:18 +0000174 return variable + "=\"" + value + "\""
asharif19c73dd2013-02-15 04:35:37 +0000175
176
177def EscapeQuoteString(string):
178 return "\\\"" + string + "\\\""
179
180
asharifd751e252013-02-15 04:35:52 +0000181def InstallTC(package_dir, install_dir):
asharif9994d292013-02-15 04:36:04 +0000182 command = ("mkdir -p " + install_dir)
183 command += ("&& for f in $(find " + package_dir +
ashariffcf8cfc2013-02-15 04:49:21 +0000184 " -name \\*.tbz2); do tar xf $f -C " +
185 install_dir + " ; done")
raymes01959ae2013-02-15 04:50:07 +0000186 retval = cmd_executer.RunCommand(command)
asharifd751e252013-02-15 04:35:52 +0000187 return retval
188
189
asharif19c73dd2013-02-15 04:35:37 +0000190def BuildTC(chromeos_root, toolchain_root, env, target, uninstall,
asharif0d3535a2013-02-15 04:50:33 +0000191 incremental_component, portage_flags, tc_enter_chroot_options):
asharif19c73dd2013-02-15 04:35:37 +0000192 """Build the toolchain."""
asharif2d815d02013-02-15 04:36:02 +0000193 portage_flags = portage_flags.strip()
194 portage_flags += " -b "
195
asharif28238cf2013-02-15 04:51:01 +0000196 binutils_version = "9999"
asharif19c73dd2013-02-15 04:35:37 +0000197 gcc_version = "9999"
asharif17621302013-02-15 04:46:35 +0000198 libc_version = "2.10.1-r2"
asharif19c73dd2013-02-15 04:35:37 +0000199 kernel_version = "2.6.30-r1"
asharif19c73dd2013-02-15 04:35:37 +0000200
raymes01959ae2013-02-15 04:50:07 +0000201 rootdir = utils.GetRoot(sys.argv[0])[0]
asharif2d815d02013-02-15 04:36:02 +0000202 env += " "
203
asharif19c73dd2013-02-15 04:35:37 +0000204 if uninstall == True:
205 tflag = " -C "
206 else:
207 tflag = " -t "
208
asharif0269d462013-02-15 04:46:31 +0000209 command = " -- sudo " + env
asharif2d815d02013-02-15 04:36:02 +0000210
211 if uninstall == True:
212 command += " crossdev " + tflag + target
raymes85ef5db2013-02-15 05:20:49 +0000213 retval = build_chromeos.ExecuteCommandInChroot(chromeos_root,
214 toolchain_root,
215 command)
asharif2d815d02013-02-15 04:36:02 +0000216 return retval
asharif19c73dd2013-02-15 04:35:37 +0000217
218 if incremental_component == "binutils":
asharife2cca302013-02-15 04:35:42 +0000219 command += (" emerge =cross-" + target + "/binutils-" + binutils_version +
220 portage_flags)
asharif19c73dd2013-02-15 04:35:37 +0000221 elif incremental_component == "gcc":
asharife2cca302013-02-15 04:35:42 +0000222 command += (" emerge =cross-" + target + "/gcc-" + gcc_version +
223 portage_flags)
asharif19c73dd2013-02-15 04:35:37 +0000224 elif incremental_component == "libc" or incremental_component == "glibc":
asharife2cca302013-02-15 04:35:42 +0000225 command += (" emerge =cross-" + target + "/glibc-" + libc_version +
226 portage_flags)
asharif19c73dd2013-02-15 04:35:37 +0000227 else:
asharif2d815d02013-02-15 04:36:02 +0000228 crossdev_flags = CreateCrossdevPortageFlags(portage_flags)
asharif19c73dd2013-02-15 04:35:37 +0000229 command += (" crossdev -v " + tflag + target +
230 " --binutils " + binutils_version +
231 " --libc " + libc_version +
232 " --gcc " + gcc_version +
233 " --kernel " + kernel_version +
asharif2d815d02013-02-15 04:36:02 +0000234 crossdev_flags)
asharifc0f71932013-02-15 04:56:18 +0000235 command += ("&& sudo cp -r $(" + env + " portageq envvar PKGDIR)/*" +
236 " /var/lib/portage/pkgs/")
asharif19c73dd2013-02-15 04:35:37 +0000237
asharif1755b432013-02-15 04:55:29 +0000238 argv = [rootdir + "/tc_enter_chroot.py",
239 "--chromeos_root=" + chromeos_root,
240 "--toolchain_root=" + toolchain_root]
241 argv += tc_enter_chroot_options
242
asharife3668f12013-02-15 04:46:29 +0000243 argv.append(command)
244 retval = tc_enter_chroot.Main(argv)
asharif19c73dd2013-02-15 04:35:37 +0000245 return retval
246
247if __name__ == "__main__":
asharif0d3535a2013-02-15 04:50:33 +0000248 Main(sys.argv)
asharifd751e252013-02-15 04:35:52 +0000249