blob: c14b6905c28fc4c50c7fd22d10f945245b95a1b5 [file] [log] [blame]
Caroline Ticef6ef4392017-04-06 17:16:05 -07001#!/usr/bin/env python2
bjanakiraman7f4a4852013-02-15 04:35:28 +00002#
Rahul Chaudhry8ac4ec02014-11-01 09:31:15 -07003# Copyright 2010 The Chromium OS Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
bjanakiraman7f4a4852013-02-15 04:35:28 +00006"""Script to build the ChromeOS toolchain.
7
8This script sets up the toolchain if you give it the gcctools directory.
9"""
10
Caroline Tice88272d42016-01-13 09:48:29 -080011from __future__ import print_function
12
Luis Lozanof2a3ef42015-12-15 13:49:30 -080013__author__ = 'asharif@google.com (Ahmad Sharif)'
bjanakiraman7f4a4852013-02-15 04:35:28 +000014
Caroline Tice88272d42016-01-13 09:48:29 -080015import argparse
asharif80c6e552013-02-15 04:35:40 +000016import getpass
asharif17621302013-02-15 04:46:35 +000017import os
bjanakiraman7f4a4852013-02-15 04:35:28 +000018import sys
asharifc97199a2013-02-15 22:48:45 +000019import tempfile
kbaclawski20082a02013-02-16 02:12:57 +000020
asharif252df0f2013-02-15 04:46:28 +000021import tc_enter_chroot
Caroline Tice88272d42016-01-13 09:48:29 -080022from cros_utils import command_executer
23from cros_utils import constants
24from cros_utils import misc
bjanakiraman7f4a4852013-02-15 04:35:28 +000025
asharif5a9bb462013-02-15 04:50:57 +000026
asharifc97199a2013-02-15 22:48:45 +000027class ToolchainPart(object):
Caroline Tice88272d42016-01-13 09:48:29 -080028 """Class to hold the toolchain pieces."""
Luis Lozanof2a3ef42015-12-15 13:49:30 -080029
30 def __init__(self,
31 name,
32 source_path,
33 chromeos_root,
34 board,
35 incremental,
36 build_env,
37 gcc_enable_ccache=False):
asharifc97199a2013-02-15 22:48:45 +000038 self._name = name
kbaclawski20082a02013-02-16 02:12:57 +000039 self._source_path = misc.CanonicalizePath(source_path)
asharifc97199a2013-02-15 22:48:45 +000040 self._chromeos_root = chromeos_root
41 self._board = board
Luis Lozanof2a3ef42015-12-15 13:49:30 -080042 self._ctarget = misc.GetCtargetFromBoard(self._board, self._chromeos_root)
Rahul Chaudhry215c12f2014-11-04 15:18:47 -080043 self._gcc_libs_dest = misc.GetGccLibsDestForBoard(self._board,
Luis Lozanof2a3ef42015-12-15 13:49:30 -080044 self._chromeos_root)
45 self.tag = '%s-%s' % (name, self._ctarget)
asharifc97199a2013-02-15 22:48:45 +000046 self._ce = command_executer.GetCommandExecuter()
Caroline Ticef6ef4392017-04-06 17:16:05 -070047 self._mask_file = os.path.join(
48 self._chromeos_root, 'chroot',
49 'etc/portage/package.mask/cross-%s' % self._ctarget)
asharifc97199a2013-02-15 22:48:45 +000050 self._new_mask_file = None
51
Han Shene4b6f222013-11-22 10:02:38 -080052 self._chroot_source_path = os.path.join(constants.MOUNTED_TOOLCHAIN_ROOT,
Luis Lozanof2a3ef42015-12-15 13:49:30 -080053 self._name).lstrip('/')
asharifc97199a2013-02-15 22:48:45 +000054 self._incremental = incremental
55 self._build_env = build_env
llozanof2726d22013-03-09 01:29:37 +000056 self._gcc_enable_ccache = gcc_enable_ccache
asharifc97199a2013-02-15 22:48:45 +000057
58 def RunSetupBoardIfNecessary(self):
Luis Lozanof2a3ef42015-12-15 13:49:30 -080059 cross_symlink = os.path.join(self._chromeos_root, 'chroot',
60 'usr/local/bin/emerge-%s' % self._board)
asharifc97199a2013-02-15 22:48:45 +000061 if not os.path.exists(cross_symlink):
Alex Klein3cef3162019-01-11 14:16:58 -070062 command = 'setup_board --board=%s' % self._board
asharifca3c6c12013-02-15 23:17:54 +000063 self._ce.ChrootRunCommand(self._chromeos_root, command)
asharifc97199a2013-02-15 22:48:45 +000064
65 def Build(self):
shenhanbecf6242013-02-19 20:43:32 +000066 rv = 1
asharifc97199a2013-02-15 22:48:45 +000067 try:
asharif7dd6d862013-02-15 23:17:46 +000068 self.UninstallTool()
asharifc97199a2013-02-15 22:48:45 +000069 self.MoveMaskFile()
cmtice80d257f2013-02-15 23:44:51 +000070 self.MountSources(False)
asharif9e3cf6e2013-02-16 03:13:38 +000071 self.RemoveCompiledFile()
shenhanbecf6242013-02-19 20:43:32 +000072 rv = self.BuildTool()
asharifc97199a2013-02-15 22:48:45 +000073 finally:
74 self.UnMoveMaskFile()
llozano9b84cb62013-03-11 21:08:31 +000075 return rv
asharifc97199a2013-02-15 22:48:45 +000076
77 def RemoveCompiledFile(self):
Luis Lozanof2a3ef42015-12-15 13:49:30 -080078 compiled_file = os.path.join(self._chromeos_root, 'chroot',
79 'var/tmp/portage/cross-%s' % self._ctarget,
80 '%s-9999' % self._name, '.compiled')
81 command = 'rm -f %s' % compiled_file
asharifc97199a2013-02-15 22:48:45 +000082 self._ce.RunCommand(command)
83
cmtice80d257f2013-02-15 23:44:51 +000084 def MountSources(self, unmount_source):
asharifc97199a2013-02-15 22:48:45 +000085 mount_points = []
Luis Lozanof2a3ef42015-12-15 13:49:30 -080086 mounted_source_path = os.path.join(self._chromeos_root, 'chroot',
asharifc97199a2013-02-15 22:48:45 +000087 self._chroot_source_path)
Luis Lozanof2a3ef42015-12-15 13:49:30 -080088 src_mp = tc_enter_chroot.MountPoint(self._source_path, mounted_source_path,
89 getpass.getuser(), 'ro')
asharifc97199a2013-02-15 22:48:45 +000090 mount_points.append(src_mp)
91
Luis Lozanof2a3ef42015-12-15 13:49:30 -080092 build_suffix = 'build-%s' % self._ctarget
93 build_dir = '%s-%s' % (self._source_path, build_suffix)
asharifc97199a2013-02-15 22:48:45 +000094
95 if not self._incremental and os.path.exists(build_dir):
Luis Lozanof2a3ef42015-12-15 13:49:30 -080096 command = 'rm -rf %s/*' % build_dir
asharifc97199a2013-02-15 22:48:45 +000097 self._ce.RunCommand(command)
98
99 # Create a -build directory for the objects.
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800100 command = 'mkdir -p %s' % build_dir
asharifc97199a2013-02-15 22:48:45 +0000101 self._ce.RunCommand(command)
102
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800103 mounted_build_dir = os.path.join(self._chromeos_root, 'chroot', '%s-%s' %
104 (self._chroot_source_path, build_suffix))
105 build_mp = tc_enter_chroot.MountPoint(build_dir, mounted_build_dir,
106 getpass.getuser())
asharifc97199a2013-02-15 22:48:45 +0000107 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]
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800111 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):
Caroline Ticef6ef4392017-04-06 17:16:05 -0700116 mounted = [
117 mp for mp, status in zip(mount_points, mount_statuses) if status
118 ]
cmtice80d257f2013-02-15 23:44:51 +0000119 unmount_statuses = [mp.UnMount() == 0 for mp in mounted]
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800120 assert all(unmount_statuses), 'Could not unmount all mount points!'
asharifc97199a2013-02-15 22:48:45 +0000121
asharif7dd6d862013-02-15 23:17:46 +0000122 def UninstallTool(self):
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800123 command = 'sudo CLEAN_DELAY=0 emerge -C cross-%s/%s' % (self._ctarget,
124 self._name)
asharifca3c6c12013-02-15 23:17:54 +0000125 self._ce.ChrootRunCommand(self._chromeos_root, command)
asharif7dd6d862013-02-15 23:17:46 +0000126
asharifc97199a2013-02-15 22:48:45 +0000127 def BuildTool(self):
128 env = self._build_env
asharif9e499162013-02-16 02:41:39 +0000129 # FEATURES=buildpkg adds minutes of time so we disable it.
shenhanfdc0f572013-02-19 18:48:36 +0000130 # TODO(shenhan): keep '-sandbox' for a while for compatibility, then remove
131 # it after a while.
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800132 features = ('nostrip userpriv userfetch -usersandbox -sandbox noclean '
133 '-buildpkg')
134 env['FEATURES'] = features
asharifc97199a2013-02-15 22:48:45 +0000135
136 if self._incremental:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800137 env['FEATURES'] += ' keepwork'
asharifc97199a2013-02-15 22:48:45 +0000138
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800139 if 'USE' in env:
140 env['USE'] += ' multislot mounted_%s' % self._name
cmticecbc868a2013-02-21 22:52:46 +0000141 else:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800142 env['USE'] = 'multislot mounted_%s' % self._name
cmticecbc868a2013-02-21 22:52:46 +0000143
llozanof2726d22013-03-09 01:29:37 +0000144 # Disable ccache in our compilers. cache may be problematic for us.
145 # It ignores compiler environments settings and it is not clear if
146 # the cache hit algorithm verifies all the compiler binaries or
147 # just the driver.
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800148 if self._name == 'gcc' and not self._gcc_enable_ccache:
149 env['USE'] += ' -wrapper_ccache'
llozanof2726d22013-03-09 01:29:37 +0000150
Caroline Ticef6ef4392017-04-06 17:16:05 -0700151 env['%s_SOURCE_PATH' % self._name.upper()] = (os.path.join(
152 '/', self._chroot_source_path))
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800153 env['ACCEPT_KEYWORDS'] = '~*'
154 env_string = ' '.join(["%s=\"%s\"" % var for var in env.items()])
155 command = 'emerge =cross-%s/%s-9999' % (self._ctarget, self._name)
156 full_command = 'sudo %s %s' % (env_string, command)
Luis Lozano4cea9de2013-03-20 12:32:56 -0700157 rv = self._ce.ChrootRunCommand(self._chromeos_root, full_command)
158 if rv != 0:
159 return rv
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800160 if self._name == 'gcc':
Caroline Ticef6ef4392017-04-06 17:16:05 -0700161 command = ('sudo cp -r /usr/lib/gcc/%s %s' % (self._ctarget,
162 self._gcc_libs_dest))
Luis Lozano4cea9de2013-03-20 12:32:56 -0700163 rv = self._ce.ChrootRunCommand(self._chromeos_root, command)
164 return rv
asharifc97199a2013-02-15 22:48:45 +0000165
asharifc97199a2013-02-15 22:48:45 +0000166 def MoveMaskFile(self):
167 self._new_mask_file = None
168 if os.path.isfile(self._mask_file):
169 self._new_mask_file = tempfile.mktemp()
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800170 command = 'sudo mv %s %s' % (self._mask_file, self._new_mask_file)
asharifc97199a2013-02-15 22:48:45 +0000171 self._ce.RunCommand(command)
172
173 def UnMoveMaskFile(self):
174 if self._new_mask_file:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800175 command = 'sudo mv %s %s' % (self._new_mask_file, self._mask_file)
asharifc97199a2013-02-15 22:48:45 +0000176 self._ce.RunCommand(command)
bjanakiraman7f4a4852013-02-15 04:35:28 +0000177
bjanakiraman7f4a4852013-02-15 04:35:28 +0000178
asharif0d3535a2013-02-15 04:50:33 +0000179def Main(argv):
asharif19c73dd2013-02-15 04:35:37 +0000180 """The main function."""
asharif5a9bb462013-02-15 04:50:57 +0000181 # Common initializations
Caroline Tice88272d42016-01-13 09:48:29 -0800182 parser = argparse.ArgumentParser()
Caroline Ticef6ef4392017-04-06 17:16:05 -0700183 parser.add_argument(
184 '-c',
185 '--chromeos_root',
186 dest='chromeos_root',
187 default='../../',
188 help=('ChromeOS root checkout directory'
189 ' uses ../.. if none given.'))
190 parser.add_argument(
191 '-g',
192 '--gcc_dir',
193 dest='gcc_dir',
194 help='The directory where gcc resides.')
195 parser.add_argument(
196 '--binutils_dir',
197 dest='binutils_dir',
198 help='The directory where binutils resides.')
199 parser.add_argument(
200 '-x',
201 '--gdb_dir',
202 dest='gdb_dir',
203 help='The directory where gdb resides.')
204 parser.add_argument(
205 '-b',
206 '--board',
207 dest='board',
208 default='x86-alex',
209 help='The target board.')
210 parser.add_argument(
211 '-n',
212 '--noincremental',
213 dest='noincremental',
214 default=False,
215 action='store_true',
216 help='Use FEATURES=keepwork to do incremental builds.')
217 parser.add_argument(
218 '--cflags',
219 dest='cflags',
220 default='',
221 help='Build a compiler with specified CFLAGS')
222 parser.add_argument(
223 '--cxxflags',
224 dest='cxxflags',
225 default='',
226 help='Build a compiler with specified CXXFLAGS')
227 parser.add_argument(
228 '--cflags_for_target',
229 dest='cflags_for_target',
230 default='',
231 help='Build the target libraries with specified flags')
232 parser.add_argument(
233 '--cxxflags_for_target',
234 dest='cxxflags_for_target',
235 default='',
236 help='Build the target libraries with specified flags')
237 parser.add_argument(
238 '--ldflags',
239 dest='ldflags',
240 default='',
241 help='Build a compiler with specified LDFLAGS')
242 parser.add_argument(
243 '-d',
244 '--debug',
245 dest='debug',
246 default=False,
247 action='store_true',
248 help='Build a compiler with -g3 -O0 appended to both'
249 ' CFLAGS and CXXFLAGS.')
250 parser.add_argument(
251 '-m',
252 '--mount_only',
253 dest='mount_only',
254 default=False,
255 action='store_true',
256 help='Just mount the tool directories.')
257 parser.add_argument(
258 '-u',
259 '--unmount_only',
260 dest='unmount_only',
261 default=False,
262 action='store_true',
263 help='Just unmount the tool directories.')
264 parser.add_argument(
265 '--extra_use_flags',
266 dest='extra_use_flags',
267 default='',
268 help='Extra flag for USE, to be passed to the ebuild. '
269 "('multislot' and 'mounted_<tool>' are always passed.)")
270 parser.add_argument(
271 '--gcc_enable_ccache',
272 dest='gcc_enable_ccache',
273 default=False,
274 action='store_true',
275 help='Enable ccache for the gcc invocations')
bjanakiraman7f4a4852013-02-15 04:35:28 +0000276
Caroline Tice88272d42016-01-13 09:48:29 -0800277 options = parser.parse_args(argv)
asharif17621302013-02-15 04:46:35 +0000278
kbaclawski20082a02013-02-16 02:12:57 +0000279 chromeos_root = misc.CanonicalizePath(options.chromeos_root)
cmtice80d257f2013-02-15 23:44:51 +0000280 if options.gcc_dir:
kbaclawski20082a02013-02-16 02:12:57 +0000281 gcc_dir = misc.CanonicalizePath(options.gcc_dir)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800282 assert gcc_dir and os.path.isdir(gcc_dir), 'gcc_dir does not exist!'
shenhan8e8b0c22013-02-19 22:34:16 +0000283 if options.binutils_dir:
284 binutils_dir = misc.CanonicalizePath(options.binutils_dir)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800285 assert os.path.isdir(binutils_dir), 'binutils_dir does not exist!'
cmtice80d257f2013-02-15 23:44:51 +0000286 if options.gdb_dir:
kbaclawski20082a02013-02-16 02:12:57 +0000287 gdb_dir = misc.CanonicalizePath(options.gdb_dir)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800288 assert os.path.isdir(gdb_dir), 'gdb_dir does not exist!'
cmtice80d257f2013-02-15 23:44:51 +0000289 if options.unmount_only:
290 options.mount_only = False
291 elif options.mount_only:
292 options.unmount_only = False
asharifc97199a2013-02-15 22:48:45 +0000293 build_env = {}
cmticee59ac272013-02-19 20:20:09 +0000294 if options.cflags:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800295 build_env['CFLAGS'] = '`portageq envvar CFLAGS` ' + options.cflags
cmticee59ac272013-02-19 20:20:09 +0000296 if options.cxxflags:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800297 build_env['CXXFLAGS'] = '`portageq envvar CXXFLAGS` ' + options.cxxflags
carrot2b549ca2013-02-19 22:45:25 +0000298 if options.cflags_for_target:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800299 build_env['CFLAGS_FOR_TARGET'] = options.cflags_for_target
carrot2b549ca2013-02-19 22:45:25 +0000300 if options.cxxflags_for_target:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800301 build_env['CXXFLAGS_FOR_TARGET'] = options.cxxflags_for_target
carrot255fd462013-02-19 22:43:16 +0000302 if options.ldflags:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800303 build_env['LDFLAGS'] = options.ldflags
asharifc97199a2013-02-15 22:48:45 +0000304 if options.debug:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800305 debug_flags = '-g3 -O0'
306 if 'CFLAGS' in build_env:
307 build_env['CFLAGS'] += ' %s' % (debug_flags)
cmticee59ac272013-02-19 20:20:09 +0000308 else:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800309 build_env['CFLAGS'] = debug_flags
310 if 'CXXFLAGS' in build_env:
311 build_env['CXXFLAGS'] += ' %s' % (debug_flags)
cmticee59ac272013-02-19 20:20:09 +0000312 else:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800313 build_env['CXXFLAGS'] = debug_flags
cmticecbc868a2013-02-21 22:52:46 +0000314 if options.extra_use_flags:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800315 build_env['USE'] = options.extra_use_flags
bjanakiraman7f4a4852013-02-15 04:35:28 +0000316
asharif86968c42013-02-15 23:44:37 +0000317 # Create toolchain parts
carrote6f773c2013-02-19 22:34:44 +0000318 toolchain_parts = {}
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800319 for board in options.board.split(','):
asharif86968c42013-02-15 23:44:37 +0000320 if options.gcc_dir:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800321 tp = ToolchainPart('gcc', gcc_dir, chromeos_root, board,
llozanof2726d22013-03-09 01:29:37 +0000322 not options.noincremental, build_env,
323 options.gcc_enable_ccache)
carrote6f773c2013-02-19 22:34:44 +0000324 toolchain_parts[tp.tag] = tp
325 tp.RunSetupBoardIfNecessary()
shenhan8e8b0c22013-02-19 22:34:16 +0000326 if options.binutils_dir:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800327 tp = ToolchainPart('binutils', binutils_dir, chromeos_root, board,
shenhan8e8b0c22013-02-19 22:34:16 +0000328 not options.noincremental, build_env)
carrote6f773c2013-02-19 22:34:44 +0000329 toolchain_parts[tp.tag] = tp
330 tp.RunSetupBoardIfNecessary()
cmtice80d257f2013-02-15 23:44:51 +0000331 if options.gdb_dir:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800332 tp = ToolchainPart('gdb', gdb_dir, chromeos_root, board,
cmtice80d257f2013-02-15 23:44:51 +0000333 not options.noincremental, build_env)
carrote6f773c2013-02-19 22:34:44 +0000334 toolchain_parts[tp.tag] = tp
335 tp.RunSetupBoardIfNecessary()
asharif86968c42013-02-15 23:44:37 +0000336
shenhanbecf6242013-02-19 20:43:32 +0000337 rv = 0
asharif86968c42013-02-15 23:44:37 +0000338 try:
carrote6f773c2013-02-19 22:34:44 +0000339 for tag in toolchain_parts:
340 tp = toolchain_parts[tag]
cmtice80d257f2013-02-15 23:44:51 +0000341 if options.mount_only or options.unmount_only:
342 tp.MountSources(options.unmount_only)
asharif86968c42013-02-15 23:44:37 +0000343 else:
shenhanbecf6242013-02-19 20:43:32 +0000344 rv = rv + tp.Build()
asharifc97199a2013-02-15 22:48:45 +0000345 finally:
Caroline Tice88272d42016-01-13 09:48:29 -0800346 print('Exiting...')
llozano5fee82f2013-03-11 17:59:40 +0000347 return rv
asharif19c73dd2013-02-15 04:35:37 +0000348
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800349
350if __name__ == '__main__':
Caroline Tice88272d42016-01-13 09:48:29 -0800351 retval = Main(sys.argv[1:])
asharif2198c512013-02-15 09:21:35 +0000352 sys.exit(retval)