blob: 6f82e8992a1b5c2a710f54c15fa2d9e06e71c540 [file] [log] [blame]
bjanakiraman7f4a4852013-02-15 04:35:28 +00001#!/usr/bin/python2.6
2#
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.")
cmtice80d257f2013-02-15 23:44:51 +0000172 parser.add_option("-x",
173 "--gdb_dir",
174 dest="gdb_dir",
175 help="The directory where gdb resides.")
asharifc97199a2013-02-15 22:48:45 +0000176 parser.add_option("-b",
177 "--board",
178 dest="board",
179 default="x86-agz",
180 help="The target board.")
181 parser.add_option("-n",
182 "--noincremental",
183 dest="noincremental",
184 default=False,
asharifd751e252013-02-15 04:35:52 +0000185 action="store_true",
asharifc97199a2013-02-15 22:48:45 +0000186 help="Use FEATURES=keepwork to do incremental builds.")
cmticee59ac272013-02-19 20:20:09 +0000187 parser.add_option("--cflags",
188 dest="cflags",
189 default="",
190 help="Build a compiler with specified CFLAGS")
191 parser.add_option("--cxxflags",
192 dest="cxxflags",
193 default="",
194 help="Build a compiler with specified CXXFLAGS")
195 parser.add_option("--ldflags",
196 dest="ldflags",
197 default="",
198 help="Build a compiler with specified LDFLAGS")
asharifc97199a2013-02-15 22:48:45 +0000199 parser.add_option("-d",
200 "--debug",
201 dest="debug",
202 default=False,
asharifd751e252013-02-15 04:35:52 +0000203 action="store_true",
cmticee59ac272013-02-19 20:20:09 +0000204 help="Build a compiler with -g3 -O0 appended to both"
205 " CFLAGS and CXXFLAGS.")
asharif86968c42013-02-15 23:44:37 +0000206 parser.add_option("-m",
207 "--mount_only",
208 dest="mount_only",
209 default=False,
210 action="store_true",
211 help="Just mount the tool directories.")
cmtice80d257f2013-02-15 23:44:51 +0000212 parser.add_option("-u",
213 "--unmount_only",
214 dest="unmount_only",
215 default=False,
216 action="store_true",
217 help="Just unmount the tool directories.")
asharif86968c42013-02-15 23:44:37 +0000218
bjanakiraman7f4a4852013-02-15 04:35:28 +0000219
asharifc97199a2013-02-15 22:48:45 +0000220 options, _ = parser.parse_args(argv)
asharif17621302013-02-15 04:46:35 +0000221
kbaclawski20082a02013-02-16 02:12:57 +0000222 chromeos_root = misc.CanonicalizePath(options.chromeos_root)
cmtice80d257f2013-02-15 23:44:51 +0000223 if options.gcc_dir:
kbaclawski20082a02013-02-16 02:12:57 +0000224 gcc_dir = misc.CanonicalizePath(options.gcc_dir)
cmtice80d257f2013-02-15 23:44:51 +0000225 if options.gdb_dir:
kbaclawski20082a02013-02-16 02:12:57 +0000226 gdb_dir = misc.CanonicalizePath(options.gdb_dir)
cmtice80d257f2013-02-15 23:44:51 +0000227 if options.unmount_only:
228 options.mount_only = False
229 elif options.mount_only:
230 options.unmount_only = False
asharifc97199a2013-02-15 22:48:45 +0000231 build_env = {}
cmticee59ac272013-02-19 20:20:09 +0000232 if options.cflags:
233 build_env["CFLAGS"] = options.cflags
234 if options.cxxflags:
235 build_env["CXXFLAGS"] = options.cxxflags
236 if options.cxxflags:
237 build_env["LDFLAGS"] = options.ldflags
asharifc97199a2013-02-15 22:48:45 +0000238 if options.debug:
239 debug_flags = "-g3 -O0"
cmticee59ac272013-02-19 20:20:09 +0000240 if "CFLAGS" in build_env:
241 build_env["CFLAGS"] += " %s" % (debug_flags)
242 else:
243 build_env["CFLAGS"] = debug_flags
244 if "CXXFLAGS" in build_env:
245 build_env["CXXFLAGS"] += " %s" % (debug_flags)
246 else:
247 build_env["CXXFLAGS"] = debug_flags
bjanakiraman7f4a4852013-02-15 04:35:28 +0000248
asharif86968c42013-02-15 23:44:37 +0000249 # Create toolchain parts
250 toolchain_parts = []
251 for board in options.board.split(","):
252 if options.gcc_dir:
asharifc97199a2013-02-15 22:48:45 +0000253 tp = ToolchainPart("gcc", gcc_dir, chromeos_root, board,
254 not options.noincremental, build_env)
asharif86968c42013-02-15 23:44:37 +0000255 toolchain_parts.append(tp)
cmtice80d257f2013-02-15 23:44:51 +0000256 if options.gdb_dir:
257 tp = ToolchainPart("gdb", gdb_dir, chromeos_root, board,
258 not options.noincremental, build_env)
259 toolchain_parts.append(tp)
asharif86968c42013-02-15 23:44:37 +0000260
shenhanbecf6242013-02-19 20:43:32 +0000261 rv = 0
asharif86968c42013-02-15 23:44:37 +0000262 try:
263 for tp in toolchain_parts:
cmtice80d257f2013-02-15 23:44:51 +0000264 if options.mount_only or options.unmount_only:
265 tp.MountSources(options.unmount_only)
asharif86968c42013-02-15 23:44:37 +0000266 else:
shenhanbecf6242013-02-19 20:43:32 +0000267 rv = rv + tp.Build()
asharifc97199a2013-02-15 22:48:45 +0000268 finally:
269 print "Exiting..."
shenhanbecf6242013-02-19 20:43:32 +0000270 return rv
asharif19c73dd2013-02-15 04:35:37 +0000271
272if __name__ == "__main__":
asharif2198c512013-02-15 09:21:35 +0000273 retval = Main(sys.argv)
274 sys.exit(retval)