blob: 4f022d29ccb3682aa7afd0a9f288e2c311b78e72 [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):
Caroline Ticef6ef4392017-04-06 17:16:05 -070062 command = ('%s/setup_board --board=%s' % (misc.CHROMEOS_SCRIPTS_DIR,
63 self._board))
asharifca3c6c12013-02-15 23:17:54 +000064 self._ce.ChrootRunCommand(self._chromeos_root, command)
asharifc97199a2013-02-15 22:48:45 +000065
66 def Build(self):
shenhanbecf6242013-02-19 20:43:32 +000067 rv = 1
asharifc97199a2013-02-15 22:48:45 +000068 try:
asharif7dd6d862013-02-15 23:17:46 +000069 self.UninstallTool()
asharifc97199a2013-02-15 22:48:45 +000070 self.MoveMaskFile()
cmtice80d257f2013-02-15 23:44:51 +000071 self.MountSources(False)
asharif9e3cf6e2013-02-16 03:13:38 +000072 self.RemoveCompiledFile()
shenhanbecf6242013-02-19 20:43:32 +000073 rv = self.BuildTool()
asharifc97199a2013-02-15 22:48:45 +000074 finally:
75 self.UnMoveMaskFile()
llozano9b84cb62013-03-11 21:08:31 +000076 return rv
asharifc97199a2013-02-15 22:48:45 +000077
78 def RemoveCompiledFile(self):
Luis Lozanof2a3ef42015-12-15 13:49:30 -080079 compiled_file = os.path.join(self._chromeos_root, 'chroot',
80 'var/tmp/portage/cross-%s' % self._ctarget,
81 '%s-9999' % self._name, '.compiled')
82 command = 'rm -f %s' % compiled_file
asharifc97199a2013-02-15 22:48:45 +000083 self._ce.RunCommand(command)
84
cmtice80d257f2013-02-15 23:44:51 +000085 def MountSources(self, unmount_source):
asharifc97199a2013-02-15 22:48:45 +000086 mount_points = []
Luis Lozanof2a3ef42015-12-15 13:49:30 -080087 mounted_source_path = os.path.join(self._chromeos_root, 'chroot',
asharifc97199a2013-02-15 22:48:45 +000088 self._chroot_source_path)
Luis Lozanof2a3ef42015-12-15 13:49:30 -080089 src_mp = tc_enter_chroot.MountPoint(self._source_path, mounted_source_path,
90 getpass.getuser(), 'ro')
asharifc97199a2013-02-15 22:48:45 +000091 mount_points.append(src_mp)
92
Luis Lozanof2a3ef42015-12-15 13:49:30 -080093 build_suffix = 'build-%s' % self._ctarget
94 build_dir = '%s-%s' % (self._source_path, build_suffix)
asharifc97199a2013-02-15 22:48:45 +000095
96 if not self._incremental and os.path.exists(build_dir):
Luis Lozanof2a3ef42015-12-15 13:49:30 -080097 command = 'rm -rf %s/*' % build_dir
asharifc97199a2013-02-15 22:48:45 +000098 self._ce.RunCommand(command)
99
100 # Create a -build directory for the objects.
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800101 command = 'mkdir -p %s' % build_dir
asharifc97199a2013-02-15 22:48:45 +0000102 self._ce.RunCommand(command)
103
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800104 mounted_build_dir = os.path.join(self._chromeos_root, 'chroot', '%s-%s' %
105 (self._chroot_source_path, build_suffix))
106 build_mp = tc_enter_chroot.MountPoint(build_dir, mounted_build_dir,
107 getpass.getuser())
asharifc97199a2013-02-15 22:48:45 +0000108 mount_points.append(build_mp)
109
cmtice80d257f2013-02-15 23:44:51 +0000110 if unmount_source:
111 unmount_statuses = [mp.UnMount() == 0 for mp in mount_points]
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800112 assert all(unmount_statuses), 'Could not unmount all mount points!'
cmtice80d257f2013-02-15 23:44:51 +0000113 else:
114 mount_statuses = [mp.DoMount() == 0 for mp in mount_points]
115
116 if not all(mount_statuses):
Caroline Ticef6ef4392017-04-06 17:16:05 -0700117 mounted = [
118 mp for mp, status in zip(mount_points, mount_statuses) if status
119 ]
cmtice80d257f2013-02-15 23:44:51 +0000120 unmount_statuses = [mp.UnMount() == 0 for mp in mounted]
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800121 assert all(unmount_statuses), 'Could not unmount all mount points!'
asharifc97199a2013-02-15 22:48:45 +0000122
asharif7dd6d862013-02-15 23:17:46 +0000123 def UninstallTool(self):
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800124 command = 'sudo CLEAN_DELAY=0 emerge -C cross-%s/%s' % (self._ctarget,
125 self._name)
asharifca3c6c12013-02-15 23:17:54 +0000126 self._ce.ChrootRunCommand(self._chromeos_root, command)
asharif7dd6d862013-02-15 23:17:46 +0000127
asharifc97199a2013-02-15 22:48:45 +0000128 def BuildTool(self):
129 env = self._build_env
asharif9e499162013-02-16 02:41:39 +0000130 # FEATURES=buildpkg adds minutes of time so we disable it.
shenhanfdc0f572013-02-19 18:48:36 +0000131 # TODO(shenhan): keep '-sandbox' for a while for compatibility, then remove
132 # it after a while.
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800133 features = ('nostrip userpriv userfetch -usersandbox -sandbox noclean '
134 '-buildpkg')
135 env['FEATURES'] = features
asharifc97199a2013-02-15 22:48:45 +0000136
137 if self._incremental:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800138 env['FEATURES'] += ' keepwork'
asharifc97199a2013-02-15 22:48:45 +0000139
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800140 if 'USE' in env:
141 env['USE'] += ' multislot mounted_%s' % self._name
cmticecbc868a2013-02-21 22:52:46 +0000142 else:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800143 env['USE'] = 'multislot mounted_%s' % self._name
cmticecbc868a2013-02-21 22:52:46 +0000144
llozanof2726d22013-03-09 01:29:37 +0000145 # Disable ccache in our compilers. cache may be problematic for us.
146 # It ignores compiler environments settings and it is not clear if
147 # the cache hit algorithm verifies all the compiler binaries or
148 # just the driver.
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800149 if self._name == 'gcc' and not self._gcc_enable_ccache:
150 env['USE'] += ' -wrapper_ccache'
llozanof2726d22013-03-09 01:29:37 +0000151
Caroline Ticef6ef4392017-04-06 17:16:05 -0700152 env['%s_SOURCE_PATH' % self._name.upper()] = (os.path.join(
153 '/', self._chroot_source_path))
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800154 env['ACCEPT_KEYWORDS'] = '~*'
155 env_string = ' '.join(["%s=\"%s\"" % var for var in env.items()])
156 command = 'emerge =cross-%s/%s-9999' % (self._ctarget, self._name)
157 full_command = 'sudo %s %s' % (env_string, command)
Luis Lozano4cea9de2013-03-20 12:32:56 -0700158 rv = self._ce.ChrootRunCommand(self._chromeos_root, full_command)
159 if rv != 0:
160 return rv
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800161 if self._name == 'gcc':
Caroline Ticef6ef4392017-04-06 17:16:05 -0700162 command = ('sudo cp -r /usr/lib/gcc/%s %s' % (self._ctarget,
163 self._gcc_libs_dest))
Luis Lozano4cea9de2013-03-20 12:32:56 -0700164 rv = self._ce.ChrootRunCommand(self._chromeos_root, command)
165 return rv
asharifc97199a2013-02-15 22:48:45 +0000166
asharifc97199a2013-02-15 22:48:45 +0000167 def MoveMaskFile(self):
168 self._new_mask_file = None
169 if os.path.isfile(self._mask_file):
170 self._new_mask_file = tempfile.mktemp()
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800171 command = 'sudo mv %s %s' % (self._mask_file, self._new_mask_file)
asharifc97199a2013-02-15 22:48:45 +0000172 self._ce.RunCommand(command)
173
174 def UnMoveMaskFile(self):
175 if self._new_mask_file:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800176 command = 'sudo mv %s %s' % (self._new_mask_file, self._mask_file)
asharifc97199a2013-02-15 22:48:45 +0000177 self._ce.RunCommand(command)
bjanakiraman7f4a4852013-02-15 04:35:28 +0000178
bjanakiraman7f4a4852013-02-15 04:35:28 +0000179
asharif0d3535a2013-02-15 04:50:33 +0000180def Main(argv):
asharif19c73dd2013-02-15 04:35:37 +0000181 """The main function."""
asharif5a9bb462013-02-15 04:50:57 +0000182 # Common initializations
Caroline Tice88272d42016-01-13 09:48:29 -0800183 parser = argparse.ArgumentParser()
Caroline Ticef6ef4392017-04-06 17:16:05 -0700184 parser.add_argument(
185 '-c',
186 '--chromeos_root',
187 dest='chromeos_root',
188 default='../../',
189 help=('ChromeOS root checkout directory'
190 ' uses ../.. if none given.'))
191 parser.add_argument(
192 '-g',
193 '--gcc_dir',
194 dest='gcc_dir',
195 help='The directory where gcc resides.')
196 parser.add_argument(
197 '--binutils_dir',
198 dest='binutils_dir',
199 help='The directory where binutils resides.')
200 parser.add_argument(
201 '-x',
202 '--gdb_dir',
203 dest='gdb_dir',
204 help='The directory where gdb resides.')
205 parser.add_argument(
206 '-b',
207 '--board',
208 dest='board',
209 default='x86-alex',
210 help='The target board.')
211 parser.add_argument(
212 '-n',
213 '--noincremental',
214 dest='noincremental',
215 default=False,
216 action='store_true',
217 help='Use FEATURES=keepwork to do incremental builds.')
218 parser.add_argument(
219 '--cflags',
220 dest='cflags',
221 default='',
222 help='Build a compiler with specified CFLAGS')
223 parser.add_argument(
224 '--cxxflags',
225 dest='cxxflags',
226 default='',
227 help='Build a compiler with specified CXXFLAGS')
228 parser.add_argument(
229 '--cflags_for_target',
230 dest='cflags_for_target',
231 default='',
232 help='Build the target libraries with specified flags')
233 parser.add_argument(
234 '--cxxflags_for_target',
235 dest='cxxflags_for_target',
236 default='',
237 help='Build the target libraries with specified flags')
238 parser.add_argument(
239 '--ldflags',
240 dest='ldflags',
241 default='',
242 help='Build a compiler with specified LDFLAGS')
243 parser.add_argument(
244 '-d',
245 '--debug',
246 dest='debug',
247 default=False,
248 action='store_true',
249 help='Build a compiler with -g3 -O0 appended to both'
250 ' CFLAGS and CXXFLAGS.')
251 parser.add_argument(
252 '-m',
253 '--mount_only',
254 dest='mount_only',
255 default=False,
256 action='store_true',
257 help='Just mount the tool directories.')
258 parser.add_argument(
259 '-u',
260 '--unmount_only',
261 dest='unmount_only',
262 default=False,
263 action='store_true',
264 help='Just unmount the tool directories.')
265 parser.add_argument(
266 '--extra_use_flags',
267 dest='extra_use_flags',
268 default='',
269 help='Extra flag for USE, to be passed to the ebuild. '
270 "('multislot' and 'mounted_<tool>' are always passed.)")
271 parser.add_argument(
272 '--gcc_enable_ccache',
273 dest='gcc_enable_ccache',
274 default=False,
275 action='store_true',
276 help='Enable ccache for the gcc invocations')
bjanakiraman7f4a4852013-02-15 04:35:28 +0000277
Caroline Tice88272d42016-01-13 09:48:29 -0800278 options = parser.parse_args(argv)
asharif17621302013-02-15 04:46:35 +0000279
kbaclawski20082a02013-02-16 02:12:57 +0000280 chromeos_root = misc.CanonicalizePath(options.chromeos_root)
cmtice80d257f2013-02-15 23:44:51 +0000281 if options.gcc_dir:
kbaclawski20082a02013-02-16 02:12:57 +0000282 gcc_dir = misc.CanonicalizePath(options.gcc_dir)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800283 assert gcc_dir and os.path.isdir(gcc_dir), 'gcc_dir does not exist!'
shenhan8e8b0c22013-02-19 22:34:16 +0000284 if options.binutils_dir:
285 binutils_dir = misc.CanonicalizePath(options.binutils_dir)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800286 assert os.path.isdir(binutils_dir), 'binutils_dir does not exist!'
cmtice80d257f2013-02-15 23:44:51 +0000287 if options.gdb_dir:
kbaclawski20082a02013-02-16 02:12:57 +0000288 gdb_dir = misc.CanonicalizePath(options.gdb_dir)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800289 assert os.path.isdir(gdb_dir), 'gdb_dir does not exist!'
cmtice80d257f2013-02-15 23:44:51 +0000290 if options.unmount_only:
291 options.mount_only = False
292 elif options.mount_only:
293 options.unmount_only = False
asharifc97199a2013-02-15 22:48:45 +0000294 build_env = {}
cmticee59ac272013-02-19 20:20:09 +0000295 if options.cflags:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800296 build_env['CFLAGS'] = '`portageq envvar CFLAGS` ' + options.cflags
cmticee59ac272013-02-19 20:20:09 +0000297 if options.cxxflags:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800298 build_env['CXXFLAGS'] = '`portageq envvar CXXFLAGS` ' + options.cxxflags
carrot2b549ca2013-02-19 22:45:25 +0000299 if options.cflags_for_target:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800300 build_env['CFLAGS_FOR_TARGET'] = options.cflags_for_target
carrot2b549ca2013-02-19 22:45:25 +0000301 if options.cxxflags_for_target:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800302 build_env['CXXFLAGS_FOR_TARGET'] = options.cxxflags_for_target
carrot255fd462013-02-19 22:43:16 +0000303 if options.ldflags:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800304 build_env['LDFLAGS'] = options.ldflags
asharifc97199a2013-02-15 22:48:45 +0000305 if options.debug:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800306 debug_flags = '-g3 -O0'
307 if 'CFLAGS' in build_env:
308 build_env['CFLAGS'] += ' %s' % (debug_flags)
cmticee59ac272013-02-19 20:20:09 +0000309 else:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800310 build_env['CFLAGS'] = debug_flags
311 if 'CXXFLAGS' in build_env:
312 build_env['CXXFLAGS'] += ' %s' % (debug_flags)
cmticee59ac272013-02-19 20:20:09 +0000313 else:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800314 build_env['CXXFLAGS'] = debug_flags
cmticecbc868a2013-02-21 22:52:46 +0000315 if options.extra_use_flags:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800316 build_env['USE'] = options.extra_use_flags
bjanakiraman7f4a4852013-02-15 04:35:28 +0000317
asharif86968c42013-02-15 23:44:37 +0000318 # Create toolchain parts
carrote6f773c2013-02-19 22:34:44 +0000319 toolchain_parts = {}
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800320 for board in options.board.split(','):
asharif86968c42013-02-15 23:44:37 +0000321 if options.gcc_dir:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800322 tp = ToolchainPart('gcc', gcc_dir, chromeos_root, board,
llozanof2726d22013-03-09 01:29:37 +0000323 not options.noincremental, build_env,
324 options.gcc_enable_ccache)
carrote6f773c2013-02-19 22:34:44 +0000325 toolchain_parts[tp.tag] = tp
326 tp.RunSetupBoardIfNecessary()
shenhan8e8b0c22013-02-19 22:34:16 +0000327 if options.binutils_dir:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800328 tp = ToolchainPart('binutils', binutils_dir, chromeos_root, board,
shenhan8e8b0c22013-02-19 22:34:16 +0000329 not options.noincremental, build_env)
carrote6f773c2013-02-19 22:34:44 +0000330 toolchain_parts[tp.tag] = tp
331 tp.RunSetupBoardIfNecessary()
cmtice80d257f2013-02-15 23:44:51 +0000332 if options.gdb_dir:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800333 tp = ToolchainPart('gdb', gdb_dir, chromeos_root, board,
cmtice80d257f2013-02-15 23:44:51 +0000334 not options.noincremental, build_env)
carrote6f773c2013-02-19 22:34:44 +0000335 toolchain_parts[tp.tag] = tp
336 tp.RunSetupBoardIfNecessary()
asharif86968c42013-02-15 23:44:37 +0000337
shenhanbecf6242013-02-19 20:43:32 +0000338 rv = 0
asharif86968c42013-02-15 23:44:37 +0000339 try:
carrote6f773c2013-02-19 22:34:44 +0000340 for tag in toolchain_parts:
341 tp = toolchain_parts[tag]
cmtice80d257f2013-02-15 23:44:51 +0000342 if options.mount_only or options.unmount_only:
343 tp.MountSources(options.unmount_only)
asharif86968c42013-02-15 23:44:37 +0000344 else:
shenhanbecf6242013-02-19 20:43:32 +0000345 rv = rv + tp.Build()
asharifc97199a2013-02-15 22:48:45 +0000346 finally:
Caroline Tice88272d42016-01-13 09:48:29 -0800347 print('Exiting...')
llozano5fee82f2013-03-11 17:59:40 +0000348 return rv
asharif19c73dd2013-02-15 04:35:37 +0000349
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800350
351if __name__ == '__main__':
Caroline Tice88272d42016-01-13 09:48:29 -0800352 retval = Main(sys.argv[1:])
asharif2198c512013-02-15 09:21:35 +0000353 sys.exit(retval)