blob: b968e37601ca5ac5383900aff14584460b75a551 [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
23# Common initializations
raymes01959ae2013-02-15 04:50:07 +000024cmd_executer = command_executer.GetCommandExecuter()
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."""
asharif0d3535a2013-02-15 04:50:33 +000029 rootdir = utils.GetRoot(sys.argv[0])[0]
30
asharif19c73dd2013-02-15 04:35:37 +000031 parser = optparse.OptionParser()
32 parser.add_option("-c", "--chromeos_root", dest="chromeos_root",
asharif0d3535a2013-02-15 04:50:33 +000033 help=("ChromeOS root checkout directory" +
34 " uses ../.. if none given."))
asharif19c73dd2013-02-15 04:35:37 +000035 parser.add_option("-t", "--toolchain_root", dest="toolchain_root",
36 help="Toolchain root directory.")
asharifd751e252013-02-15 04:35:52 +000037 parser.add_option("-b", "--board", dest="board", default="x86-generic",
asharif19c73dd2013-02-15 04:35:37 +000038 help="board is the argument to the setup_board command.")
asharifd751e252013-02-15 04:35:52 +000039 parser.add_option("-C", "--clean", dest="clean", default=False,
40 action="store_true",
asharife2cca302013-02-15 04:35:42 +000041 help="Uninstall the toolchain.")
asharifd751e252013-02-15 04:35:52 +000042 parser.add_option("-f", "--force", dest="force", default=False,
43 action="store_true",
asharife2cca302013-02-15 04:35:42 +000044 help="Do an uninstall/install cycle.")
asharif19c73dd2013-02-15 04:35:37 +000045 parser.add_option("-i", "--incremental", dest="incremental",
46 help="The toolchain component that should be "
47 "incrementally compiled.")
asharife2cca302013-02-15 04:35:42 +000048 parser.add_option("-B", "--binary", dest="binary",
49 action="store_true", default=False,
50 help="The toolchain should use binaries stored in "
51 "the install/ directory.")
asharif0d3535a2013-02-15 04:50:33 +000052 parser.add_option("-s", "--setup-chromeos-options",
53 dest="setup_chromeos_options",
54 help="Additional options that should be passed on to"
55 "the setup_chromeos script.")
56 parser.add_option("-o", "--output", dest="output",
57 default=rootdir + "/output",
58 help="The output directory where logs,pkgs, etc. go.")
bjanakiraman7f4a4852013-02-15 04:35:28 +000059
asharif0d3535a2013-02-15 04:50:33 +000060 options = parser.parse_args(argv)[0]
asharif17621302013-02-15 04:46:35 +000061
asharif19c73dd2013-02-15 04:35:37 +000062 if options.toolchain_root is None or options.board is None:
63 parser.print_help()
64 sys.exit()
bjanakiraman7f4a4852013-02-15 04:35:28 +000065
asharif0d3535a2013-02-15 04:50:33 +000066 if options.chromeos_root is None:
67 if os.path.exists("enter_chroot.sh"):
68 options.chromeos_root = "../.."
69 else:
70 logger.GetLogger().LogError("--chromeos_root not given")
71 parser.print_help()
72 sys.exit()
73 else:
74 options.chromeos_root = os.path.expanduser(options.chromeos_root)
75
asharif88f01422013-02-15 04:50:35 +000076 if ((not os.path.exists(options.chromeos_root)) or
77 (not os.path.exists(options.chromeos_root +
78 "/src/scripts/enter_chroot.sh"))):
asharif0d3535a2013-02-15 04:50:33 +000079 logger.GetLogger().LogOutput("Creating a chromeos checkout at: %s" %
80 options.chromeos_root)
asharif88f01422013-02-15 04:50:35 +000081 sc_args = []
asharif6ba78c42013-02-15 04:50:40 +000082 sc_args.append("--minilayout")
asharif88f01422013-02-15 04:50:35 +000083 sc_args.append("--dir=%s" % options.chromeos_root)
84 if options.setup_chromeos_options:
85 sc_args.append(options.setup_chromeos_options)
asharif0d3535a2013-02-15 04:50:33 +000086 setup_chromeos.Main(sc_args)
87
88 output = options.output
89
90 if output.startswith("/") == False:
91 output = os.getcwd() + "/" + output
92 else:
93 output = os.path.expanduser(output)
94
95 chroot_mount = "/usr/local/toolchain_root/"
96 chroot_base = utils.GetRoot(output)[1]
97 chroot_output = chroot_mount + chroot_base
98
99 tc_enter_chroot_options = []
asharif541b6392013-02-15 04:50:38 +0000100 output_mount = ("--output=" + output)
asharif0d3535a2013-02-15 04:50:33 +0000101 tc_enter_chroot_options.append(output_mount)
102
asharife3668f12013-02-15 04:46:29 +0000103 build_chromeos.MakeChroot(options.chromeos_root)
104
asharife2cca302013-02-15 04:35:42 +0000105 portage_flags = ""
106 if options.binary == True:
107 # FIXME(asharif): This should be using --usepkg but that was not working.
108 portage_flags = "--usepkgonly"
109
asharif19c73dd2013-02-15 04:35:37 +0000110 f = open(options.chromeos_root + "/src/overlays/overlay-" +
111 options.board + "/toolchain.conf", "r")
112 target = f.read()
113 f.close()
114 target = target.strip()
asharif2d815d02013-02-15 04:36:02 +0000115 features = "noclean userfetch userpriv usersandbox -strict"
asharif09bfb6f2013-02-15 04:35:44 +0000116 if options.incremental is not None and options.incremental:
117 features += " keepwork"
asharif19c73dd2013-02-15 04:35:37 +0000118 env = CreateEnvVarString(" FEATURES", features)
asharif80c6e552013-02-15 04:35:40 +0000119 env += CreateEnvVarString(" PORTAGE_USERNAME", getpass.getuser())
asharif0d3535a2013-02-15 04:50:33 +0000120 logdir = "/logs"
121 pkgdir = "/pkgs"
122 tmpdir = "/objects"
123 installdir = "/install"
124 package_dir = output + pkgdir
125 portage_logdir = chroot_output + logdir
126 portage_pkgdir = chroot_output + pkgdir
127 portage_tmpdir = chroot_output + tmpdir
128 env += CreateEnvVarString(" PORT_LOGDIR", portage_logdir)
129 env += CreateEnvVarString(" PKGDIR", portage_pkgdir)
130 env += CreateEnvVarString(" PORTAGE_BINHOST", portage_pkgdir)
131 env += CreateEnvVarString(" PORTAGE_TMPDIR", portage_tmpdir)
asharif252df0f2013-02-15 04:46:28 +0000132 env += CreateEnvVarString(" USE", "mounted_sources")
asharif2d815d02013-02-15 04:36:02 +0000133
asharifd751e252013-02-15 04:35:52 +0000134 retval = 0
asharif80c6e552013-02-15 04:35:40 +0000135 if options.force == True:
asharifd751e252013-02-15 04:35:52 +0000136 retval = BuildTC(options.chromeos_root, options.toolchain_root, env,
asharif0d3535a2013-02-15 04:50:33 +0000137 target, True, options.incremental, portage_flags,
138 tc_enter_chroot_options)
asharifd751e252013-02-15 04:35:52 +0000139 retval = BuildTC(options.chromeos_root, options.toolchain_root, env,
asharif0d3535a2013-02-15 04:50:33 +0000140 target, options.clean, options.incremental, portage_flags,
141 tc_enter_chroot_options)
asharifd751e252013-02-15 04:35:52 +0000142 utils.AssertTrue(retval == 0, "Build toolchain failed!")
asharif0d3535a2013-02-15 04:50:33 +0000143 command = "sudo chown -R " + getpass.getuser() + " " + package_dir
asharifd751e252013-02-15 04:35:52 +0000144
145 if options.incremental is None and not options.clean:
asharif0d3535a2013-02-15 04:50:33 +0000146 install_dir = output + installdir
asharifd751e252013-02-15 04:35:52 +0000147 retval = InstallTC(package_dir, install_dir)
148 utils.AssertTrue(retval == 0, "Installation of the toolchain failed!")
149
150 return retval
asharife2cca302013-02-15 04:35:42 +0000151
152
153def CreateCrossdevPortageFlags(portage_flags):
asharif2d815d02013-02-15 04:36:02 +0000154 portage_flags = portage_flags.strip()
asharife2cca302013-02-15 04:35:42 +0000155 if not portage_flags:
156 return ""
157 crossdev_flags = " --portage "
158 crossdev_flags += " --portage ".join(portage_flags.split(" "))
159 return crossdev_flags
asharif19c73dd2013-02-15 04:35:37 +0000160
161
162def CreateEnvVarString(variable, value):
163 return variable + "=" + EscapeQuoteString(value)
164
165
166def EscapeQuoteString(string):
167 return "\\\"" + string + "\\\""
168
169
asharifd751e252013-02-15 04:35:52 +0000170def InstallTC(package_dir, install_dir):
asharif9994d292013-02-15 04:36:04 +0000171 command = ("mkdir -p " + install_dir)
172 command += ("&& for f in $(find " + package_dir +
ashariffcf8cfc2013-02-15 04:49:21 +0000173 " -name \\*.tbz2); do tar xf $f -C " +
174 install_dir + " ; done")
raymes01959ae2013-02-15 04:50:07 +0000175 retval = cmd_executer.RunCommand(command)
asharifd751e252013-02-15 04:35:52 +0000176 return retval
177
178
asharif19c73dd2013-02-15 04:35:37 +0000179def BuildTC(chromeos_root, toolchain_root, env, target, uninstall,
asharif0d3535a2013-02-15 04:50:33 +0000180 incremental_component, portage_flags, tc_enter_chroot_options):
asharif19c73dd2013-02-15 04:35:37 +0000181 """Build the toolchain."""
asharif2d815d02013-02-15 04:36:02 +0000182 portage_flags = portage_flags.strip()
183 portage_flags += " -b "
184
asharif19c73dd2013-02-15 04:35:37 +0000185 binutils_version = "2.20.1-r1"
186 gcc_version = "9999"
asharif17621302013-02-15 04:46:35 +0000187 libc_version = "2.10.1-r2"
asharif19c73dd2013-02-15 04:35:37 +0000188 kernel_version = "2.6.30-r1"
asharif19c73dd2013-02-15 04:35:37 +0000189
raymes01959ae2013-02-15 04:50:07 +0000190 rootdir = utils.GetRoot(sys.argv[0])[0]
asharif0d3535a2013-02-15 04:50:33 +0000191 argv = [rootdir + "/tc_enter_chroot.py",
asharife3668f12013-02-15 04:46:29 +0000192 "--chromeos_root=" + chromeos_root,
193 "--toolchain_root=" + toolchain_root]
asharif0d3535a2013-02-15 04:50:33 +0000194 argv += tc_enter_chroot_options
asharif252df0f2013-02-15 04:46:28 +0000195
asharif2d815d02013-02-15 04:36:02 +0000196 env += " "
197
asharif19c73dd2013-02-15 04:35:37 +0000198 if uninstall == True:
199 tflag = " -C "
200 else:
201 tflag = " -t "
202
asharif0269d462013-02-15 04:46:31 +0000203 command = " -- sudo " + env
asharif2d815d02013-02-15 04:36:02 +0000204
205 if uninstall == True:
206 command += " crossdev " + tflag + target
asharife3668f12013-02-15 04:46:29 +0000207 argv.append(command)
208 retval = tc_enter_chroot.Main(argv)
asharif2d815d02013-02-15 04:36:02 +0000209 return retval
asharif19c73dd2013-02-15 04:35:37 +0000210
211 if incremental_component == "binutils":
asharife2cca302013-02-15 04:35:42 +0000212 command += (" emerge =cross-" + target + "/binutils-" + binutils_version +
213 portage_flags)
asharif19c73dd2013-02-15 04:35:37 +0000214 elif incremental_component == "gcc":
asharife2cca302013-02-15 04:35:42 +0000215 command += (" emerge =cross-" + target + "/gcc-" + gcc_version +
216 portage_flags)
asharif19c73dd2013-02-15 04:35:37 +0000217 elif incremental_component == "libc" or incremental_component == "glibc":
asharife2cca302013-02-15 04:35:42 +0000218 command += (" emerge =cross-" + target + "/glibc-" + libc_version +
219 portage_flags)
asharif19c73dd2013-02-15 04:35:37 +0000220 else:
asharif2d815d02013-02-15 04:36:02 +0000221 crossdev_flags = CreateCrossdevPortageFlags(portage_flags)
asharif19c73dd2013-02-15 04:35:37 +0000222 command += (" crossdev -v " + tflag + target +
223 " --binutils " + binutils_version +
224 " --libc " + libc_version +
225 " --gcc " + gcc_version +
226 " --kernel " + kernel_version +
asharif2d815d02013-02-15 04:36:02 +0000227 crossdev_flags)
asharif19c73dd2013-02-15 04:35:37 +0000228
asharife3668f12013-02-15 04:46:29 +0000229 argv.append(command)
230 retval = tc_enter_chroot.Main(argv)
asharif19c73dd2013-02-15 04:35:37 +0000231 return retval
232
233if __name__ == "__main__":
asharif0d3535a2013-02-15 04:50:33 +0000234 Main(sys.argv)
asharifd751e252013-02-15 04:35:52 +0000235