blob: 7084b996f6ed57dfe496ec00c78a5afdd2d8db56 [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,
26 build_env):
27 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)
asharifc97199a2013-02-15 22:48:45 +000033 self._ce = command_executer.GetCommandExecuter()
34 self._mask_file = os.path.join(
35 self._chromeos_root,
36 "chroot",
37 "etc/portage/package.mask/cross-%s" % self._ctarget)
38 self._new_mask_file = None
39
shenhanad803432013-02-16 03:14:51 +000040 self._chroot_source_path = os.path.join(constants.mounted_toolchain_root,
llozano85b37152013-02-16 03:14:57 +000041 self._name).lstrip("/")
asharifc97199a2013-02-15 22:48:45 +000042 self._incremental = incremental
43 self._build_env = build_env
44
45 def RunSetupBoardIfNecessary(self):
46 cross_symlink = os.path.join(
47 self._chromeos_root,
48 "chroot",
49 "usr/local/portage/crossdev/cross-%s" % self._ctarget)
50 if not os.path.exists(cross_symlink):
51 command = "./setup_board --board=%s" % self._board
asharifca3c6c12013-02-15 23:17:54 +000052 self._ce.ChrootRunCommand(self._chromeos_root, command)
asharifc97199a2013-02-15 22:48:45 +000053
54 def Build(self):
55 self.RunSetupBoardIfNecessary()
56
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()
shenhanbecf6242013-02-19 20:43:32 +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
136 env["USE"] = "multislot mounted_%s" % self._name
137 env["%s_SOURCE_PATH" % self._name.upper()] = (
138 os.path.join("/", self._chroot_source_path))
139 env["ACCEPT_KEYWORDS"] = "~*"
140 env_string = " ".join(["%s=\"%s\"" % var for var in env.items()])
141 command = "emerge =cross-%s/%s-9999" % (self._ctarget, self._name)
142 full_command = "sudo %s %s" % (env_string, command)
shenhanbecf6242013-02-19 20:43:32 +0000143 return self._ce.ChrootRunCommand(self._chromeos_root, full_command)
asharifc97199a2013-02-15 22:48:45 +0000144
asharifc97199a2013-02-15 22:48:45 +0000145 def MoveMaskFile(self):
146 self._new_mask_file = None
147 if os.path.isfile(self._mask_file):
148 self._new_mask_file = tempfile.mktemp()
149 command = "sudo mv %s %s" % (self._mask_file, self._new_mask_file)
150 self._ce.RunCommand(command)
151
152 def UnMoveMaskFile(self):
153 if self._new_mask_file:
154 command = "sudo mv %s %s" % (self._new_mask_file, self._mask_file)
155 self._ce.RunCommand(command)
bjanakiraman7f4a4852013-02-15 04:35:28 +0000156
bjanakiraman7f4a4852013-02-15 04:35:28 +0000157
asharif0d3535a2013-02-15 04:50:33 +0000158def Main(argv):
asharif19c73dd2013-02-15 04:35:37 +0000159 """The main function."""
asharif5a9bb462013-02-15 04:50:57 +0000160 # Common initializations
asharif19c73dd2013-02-15 04:35:37 +0000161 parser = optparse.OptionParser()
asharifc97199a2013-02-15 22:48:45 +0000162 parser.add_option("-c",
163 "--chromeos_root",
164 dest="chromeos_root",
asharifbcdd4e52013-02-16 01:05:17 +0000165 default="../../",
asharifc97199a2013-02-15 22:48:45 +0000166 help=("ChromeOS root checkout directory"
167 " uses ../.. if none given."))
168 parser.add_option("-g",
169 "--gcc_dir",
170 dest="gcc_dir",
171 help="The directory where gcc resides.")
shenhan8e8b0c22013-02-19 22:34:16 +0000172 parser.add_option("--binutils_dir",
173 dest="binutils_dir",
174 help="The directory where binutils resides.")
cmtice80d257f2013-02-15 23:44:51 +0000175 parser.add_option("-x",
176 "--gdb_dir",
177 dest="gdb_dir",
178 help="The directory where gdb resides.")
asharifc97199a2013-02-15 22:48:45 +0000179 parser.add_option("-b",
180 "--board",
181 dest="board",
182 default="x86-agz",
183 help="The target board.")
184 parser.add_option("-n",
185 "--noincremental",
186 dest="noincremental",
187 default=False,
asharifd751e252013-02-15 04:35:52 +0000188 action="store_true",
asharifc97199a2013-02-15 22:48:45 +0000189 help="Use FEATURES=keepwork to do incremental builds.")
cmticee59ac272013-02-19 20:20:09 +0000190 parser.add_option("--cflags",
191 dest="cflags",
192 default="",
193 help="Build a compiler with specified CFLAGS")
194 parser.add_option("--cxxflags",
195 dest="cxxflags",
196 default="",
197 help="Build a compiler with specified CXXFLAGS")
198 parser.add_option("--ldflags",
199 dest="ldflags",
200 default="",
201 help="Build a compiler with specified LDFLAGS")
asharifc97199a2013-02-15 22:48:45 +0000202 parser.add_option("-d",
203 "--debug",
204 dest="debug",
205 default=False,
asharifd751e252013-02-15 04:35:52 +0000206 action="store_true",
cmticee59ac272013-02-19 20:20:09 +0000207 help="Build a compiler with -g3 -O0 appended to both"
208 " CFLAGS and CXXFLAGS.")
asharif86968c42013-02-15 23:44:37 +0000209 parser.add_option("-m",
210 "--mount_only",
211 dest="mount_only",
212 default=False,
213 action="store_true",
214 help="Just mount the tool directories.")
cmtice80d257f2013-02-15 23:44:51 +0000215 parser.add_option("-u",
216 "--unmount_only",
217 dest="unmount_only",
218 default=False,
219 action="store_true",
220 help="Just unmount the tool directories.")
asharif86968c42013-02-15 23:44:37 +0000221
bjanakiraman7f4a4852013-02-15 04:35:28 +0000222
asharifc97199a2013-02-15 22:48:45 +0000223 options, _ = parser.parse_args(argv)
asharif17621302013-02-15 04:46:35 +0000224
kbaclawski20082a02013-02-16 02:12:57 +0000225 chromeos_root = misc.CanonicalizePath(options.chromeos_root)
cmtice80d257f2013-02-15 23:44:51 +0000226 if options.gcc_dir:
kbaclawski20082a02013-02-16 02:12:57 +0000227 gcc_dir = misc.CanonicalizePath(options.gcc_dir)
shenhan8e8b0c22013-02-19 22:34:16 +0000228 if options.binutils_dir:
229 binutils_dir = misc.CanonicalizePath(options.binutils_dir)
cmtice80d257f2013-02-15 23:44:51 +0000230 if options.gdb_dir:
kbaclawski20082a02013-02-16 02:12:57 +0000231 gdb_dir = misc.CanonicalizePath(options.gdb_dir)
cmtice80d257f2013-02-15 23:44:51 +0000232 if options.unmount_only:
233 options.mount_only = False
234 elif options.mount_only:
235 options.unmount_only = False
asharifc97199a2013-02-15 22:48:45 +0000236 build_env = {}
cmticee59ac272013-02-19 20:20:09 +0000237 if options.cflags:
238 build_env["CFLAGS"] = options.cflags
239 if options.cxxflags:
240 build_env["CXXFLAGS"] = options.cxxflags
241 if options.cxxflags:
242 build_env["LDFLAGS"] = options.ldflags
asharifc97199a2013-02-15 22:48:45 +0000243 if options.debug:
244 debug_flags = "-g3 -O0"
cmticee59ac272013-02-19 20:20:09 +0000245 if "CFLAGS" in build_env:
246 build_env["CFLAGS"] += " %s" % (debug_flags)
247 else:
248 build_env["CFLAGS"] = debug_flags
249 if "CXXFLAGS" in build_env:
250 build_env["CXXFLAGS"] += " %s" % (debug_flags)
251 else:
252 build_env["CXXFLAGS"] = debug_flags
bjanakiraman7f4a4852013-02-15 04:35:28 +0000253
asharif86968c42013-02-15 23:44:37 +0000254 # Create toolchain parts
255 toolchain_parts = []
256 for board in options.board.split(","):
257 if options.gcc_dir:
asharifc97199a2013-02-15 22:48:45 +0000258 tp = ToolchainPart("gcc", gcc_dir, chromeos_root, board,
259 not options.noincremental, build_env)
asharif86968c42013-02-15 23:44:37 +0000260 toolchain_parts.append(tp)
shenhan8e8b0c22013-02-19 22:34:16 +0000261 if options.binutils_dir:
262 tp = ToolchainPart("binutils", binutils_dir, chromeos_root, board,
263 not options.noincremental, build_env)
264 toolchain_parts.append(tp)
cmtice80d257f2013-02-15 23:44:51 +0000265 if options.gdb_dir:
266 tp = ToolchainPart("gdb", gdb_dir, chromeos_root, board,
267 not options.noincremental, build_env)
268 toolchain_parts.append(tp)
asharif86968c42013-02-15 23:44:37 +0000269
shenhanbecf6242013-02-19 20:43:32 +0000270 rv = 0
asharif86968c42013-02-15 23:44:37 +0000271 try:
272 for tp in toolchain_parts:
cmtice80d257f2013-02-15 23:44:51 +0000273 if options.mount_only or options.unmount_only:
274 tp.MountSources(options.unmount_only)
asharif86968c42013-02-15 23:44:37 +0000275 else:
shenhanbecf6242013-02-19 20:43:32 +0000276 rv = rv + tp.Build()
asharifc97199a2013-02-15 22:48:45 +0000277 finally:
278 print "Exiting..."
shenhanbecf6242013-02-19 20:43:32 +0000279 return rv
asharif19c73dd2013-02-15 04:35:37 +0000280
281if __name__ == "__main__":
asharif2198c512013-02-15 09:21:35 +0000282 retval = Main(sys.argv)
283 sys.exit(retval)