blob: 46b5330e20e6335db41f891c377f539168657a44 [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",
60 default=rootdir + "/output",
61 help="The output directory where logs,pkgs, etc. go.")
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
asharif19c73dd2013-02-15 04:35:37 +000065 if options.toolchain_root is None or options.board is None:
66 parser.print_help()
67 sys.exit()
bjanakiraman7f4a4852013-02-15 04:35:28 +000068
asharif0d3535a2013-02-15 04:50:33 +000069 if options.chromeos_root is None:
70 if os.path.exists("enter_chroot.sh"):
71 options.chromeos_root = "../.."
72 else:
73 logger.GetLogger().LogError("--chromeos_root not given")
74 parser.print_help()
75 sys.exit()
76 else:
77 options.chromeos_root = os.path.expanduser(options.chromeos_root)
78
asharif88f01422013-02-15 04:50:35 +000079 if ((not os.path.exists(options.chromeos_root)) or
80 (not os.path.exists(options.chromeos_root +
81 "/src/scripts/enter_chroot.sh"))):
asharif0d3535a2013-02-15 04:50:33 +000082 logger.GetLogger().LogOutput("Creating a chromeos checkout at: %s" %
83 options.chromeos_root)
asharif88f01422013-02-15 04:50:35 +000084 sc_args = []
asharif6ba78c42013-02-15 04:50:40 +000085 sc_args.append("--minilayout")
asharif88f01422013-02-15 04:50:35 +000086 sc_args.append("--dir=%s" % options.chromeos_root)
87 if options.setup_chromeos_options:
88 sc_args.append(options.setup_chromeos_options)
asharif0d3535a2013-02-15 04:50:33 +000089 setup_chromeos.Main(sc_args)
90
91 output = options.output
92
93 if output.startswith("/") == False:
94 output = os.getcwd() + "/" + output
95 else:
96 output = os.path.expanduser(output)
97
98 chroot_mount = "/usr/local/toolchain_root/"
99 chroot_base = utils.GetRoot(output)[1]
100 chroot_output = chroot_mount + chroot_base
101
102 tc_enter_chroot_options = []
asharif541b6392013-02-15 04:50:38 +0000103 output_mount = ("--output=" + output)
asharif0d3535a2013-02-15 04:50:33 +0000104 tc_enter_chroot_options.append(output_mount)
105
asharife3668f12013-02-15 04:46:29 +0000106 build_chromeos.MakeChroot(options.chromeos_root)
107
asharife2cca302013-02-15 04:35:42 +0000108 portage_flags = ""
109 if options.binary == True:
110 # FIXME(asharif): This should be using --usepkg but that was not working.
111 portage_flags = "--usepkgonly"
112
asharif19c73dd2013-02-15 04:35:37 +0000113 f = open(options.chromeos_root + "/src/overlays/overlay-" +
114 options.board + "/toolchain.conf", "r")
115 target = f.read()
116 f.close()
117 target = target.strip()
asharif2d815d02013-02-15 04:36:02 +0000118 features = "noclean userfetch userpriv usersandbox -strict"
asharif09bfb6f2013-02-15 04:35:44 +0000119 if options.incremental is not None and options.incremental:
120 features += " keepwork"
asharif19c73dd2013-02-15 04:35:37 +0000121 env = CreateEnvVarString(" FEATURES", features)
asharif80c6e552013-02-15 04:35:40 +0000122 env += CreateEnvVarString(" PORTAGE_USERNAME", getpass.getuser())
asharif0d3535a2013-02-15 04:50:33 +0000123 logdir = "/logs"
124 pkgdir = "/pkgs"
125 tmpdir = "/objects"
126 installdir = "/install"
127 package_dir = output + pkgdir
128 portage_logdir = chroot_output + logdir
129 portage_pkgdir = chroot_output + pkgdir
130 portage_tmpdir = chroot_output + tmpdir
131 env += CreateEnvVarString(" PORT_LOGDIR", portage_logdir)
132 env += CreateEnvVarString(" PKGDIR", portage_pkgdir)
133 env += CreateEnvVarString(" PORTAGE_BINHOST", portage_pkgdir)
134 env += CreateEnvVarString(" PORTAGE_TMPDIR", portage_tmpdir)
asharif252df0f2013-02-15 04:46:28 +0000135 env += CreateEnvVarString(" USE", "mounted_sources")
asharif2d815d02013-02-15 04:36:02 +0000136
asharifd751e252013-02-15 04:35:52 +0000137 retval = 0
asharif80c6e552013-02-15 04:35:40 +0000138 if options.force == True:
asharifd751e252013-02-15 04:35:52 +0000139 retval = BuildTC(options.chromeos_root, options.toolchain_root, env,
asharif0d3535a2013-02-15 04:50:33 +0000140 target, True, options.incremental, portage_flags,
141 tc_enter_chroot_options)
asharifd751e252013-02-15 04:35:52 +0000142 retval = BuildTC(options.chromeos_root, options.toolchain_root, env,
asharif0d3535a2013-02-15 04:50:33 +0000143 target, options.clean, options.incremental, portage_flags,
144 tc_enter_chroot_options)
asharifd751e252013-02-15 04:35:52 +0000145 utils.AssertTrue(retval == 0, "Build toolchain failed!")
asharif0d3535a2013-02-15 04:50:33 +0000146 command = "sudo chown -R " + getpass.getuser() + " " + package_dir
asharifd751e252013-02-15 04:35:52 +0000147
148 if options.incremental is None and not options.clean:
asharif0d3535a2013-02-15 04:50:33 +0000149 install_dir = output + installdir
asharifd751e252013-02-15 04:35:52 +0000150 retval = InstallTC(package_dir, install_dir)
151 utils.AssertTrue(retval == 0, "Installation of the toolchain failed!")
152
153 return retval
asharife2cca302013-02-15 04:35:42 +0000154
155
156def CreateCrossdevPortageFlags(portage_flags):
asharif2d815d02013-02-15 04:36:02 +0000157 portage_flags = portage_flags.strip()
asharife2cca302013-02-15 04:35:42 +0000158 if not portage_flags:
159 return ""
160 crossdev_flags = " --portage "
161 crossdev_flags += " --portage ".join(portage_flags.split(" "))
162 return crossdev_flags
asharif19c73dd2013-02-15 04:35:37 +0000163
164
165def CreateEnvVarString(variable, value):
166 return variable + "=" + EscapeQuoteString(value)
167
168
169def EscapeQuoteString(string):
170 return "\\\"" + string + "\\\""
171
172
asharifd751e252013-02-15 04:35:52 +0000173def InstallTC(package_dir, install_dir):
asharif9994d292013-02-15 04:36:04 +0000174 command = ("mkdir -p " + install_dir)
175 command += ("&& for f in $(find " + package_dir +
ashariffcf8cfc2013-02-15 04:49:21 +0000176 " -name \\*.tbz2); do tar xf $f -C " +
177 install_dir + " ; done")
raymes01959ae2013-02-15 04:50:07 +0000178 retval = cmd_executer.RunCommand(command)
asharifd751e252013-02-15 04:35:52 +0000179 return retval
180
181
asharif19c73dd2013-02-15 04:35:37 +0000182def BuildTC(chromeos_root, toolchain_root, env, target, uninstall,
asharif0d3535a2013-02-15 04:50:33 +0000183 incremental_component, portage_flags, tc_enter_chroot_options):
asharif19c73dd2013-02-15 04:35:37 +0000184 """Build the toolchain."""
asharif2d815d02013-02-15 04:36:02 +0000185 portage_flags = portage_flags.strip()
186 portage_flags += " -b "
187
asharif28238cf2013-02-15 04:51:01 +0000188 binutils_version = "9999"
asharif19c73dd2013-02-15 04:35:37 +0000189 gcc_version = "9999"
asharif17621302013-02-15 04:46:35 +0000190 libc_version = "2.10.1-r2"
asharif19c73dd2013-02-15 04:35:37 +0000191 kernel_version = "2.6.30-r1"
asharif19c73dd2013-02-15 04:35:37 +0000192
raymes01959ae2013-02-15 04:50:07 +0000193 rootdir = utils.GetRoot(sys.argv[0])[0]
asharif0d3535a2013-02-15 04:50:33 +0000194 argv = [rootdir + "/tc_enter_chroot.py",
asharife3668f12013-02-15 04:46:29 +0000195 "--chromeos_root=" + chromeos_root,
196 "--toolchain_root=" + toolchain_root]
asharif0d3535a2013-02-15 04:50:33 +0000197 argv += tc_enter_chroot_options
asharif252df0f2013-02-15 04:46:28 +0000198
asharif2d815d02013-02-15 04:36:02 +0000199 env += " "
200
asharif19c73dd2013-02-15 04:35:37 +0000201 if uninstall == True:
202 tflag = " -C "
203 else:
204 tflag = " -t "
205
asharif0269d462013-02-15 04:46:31 +0000206 command = " -- sudo " + env
asharif2d815d02013-02-15 04:36:02 +0000207
208 if uninstall == True:
209 command += " crossdev " + tflag + target
asharife3668f12013-02-15 04:46:29 +0000210 argv.append(command)
211 retval = tc_enter_chroot.Main(argv)
asharif2d815d02013-02-15 04:36:02 +0000212 return retval
asharif19c73dd2013-02-15 04:35:37 +0000213
214 if incremental_component == "binutils":
asharife2cca302013-02-15 04:35:42 +0000215 command += (" emerge =cross-" + target + "/binutils-" + binutils_version +
216 portage_flags)
asharif19c73dd2013-02-15 04:35:37 +0000217 elif incremental_component == "gcc":
asharife2cca302013-02-15 04:35:42 +0000218 command += (" emerge =cross-" + target + "/gcc-" + gcc_version +
219 portage_flags)
asharif19c73dd2013-02-15 04:35:37 +0000220 elif incremental_component == "libc" or incremental_component == "glibc":
asharife2cca302013-02-15 04:35:42 +0000221 command += (" emerge =cross-" + target + "/glibc-" + libc_version +
222 portage_flags)
asharif19c73dd2013-02-15 04:35:37 +0000223 else:
asharif2d815d02013-02-15 04:36:02 +0000224 crossdev_flags = CreateCrossdevPortageFlags(portage_flags)
asharif19c73dd2013-02-15 04:35:37 +0000225 command += (" crossdev -v " + tflag + target +
226 " --binutils " + binutils_version +
227 " --libc " + libc_version +
228 " --gcc " + gcc_version +
229 " --kernel " + kernel_version +
asharif2d815d02013-02-15 04:36:02 +0000230 crossdev_flags)
asharif19c73dd2013-02-15 04:35:37 +0000231
asharife3668f12013-02-15 04:46:29 +0000232 argv.append(command)
233 retval = tc_enter_chroot.Main(argv)
asharif19c73dd2013-02-15 04:35:37 +0000234 return retval
235
236if __name__ == "__main__":
asharif0d3535a2013-02-15 04:50:33 +0000237 Main(sys.argv)
asharifd751e252013-02-15 04:35:52 +0000238