blob: fa2bedaa8eb47dfa46ee8ce31a63e0b1a926b16d [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
kbaclawski20082a02013-02-16 02:12:57 +000020from utils import misc
bjanakiraman7f4a4852013-02-15 04:35:28 +000021
asharif5a9bb462013-02-15 04:50:57 +000022
asharifc97199a2013-02-15 22:48:45 +000023class ToolchainPart(object):
24 def __init__(self, name, source_path, chromeos_root, board, incremental,
25 build_env):
26 self._name = name
kbaclawski20082a02013-02-16 02:12:57 +000027 self._source_path = misc.CanonicalizePath(source_path)
asharifc97199a2013-02-15 22:48:45 +000028 self._chromeos_root = chromeos_root
29 self._board = board
kbaclawski20082a02013-02-16 02:12:57 +000030 self._ctarget = misc.GetCtargetFromBoard(self._board,
asharif77bd80d2013-02-15 22:49:32 +000031 self._chromeos_root)
asharifc97199a2013-02-15 22:48:45 +000032 self._ce = command_executer.GetCommandExecuter()
33 self._mask_file = os.path.join(
34 self._chromeos_root,
35 "chroot",
36 "etc/portage/package.mask/cross-%s" % self._ctarget)
37 self._new_mask_file = None
38
39 self._chroot_source_path = "usr/local/toolchain_root/%s" % self._name
40 self._incremental = incremental
41 self._build_env = build_env
42
43 def RunSetupBoardIfNecessary(self):
44 cross_symlink = os.path.join(
45 self._chromeos_root,
46 "chroot",
47 "usr/local/portage/crossdev/cross-%s" % self._ctarget)
48 if not os.path.exists(cross_symlink):
49 command = "./setup_board --board=%s" % self._board
asharifca3c6c12013-02-15 23:17:54 +000050 self._ce.ChrootRunCommand(self._chromeos_root, command)
asharifc97199a2013-02-15 22:48:45 +000051
52 def Build(self):
53 self.RunSetupBoardIfNecessary()
54
55 try:
asharif7dd6d862013-02-15 23:17:46 +000056 self.UninstallTool()
asharifc97199a2013-02-15 22:48:45 +000057 self.MoveMaskFile()
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()
asharifc97199a2013-02-15 22:48:45 +000064
65 def RemoveCompiledFile(self):
66 compiled_file = os.path.join(self._chromeos_root,
67 "chroot",
68 "var/tmp/portage/cross-%s" % self._ctarget,
69 "%s-9999" % self._name,
70 ".compiled")
71 command = "rm -rf %s" % compiled_file
72 self._ce.RunCommand(command)
73
cmtice80d257f2013-02-15 23:44:51 +000074 def MountSources(self, unmount_source):
asharifc97199a2013-02-15 22:48:45 +000075 mount_points = []
76 mounted_source_path = os.path.join(self._chromeos_root,
77 "chroot",
78 self._chroot_source_path)
79 src_mp = tc_enter_chroot.MountPoint(
80 self._source_path,
81 mounted_source_path,
82 getpass.getuser(),
83 "ro")
84 mount_points.append(src_mp)
85
86 build_suffix = "build-%s" % self._ctarget
87 build_dir = "%s-%s" % (self._source_path, build_suffix)
88
89 if not self._incremental and os.path.exists(build_dir):
90 command = "rm -rf %s/*" % build_dir
91 self._ce.RunCommand(command)
92
93 # Create a -build directory for the objects.
94 command = "mkdir -p %s" % build_dir
95 self._ce.RunCommand(command)
96
97 mounted_build_dir = os.path.join(
98 self._chromeos_root, "chroot", "%s-%s" %
99 (self._chroot_source_path, build_suffix))
100 build_mp = tc_enter_chroot.MountPoint(
101 build_dir,
102 mounted_build_dir,
103 getpass.getuser())
104 mount_points.append(build_mp)
105
cmtice80d257f2013-02-15 23:44:51 +0000106 if unmount_source:
107 unmount_statuses = [mp.UnMount() == 0 for mp in mount_points]
asharifc97199a2013-02-15 22:48:45 +0000108 assert all(unmount_statuses), "Could not unmount all mount points!"
cmtice80d257f2013-02-15 23:44:51 +0000109 else:
110 mount_statuses = [mp.DoMount() == 0 for mp in mount_points]
111
112 if not all(mount_statuses):
113 mounted = [mp for mp, status in zip(mount_points, mount_statuses) if status]
114 unmount_statuses = [mp.UnMount() == 0 for mp in mounted]
115 assert all(unmount_statuses), "Could not unmount all mount points!"
116
asharifc97199a2013-02-15 22:48:45 +0000117
asharif7dd6d862013-02-15 23:17:46 +0000118 def UninstallTool(self):
119 command = "sudo CLEAN_DELAY=0 emerge -C cross-%s/%s" % (self._ctarget, self._name)
asharifca3c6c12013-02-15 23:17:54 +0000120 self._ce.ChrootRunCommand(self._chromeos_root, command)
asharif7dd6d862013-02-15 23:17:46 +0000121
asharifc97199a2013-02-15 22:48:45 +0000122 def BuildTool(self):
123 env = self._build_env
asharif9e499162013-02-16 02:41:39 +0000124 # FEATURES=buildpkg adds minutes of time so we disable it.
125 features = "nostrip userpriv userfetch -sandbox noclean -buildpkg"
asharifc97199a2013-02-15 22:48:45 +0000126 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
asharifc97199a2013-02-15 22:48:45 +0000140 def MoveMaskFile(self):
141 self._new_mask_file = None
142 if os.path.isfile(self._mask_file):
143 self._new_mask_file = tempfile.mktemp()
144 command = "sudo mv %s %s" % (self._mask_file, self._new_mask_file)
145 self._ce.RunCommand(command)
146
147 def UnMoveMaskFile(self):
148 if self._new_mask_file:
149 command = "sudo mv %s %s" % (self._new_mask_file, self._mask_file)
150 self._ce.RunCommand(command)
bjanakiraman7f4a4852013-02-15 04:35:28 +0000151
bjanakiraman7f4a4852013-02-15 04:35:28 +0000152
asharif0d3535a2013-02-15 04:50:33 +0000153def Main(argv):
asharif19c73dd2013-02-15 04:35:37 +0000154 """The main function."""
asharif5a9bb462013-02-15 04:50:57 +0000155 # Common initializations
asharif19c73dd2013-02-15 04:35:37 +0000156 parser = optparse.OptionParser()
asharifc97199a2013-02-15 22:48:45 +0000157 parser.add_option("-c",
158 "--chromeos_root",
159 dest="chromeos_root",
asharifbcdd4e52013-02-16 01:05:17 +0000160 default="../../",
asharifc97199a2013-02-15 22:48:45 +0000161 help=("ChromeOS root checkout directory"
162 " uses ../.. if none given."))
163 parser.add_option("-g",
164 "--gcc_dir",
165 dest="gcc_dir",
166 help="The directory where gcc resides.")
cmtice80d257f2013-02-15 23:44:51 +0000167 parser.add_option("-x",
168 "--gdb_dir",
169 dest="gdb_dir",
170 help="The directory where gdb resides.")
asharifc97199a2013-02-15 22:48:45 +0000171 parser.add_option("-b",
172 "--board",
173 dest="board",
174 default="x86-agz",
175 help="The target board.")
176 parser.add_option("-n",
177 "--noincremental",
178 dest="noincremental",
179 default=False,
asharifd751e252013-02-15 04:35:52 +0000180 action="store_true",
asharifc97199a2013-02-15 22:48:45 +0000181 help="Use FEATURES=keepwork to do incremental builds.")
182 parser.add_option("-d",
183 "--debug",
184 dest="debug",
185 default=False,
asharifd751e252013-02-15 04:35:52 +0000186 action="store_true",
asharifc97199a2013-02-15 22:48:45 +0000187 help="Build a compiler with -g3 -O0.")
asharif86968c42013-02-15 23:44:37 +0000188 parser.add_option("-m",
189 "--mount_only",
190 dest="mount_only",
191 default=False,
192 action="store_true",
193 help="Just mount the tool directories.")
cmtice80d257f2013-02-15 23:44:51 +0000194 parser.add_option("-u",
195 "--unmount_only",
196 dest="unmount_only",
197 default=False,
198 action="store_true",
199 help="Just unmount the tool directories.")
asharif86968c42013-02-15 23:44:37 +0000200
bjanakiraman7f4a4852013-02-15 04:35:28 +0000201
asharifc97199a2013-02-15 22:48:45 +0000202 options, _ = parser.parse_args(argv)
asharif17621302013-02-15 04:46:35 +0000203
kbaclawski20082a02013-02-16 02:12:57 +0000204 chromeos_root = misc.CanonicalizePath(options.chromeos_root)
cmtice80d257f2013-02-15 23:44:51 +0000205 if options.gcc_dir:
kbaclawski20082a02013-02-16 02:12:57 +0000206 gcc_dir = misc.CanonicalizePath(options.gcc_dir)
cmtice80d257f2013-02-15 23:44:51 +0000207 if options.gdb_dir:
kbaclawski20082a02013-02-16 02:12:57 +0000208 gdb_dir = misc.CanonicalizePath(options.gdb_dir)
cmtice80d257f2013-02-15 23:44:51 +0000209 if options.unmount_only:
210 options.mount_only = False
211 elif options.mount_only:
212 options.unmount_only = False
asharifc97199a2013-02-15 22:48:45 +0000213 build_env = {}
214 if options.debug:
215 debug_flags = "-g3 -O0"
216 build_env["CFLAGS"] = debug_flags
217 build_env["CXXFLAGS"] = debug_flags
bjanakiraman7f4a4852013-02-15 04:35:28 +0000218
asharif86968c42013-02-15 23:44:37 +0000219 # Create toolchain parts
220 toolchain_parts = []
221 for board in options.board.split(","):
222 if options.gcc_dir:
asharifc97199a2013-02-15 22:48:45 +0000223 tp = ToolchainPart("gcc", gcc_dir, chromeos_root, board,
224 not options.noincremental, build_env)
asharif86968c42013-02-15 23:44:37 +0000225 toolchain_parts.append(tp)
cmtice80d257f2013-02-15 23:44:51 +0000226 if options.gdb_dir:
227 tp = ToolchainPart("gdb", gdb_dir, chromeos_root, board,
228 not options.noincremental, build_env)
229 toolchain_parts.append(tp)
asharif86968c42013-02-15 23:44:37 +0000230
231 try:
232 for tp in toolchain_parts:
cmtice80d257f2013-02-15 23:44:51 +0000233 if options.mount_only or options.unmount_only:
234 tp.MountSources(options.unmount_only)
asharif86968c42013-02-15 23:44:37 +0000235 else:
236 tp.Build()
asharifc97199a2013-02-15 22:48:45 +0000237 finally:
238 print "Exiting..."
239 return 0
asharif0d3535a2013-02-15 04:50:33 +0000240
asharif19c73dd2013-02-15 04:35:37 +0000241
242if __name__ == "__main__":
asharif2198c512013-02-15 09:21:35 +0000243 retval = Main(sys.argv)
244 sys.exit(retval)