blob: e0f35ee84d4a4d49ec086bc6317f029a31516fd3 [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
asharif252df0f2013-02-15 04:46:28 +000017import tc_enter_chroot
raymes01959ae2013-02-15 04:50:07 +000018from utils import command_executer
bjanakiraman7f4a4852013-02-15 04:35:28 +000019from utils import utils
20
asharif5a9bb462013-02-15 04:50:57 +000021
asharifc97199a2013-02-15 22:48:45 +000022class ToolchainPart(object):
23 def __init__(self, name, source_path, chromeos_root, board, incremental,
24 build_env):
25 self._name = name
26 self._source_path = utils.CanonicalizePath(source_path)
27 self._chromeos_root = chromeos_root
28 self._board = board
asharif77bd80d2013-02-15 22:49:32 +000029 self._ctarget = utils.GetCtargetFromBoard(self._board,
30 self._chromeos_root)
asharifc97199a2013-02-15 22:48:45 +000031 self._ce = command_executer.GetCommandExecuter()
32 self._mask_file = os.path.join(
33 self._chromeos_root,
34 "chroot",
35 "etc/portage/package.mask/cross-%s" % self._ctarget)
36 self._new_mask_file = None
37
38 self._chroot_source_path = "usr/local/toolchain_root/%s" % self._name
39 self._incremental = incremental
40 self._build_env = build_env
41
42 def RunSetupBoardIfNecessary(self):
43 cross_symlink = os.path.join(
44 self._chromeos_root,
45 "chroot",
46 "usr/local/portage/crossdev/cross-%s" % self._ctarget)
47 if not os.path.exists(cross_symlink):
48 command = "./setup_board --board=%s" % self._board
asharifca3c6c12013-02-15 23:17:54 +000049 self._ce.ChrootRunCommand(self._chromeos_root, command)
asharifc97199a2013-02-15 22:48:45 +000050
51 def Build(self):
52 self.RunSetupBoardIfNecessary()
53
54 try:
asharif7dd6d862013-02-15 23:17:46 +000055 self.UninstallTool()
asharifc97199a2013-02-15 22:48:45 +000056 self.MoveMaskFile()
57 self.SwitchToBFD()
cmtice80d257f2013-02-15 23:44:51 +000058 self.MountSources(False)
asharifc97199a2013-02-15 22:48:45 +000059 if not self._incremental:
60 self.RemoveCompiledFile()
61 self.BuildTool()
62 finally:
63 self.UnMoveMaskFile()
64 self.SwitchToOriginalLD()
65
66 def RemoveCompiledFile(self):
67 compiled_file = os.path.join(self._chromeos_root,
68 "chroot",
69 "var/tmp/portage/cross-%s" % self._ctarget,
70 "%s-9999" % self._name,
71 ".compiled")
72 command = "rm -rf %s" % compiled_file
73 self._ce.RunCommand(command)
74
cmtice80d257f2013-02-15 23:44:51 +000075 def MountSources(self, unmount_source):
asharifc97199a2013-02-15 22:48:45 +000076 mount_points = []
77 mounted_source_path = os.path.join(self._chromeos_root,
78 "chroot",
79 self._chroot_source_path)
80 src_mp = tc_enter_chroot.MountPoint(
81 self._source_path,
82 mounted_source_path,
83 getpass.getuser(),
84 "ro")
85 mount_points.append(src_mp)
86
87 build_suffix = "build-%s" % self._ctarget
88 build_dir = "%s-%s" % (self._source_path, build_suffix)
89
90 if not self._incremental and os.path.exists(build_dir):
91 command = "rm -rf %s/*" % build_dir
92 self._ce.RunCommand(command)
93
94 # Create a -build directory for the objects.
95 command = "mkdir -p %s" % build_dir
96 self._ce.RunCommand(command)
97
98 mounted_build_dir = os.path.join(
99 self._chromeos_root, "chroot", "%s-%s" %
100 (self._chroot_source_path, build_suffix))
101 build_mp = tc_enter_chroot.MountPoint(
102 build_dir,
103 mounted_build_dir,
104 getpass.getuser())
105 mount_points.append(build_mp)
106
cmtice80d257f2013-02-15 23:44:51 +0000107 if unmount_source:
108 unmount_statuses = [mp.UnMount() == 0 for mp in mount_points]
asharifc97199a2013-02-15 22:48:45 +0000109 assert all(unmount_statuses), "Could not unmount all mount points!"
cmtice80d257f2013-02-15 23:44:51 +0000110 else:
111 mount_statuses = [mp.DoMount() == 0 for mp in mount_points]
112
113 if not all(mount_statuses):
114 mounted = [mp for mp, status in zip(mount_points, mount_statuses) if status]
115 unmount_statuses = [mp.UnMount() == 0 for mp in mounted]
116 assert all(unmount_statuses), "Could not unmount all mount points!"
117
asharifc97199a2013-02-15 22:48:45 +0000118
asharif7dd6d862013-02-15 23:17:46 +0000119 def UninstallTool(self):
120 command = "sudo CLEAN_DELAY=0 emerge -C cross-%s/%s" % (self._ctarget, self._name)
asharifca3c6c12013-02-15 23:17:54 +0000121 self._ce.ChrootRunCommand(self._chromeos_root, command)
asharif7dd6d862013-02-15 23:17:46 +0000122
asharifc97199a2013-02-15 22:48:45 +0000123 def BuildTool(self):
124 env = self._build_env
125 features = "nostrip userpriv userfetch -sandbox noclean"
126 env["FEATURES"] = features
127
128 if self._incremental:
129 env["FEATURES"] += " keepwork"
130
131 env["USE"] = "multislot mounted_%s" % self._name
132 env["%s_SOURCE_PATH" % self._name.upper()] = (
133 os.path.join("/", self._chroot_source_path))
134 env["ACCEPT_KEYWORDS"] = "~*"
135 env_string = " ".join(["%s=\"%s\"" % var for var in env.items()])
136 command = "emerge =cross-%s/%s-9999" % (self._ctarget, self._name)
137 full_command = "sudo %s %s" % (env_string, command)
asharifca3c6c12013-02-15 23:17:54 +0000138 self._ce.ChrootRunCommand(self._chromeos_root, full_command)
asharifc97199a2013-02-15 22:48:45 +0000139
140 def SwitchToBFD(self):
141 command = "sudo binutils-config %s-2.21" % self._ctarget
asharifca3c6c12013-02-15 23:17:54 +0000142 self._ce.ChrootRunCommand(self._chromeos_root, command)
asharifc97199a2013-02-15 22:48:45 +0000143
144 def SwitchToOriginalLD(self):
145 pass
146
147 def MoveMaskFile(self):
148 self._new_mask_file = None
149 if os.path.isfile(self._mask_file):
150 self._new_mask_file = tempfile.mktemp()
151 command = "sudo mv %s %s" % (self._mask_file, self._new_mask_file)
152 self._ce.RunCommand(command)
153
154 def UnMoveMaskFile(self):
155 if self._new_mask_file:
156 command = "sudo mv %s %s" % (self._new_mask_file, self._mask_file)
157 self._ce.RunCommand(command)
bjanakiraman7f4a4852013-02-15 04:35:28 +0000158
bjanakiraman7f4a4852013-02-15 04:35:28 +0000159
asharif0d3535a2013-02-15 04:50:33 +0000160def Main(argv):
asharif19c73dd2013-02-15 04:35:37 +0000161 """The main function."""
asharif5a9bb462013-02-15 04:50:57 +0000162 # Common initializations
asharif19c73dd2013-02-15 04:35:37 +0000163 parser = optparse.OptionParser()
asharifc97199a2013-02-15 22:48:45 +0000164 parser.add_option("-c",
165 "--chromeos_root",
166 dest="chromeos_root",
167 help=("ChromeOS root checkout directory"
168 " uses ../.. if none given."))
169 parser.add_option("-g",
170 "--gcc_dir",
171 dest="gcc_dir",
172 help="The directory where gcc resides.")
cmtice80d257f2013-02-15 23:44:51 +0000173 parser.add_option("-x",
174 "--gdb_dir",
175 dest="gdb_dir",
176 help="The directory where gdb resides.")
asharifc97199a2013-02-15 22:48:45 +0000177 parser.add_option("-b",
178 "--board",
179 dest="board",
180 default="x86-agz",
181 help="The target board.")
182 parser.add_option("-n",
183 "--noincremental",
184 dest="noincremental",
185 default=False,
asharifd751e252013-02-15 04:35:52 +0000186 action="store_true",
asharifc97199a2013-02-15 22:48:45 +0000187 help="Use FEATURES=keepwork to do incremental builds.")
188 parser.add_option("-d",
189 "--debug",
190 dest="debug",
191 default=False,
asharifd751e252013-02-15 04:35:52 +0000192 action="store_true",
asharifc97199a2013-02-15 22:48:45 +0000193 help="Build a compiler with -g3 -O0.")
asharif86968c42013-02-15 23:44:37 +0000194 parser.add_option("-m",
195 "--mount_only",
196 dest="mount_only",
197 default=False,
198 action="store_true",
199 help="Just mount the tool directories.")
cmtice80d257f2013-02-15 23:44:51 +0000200 parser.add_option("-u",
201 "--unmount_only",
202 dest="unmount_only",
203 default=False,
204 action="store_true",
205 help="Just unmount the tool directories.")
asharif86968c42013-02-15 23:44:37 +0000206
bjanakiraman7f4a4852013-02-15 04:35:28 +0000207
asharifc97199a2013-02-15 22:48:45 +0000208 options, _ = parser.parse_args(argv)
asharif17621302013-02-15 04:46:35 +0000209
asharifc97199a2013-02-15 22:48:45 +0000210 chromeos_root = utils.CanonicalizePath(options.chromeos_root)
cmtice80d257f2013-02-15 23:44:51 +0000211 if options.gcc_dir:
212 gcc_dir = utils.CanonicalizePath(options.gcc_dir)
213 if options.gdb_dir:
214 gdb_dir = utils.CanonicalizePath(options.gdb_dir)
215 if options.unmount_only:
216 options.mount_only = False
217 elif options.mount_only:
218 options.unmount_only = False
asharifc97199a2013-02-15 22:48:45 +0000219 build_env = {}
220 if options.debug:
221 debug_flags = "-g3 -O0"
222 build_env["CFLAGS"] = debug_flags
223 build_env["CXXFLAGS"] = debug_flags
bjanakiraman7f4a4852013-02-15 04:35:28 +0000224
asharif86968c42013-02-15 23:44:37 +0000225 # Create toolchain parts
226 toolchain_parts = []
227 for board in options.board.split(","):
228 if options.gcc_dir:
asharifc97199a2013-02-15 22:48:45 +0000229 tp = ToolchainPart("gcc", gcc_dir, chromeos_root, board,
230 not options.noincremental, build_env)
asharif86968c42013-02-15 23:44:37 +0000231 toolchain_parts.append(tp)
cmtice80d257f2013-02-15 23:44:51 +0000232 if options.gdb_dir:
233 tp = ToolchainPart("gdb", gdb_dir, chromeos_root, board,
234 not options.noincremental, build_env)
235 toolchain_parts.append(tp)
asharif86968c42013-02-15 23:44:37 +0000236
237 try:
238 for tp in toolchain_parts:
cmtice80d257f2013-02-15 23:44:51 +0000239 if options.mount_only or options.unmount_only:
240 tp.MountSources(options.unmount_only)
asharif86968c42013-02-15 23:44:37 +0000241 else:
242 tp.Build()
asharifc97199a2013-02-15 22:48:45 +0000243 finally:
244 print "Exiting..."
245 return 0
asharif0d3535a2013-02-15 04:50:33 +0000246
asharif19c73dd2013-02-15 04:35:37 +0000247
248if __name__ == "__main__":
asharif2198c512013-02-15 09:21:35 +0000249 retval = Main(sys.argv)
250 sys.exit(retval)