blob: 02918b68212c1e5745c10863cb1a8c138c336be6 [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,
llozanof2726d22013-03-09 01:29:37 +000026 build_env, gcc_enable_ccache=False):
asharifc97199a2013-02-15 22:48:45 +000027 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
llozanof2726d22013-03-09 01:29:37 +000045 self._gcc_enable_ccache = gcc_enable_ccache
asharifc97199a2013-02-15 22:48:45 +000046
47 def RunSetupBoardIfNecessary(self):
48 cross_symlink = os.path.join(
49 self._chromeos_root,
50 "chroot",
carrotccae6272013-02-19 22:34:51 +000051 "usr/local/bin/emerge-%s" % self._board)
asharifc97199a2013-02-15 22:48:45 +000052 if not os.path.exists(cross_symlink):
53 command = "./setup_board --board=%s" % self._board
asharifca3c6c12013-02-15 23:17:54 +000054 self._ce.ChrootRunCommand(self._chromeos_root, command)
asharifc97199a2013-02-15 22:48:45 +000055
56 def Build(self):
shenhanbecf6242013-02-19 20:43:32 +000057 rv = 1
asharifc97199a2013-02-15 22:48:45 +000058 try:
asharif7dd6d862013-02-15 23:17:46 +000059 self.UninstallTool()
asharifc97199a2013-02-15 22:48:45 +000060 self.MoveMaskFile()
cmtice80d257f2013-02-15 23:44:51 +000061 self.MountSources(False)
asharif9e3cf6e2013-02-16 03:13:38 +000062 self.RemoveCompiledFile()
shenhanbecf6242013-02-19 20:43:32 +000063 rv = self.BuildTool()
asharifc97199a2013-02-15 22:48:45 +000064 finally:
65 self.UnMoveMaskFile()
llozano9b84cb62013-03-11 21:08:31 +000066 return rv
asharifc97199a2013-02-15 22:48:45 +000067
68 def RemoveCompiledFile(self):
69 compiled_file = os.path.join(self._chromeos_root,
70 "chroot",
71 "var/tmp/portage/cross-%s" % self._ctarget,
72 "%s-9999" % self._name,
73 ".compiled")
asharif9e3cf6e2013-02-16 03:13:38 +000074 command = "rm -f %s" % compiled_file
asharifc97199a2013-02-15 22:48:45 +000075 self._ce.RunCommand(command)
76
cmtice80d257f2013-02-15 23:44:51 +000077 def MountSources(self, unmount_source):
asharifc97199a2013-02-15 22:48:45 +000078 mount_points = []
79 mounted_source_path = os.path.join(self._chromeos_root,
80 "chroot",
81 self._chroot_source_path)
82 src_mp = tc_enter_chroot.MountPoint(
83 self._source_path,
84 mounted_source_path,
85 getpass.getuser(),
86 "ro")
87 mount_points.append(src_mp)
88
89 build_suffix = "build-%s" % self._ctarget
90 build_dir = "%s-%s" % (self._source_path, build_suffix)
91
92 if not self._incremental and os.path.exists(build_dir):
93 command = "rm -rf %s/*" % build_dir
94 self._ce.RunCommand(command)
95
96 # Create a -build directory for the objects.
97 command = "mkdir -p %s" % build_dir
98 self._ce.RunCommand(command)
99
100 mounted_build_dir = os.path.join(
101 self._chromeos_root, "chroot", "%s-%s" %
102 (self._chroot_source_path, build_suffix))
103 build_mp = tc_enter_chroot.MountPoint(
104 build_dir,
105 mounted_build_dir,
106 getpass.getuser())
107 mount_points.append(build_mp)
108
cmtice80d257f2013-02-15 23:44:51 +0000109 if unmount_source:
110 unmount_statuses = [mp.UnMount() == 0 for mp in mount_points]
asharifc97199a2013-02-15 22:48:45 +0000111 assert all(unmount_statuses), "Could not unmount all mount points!"
cmtice80d257f2013-02-15 23:44:51 +0000112 else:
113 mount_statuses = [mp.DoMount() == 0 for mp in mount_points]
114
115 if not all(mount_statuses):
116 mounted = [mp for mp, status in zip(mount_points, mount_statuses) if status]
117 unmount_statuses = [mp.UnMount() == 0 for mp in mounted]
118 assert all(unmount_statuses), "Could not unmount all mount points!"
119
asharifc97199a2013-02-15 22:48:45 +0000120
asharif7dd6d862013-02-15 23:17:46 +0000121 def UninstallTool(self):
122 command = "sudo CLEAN_DELAY=0 emerge -C cross-%s/%s" % (self._ctarget, self._name)
asharifca3c6c12013-02-15 23:17:54 +0000123 self._ce.ChrootRunCommand(self._chromeos_root, command)
asharif7dd6d862013-02-15 23:17:46 +0000124
asharifc97199a2013-02-15 22:48:45 +0000125 def BuildTool(self):
126 env = self._build_env
asharif9e499162013-02-16 02:41:39 +0000127 # FEATURES=buildpkg adds minutes of time so we disable it.
shenhanfdc0f572013-02-19 18:48:36 +0000128 # TODO(shenhan): keep '-sandbox' for a while for compatibility, then remove
129 # it after a while.
130 features = "nostrip userpriv userfetch -usersandbox -sandbox noclean -buildpkg"
asharifc97199a2013-02-15 22:48:45 +0000131 env["FEATURES"] = features
132
133 if self._incremental:
134 env["FEATURES"] += " keepwork"
135
cmticecbc868a2013-02-21 22:52:46 +0000136 if "USE" in env:
137 env["USE"] += " multislot mounted_%s" % self._name
138 else:
139 env["USE"] = "multislot mounted_%s" % self._name
140
llozanof2726d22013-03-09 01:29:37 +0000141 # Disable ccache in our compilers. cache may be problematic for us.
142 # It ignores compiler environments settings and it is not clear if
143 # the cache hit algorithm verifies all the compiler binaries or
144 # just the driver.
145 if self._name == "gcc" and not self._gcc_enable_ccache:
146 env["USE"] += " -wrapper_ccache"
147
asharifc97199a2013-02-15 22:48:45 +0000148 env["%s_SOURCE_PATH" % self._name.upper()] = (
149 os.path.join("/", self._chroot_source_path))
150 env["ACCEPT_KEYWORDS"] = "~*"
151 env_string = " ".join(["%s=\"%s\"" % var for var in env.items()])
152 command = "emerge =cross-%s/%s-9999" % (self._ctarget, self._name)
153 full_command = "sudo %s %s" % (env_string, command)
Luis Lozano4cea9de2013-03-20 12:32:56 -0700154 rv = self._ce.ChrootRunCommand(self._chromeos_root, full_command)
155 if rv != 0:
156 return rv
157 if self._name == "gcc":
158 command = ("sudo cp -r /usr/lib/gcc/%s /build/%s/usr/lib/gcc/." %
159 (self._ctarget, self._board))
160 rv = self._ce.ChrootRunCommand(self._chromeos_root, command)
161 return rv
asharifc97199a2013-02-15 22:48:45 +0000162
asharifc97199a2013-02-15 22:48:45 +0000163 def MoveMaskFile(self):
164 self._new_mask_file = None
165 if os.path.isfile(self._mask_file):
166 self._new_mask_file = tempfile.mktemp()
167 command = "sudo mv %s %s" % (self._mask_file, self._new_mask_file)
168 self._ce.RunCommand(command)
169
170 def UnMoveMaskFile(self):
171 if self._new_mask_file:
172 command = "sudo mv %s %s" % (self._new_mask_file, self._mask_file)
173 self._ce.RunCommand(command)
bjanakiraman7f4a4852013-02-15 04:35:28 +0000174
bjanakiraman7f4a4852013-02-15 04:35:28 +0000175
asharif0d3535a2013-02-15 04:50:33 +0000176def Main(argv):
asharif19c73dd2013-02-15 04:35:37 +0000177 """The main function."""
asharif5a9bb462013-02-15 04:50:57 +0000178 # Common initializations
asharif19c73dd2013-02-15 04:35:37 +0000179 parser = optparse.OptionParser()
asharifc97199a2013-02-15 22:48:45 +0000180 parser.add_option("-c",
181 "--chromeos_root",
182 dest="chromeos_root",
asharifbcdd4e52013-02-16 01:05:17 +0000183 default="../../",
asharifc97199a2013-02-15 22:48:45 +0000184 help=("ChromeOS root checkout directory"
185 " uses ../.. if none given."))
186 parser.add_option("-g",
187 "--gcc_dir",
188 dest="gcc_dir",
189 help="The directory where gcc resides.")
shenhan8e8b0c22013-02-19 22:34:16 +0000190 parser.add_option("--binutils_dir",
191 dest="binutils_dir",
192 help="The directory where binutils resides.")
cmtice80d257f2013-02-15 23:44:51 +0000193 parser.add_option("-x",
194 "--gdb_dir",
195 dest="gdb_dir",
196 help="The directory where gdb resides.")
asharifc97199a2013-02-15 22:48:45 +0000197 parser.add_option("-b",
198 "--board",
199 dest="board",
200 default="x86-agz",
201 help="The target board.")
202 parser.add_option("-n",
203 "--noincremental",
204 dest="noincremental",
205 default=False,
asharifd751e252013-02-15 04:35:52 +0000206 action="store_true",
asharifc97199a2013-02-15 22:48:45 +0000207 help="Use FEATURES=keepwork to do incremental builds.")
cmticee59ac272013-02-19 20:20:09 +0000208 parser.add_option("--cflags",
209 dest="cflags",
210 default="",
211 help="Build a compiler with specified CFLAGS")
212 parser.add_option("--cxxflags",
213 dest="cxxflags",
214 default="",
215 help="Build a compiler with specified CXXFLAGS")
carrot2b549ca2013-02-19 22:45:25 +0000216 parser.add_option("--cflags_for_target",
217 dest="cflags_for_target",
218 default="",
219 help="Build the target libraries with specified flags")
220 parser.add_option("--cxxflags_for_target",
221 dest="cxxflags_for_target",
222 default="",
223 help="Build the target libraries with specified flags")
cmticee59ac272013-02-19 20:20:09 +0000224 parser.add_option("--ldflags",
225 dest="ldflags",
226 default="",
227 help="Build a compiler with specified LDFLAGS")
asharifc97199a2013-02-15 22:48:45 +0000228 parser.add_option("-d",
229 "--debug",
230 dest="debug",
231 default=False,
asharifd751e252013-02-15 04:35:52 +0000232 action="store_true",
cmticee59ac272013-02-19 20:20:09 +0000233 help="Build a compiler with -g3 -O0 appended to both"
234 " CFLAGS and CXXFLAGS.")
asharif86968c42013-02-15 23:44:37 +0000235 parser.add_option("-m",
236 "--mount_only",
237 dest="mount_only",
238 default=False,
239 action="store_true",
240 help="Just mount the tool directories.")
cmtice80d257f2013-02-15 23:44:51 +0000241 parser.add_option("-u",
242 "--unmount_only",
243 dest="unmount_only",
244 default=False,
245 action="store_true",
246 help="Just unmount the tool directories.")
cmticecbc868a2013-02-21 22:52:46 +0000247 parser.add_option("--extra_use_flags",
248 dest="extra_use_flags",
249 default="",
250 help="Extra flag for USE, to be passed to the ebuild. "
251 "('multislot' and 'mounted_<tool>' are always passed.)")
llozanof2726d22013-03-09 01:29:37 +0000252 parser.add_option("--gcc_enable_ccache",
253 dest="gcc_enable_ccache",
254 default=False,
255 action="store_true",
256 help="Enable ccache for the gcc invocations")
bjanakiraman7f4a4852013-02-15 04:35:28 +0000257
asharifc97199a2013-02-15 22:48:45 +0000258 options, _ = parser.parse_args(argv)
asharif17621302013-02-15 04:46:35 +0000259
kbaclawski20082a02013-02-16 02:12:57 +0000260 chromeos_root = misc.CanonicalizePath(options.chromeos_root)
cmtice80d257f2013-02-15 23:44:51 +0000261 if options.gcc_dir:
kbaclawski20082a02013-02-16 02:12:57 +0000262 gcc_dir = misc.CanonicalizePath(options.gcc_dir)
Luis Lozano4cea9de2013-03-20 12:32:56 -0700263 assert gcc_dir and os.path.isdir(gcc_dir), "gcc_dir does not exist!"
shenhan8e8b0c22013-02-19 22:34:16 +0000264 if options.binutils_dir:
265 binutils_dir = misc.CanonicalizePath(options.binutils_dir)
Luis Lozano4cea9de2013-03-20 12:32:56 -0700266 assert os.path.isdir(binutils_dir), "binutils_dir does not exist!"
cmtice80d257f2013-02-15 23:44:51 +0000267 if options.gdb_dir:
kbaclawski20082a02013-02-16 02:12:57 +0000268 gdb_dir = misc.CanonicalizePath(options.gdb_dir)
Luis Lozano4cea9de2013-03-20 12:32:56 -0700269 assert os.path.isdir(gdb_dir), "gdb_dir does not exist!"
cmtice80d257f2013-02-15 23:44:51 +0000270 if options.unmount_only:
271 options.mount_only = False
272 elif options.mount_only:
273 options.unmount_only = False
asharifc97199a2013-02-15 22:48:45 +0000274 build_env = {}
cmticee59ac272013-02-19 20:20:09 +0000275 if options.cflags:
carrot255fd462013-02-19 22:43:16 +0000276 build_env["CFLAGS"] = "`portageq envvar CFLAGS` " + options.cflags
cmticee59ac272013-02-19 20:20:09 +0000277 if options.cxxflags:
carrot255fd462013-02-19 22:43:16 +0000278 build_env["CXXFLAGS"] = "`portageq envvar CXXFLAGS` " + options.cxxflags
carrot2b549ca2013-02-19 22:45:25 +0000279 if options.cflags_for_target:
280 build_env["CFLAGS_FOR_TARGET"] = options.cflags_for_target
281 if options.cxxflags_for_target:
282 build_env["CXXFLAGS_FOR_TARGET"] = options.cxxflags_for_target
carrot255fd462013-02-19 22:43:16 +0000283 if options.ldflags:
cmticee59ac272013-02-19 20:20:09 +0000284 build_env["LDFLAGS"] = options.ldflags
asharifc97199a2013-02-15 22:48:45 +0000285 if options.debug:
286 debug_flags = "-g3 -O0"
cmticee59ac272013-02-19 20:20:09 +0000287 if "CFLAGS" in build_env:
288 build_env["CFLAGS"] += " %s" % (debug_flags)
289 else:
290 build_env["CFLAGS"] = debug_flags
291 if "CXXFLAGS" in build_env:
292 build_env["CXXFLAGS"] += " %s" % (debug_flags)
293 else:
294 build_env["CXXFLAGS"] = debug_flags
cmticecbc868a2013-02-21 22:52:46 +0000295 if options.extra_use_flags:
296 build_env["USE"] = options.extra_use_flags
bjanakiraman7f4a4852013-02-15 04:35:28 +0000297
asharif86968c42013-02-15 23:44:37 +0000298 # Create toolchain parts
carrote6f773c2013-02-19 22:34:44 +0000299 toolchain_parts = {}
asharif86968c42013-02-15 23:44:37 +0000300 for board in options.board.split(","):
301 if options.gcc_dir:
asharifc97199a2013-02-15 22:48:45 +0000302 tp = ToolchainPart("gcc", gcc_dir, chromeos_root, board,
llozanof2726d22013-03-09 01:29:37 +0000303 not options.noincremental, build_env,
304 options.gcc_enable_ccache)
carrote6f773c2013-02-19 22:34:44 +0000305 toolchain_parts[tp.tag] = tp
306 tp.RunSetupBoardIfNecessary()
shenhan8e8b0c22013-02-19 22:34:16 +0000307 if options.binutils_dir:
308 tp = ToolchainPart("binutils", binutils_dir, chromeos_root, board,
309 not options.noincremental, build_env)
carrote6f773c2013-02-19 22:34:44 +0000310 toolchain_parts[tp.tag] = tp
311 tp.RunSetupBoardIfNecessary()
cmtice80d257f2013-02-15 23:44:51 +0000312 if options.gdb_dir:
313 tp = ToolchainPart("gdb", gdb_dir, chromeos_root, board,
314 not options.noincremental, build_env)
carrote6f773c2013-02-19 22:34:44 +0000315 toolchain_parts[tp.tag] = tp
316 tp.RunSetupBoardIfNecessary()
asharif86968c42013-02-15 23:44:37 +0000317
shenhanbecf6242013-02-19 20:43:32 +0000318 rv = 0
asharif86968c42013-02-15 23:44:37 +0000319 try:
carrote6f773c2013-02-19 22:34:44 +0000320 for tag in toolchain_parts:
321 tp = toolchain_parts[tag]
cmtice80d257f2013-02-15 23:44:51 +0000322 if options.mount_only or options.unmount_only:
323 tp.MountSources(options.unmount_only)
asharif86968c42013-02-15 23:44:37 +0000324 else:
shenhanbecf6242013-02-19 20:43:32 +0000325 rv = rv + tp.Build()
asharifc97199a2013-02-15 22:48:45 +0000326 finally:
327 print "Exiting..."
llozano5fee82f2013-03-11 17:59:40 +0000328 return rv
asharif19c73dd2013-02-15 04:35:37 +0000329
330if __name__ == "__main__":
asharif2198c512013-02-15 09:21:35 +0000331 retval = Main(sys.argv)
332 sys.exit(retval)