blob: ae99e9a478f95258592f91d3b6b2ca748a340f99 [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",
26 help="ChromeOS root checkout directory.")
27 parser.add_option("-t", "--toolchain_root", dest="toolchain_root",
28 help="Toolchain root directory.")
29 parser.add_option("-b", "--board", dest="board",
30 help="board is the argument to the setup_board command.")
31 parser.add_option("-C", "--clean", dest="clean",
asharife2cca302013-02-15 04:35:42 +000032 action="store_true", default=False,
33 help="Uninstall the toolchain.")
asharif80c6e552013-02-15 04:35:40 +000034 parser.add_option("-f", "--force", dest="force",
asharife2cca302013-02-15 04:35:42 +000035 action="store_true", default=False,
36 help="Do an uninstall/install cycle.")
asharif19c73dd2013-02-15 04:35:37 +000037 parser.add_option("-i", "--incremental", dest="incremental",
38 help="The toolchain component that should be "
39 "incrementally compiled.")
asharife2cca302013-02-15 04:35:42 +000040 parser.add_option("-B", "--binary", dest="binary",
41 action="store_true", default=False,
42 help="The toolchain should use binaries stored in "
43 "the install/ directory.")
bjanakiraman7f4a4852013-02-15 04:35:28 +000044
asharif19c73dd2013-02-15 04:35:37 +000045 options = parser.parse_args()[0]
bjanakiraman7f4a4852013-02-15 04:35:28 +000046
asharif19c73dd2013-02-15 04:35:37 +000047 if options.toolchain_root is None or options.board is None:
48 parser.print_help()
49 sys.exit()
bjanakiraman7f4a4852013-02-15 04:35:28 +000050
asharif19c73dd2013-02-15 04:35:37 +000051 if options.chromeos_root is None:
52 options.chromeos_root = "../.."
53
asharife2cca302013-02-15 04:35:42 +000054 portage_flags = ""
55 if options.binary == True:
56 # FIXME(asharif): This should be using --usepkg but that was not working.
57 portage_flags = "--usepkgonly"
58
asharif19c73dd2013-02-15 04:35:37 +000059 f = open(options.chromeos_root + "/src/overlays/overlay-" +
60 options.board + "/toolchain.conf", "r")
61 target = f.read()
62 f.close()
63 target = target.strip()
asharif80c6e552013-02-15 04:35:40 +000064 features = "noclean userfetch userpriv usersandbox"
asharif09bfb6f2013-02-15 04:35:44 +000065 if options.incremental is not None and options.incremental:
66 features += " keepwork"
asharif19c73dd2013-02-15 04:35:37 +000067 env = CreateEnvVarString(" FEATURES", features)
asharif80c6e552013-02-15 04:35:40 +000068 env += CreateEnvVarString(" PORTAGE_USERNAME", getpass.getuser())
69 version_number = utils.GetRoot(rootdir)[1]
70 version_dir = "/home/${USER}/toolchain_root/" + version_number
71 env += CreateEnvVarString(" PORT_LOGDIR", version_dir + "/logs")
72 env += CreateEnvVarString(" PKGDIR", version_dir + "/install")
asharife2cca302013-02-15 04:35:42 +000073 env += CreateEnvVarString(" PORTAGE_BINHOST", version_dir +
74 "/cross/" + target)
asharif80c6e552013-02-15 04:35:40 +000075 env += CreateEnvVarString(" PORTAGE_TMPDIR", version_dir + "/objects")
76 if options.force == True:
77 BuildTC(options.chromeos_root, options.toolchain_root, env, target,
asharife2cca302013-02-15 04:35:42 +000078 True, options.incremental, portage_flags)
asharif19c73dd2013-02-15 04:35:37 +000079 BuildTC(options.chromeos_root, options.toolchain_root, env, target,
asharife2cca302013-02-15 04:35:42 +000080 options.clean, options.incremental, portage_flags)
81
82
83def CreateCrossdevPortageFlags(portage_flags):
84 if not portage_flags:
85 return ""
86 crossdev_flags = " --portage "
87 crossdev_flags += " --portage ".join(portage_flags.split(" "))
88 return crossdev_flags
asharif19c73dd2013-02-15 04:35:37 +000089
90
91def CreateEnvVarString(variable, value):
92 return variable + "=" + EscapeQuoteString(value)
93
94
95def EscapeQuoteString(string):
96 return "\\\"" + string + "\\\""
97
98
99def BuildTC(chromeos_root, toolchain_root, env, target, uninstall,
asharife2cca302013-02-15 04:35:42 +0000100 incremental_component, portage_flags):
asharif19c73dd2013-02-15 04:35:37 +0000101 """Build the toolchain."""
102 binutils_version = "2.20.1-r1"
103 gcc_version = "9999"
104 libc_version = "2.10.1-r1"
105 kernel_version = "2.6.30-r1"
asharif19c73dd2013-02-15 04:35:37 +0000106
107 if uninstall == True:
108 tflag = " -C "
109 else:
110 tflag = " -t "
111
112 command = (rootdir + "/tc-enter-chroot.sh")
113 if chromeos_root is not None:
114 command += " --chromeos_root=" + chromeos_root
115 if toolchain_root is not None:
116 command += " --toolchain_root=" + toolchain_root
117 command += " -- sudo " + env
118
119 if incremental_component == "binutils":
asharife2cca302013-02-15 04:35:42 +0000120 command += (" emerge =cross-" + target + "/binutils-" + binutils_version +
121 portage_flags)
asharif19c73dd2013-02-15 04:35:37 +0000122 elif incremental_component == "gcc":
asharife2cca302013-02-15 04:35:42 +0000123 command += (" emerge =cross-" + target + "/gcc-" + gcc_version +
124 portage_flags)
asharif19c73dd2013-02-15 04:35:37 +0000125 elif incremental_component == "libc" or incremental_component == "glibc":
asharife2cca302013-02-15 04:35:42 +0000126 command += (" emerge =cross-" + target + "/glibc-" + libc_version +
127 portage_flags)
asharif19c73dd2013-02-15 04:35:37 +0000128 else:
129 command += (" crossdev -v " + tflag + target +
130 " --binutils " + binutils_version +
131 " --libc " + libc_version +
132 " --gcc " + gcc_version +
133 " --kernel " + kernel_version +
134 " --portage -b --portage --newuse")
asharife2cca302013-02-15 04:35:42 +0000135 crossdev_flags = CreateCrossdevPortageFlags(portage_flags)
136 command += crossdev_flags
asharif19c73dd2013-02-15 04:35:37 +0000137
138 retval = utils.RunCommand(command)
139 return retval
140
141if __name__ == "__main__":
142 Main()