bjanakiraman | 7f4a485 | 2013-02-15 04:35:28 +0000 | [diff] [blame] | 1 | #!/usr/bin/python2.6 |
| 2 | # |
| 3 | # Copyright 2010 Google Inc. All Rights Reserved. |
| 4 | |
| 5 | """Script to build the ChromeOS toolchain. |
| 6 | |
| 7 | This script sets up the toolchain if you give it the gcctools directory. |
| 8 | """ |
| 9 | |
| 10 | __author__ = "asharif@google.com (Ahmad Sharif)" |
| 11 | |
asharif | 80c6e55 | 2013-02-15 04:35:40 +0000 | [diff] [blame] | 12 | import getpass |
bjanakiraman | 7f4a485 | 2013-02-15 04:35:28 +0000 | [diff] [blame] | 13 | import optparse |
asharif | 1762130 | 2013-02-15 04:46:35 +0000 | [diff] [blame] | 14 | import os |
bjanakiraman | 7f4a485 | 2013-02-15 04:35:28 +0000 | [diff] [blame] | 15 | import sys |
asharif | c97199a | 2013-02-15 22:48:45 +0000 | [diff] [blame] | 16 | import tempfile |
kbaclawski | 20082a0 | 2013-02-16 02:12:57 +0000 | [diff] [blame] | 17 | |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 18 | import tc_enter_chroot |
raymes | 01959ae | 2013-02-15 04:50:07 +0000 | [diff] [blame] | 19 | from utils import command_executer |
shenhan | ad80343 | 2013-02-16 03:14:51 +0000 | [diff] [blame] | 20 | from utils import constants |
kbaclawski | 20082a0 | 2013-02-16 02:12:57 +0000 | [diff] [blame] | 21 | from utils import misc |
bjanakiraman | 7f4a485 | 2013-02-15 04:35:28 +0000 | [diff] [blame] | 22 | |
asharif | 5a9bb46 | 2013-02-15 04:50:57 +0000 | [diff] [blame] | 23 | |
asharif | c97199a | 2013-02-15 22:48:45 +0000 | [diff] [blame] | 24 | class ToolchainPart(object): |
| 25 | def __init__(self, name, source_path, chromeos_root, board, incremental, |
| 26 | build_env): |
| 27 | self._name = name |
kbaclawski | 20082a0 | 2013-02-16 02:12:57 +0000 | [diff] [blame] | 28 | self._source_path = misc.CanonicalizePath(source_path) |
asharif | c97199a | 2013-02-15 22:48:45 +0000 | [diff] [blame] | 29 | self._chromeos_root = chromeos_root |
| 30 | self._board = board |
kbaclawski | 20082a0 | 2013-02-16 02:12:57 +0000 | [diff] [blame] | 31 | self._ctarget = misc.GetCtargetFromBoard(self._board, |
asharif | 77bd80d | 2013-02-15 22:49:32 +0000 | [diff] [blame] | 32 | self._chromeos_root) |
asharif | c97199a | 2013-02-15 22:48:45 +0000 | [diff] [blame] | 33 | 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 | |
shenhan | ad80343 | 2013-02-16 03:14:51 +0000 | [diff] [blame] | 40 | self._chroot_source_path = os.path.join(constants.mounted_toolchain_root, |
llozano | 85b3715 | 2013-02-16 03:14:57 +0000 | [diff] [blame] | 41 | self._name).lstrip("/") |
asharif | c97199a | 2013-02-15 22:48:45 +0000 | [diff] [blame] | 42 | 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 |
asharif | ca3c6c1 | 2013-02-15 23:17:54 +0000 | [diff] [blame] | 52 | self._ce.ChrootRunCommand(self._chromeos_root, command) |
asharif | c97199a | 2013-02-15 22:48:45 +0000 | [diff] [blame] | 53 | |
| 54 | def Build(self): |
| 55 | self.RunSetupBoardIfNecessary() |
| 56 | |
shenhan | becf624 | 2013-02-19 20:43:32 +0000 | [diff] [blame^] | 57 | rv = 1 |
asharif | c97199a | 2013-02-15 22:48:45 +0000 | [diff] [blame] | 58 | try: |
asharif | 7dd6d86 | 2013-02-15 23:17:46 +0000 | [diff] [blame] | 59 | self.UninstallTool() |
asharif | c97199a | 2013-02-15 22:48:45 +0000 | [diff] [blame] | 60 | self.MoveMaskFile() |
cmtice | 80d257f | 2013-02-15 23:44:51 +0000 | [diff] [blame] | 61 | self.MountSources(False) |
asharif | 9e3cf6e | 2013-02-16 03:13:38 +0000 | [diff] [blame] | 62 | self.RemoveCompiledFile() |
shenhan | becf624 | 2013-02-19 20:43:32 +0000 | [diff] [blame^] | 63 | rv = self.BuildTool() |
asharif | c97199a | 2013-02-15 22:48:45 +0000 | [diff] [blame] | 64 | finally: |
| 65 | self.UnMoveMaskFile() |
shenhan | becf624 | 2013-02-19 20:43:32 +0000 | [diff] [blame^] | 66 | return rv |
asharif | c97199a | 2013-02-15 22:48:45 +0000 | [diff] [blame] | 67 | |
| 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") |
asharif | 9e3cf6e | 2013-02-16 03:13:38 +0000 | [diff] [blame] | 74 | command = "rm -f %s" % compiled_file |
asharif | c97199a | 2013-02-15 22:48:45 +0000 | [diff] [blame] | 75 | self._ce.RunCommand(command) |
| 76 | |
cmtice | 80d257f | 2013-02-15 23:44:51 +0000 | [diff] [blame] | 77 | def MountSources(self, unmount_source): |
asharif | c97199a | 2013-02-15 22:48:45 +0000 | [diff] [blame] | 78 | 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 | |
cmtice | 80d257f | 2013-02-15 23:44:51 +0000 | [diff] [blame] | 109 | if unmount_source: |
| 110 | unmount_statuses = [mp.UnMount() == 0 for mp in mount_points] |
asharif | c97199a | 2013-02-15 22:48:45 +0000 | [diff] [blame] | 111 | assert all(unmount_statuses), "Could not unmount all mount points!" |
cmtice | 80d257f | 2013-02-15 23:44:51 +0000 | [diff] [blame] | 112 | 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 | |
asharif | c97199a | 2013-02-15 22:48:45 +0000 | [diff] [blame] | 120 | |
asharif | 7dd6d86 | 2013-02-15 23:17:46 +0000 | [diff] [blame] | 121 | def UninstallTool(self): |
| 122 | command = "sudo CLEAN_DELAY=0 emerge -C cross-%s/%s" % (self._ctarget, self._name) |
asharif | ca3c6c1 | 2013-02-15 23:17:54 +0000 | [diff] [blame] | 123 | self._ce.ChrootRunCommand(self._chromeos_root, command) |
asharif | 7dd6d86 | 2013-02-15 23:17:46 +0000 | [diff] [blame] | 124 | |
asharif | c97199a | 2013-02-15 22:48:45 +0000 | [diff] [blame] | 125 | def BuildTool(self): |
| 126 | env = self._build_env |
asharif | 9e49916 | 2013-02-16 02:41:39 +0000 | [diff] [blame] | 127 | # FEATURES=buildpkg adds minutes of time so we disable it. |
shenhan | fdc0f57 | 2013-02-19 18:48:36 +0000 | [diff] [blame] | 128 | # 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" |
asharif | c97199a | 2013-02-15 22:48:45 +0000 | [diff] [blame] | 131 | 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) |
shenhan | becf624 | 2013-02-19 20:43:32 +0000 | [diff] [blame^] | 143 | return self._ce.ChrootRunCommand(self._chromeos_root, full_command) |
asharif | c97199a | 2013-02-15 22:48:45 +0000 | [diff] [blame] | 144 | |
asharif | c97199a | 2013-02-15 22:48:45 +0000 | [diff] [blame] | 145 | 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) |
bjanakiraman | 7f4a485 | 2013-02-15 04:35:28 +0000 | [diff] [blame] | 156 | |
bjanakiraman | 7f4a485 | 2013-02-15 04:35:28 +0000 | [diff] [blame] | 157 | |
asharif | 0d3535a | 2013-02-15 04:50:33 +0000 | [diff] [blame] | 158 | def Main(argv): |
asharif | 19c73dd | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 159 | """The main function.""" |
asharif | 5a9bb46 | 2013-02-15 04:50:57 +0000 | [diff] [blame] | 160 | # Common initializations |
asharif | 19c73dd | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 161 | parser = optparse.OptionParser() |
asharif | c97199a | 2013-02-15 22:48:45 +0000 | [diff] [blame] | 162 | parser.add_option("-c", |
| 163 | "--chromeos_root", |
| 164 | dest="chromeos_root", |
asharif | bcdd4e5 | 2013-02-16 01:05:17 +0000 | [diff] [blame] | 165 | default="../../", |
asharif | c97199a | 2013-02-15 22:48:45 +0000 | [diff] [blame] | 166 | 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.") |
cmtice | 80d257f | 2013-02-15 23:44:51 +0000 | [diff] [blame] | 172 | parser.add_option("-x", |
| 173 | "--gdb_dir", |
| 174 | dest="gdb_dir", |
| 175 | help="The directory where gdb resides.") |
asharif | c97199a | 2013-02-15 22:48:45 +0000 | [diff] [blame] | 176 | 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, |
asharif | d751e25 | 2013-02-15 04:35:52 +0000 | [diff] [blame] | 185 | action="store_true", |
asharif | c97199a | 2013-02-15 22:48:45 +0000 | [diff] [blame] | 186 | help="Use FEATURES=keepwork to do incremental builds.") |
cmtice | e59ac27 | 2013-02-19 20:20:09 +0000 | [diff] [blame] | 187 | 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") |
asharif | c97199a | 2013-02-15 22:48:45 +0000 | [diff] [blame] | 199 | parser.add_option("-d", |
| 200 | "--debug", |
| 201 | dest="debug", |
| 202 | default=False, |
asharif | d751e25 | 2013-02-15 04:35:52 +0000 | [diff] [blame] | 203 | action="store_true", |
cmtice | e59ac27 | 2013-02-19 20:20:09 +0000 | [diff] [blame] | 204 | help="Build a compiler with -g3 -O0 appended to both" |
| 205 | " CFLAGS and CXXFLAGS.") |
asharif | 86968c4 | 2013-02-15 23:44:37 +0000 | [diff] [blame] | 206 | 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.") |
cmtice | 80d257f | 2013-02-15 23:44:51 +0000 | [diff] [blame] | 212 | 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.") |
asharif | 86968c4 | 2013-02-15 23:44:37 +0000 | [diff] [blame] | 218 | |
bjanakiraman | 7f4a485 | 2013-02-15 04:35:28 +0000 | [diff] [blame] | 219 | |
asharif | c97199a | 2013-02-15 22:48:45 +0000 | [diff] [blame] | 220 | options, _ = parser.parse_args(argv) |
asharif | 1762130 | 2013-02-15 04:46:35 +0000 | [diff] [blame] | 221 | |
kbaclawski | 20082a0 | 2013-02-16 02:12:57 +0000 | [diff] [blame] | 222 | chromeos_root = misc.CanonicalizePath(options.chromeos_root) |
cmtice | 80d257f | 2013-02-15 23:44:51 +0000 | [diff] [blame] | 223 | if options.gcc_dir: |
kbaclawski | 20082a0 | 2013-02-16 02:12:57 +0000 | [diff] [blame] | 224 | gcc_dir = misc.CanonicalizePath(options.gcc_dir) |
cmtice | 80d257f | 2013-02-15 23:44:51 +0000 | [diff] [blame] | 225 | if options.gdb_dir: |
kbaclawski | 20082a0 | 2013-02-16 02:12:57 +0000 | [diff] [blame] | 226 | gdb_dir = misc.CanonicalizePath(options.gdb_dir) |
cmtice | 80d257f | 2013-02-15 23:44:51 +0000 | [diff] [blame] | 227 | if options.unmount_only: |
| 228 | options.mount_only = False |
| 229 | elif options.mount_only: |
| 230 | options.unmount_only = False |
asharif | c97199a | 2013-02-15 22:48:45 +0000 | [diff] [blame] | 231 | build_env = {} |
cmtice | e59ac27 | 2013-02-19 20:20:09 +0000 | [diff] [blame] | 232 | 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 |
asharif | c97199a | 2013-02-15 22:48:45 +0000 | [diff] [blame] | 238 | if options.debug: |
| 239 | debug_flags = "-g3 -O0" |
cmtice | e59ac27 | 2013-02-19 20:20:09 +0000 | [diff] [blame] | 240 | 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 |
bjanakiraman | 7f4a485 | 2013-02-15 04:35:28 +0000 | [diff] [blame] | 248 | |
asharif | 86968c4 | 2013-02-15 23:44:37 +0000 | [diff] [blame] | 249 | # Create toolchain parts |
| 250 | toolchain_parts = [] |
| 251 | for board in options.board.split(","): |
| 252 | if options.gcc_dir: |
asharif | c97199a | 2013-02-15 22:48:45 +0000 | [diff] [blame] | 253 | tp = ToolchainPart("gcc", gcc_dir, chromeos_root, board, |
| 254 | not options.noincremental, build_env) |
asharif | 86968c4 | 2013-02-15 23:44:37 +0000 | [diff] [blame] | 255 | toolchain_parts.append(tp) |
cmtice | 80d257f | 2013-02-15 23:44:51 +0000 | [diff] [blame] | 256 | if options.gdb_dir: |
| 257 | tp = ToolchainPart("gdb", gdb_dir, chromeos_root, board, |
| 258 | not options.noincremental, build_env) |
| 259 | toolchain_parts.append(tp) |
asharif | 86968c4 | 2013-02-15 23:44:37 +0000 | [diff] [blame] | 260 | |
shenhan | becf624 | 2013-02-19 20:43:32 +0000 | [diff] [blame^] | 261 | rv = 0 |
asharif | 86968c4 | 2013-02-15 23:44:37 +0000 | [diff] [blame] | 262 | try: |
| 263 | for tp in toolchain_parts: |
cmtice | 80d257f | 2013-02-15 23:44:51 +0000 | [diff] [blame] | 264 | if options.mount_only or options.unmount_only: |
| 265 | tp.MountSources(options.unmount_only) |
asharif | 86968c4 | 2013-02-15 23:44:37 +0000 | [diff] [blame] | 266 | else: |
shenhan | becf624 | 2013-02-19 20:43:32 +0000 | [diff] [blame^] | 267 | rv = rv + tp.Build() |
asharif | c97199a | 2013-02-15 22:48:45 +0000 | [diff] [blame] | 268 | finally: |
| 269 | print "Exiting..." |
shenhan | becf624 | 2013-02-19 20:43:32 +0000 | [diff] [blame^] | 270 | return rv |
asharif | 19c73dd | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 271 | |
| 272 | if __name__ == "__main__": |
asharif | 2198c51 | 2013-02-15 09:21:35 +0000 | [diff] [blame] | 273 | retval = Main(sys.argv) |
| 274 | sys.exit(retval) |