blob: ea499c32ec75cf06b9fac43def96623096a93583 [file] [log] [blame]
shenhan8e8b0c22013-02-19 22:34:16 +00001#!/usr/bin/python
bjanakiraman7f4a4852013-02-15 04:35:28 +00002#
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
asharifc97199a2013-02-15 22:48:45 +000016import tempfile
kbaclawski20082a02013-02-16 02:12:57 +000017
asharif252df0f2013-02-15 04:46:28 +000018import tc_enter_chroot
raymes01959ae2013-02-15 04:50:07 +000019from utils import command_executer
shenhanad803432013-02-16 03:14:51 +000020from utils import constants
kbaclawski20082a02013-02-16 02:12:57 +000021from utils import misc
bjanakiraman7f4a4852013-02-15 04:35:28 +000022
asharif5a9bb462013-02-15 04:50:57 +000023
asharifc97199a2013-02-15 22:48:45 +000024class ToolchainPart(object):
25 def __init__(self, name, source_path, chromeos_root, board, incremental,
26 build_env):
27 self._name = name
kbaclawski20082a02013-02-16 02:12:57 +000028 self._source_path = misc.CanonicalizePath(source_path)
asharifc97199a2013-02-15 22:48:45 +000029 self._chromeos_root = chromeos_root
30 self._board = board
kbaclawski20082a02013-02-16 02:12:57 +000031 self._ctarget = misc.GetCtargetFromBoard(self._board,
asharif77bd80d2013-02-15 22:49:32 +000032 self._chromeos_root)
carrote6f773c2013-02-19 22:34:44 +000033 self.tag = "%s-%s" % (name, self._ctarget)
asharifc97199a2013-02-15 22:48:45 +000034 self._ce = command_executer.GetCommandExecuter()
35 self._mask_file = os.path.join(
36 self._chromeos_root,
37 "chroot",
38 "etc/portage/package.mask/cross-%s" % self._ctarget)
39 self._new_mask_file = None
40
shenhanad803432013-02-16 03:14:51 +000041 self._chroot_source_path = os.path.join(constants.mounted_toolchain_root,
llozano85b37152013-02-16 03:14:57 +000042 self._name).lstrip("/")
asharifc97199a2013-02-15 22:48:45 +000043 self._incremental = incremental
44 self._build_env = build_env
45
46 def RunSetupBoardIfNecessary(self):
47 cross_symlink = os.path.join(
48 self._chromeos_root,
49 "chroot",
carrotccae6272013-02-19 22:34:51 +000050 "usr/local/bin/emerge-%s" % self._board)
asharifc97199a2013-02-15 22:48:45 +000051 if not os.path.exists(cross_symlink):
52 command = "./setup_board --board=%s" % self._board
asharifca3c6c12013-02-15 23:17:54 +000053 self._ce.ChrootRunCommand(self._chromeos_root, command)
asharifc97199a2013-02-15 22:48:45 +000054
55 def Build(self):
shenhanbecf6242013-02-19 20:43:32 +000056 rv = 1
asharifc97199a2013-02-15 22:48:45 +000057 try:
asharif7dd6d862013-02-15 23:17:46 +000058 self.UninstallTool()
asharifc97199a2013-02-15 22:48:45 +000059 self.MoveMaskFile()
cmtice80d257f2013-02-15 23:44:51 +000060 self.MountSources(False)
asharif9e3cf6e2013-02-16 03:13:38 +000061 self.RemoveCompiledFile()
shenhanbecf6242013-02-19 20:43:32 +000062 rv = self.BuildTool()
asharifc97199a2013-02-15 22:48:45 +000063 finally:
64 self.UnMoveMaskFile()
shenhanbecf6242013-02-19 20:43:32 +000065 return rv
asharifc97199a2013-02-15 22:48:45 +000066
67 def RemoveCompiledFile(self):
68 compiled_file = os.path.join(self._chromeos_root,
69 "chroot",
70 "var/tmp/portage/cross-%s" % self._ctarget,
71 "%s-9999" % self._name,
72 ".compiled")
asharif9e3cf6e2013-02-16 03:13:38 +000073 command = "rm -f %s" % compiled_file
asharifc97199a2013-02-15 22:48:45 +000074 self._ce.RunCommand(command)
75
cmtice80d257f2013-02-15 23:44:51 +000076 def MountSources(self, unmount_source):
asharifc97199a2013-02-15 22:48:45 +000077 mount_points = []
78 mounted_source_path = os.path.join(self._chromeos_root,
79 "chroot",
80 self._chroot_source_path)
81 src_mp = tc_enter_chroot.MountPoint(
82 self._source_path,
83 mounted_source_path,
84 getpass.getuser(),
85 "ro")
86 mount_points.append(src_mp)
87
88 build_suffix = "build-%s" % self._ctarget
89 build_dir = "%s-%s" % (self._source_path, build_suffix)
90
91 if not self._incremental and os.path.exists(build_dir):
92 command = "rm -rf %s/*" % build_dir
93 self._ce.RunCommand(command)
94
95 # Create a -build directory for the objects.
96 command = "mkdir -p %s" % build_dir
97 self._ce.RunCommand(command)
98
99 mounted_build_dir = os.path.join(
100 self._chromeos_root, "chroot", "%s-%s" %
101 (self._chroot_source_path, build_suffix))
102 build_mp = tc_enter_chroot.MountPoint(
103 build_dir,
104 mounted_build_dir,
105 getpass.getuser())
106 mount_points.append(build_mp)
107
cmtice80d257f2013-02-15 23:44:51 +0000108 if unmount_source:
109 unmount_statuses = [mp.UnMount() == 0 for mp in mount_points]
asharifc97199a2013-02-15 22:48:45 +0000110 assert all(unmount_statuses), "Could not unmount all mount points!"
cmtice80d257f2013-02-15 23:44:51 +0000111 else:
112 mount_statuses = [mp.DoMount() == 0 for mp in mount_points]
113
114 if not all(mount_statuses):
115 mounted = [mp for mp, status in zip(mount_points, mount_statuses) if status]
116 unmount_statuses = [mp.UnMount() == 0 for mp in mounted]
117 assert all(unmount_statuses), "Could not unmount all mount points!"
118
asharifc97199a2013-02-15 22:48:45 +0000119
asharif7dd6d862013-02-15 23:17:46 +0000120 def UninstallTool(self):
121 command = "sudo CLEAN_DELAY=0 emerge -C cross-%s/%s" % (self._ctarget, self._name)
asharifca3c6c12013-02-15 23:17:54 +0000122 self._ce.ChrootRunCommand(self._chromeos_root, command)
asharif7dd6d862013-02-15 23:17:46 +0000123
asharifc97199a2013-02-15 22:48:45 +0000124 def BuildTool(self):
125 env = self._build_env
asharif9e499162013-02-16 02:41:39 +0000126 # FEATURES=buildpkg adds minutes of time so we disable it.
shenhanfdc0f572013-02-19 18:48:36 +0000127 # TODO(shenhan): keep '-sandbox' for a while for compatibility, then remove
128 # it after a while.
129 features = "nostrip userpriv userfetch -usersandbox -sandbox noclean -buildpkg"
asharifc97199a2013-02-15 22:48:45 +0000130 env["FEATURES"] = features
131
132 if self._incremental:
133 env["FEATURES"] += " keepwork"
134
135 env["USE"] = "multislot mounted_%s" % self._name
136 env["%s_SOURCE_PATH" % self._name.upper()] = (
137 os.path.join("/", self._chroot_source_path))
138 env["ACCEPT_KEYWORDS"] = "~*"
139 env_string = " ".join(["%s=\"%s\"" % var for var in env.items()])
140 command = "emerge =cross-%s/%s-9999" % (self._ctarget, self._name)
141 full_command = "sudo %s %s" % (env_string, command)
shenhanbecf6242013-02-19 20:43:32 +0000142 return self._ce.ChrootRunCommand(self._chromeos_root, full_command)
asharifc97199a2013-02-15 22:48:45 +0000143
asharifc97199a2013-02-15 22:48:45 +0000144 def MoveMaskFile(self):
145 self._new_mask_file = None
146 if os.path.isfile(self._mask_file):
147 self._new_mask_file = tempfile.mktemp()
148 command = "sudo mv %s %s" % (self._mask_file, self._new_mask_file)
149 self._ce.RunCommand(command)
150
151 def UnMoveMaskFile(self):
152 if self._new_mask_file:
153 command = "sudo mv %s %s" % (self._new_mask_file, self._mask_file)
154 self._ce.RunCommand(command)
bjanakiraman7f4a4852013-02-15 04:35:28 +0000155
bjanakiraman7f4a4852013-02-15 04:35:28 +0000156
asharif0d3535a2013-02-15 04:50:33 +0000157def Main(argv):
asharif19c73dd2013-02-15 04:35:37 +0000158 """The main function."""
asharif5a9bb462013-02-15 04:50:57 +0000159 # Common initializations
asharif19c73dd2013-02-15 04:35:37 +0000160 parser = optparse.OptionParser()
asharifc97199a2013-02-15 22:48:45 +0000161 parser.add_option("-c",
162 "--chromeos_root",
163 dest="chromeos_root",
asharifbcdd4e52013-02-16 01:05:17 +0000164 default="../../",
asharifc97199a2013-02-15 22:48:45 +0000165 help=("ChromeOS root checkout directory"
166 " uses ../.. if none given."))
167 parser.add_option("-g",
168 "--gcc_dir",
169 dest="gcc_dir",
170 help="The directory where gcc resides.")
shenhan8e8b0c22013-02-19 22:34:16 +0000171 parser.add_option("--binutils_dir",
172 dest="binutils_dir",
173 help="The directory where binutils resides.")
cmtice80d257f2013-02-15 23:44:51 +0000174 parser.add_option("-x",
175 "--gdb_dir",
176 dest="gdb_dir",
177 help="The directory where gdb resides.")
asharifc97199a2013-02-15 22:48:45 +0000178 parser.add_option("-b",
179 "--board",
180 dest="board",
181 default="x86-agz",
182 help="The target board.")
183 parser.add_option("-n",
184 "--noincremental",
185 dest="noincremental",
186 default=False,
asharifd751e252013-02-15 04:35:52 +0000187 action="store_true",
asharifc97199a2013-02-15 22:48:45 +0000188 help="Use FEATURES=keepwork to do incremental builds.")
cmticee59ac272013-02-19 20:20:09 +0000189 parser.add_option("--cflags",
190 dest="cflags",
191 default="",
192 help="Build a compiler with specified CFLAGS")
193 parser.add_option("--cxxflags",
194 dest="cxxflags",
195 default="",
196 help="Build a compiler with specified CXXFLAGS")
197 parser.add_option("--ldflags",
198 dest="ldflags",
199 default="",
200 help="Build a compiler with specified LDFLAGS")
asharifc97199a2013-02-15 22:48:45 +0000201 parser.add_option("-d",
202 "--debug",
203 dest="debug",
204 default=False,
asharifd751e252013-02-15 04:35:52 +0000205 action="store_true",
cmticee59ac272013-02-19 20:20:09 +0000206 help="Build a compiler with -g3 -O0 appended to both"
207 " CFLAGS and CXXFLAGS.")
asharif86968c42013-02-15 23:44:37 +0000208 parser.add_option("-m",
209 "--mount_only",
210 dest="mount_only",
211 default=False,
212 action="store_true",
213 help="Just mount the tool directories.")
cmtice80d257f2013-02-15 23:44:51 +0000214 parser.add_option("-u",
215 "--unmount_only",
216 dest="unmount_only",
217 default=False,
218 action="store_true",
219 help="Just unmount the tool directories.")
asharif86968c42013-02-15 23:44:37 +0000220
bjanakiraman7f4a4852013-02-15 04:35:28 +0000221
asharifc97199a2013-02-15 22:48:45 +0000222 options, _ = parser.parse_args(argv)
asharif17621302013-02-15 04:46:35 +0000223
kbaclawski20082a02013-02-16 02:12:57 +0000224 chromeos_root = misc.CanonicalizePath(options.chromeos_root)
cmtice80d257f2013-02-15 23:44:51 +0000225 if options.gcc_dir:
kbaclawski20082a02013-02-16 02:12:57 +0000226 gcc_dir = misc.CanonicalizePath(options.gcc_dir)
shenhan8e8b0c22013-02-19 22:34:16 +0000227 if options.binutils_dir:
228 binutils_dir = misc.CanonicalizePath(options.binutils_dir)
cmtice80d257f2013-02-15 23:44:51 +0000229 if options.gdb_dir:
kbaclawski20082a02013-02-16 02:12:57 +0000230 gdb_dir = misc.CanonicalizePath(options.gdb_dir)
cmtice80d257f2013-02-15 23:44:51 +0000231 if options.unmount_only:
232 options.mount_only = False
233 elif options.mount_only:
234 options.unmount_only = False
asharifc97199a2013-02-15 22:48:45 +0000235 build_env = {}
cmticee59ac272013-02-19 20:20:09 +0000236 if options.cflags:
carrot255fd462013-02-19 22:43:16 +0000237 build_env["CFLAGS"] = "`portageq envvar CFLAGS` " + options.cflags
cmticee59ac272013-02-19 20:20:09 +0000238 if options.cxxflags:
carrot255fd462013-02-19 22:43:16 +0000239 build_env["CXXFLAGS"] = "`portageq envvar CXXFLAGS` " + options.cxxflags
240 if options.ldflags:
cmticee59ac272013-02-19 20:20:09 +0000241 build_env["LDFLAGS"] = options.ldflags
asharifc97199a2013-02-15 22:48:45 +0000242 if options.debug:
243 debug_flags = "-g3 -O0"
cmticee59ac272013-02-19 20:20:09 +0000244 if "CFLAGS" in build_env:
245 build_env["CFLAGS"] += " %s" % (debug_flags)
246 else:
247 build_env["CFLAGS"] = debug_flags
248 if "CXXFLAGS" in build_env:
249 build_env["CXXFLAGS"] += " %s" % (debug_flags)
250 else:
251 build_env["CXXFLAGS"] = debug_flags
bjanakiraman7f4a4852013-02-15 04:35:28 +0000252
asharif86968c42013-02-15 23:44:37 +0000253 # Create toolchain parts
carrote6f773c2013-02-19 22:34:44 +0000254 toolchain_parts = {}
asharif86968c42013-02-15 23:44:37 +0000255 for board in options.board.split(","):
256 if options.gcc_dir:
asharifc97199a2013-02-15 22:48:45 +0000257 tp = ToolchainPart("gcc", gcc_dir, chromeos_root, board,
258 not options.noincremental, build_env)
carrote6f773c2013-02-19 22:34:44 +0000259 toolchain_parts[tp.tag] = tp
260 tp.RunSetupBoardIfNecessary()
shenhan8e8b0c22013-02-19 22:34:16 +0000261 if options.binutils_dir:
262 tp = ToolchainPart("binutils", binutils_dir, chromeos_root, board,
263 not options.noincremental, build_env)
carrote6f773c2013-02-19 22:34:44 +0000264 toolchain_parts[tp.tag] = tp
265 tp.RunSetupBoardIfNecessary()
cmtice80d257f2013-02-15 23:44:51 +0000266 if options.gdb_dir:
267 tp = ToolchainPart("gdb", gdb_dir, chromeos_root, board,
268 not options.noincremental, build_env)
carrote6f773c2013-02-19 22:34:44 +0000269 toolchain_parts[tp.tag] = tp
270 tp.RunSetupBoardIfNecessary()
asharif86968c42013-02-15 23:44:37 +0000271
shenhanbecf6242013-02-19 20:43:32 +0000272 rv = 0
asharif86968c42013-02-15 23:44:37 +0000273 try:
carrote6f773c2013-02-19 22:34:44 +0000274 for tag in toolchain_parts:
275 tp = toolchain_parts[tag]
cmtice80d257f2013-02-15 23:44:51 +0000276 if options.mount_only or options.unmount_only:
277 tp.MountSources(options.unmount_only)
asharif86968c42013-02-15 23:44:37 +0000278 else:
shenhanbecf6242013-02-19 20:43:32 +0000279 rv = rv + tp.Build()
asharifc97199a2013-02-15 22:48:45 +0000280 finally:
281 print "Exiting..."
shenhanbecf6242013-02-19 20:43:32 +0000282 return rv
asharif19c73dd2013-02-15 04:35:37 +0000283
284if __name__ == "__main__":
asharif2198c512013-02-15 09:21:35 +0000285 retval = Main(sys.argv)
286 sys.exit(retval)