blob: 0c009b3a43b66adaef3e2bf8a94c07c3276d8f06 [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)
shenhanbecf6242013-02-19 20:43:32 +0000154 return self._ce.ChrootRunCommand(self._chromeos_root, full_command)
asharifc97199a2013-02-15 22:48:45 +0000155
asharifc97199a2013-02-15 22:48:45 +0000156 def MoveMaskFile(self):
157 self._new_mask_file = None
158 if os.path.isfile(self._mask_file):
159 self._new_mask_file = tempfile.mktemp()
160 command = "sudo mv %s %s" % (self._mask_file, self._new_mask_file)
161 self._ce.RunCommand(command)
162
163 def UnMoveMaskFile(self):
164 if self._new_mask_file:
165 command = "sudo mv %s %s" % (self._new_mask_file, self._mask_file)
166 self._ce.RunCommand(command)
bjanakiraman7f4a4852013-02-15 04:35:28 +0000167
bjanakiraman7f4a4852013-02-15 04:35:28 +0000168
asharif0d3535a2013-02-15 04:50:33 +0000169def Main(argv):
asharif19c73dd2013-02-15 04:35:37 +0000170 """The main function."""
asharif5a9bb462013-02-15 04:50:57 +0000171 # Common initializations
asharif19c73dd2013-02-15 04:35:37 +0000172 parser = optparse.OptionParser()
asharifc97199a2013-02-15 22:48:45 +0000173 parser.add_option("-c",
174 "--chromeos_root",
175 dest="chromeos_root",
asharifbcdd4e52013-02-16 01:05:17 +0000176 default="../../",
asharifc97199a2013-02-15 22:48:45 +0000177 help=("ChromeOS root checkout directory"
178 " uses ../.. if none given."))
179 parser.add_option("-g",
180 "--gcc_dir",
181 dest="gcc_dir",
182 help="The directory where gcc resides.")
shenhan8e8b0c22013-02-19 22:34:16 +0000183 parser.add_option("--binutils_dir",
184 dest="binutils_dir",
185 help="The directory where binutils resides.")
cmtice80d257f2013-02-15 23:44:51 +0000186 parser.add_option("-x",
187 "--gdb_dir",
188 dest="gdb_dir",
189 help="The directory where gdb resides.")
asharifc97199a2013-02-15 22:48:45 +0000190 parser.add_option("-b",
191 "--board",
192 dest="board",
193 default="x86-agz",
194 help="The target board.")
195 parser.add_option("-n",
196 "--noincremental",
197 dest="noincremental",
198 default=False,
asharifd751e252013-02-15 04:35:52 +0000199 action="store_true",
asharifc97199a2013-02-15 22:48:45 +0000200 help="Use FEATURES=keepwork to do incremental builds.")
cmticee59ac272013-02-19 20:20:09 +0000201 parser.add_option("--cflags",
202 dest="cflags",
203 default="",
204 help="Build a compiler with specified CFLAGS")
205 parser.add_option("--cxxflags",
206 dest="cxxflags",
207 default="",
208 help="Build a compiler with specified CXXFLAGS")
carrot2b549ca2013-02-19 22:45:25 +0000209 parser.add_option("--cflags_for_target",
210 dest="cflags_for_target",
211 default="",
212 help="Build the target libraries with specified flags")
213 parser.add_option("--cxxflags_for_target",
214 dest="cxxflags_for_target",
215 default="",
216 help="Build the target libraries with specified flags")
cmticee59ac272013-02-19 20:20:09 +0000217 parser.add_option("--ldflags",
218 dest="ldflags",
219 default="",
220 help="Build a compiler with specified LDFLAGS")
asharifc97199a2013-02-15 22:48:45 +0000221 parser.add_option("-d",
222 "--debug",
223 dest="debug",
224 default=False,
asharifd751e252013-02-15 04:35:52 +0000225 action="store_true",
cmticee59ac272013-02-19 20:20:09 +0000226 help="Build a compiler with -g3 -O0 appended to both"
227 " CFLAGS and CXXFLAGS.")
asharif86968c42013-02-15 23:44:37 +0000228 parser.add_option("-m",
229 "--mount_only",
230 dest="mount_only",
231 default=False,
232 action="store_true",
233 help="Just mount the tool directories.")
cmtice80d257f2013-02-15 23:44:51 +0000234 parser.add_option("-u",
235 "--unmount_only",
236 dest="unmount_only",
237 default=False,
238 action="store_true",
239 help="Just unmount the tool directories.")
cmticecbc868a2013-02-21 22:52:46 +0000240 parser.add_option("--extra_use_flags",
241 dest="extra_use_flags",
242 default="",
243 help="Extra flag for USE, to be passed to the ebuild. "
244 "('multislot' and 'mounted_<tool>' are always passed.)")
llozanof2726d22013-03-09 01:29:37 +0000245 parser.add_option("--gcc_enable_ccache",
246 dest="gcc_enable_ccache",
247 default=False,
248 action="store_true",
249 help="Enable ccache for the gcc invocations")
bjanakiraman7f4a4852013-02-15 04:35:28 +0000250
asharifc97199a2013-02-15 22:48:45 +0000251 options, _ = parser.parse_args(argv)
asharif17621302013-02-15 04:46:35 +0000252
kbaclawski20082a02013-02-16 02:12:57 +0000253 chromeos_root = misc.CanonicalizePath(options.chromeos_root)
cmtice80d257f2013-02-15 23:44:51 +0000254 if options.gcc_dir:
kbaclawski20082a02013-02-16 02:12:57 +0000255 gcc_dir = misc.CanonicalizePath(options.gcc_dir)
shenhan8e8b0c22013-02-19 22:34:16 +0000256 if options.binutils_dir:
257 binutils_dir = misc.CanonicalizePath(options.binutils_dir)
cmtice80d257f2013-02-15 23:44:51 +0000258 if options.gdb_dir:
kbaclawski20082a02013-02-16 02:12:57 +0000259 gdb_dir = misc.CanonicalizePath(options.gdb_dir)
cmtice80d257f2013-02-15 23:44:51 +0000260 if options.unmount_only:
261 options.mount_only = False
262 elif options.mount_only:
263 options.unmount_only = False
asharifc97199a2013-02-15 22:48:45 +0000264 build_env = {}
cmticee59ac272013-02-19 20:20:09 +0000265 if options.cflags:
carrot255fd462013-02-19 22:43:16 +0000266 build_env["CFLAGS"] = "`portageq envvar CFLAGS` " + options.cflags
cmticee59ac272013-02-19 20:20:09 +0000267 if options.cxxflags:
carrot255fd462013-02-19 22:43:16 +0000268 build_env["CXXFLAGS"] = "`portageq envvar CXXFLAGS` " + options.cxxflags
carrot2b549ca2013-02-19 22:45:25 +0000269 if options.cflags_for_target:
270 build_env["CFLAGS_FOR_TARGET"] = options.cflags_for_target
271 if options.cxxflags_for_target:
272 build_env["CXXFLAGS_FOR_TARGET"] = options.cxxflags_for_target
carrot255fd462013-02-19 22:43:16 +0000273 if options.ldflags:
cmticee59ac272013-02-19 20:20:09 +0000274 build_env["LDFLAGS"] = options.ldflags
asharifc97199a2013-02-15 22:48:45 +0000275 if options.debug:
276 debug_flags = "-g3 -O0"
cmticee59ac272013-02-19 20:20:09 +0000277 if "CFLAGS" in build_env:
278 build_env["CFLAGS"] += " %s" % (debug_flags)
279 else:
280 build_env["CFLAGS"] = debug_flags
281 if "CXXFLAGS" in build_env:
282 build_env["CXXFLAGS"] += " %s" % (debug_flags)
283 else:
284 build_env["CXXFLAGS"] = debug_flags
cmticecbc868a2013-02-21 22:52:46 +0000285 if options.extra_use_flags:
286 build_env["USE"] = options.extra_use_flags
bjanakiraman7f4a4852013-02-15 04:35:28 +0000287
asharif86968c42013-02-15 23:44:37 +0000288 # Create toolchain parts
carrote6f773c2013-02-19 22:34:44 +0000289 toolchain_parts = {}
asharif86968c42013-02-15 23:44:37 +0000290 for board in options.board.split(","):
291 if options.gcc_dir:
asharifc97199a2013-02-15 22:48:45 +0000292 tp = ToolchainPart("gcc", gcc_dir, chromeos_root, board,
llozanof2726d22013-03-09 01:29:37 +0000293 not options.noincremental, build_env,
294 options.gcc_enable_ccache)
carrote6f773c2013-02-19 22:34:44 +0000295 toolchain_parts[tp.tag] = tp
296 tp.RunSetupBoardIfNecessary()
shenhan8e8b0c22013-02-19 22:34:16 +0000297 if options.binutils_dir:
298 tp = ToolchainPart("binutils", binutils_dir, chromeos_root, board,
299 not options.noincremental, build_env)
carrote6f773c2013-02-19 22:34:44 +0000300 toolchain_parts[tp.tag] = tp
301 tp.RunSetupBoardIfNecessary()
cmtice80d257f2013-02-15 23:44:51 +0000302 if options.gdb_dir:
303 tp = ToolchainPart("gdb", gdb_dir, chromeos_root, board,
304 not options.noincremental, build_env)
carrote6f773c2013-02-19 22:34:44 +0000305 toolchain_parts[tp.tag] = tp
306 tp.RunSetupBoardIfNecessary()
asharif86968c42013-02-15 23:44:37 +0000307
shenhanbecf6242013-02-19 20:43:32 +0000308 rv = 0
asharif86968c42013-02-15 23:44:37 +0000309 try:
carrote6f773c2013-02-19 22:34:44 +0000310 for tag in toolchain_parts:
311 tp = toolchain_parts[tag]
cmtice80d257f2013-02-15 23:44:51 +0000312 if options.mount_only or options.unmount_only:
313 tp.MountSources(options.unmount_only)
asharif86968c42013-02-15 23:44:37 +0000314 else:
shenhanbecf6242013-02-19 20:43:32 +0000315 rv = rv + tp.Build()
asharifc97199a2013-02-15 22:48:45 +0000316 finally:
317 print "Exiting..."
llozano5fee82f2013-03-11 17:59:40 +0000318 return rv
asharif19c73dd2013-02-15 04:35:37 +0000319
320if __name__ == "__main__":
asharif2198c512013-02-15 09:21:35 +0000321 retval = Main(sys.argv)
322 sys.exit(retval)