blob: e6dd46fc5a5ce1f616da9a2d5d347c0c626b6f5f [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()
cmtice80d257f2013-02-15 23:44:51 +000057 self.MountSources(False)
asharifc97199a2013-02-15 22:48:45 +000058 if not self._incremental:
59 self.RemoveCompiledFile()
60 self.BuildTool()
61 finally:
62 self.UnMoveMaskFile()
asharifc97199a2013-02-15 22:48:45 +000063
64 def RemoveCompiledFile(self):
65 compiled_file = os.path.join(self._chromeos_root,
66 "chroot",
67 "var/tmp/portage/cross-%s" % self._ctarget,
68 "%s-9999" % self._name,
69 ".compiled")
70 command = "rm -rf %s" % compiled_file
71 self._ce.RunCommand(command)
72
cmtice80d257f2013-02-15 23:44:51 +000073 def MountSources(self, unmount_source):
asharifc97199a2013-02-15 22:48:45 +000074 mount_points = []
75 mounted_source_path = os.path.join(self._chromeos_root,
76 "chroot",
77 self._chroot_source_path)
78 src_mp = tc_enter_chroot.MountPoint(
79 self._source_path,
80 mounted_source_path,
81 getpass.getuser(),
82 "ro")
83 mount_points.append(src_mp)
84
85 build_suffix = "build-%s" % self._ctarget
86 build_dir = "%s-%s" % (self._source_path, build_suffix)
87
88 if not self._incremental and os.path.exists(build_dir):
89 command = "rm -rf %s/*" % build_dir
90 self._ce.RunCommand(command)
91
92 # Create a -build directory for the objects.
93 command = "mkdir -p %s" % build_dir
94 self._ce.RunCommand(command)
95
96 mounted_build_dir = os.path.join(
97 self._chromeos_root, "chroot", "%s-%s" %
98 (self._chroot_source_path, build_suffix))
99 build_mp = tc_enter_chroot.MountPoint(
100 build_dir,
101 mounted_build_dir,
102 getpass.getuser())
103 mount_points.append(build_mp)
104
cmtice80d257f2013-02-15 23:44:51 +0000105 if unmount_source:
106 unmount_statuses = [mp.UnMount() == 0 for mp in mount_points]
asharifc97199a2013-02-15 22:48:45 +0000107 assert all(unmount_statuses), "Could not unmount all mount points!"
cmtice80d257f2013-02-15 23:44:51 +0000108 else:
109 mount_statuses = [mp.DoMount() == 0 for mp in mount_points]
110
111 if not all(mount_statuses):
112 mounted = [mp for mp, status in zip(mount_points, mount_statuses) if status]
113 unmount_statuses = [mp.UnMount() == 0 for mp in mounted]
114 assert all(unmount_statuses), "Could not unmount all mount points!"
115
asharifc97199a2013-02-15 22:48:45 +0000116
asharif7dd6d862013-02-15 23:17:46 +0000117 def UninstallTool(self):
118 command = "sudo CLEAN_DELAY=0 emerge -C cross-%s/%s" % (self._ctarget, self._name)
asharifca3c6c12013-02-15 23:17:54 +0000119 self._ce.ChrootRunCommand(self._chromeos_root, command)
asharif7dd6d862013-02-15 23:17:46 +0000120
asharifc97199a2013-02-15 22:48:45 +0000121 def BuildTool(self):
122 env = self._build_env
123 features = "nostrip userpriv userfetch -sandbox noclean"
124 env["FEATURES"] = features
125
126 if self._incremental:
127 env["FEATURES"] += " keepwork"
128
129 env["USE"] = "multislot mounted_%s" % self._name
130 env["%s_SOURCE_PATH" % self._name.upper()] = (
131 os.path.join("/", self._chroot_source_path))
132 env["ACCEPT_KEYWORDS"] = "~*"
133 env_string = " ".join(["%s=\"%s\"" % var for var in env.items()])
134 command = "emerge =cross-%s/%s-9999" % (self._ctarget, self._name)
135 full_command = "sudo %s %s" % (env_string, command)
asharifca3c6c12013-02-15 23:17:54 +0000136 self._ce.ChrootRunCommand(self._chromeos_root, full_command)
asharifc97199a2013-02-15 22:48:45 +0000137
asharifc97199a2013-02-15 22:48:45 +0000138 def MoveMaskFile(self):
139 self._new_mask_file = None
140 if os.path.isfile(self._mask_file):
141 self._new_mask_file = tempfile.mktemp()
142 command = "sudo mv %s %s" % (self._mask_file, self._new_mask_file)
143 self._ce.RunCommand(command)
144
145 def UnMoveMaskFile(self):
146 if self._new_mask_file:
147 command = "sudo mv %s %s" % (self._new_mask_file, self._mask_file)
148 self._ce.RunCommand(command)
bjanakiraman7f4a4852013-02-15 04:35:28 +0000149
bjanakiraman7f4a4852013-02-15 04:35:28 +0000150
asharif0d3535a2013-02-15 04:50:33 +0000151def Main(argv):
asharif19c73dd2013-02-15 04:35:37 +0000152 """The main function."""
asharif5a9bb462013-02-15 04:50:57 +0000153 # Common initializations
asharif19c73dd2013-02-15 04:35:37 +0000154 parser = optparse.OptionParser()
asharifc97199a2013-02-15 22:48:45 +0000155 parser.add_option("-c",
156 "--chromeos_root",
157 dest="chromeos_root",
asharifbcdd4e52013-02-16 01:05:17 +0000158 default="../../",
asharifc97199a2013-02-15 22:48:45 +0000159 help=("ChromeOS root checkout directory"
160 " uses ../.. if none given."))
161 parser.add_option("-g",
162 "--gcc_dir",
163 dest="gcc_dir",
164 help="The directory where gcc resides.")
cmtice80d257f2013-02-15 23:44:51 +0000165 parser.add_option("-x",
166 "--gdb_dir",
167 dest="gdb_dir",
168 help="The directory where gdb resides.")
asharifc97199a2013-02-15 22:48:45 +0000169 parser.add_option("-b",
170 "--board",
171 dest="board",
172 default="x86-agz",
173 help="The target board.")
174 parser.add_option("-n",
175 "--noincremental",
176 dest="noincremental",
177 default=False,
asharifd751e252013-02-15 04:35:52 +0000178 action="store_true",
asharifc97199a2013-02-15 22:48:45 +0000179 help="Use FEATURES=keepwork to do incremental builds.")
180 parser.add_option("-d",
181 "--debug",
182 dest="debug",
183 default=False,
asharifd751e252013-02-15 04:35:52 +0000184 action="store_true",
asharifc97199a2013-02-15 22:48:45 +0000185 help="Build a compiler with -g3 -O0.")
asharif86968c42013-02-15 23:44:37 +0000186 parser.add_option("-m",
187 "--mount_only",
188 dest="mount_only",
189 default=False,
190 action="store_true",
191 help="Just mount the tool directories.")
cmtice80d257f2013-02-15 23:44:51 +0000192 parser.add_option("-u",
193 "--unmount_only",
194 dest="unmount_only",
195 default=False,
196 action="store_true",
197 help="Just unmount the tool directories.")
asharif86968c42013-02-15 23:44:37 +0000198
bjanakiraman7f4a4852013-02-15 04:35:28 +0000199
asharifc97199a2013-02-15 22:48:45 +0000200 options, _ = parser.parse_args(argv)
asharif17621302013-02-15 04:46:35 +0000201
asharifc97199a2013-02-15 22:48:45 +0000202 chromeos_root = utils.CanonicalizePath(options.chromeos_root)
cmtice80d257f2013-02-15 23:44:51 +0000203 if options.gcc_dir:
204 gcc_dir = utils.CanonicalizePath(options.gcc_dir)
205 if options.gdb_dir:
206 gdb_dir = utils.CanonicalizePath(options.gdb_dir)
207 if options.unmount_only:
208 options.mount_only = False
209 elif options.mount_only:
210 options.unmount_only = False
asharifc97199a2013-02-15 22:48:45 +0000211 build_env = {}
212 if options.debug:
213 debug_flags = "-g3 -O0"
214 build_env["CFLAGS"] = debug_flags
215 build_env["CXXFLAGS"] = debug_flags
bjanakiraman7f4a4852013-02-15 04:35:28 +0000216
asharif86968c42013-02-15 23:44:37 +0000217 # Create toolchain parts
218 toolchain_parts = []
219 for board in options.board.split(","):
220 if options.gcc_dir:
asharifc97199a2013-02-15 22:48:45 +0000221 tp = ToolchainPart("gcc", gcc_dir, chromeos_root, board,
222 not options.noincremental, build_env)
asharif86968c42013-02-15 23:44:37 +0000223 toolchain_parts.append(tp)
cmtice80d257f2013-02-15 23:44:51 +0000224 if options.gdb_dir:
225 tp = ToolchainPart("gdb", gdb_dir, chromeos_root, board,
226 not options.noincremental, build_env)
227 toolchain_parts.append(tp)
asharif86968c42013-02-15 23:44:37 +0000228
229 try:
230 for tp in toolchain_parts:
cmtice80d257f2013-02-15 23:44:51 +0000231 if options.mount_only or options.unmount_only:
232 tp.MountSources(options.unmount_only)
asharif86968c42013-02-15 23:44:37 +0000233 else:
234 tp.Build()
asharifc97199a2013-02-15 22:48:45 +0000235 finally:
236 print "Exiting..."
237 return 0
asharif0d3535a2013-02-15 04:50:33 +0000238
asharif19c73dd2013-02-15 04:35:37 +0000239
240if __name__ == "__main__":
asharif2198c512013-02-15 09:21:35 +0000241 retval = Main(sys.argv)
242 sys.exit(retval)