blob: 55fc5b70ff4de568c486e6ac0898e2b7853624cb [file] [log] [blame]
Caroline Tice88272d42016-01-13 09:48:29 -08001#!/usr/bin/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()
Luis Lozanof2a3ef42015-12-15 13:49:30 -080047 self._mask_file = os.path.join(self._chromeos_root, 'chroot',
48 'etc/portage/package.mask/cross-%s' %
49 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):
Luis Lozanof2a3ef42015-12-15 13:49:30 -080062 command = ('%s/setup_board --board=%s' %
Rahul Chaudhry8ac4ec02014-11-01 09:31:15 -070063 (misc.CHROMEOS_SCRIPTS_DIR, 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):
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800117 mounted = [mp
118 for mp, status in zip(mount_points, mount_statuses)
119 if status]
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
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800152 env['%s_SOURCE_PATH' % self._name.upper()] = (
153 os.path.join('/', self._chroot_source_path))
154 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':
162 command = ('sudo cp -r /usr/lib/gcc/%s %s' %
Rahul Chaudhry215c12f2014-11-04 15:18:47 -0800163 (self._ctarget, 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()
184 parser.add_argument('-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('-g',
191 '--gcc_dir',
192 dest='gcc_dir',
193 help='The directory where gcc resides.')
194 parser.add_argument('--binutils_dir',
195 dest='binutils_dir',
196 help='The directory where binutils resides.')
197 parser.add_argument('-x',
198 '--gdb_dir',
199 dest='gdb_dir',
200 help='The directory where gdb resides.')
201 parser.add_argument('-b',
202 '--board',
203 dest='board',
204 default='x86-alex',
205 help='The target board.')
206 parser.add_argument('-n',
207 '--noincremental',
208 dest='noincremental',
209 default=False,
210 action='store_true',
211 help='Use FEATURES=keepwork to do incremental builds.')
212 parser.add_argument('--cflags',
213 dest='cflags',
214 default='',
215 help='Build a compiler with specified CFLAGS')
216 parser.add_argument('--cxxflags',
217 dest='cxxflags',
218 default='',
219 help='Build a compiler with specified CXXFLAGS')
220 parser.add_argument('--cflags_for_target',
221 dest='cflags_for_target',
222 default='',
223 help='Build the target libraries with specified flags')
224 parser.add_argument('--cxxflags_for_target',
225 dest='cxxflags_for_target',
226 default='',
227 help='Build the target libraries with specified flags')
228 parser.add_argument('--ldflags',
229 dest='ldflags',
230 default='',
231 help='Build a compiler with specified LDFLAGS')
232 parser.add_argument('-d',
233 '--debug',
234 dest='debug',
235 default=False,
236 action='store_true',
237 help='Build a compiler with -g3 -O0 appended to both'
238 ' CFLAGS and CXXFLAGS.')
239 parser.add_argument('-m',
240 '--mount_only',
241 dest='mount_only',
242 default=False,
243 action='store_true',
244 help='Just mount the tool directories.')
245 parser.add_argument('-u',
246 '--unmount_only',
247 dest='unmount_only',
248 default=False,
249 action='store_true',
250 help='Just unmount the tool directories.')
251 parser.add_argument('--extra_use_flags',
252 dest='extra_use_flags',
253 default='',
254 help='Extra flag for USE, to be passed to the ebuild. '
255 "('multislot' and 'mounted_<tool>' are always passed.)")
256 parser.add_argument('--gcc_enable_ccache',
257 dest='gcc_enable_ccache',
258 default=False,
259 action='store_true',
260 help='Enable ccache for the gcc invocations')
bjanakiraman7f4a4852013-02-15 04:35:28 +0000261
Caroline Tice88272d42016-01-13 09:48:29 -0800262 options = parser.parse_args(argv)
asharif17621302013-02-15 04:46:35 +0000263
kbaclawski20082a02013-02-16 02:12:57 +0000264 chromeos_root = misc.CanonicalizePath(options.chromeos_root)
cmtice80d257f2013-02-15 23:44:51 +0000265 if options.gcc_dir:
kbaclawski20082a02013-02-16 02:12:57 +0000266 gcc_dir = misc.CanonicalizePath(options.gcc_dir)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800267 assert gcc_dir and os.path.isdir(gcc_dir), 'gcc_dir does not exist!'
shenhan8e8b0c22013-02-19 22:34:16 +0000268 if options.binutils_dir:
269 binutils_dir = misc.CanonicalizePath(options.binutils_dir)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800270 assert os.path.isdir(binutils_dir), 'binutils_dir does not exist!'
cmtice80d257f2013-02-15 23:44:51 +0000271 if options.gdb_dir:
kbaclawski20082a02013-02-16 02:12:57 +0000272 gdb_dir = misc.CanonicalizePath(options.gdb_dir)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800273 assert os.path.isdir(gdb_dir), 'gdb_dir does not exist!'
cmtice80d257f2013-02-15 23:44:51 +0000274 if options.unmount_only:
275 options.mount_only = False
276 elif options.mount_only:
277 options.unmount_only = False
asharifc97199a2013-02-15 22:48:45 +0000278 build_env = {}
cmticee59ac272013-02-19 20:20:09 +0000279 if options.cflags:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800280 build_env['CFLAGS'] = '`portageq envvar CFLAGS` ' + options.cflags
cmticee59ac272013-02-19 20:20:09 +0000281 if options.cxxflags:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800282 build_env['CXXFLAGS'] = '`portageq envvar CXXFLAGS` ' + options.cxxflags
carrot2b549ca2013-02-19 22:45:25 +0000283 if options.cflags_for_target:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800284 build_env['CFLAGS_FOR_TARGET'] = options.cflags_for_target
carrot2b549ca2013-02-19 22:45:25 +0000285 if options.cxxflags_for_target:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800286 build_env['CXXFLAGS_FOR_TARGET'] = options.cxxflags_for_target
carrot255fd462013-02-19 22:43:16 +0000287 if options.ldflags:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800288 build_env['LDFLAGS'] = options.ldflags
asharifc97199a2013-02-15 22:48:45 +0000289 if options.debug:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800290 debug_flags = '-g3 -O0'
291 if 'CFLAGS' in build_env:
292 build_env['CFLAGS'] += ' %s' % (debug_flags)
cmticee59ac272013-02-19 20:20:09 +0000293 else:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800294 build_env['CFLAGS'] = debug_flags
295 if 'CXXFLAGS' in build_env:
296 build_env['CXXFLAGS'] += ' %s' % (debug_flags)
cmticee59ac272013-02-19 20:20:09 +0000297 else:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800298 build_env['CXXFLAGS'] = debug_flags
cmticecbc868a2013-02-21 22:52:46 +0000299 if options.extra_use_flags:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800300 build_env['USE'] = options.extra_use_flags
bjanakiraman7f4a4852013-02-15 04:35:28 +0000301
asharif86968c42013-02-15 23:44:37 +0000302 # Create toolchain parts
carrote6f773c2013-02-19 22:34:44 +0000303 toolchain_parts = {}
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800304 for board in options.board.split(','):
asharif86968c42013-02-15 23:44:37 +0000305 if options.gcc_dir:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800306 tp = ToolchainPart('gcc', gcc_dir, chromeos_root, board,
llozanof2726d22013-03-09 01:29:37 +0000307 not options.noincremental, build_env,
308 options.gcc_enable_ccache)
carrote6f773c2013-02-19 22:34:44 +0000309 toolchain_parts[tp.tag] = tp
310 tp.RunSetupBoardIfNecessary()
shenhan8e8b0c22013-02-19 22:34:16 +0000311 if options.binutils_dir:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800312 tp = ToolchainPart('binutils', binutils_dir, chromeos_root, board,
shenhan8e8b0c22013-02-19 22:34:16 +0000313 not options.noincremental, build_env)
carrote6f773c2013-02-19 22:34:44 +0000314 toolchain_parts[tp.tag] = tp
315 tp.RunSetupBoardIfNecessary()
cmtice80d257f2013-02-15 23:44:51 +0000316 if options.gdb_dir:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800317 tp = ToolchainPart('gdb', gdb_dir, chromeos_root, board,
cmtice80d257f2013-02-15 23:44:51 +0000318 not options.noincremental, build_env)
carrote6f773c2013-02-19 22:34:44 +0000319 toolchain_parts[tp.tag] = tp
320 tp.RunSetupBoardIfNecessary()
asharif86968c42013-02-15 23:44:37 +0000321
shenhanbecf6242013-02-19 20:43:32 +0000322 rv = 0
asharif86968c42013-02-15 23:44:37 +0000323 try:
carrote6f773c2013-02-19 22:34:44 +0000324 for tag in toolchain_parts:
325 tp = toolchain_parts[tag]
cmtice80d257f2013-02-15 23:44:51 +0000326 if options.mount_only or options.unmount_only:
327 tp.MountSources(options.unmount_only)
asharif86968c42013-02-15 23:44:37 +0000328 else:
shenhanbecf6242013-02-19 20:43:32 +0000329 rv = rv + tp.Build()
asharifc97199a2013-02-15 22:48:45 +0000330 finally:
Caroline Tice88272d42016-01-13 09:48:29 -0800331 print('Exiting...')
llozano5fee82f2013-03-11 17:59:40 +0000332 return rv
asharif19c73dd2013-02-15 04:35:37 +0000333
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800334
335if __name__ == '__main__':
Caroline Tice88272d42016-01-13 09:48:29 -0800336 retval = Main(sys.argv[1:])
asharif2198c512013-02-15 09:21:35 +0000337 sys.exit(retval)