blob: 0d8130877072f1c18a5f499bde40c9f575405e29 [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
14import sys
15from utils import utils
16
17# Common initializations
18(rootdir, basename) = utils.GetRoot(sys.argv[0])
19utils.InitLogger(rootdir, basename)
20
bjanakiraman7f4a4852013-02-15 04:35:28 +000021
asharif19c73dd2013-02-15 04:35:37 +000022def Main():
23 """The main function."""
24 parser = optparse.OptionParser()
25 parser.add_option("-c", "--chromeos_root", dest="chromeos_root",
asharifd751e252013-02-15 04:35:52 +000026 default="../..",
asharif19c73dd2013-02-15 04:35:37 +000027 help="ChromeOS root checkout directory.")
28 parser.add_option("-t", "--toolchain_root", dest="toolchain_root",
29 help="Toolchain root directory.")
asharifd751e252013-02-15 04:35:52 +000030 parser.add_option("-b", "--board", dest="board", default="x86-generic",
asharif19c73dd2013-02-15 04:35:37 +000031 help="board is the argument to the setup_board command.")
asharifd751e252013-02-15 04:35:52 +000032 parser.add_option("-C", "--clean", dest="clean", default=False,
33 action="store_true",
asharife2cca302013-02-15 04:35:42 +000034 help="Uninstall the toolchain.")
asharifd751e252013-02-15 04:35:52 +000035 parser.add_option("-f", "--force", dest="force", default=False,
36 action="store_true",
asharife2cca302013-02-15 04:35:42 +000037 help="Do an uninstall/install cycle.")
asharif19c73dd2013-02-15 04:35:37 +000038 parser.add_option("-i", "--incremental", dest="incremental",
39 help="The toolchain component that should be "
40 "incrementally compiled.")
asharife2cca302013-02-15 04:35:42 +000041 parser.add_option("-B", "--binary", dest="binary",
42 action="store_true", default=False,
43 help="The toolchain should use binaries stored in "
44 "the install/ directory.")
bjanakiraman7f4a4852013-02-15 04:35:28 +000045
asharif19c73dd2013-02-15 04:35:37 +000046 options = parser.parse_args()[0]
bjanakiraman7f4a4852013-02-15 04:35:28 +000047
asharif19c73dd2013-02-15 04:35:37 +000048 if options.toolchain_root is None or options.board is None:
49 parser.print_help()
50 sys.exit()
bjanakiraman7f4a4852013-02-15 04:35:28 +000051
asharife2cca302013-02-15 04:35:42 +000052 portage_flags = ""
53 if options.binary == True:
54 # FIXME(asharif): This should be using --usepkg but that was not working.
55 portage_flags = "--usepkgonly"
56
asharif19c73dd2013-02-15 04:35:37 +000057 f = open(options.chromeos_root + "/src/overlays/overlay-" +
58 options.board + "/toolchain.conf", "r")
59 target = f.read()
60 f.close()
61 target = target.strip()
asharif2d815d02013-02-15 04:36:02 +000062 features = "noclean userfetch userpriv usersandbox -strict"
asharif09bfb6f2013-02-15 04:35:44 +000063 if options.incremental is not None and options.incremental:
64 features += " keepwork"
asharif19c73dd2013-02-15 04:35:37 +000065 env = CreateEnvVarString(" FEATURES", features)
asharif80c6e552013-02-15 04:35:40 +000066 env += CreateEnvVarString(" PORTAGE_USERNAME", getpass.getuser())
67 version_number = utils.GetRoot(rootdir)[1]
68 version_dir = "/home/${USER}/toolchain_root/" + version_number
69 env += CreateEnvVarString(" PORT_LOGDIR", version_dir + "/logs")
asharifd751e252013-02-15 04:35:52 +000070 env += CreateEnvVarString(" PKGDIR", version_dir + "/pkgs")
asharif2d815d02013-02-15 04:36:02 +000071 env += CreateEnvVarString(" PORTAGE_BINHOST", version_dir + "/pkgs")
asharif80c6e552013-02-15 04:35:40 +000072 env += CreateEnvVarString(" PORTAGE_TMPDIR", version_dir + "/objects")
asharif2d815d02013-02-15 04:36:02 +000073
asharifd751e252013-02-15 04:35:52 +000074 retval = 0
asharif80c6e552013-02-15 04:35:40 +000075 if options.force == True:
asharifd751e252013-02-15 04:35:52 +000076 retval = BuildTC(options.chromeos_root, options.toolchain_root, env,
77 target, True, options.incremental, portage_flags)
asharifd751e252013-02-15 04:35:52 +000078 retval = BuildTC(options.chromeos_root, options.toolchain_root, env,
79 target, options.clean, options.incremental, portage_flags)
80 utils.AssertTrue(retval == 0, "Build toolchain failed!")
81
82 if options.incremental is None and not options.clean:
83 install_dir = rootdir + "/install"
asharif2d815d02013-02-15 04:36:02 +000084 package_dir = (rootdir + "/pkgs/" + target + "/" +
asharifd751e252013-02-15 04:35:52 +000085 "cross-" + target + "/")
86 retval = InstallTC(package_dir, install_dir)
87 utils.AssertTrue(retval == 0, "Installation of the toolchain failed!")
88
89 return retval
asharife2cca302013-02-15 04:35:42 +000090
91
92def CreateCrossdevPortageFlags(portage_flags):
asharif2d815d02013-02-15 04:36:02 +000093 portage_flags = portage_flags.strip()
asharife2cca302013-02-15 04:35:42 +000094 if not portage_flags:
95 return ""
96 crossdev_flags = " --portage "
97 crossdev_flags += " --portage ".join(portage_flags.split(" "))
98 return crossdev_flags
asharif19c73dd2013-02-15 04:35:37 +000099
100
101def CreateEnvVarString(variable, value):
102 return variable + "=" + EscapeQuoteString(value)
103
104
105def EscapeQuoteString(string):
106 return "\\\"" + string + "\\\""
107
108
asharifd751e252013-02-15 04:35:52 +0000109def InstallTC(package_dir, install_dir):
110 command = ("mkdir -p " + install_dir + ";")
111 command += ("for f in " + package_dir + "*; do tar xvf $f -C " + install_dir +
112 "; done")
113 retval = utils.RunCommand(command)
114 return retval
115
116
asharif19c73dd2013-02-15 04:35:37 +0000117def BuildTC(chromeos_root, toolchain_root, env, target, uninstall,
asharife2cca302013-02-15 04:35:42 +0000118 incremental_component, portage_flags):
asharif19c73dd2013-02-15 04:35:37 +0000119 """Build the toolchain."""
asharif2d815d02013-02-15 04:36:02 +0000120 portage_flags = portage_flags.strip()
121 portage_flags += " -b "
122
asharif19c73dd2013-02-15 04:35:37 +0000123 binutils_version = "2.20.1-r1"
124 gcc_version = "9999"
125 libc_version = "2.10.1-r1"
126 kernel_version = "2.6.30-r1"
asharif19c73dd2013-02-15 04:35:37 +0000127
asharif2d815d02013-02-15 04:36:02 +0000128 env += " "
129
asharif19c73dd2013-02-15 04:35:37 +0000130 if uninstall == True:
131 tflag = " -C "
132 else:
133 tflag = " -t "
134
135 command = (rootdir + "/tc-enter-chroot.sh")
136 if chromeos_root is not None:
137 command += " --chromeos_root=" + chromeos_root
138 if toolchain_root is not None:
139 command += " --toolchain_root=" + toolchain_root
asharif2d815d02013-02-15 04:36:02 +0000140 command += " -- \"sudo " + env
141
142 if uninstall == True:
143 command += " crossdev " + tflag + target
144 command += "\""
145 retval = utils.RunCommand(command)
146 return retval
asharif19c73dd2013-02-15 04:35:37 +0000147
148 if incremental_component == "binutils":
asharife2cca302013-02-15 04:35:42 +0000149 command += (" emerge =cross-" + target + "/binutils-" + binutils_version +
150 portage_flags)
asharif19c73dd2013-02-15 04:35:37 +0000151 elif incremental_component == "gcc":
asharife2cca302013-02-15 04:35:42 +0000152 command += (" emerge =cross-" + target + "/gcc-" + gcc_version +
153 portage_flags)
asharif19c73dd2013-02-15 04:35:37 +0000154 elif incremental_component == "libc" or incremental_component == "glibc":
asharife2cca302013-02-15 04:35:42 +0000155 command += (" emerge =cross-" + target + "/glibc-" + libc_version +
156 portage_flags)
asharif19c73dd2013-02-15 04:35:37 +0000157 else:
asharif2d815d02013-02-15 04:36:02 +0000158 crossdev_flags = CreateCrossdevPortageFlags(portage_flags)
asharif19c73dd2013-02-15 04:35:37 +0000159 command += (" crossdev -v " + tflag + target +
160 " --binutils " + binutils_version +
161 " --libc " + libc_version +
162 " --gcc " + gcc_version +
163 " --kernel " + kernel_version +
asharif2d815d02013-02-15 04:36:02 +0000164 crossdev_flags)
asharif19c73dd2013-02-15 04:35:37 +0000165
asharif2d815d02013-02-15 04:36:02 +0000166 command += "\""
asharif19c73dd2013-02-15 04:35:37 +0000167 retval = utils.RunCommand(command)
168 return retval
169
170if __name__ == "__main__":
171 Main()
asharifd751e252013-02-15 04:35:52 +0000172