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 |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 17 | import tc_enter_chroot |
raymes | 01959ae | 2013-02-15 04:50:07 +0000 | [diff] [blame] | 18 | from utils import command_executer |
bjanakiraman | 7f4a485 | 2013-02-15 04:35:28 +0000 | [diff] [blame] | 19 | from utils import utils |
| 20 | |
asharif | 5a9bb46 | 2013-02-15 04:50:57 +0000 | [diff] [blame] | 21 | |
asharif | c97199a | 2013-02-15 22:48:45 +0000 | [diff] [blame] | 22 | class 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 |
asharif | 77bd80d | 2013-02-15 22:49:32 +0000 | [diff] [blame] | 29 | self._ctarget = utils.GetCtargetFromBoard(self._board, |
| 30 | self._chromeos_root) |
asharif | c97199a | 2013-02-15 22:48:45 +0000 | [diff] [blame] | 31 | 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 |
asharif | ca3c6c1 | 2013-02-15 23:17:54 +0000 | [diff] [blame] | 49 | self._ce.ChrootRunCommand(self._chromeos_root, command) |
asharif | c97199a | 2013-02-15 22:48:45 +0000 | [diff] [blame] | 50 | |
| 51 | def Build(self): |
| 52 | self.RunSetupBoardIfNecessary() |
| 53 | |
| 54 | try: |
asharif | 7dd6d86 | 2013-02-15 23:17:46 +0000 | [diff] [blame] | 55 | self.UninstallTool() |
asharif | c97199a | 2013-02-15 22:48:45 +0000 | [diff] [blame] | 56 | self.MoveMaskFile() |
| 57 | self.SwitchToBFD() |
cmtice | 80d257f | 2013-02-15 23:44:51 +0000 | [diff] [blame^] | 58 | self.MountSources(False) |
asharif | c97199a | 2013-02-15 22:48:45 +0000 | [diff] [blame] | 59 | 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 | |
cmtice | 80d257f | 2013-02-15 23:44:51 +0000 | [diff] [blame^] | 75 | def MountSources(self, unmount_source): |
asharif | c97199a | 2013-02-15 22:48:45 +0000 | [diff] [blame] | 76 | 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 | |
cmtice | 80d257f | 2013-02-15 23:44:51 +0000 | [diff] [blame^] | 107 | if unmount_source: |
| 108 | unmount_statuses = [mp.UnMount() == 0 for mp in mount_points] |
asharif | c97199a | 2013-02-15 22:48:45 +0000 | [diff] [blame] | 109 | assert all(unmount_statuses), "Could not unmount all mount points!" |
cmtice | 80d257f | 2013-02-15 23:44:51 +0000 | [diff] [blame^] | 110 | 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 | |
asharif | c97199a | 2013-02-15 22:48:45 +0000 | [diff] [blame] | 118 | |
asharif | 7dd6d86 | 2013-02-15 23:17:46 +0000 | [diff] [blame] | 119 | def UninstallTool(self): |
| 120 | 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] | 121 | self._ce.ChrootRunCommand(self._chromeos_root, command) |
asharif | 7dd6d86 | 2013-02-15 23:17:46 +0000 | [diff] [blame] | 122 | |
asharif | c97199a | 2013-02-15 22:48:45 +0000 | [diff] [blame] | 123 | 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) |
asharif | ca3c6c1 | 2013-02-15 23:17:54 +0000 | [diff] [blame] | 138 | self._ce.ChrootRunCommand(self._chromeos_root, full_command) |
asharif | c97199a | 2013-02-15 22:48:45 +0000 | [diff] [blame] | 139 | |
| 140 | def SwitchToBFD(self): |
| 141 | command = "sudo binutils-config %s-2.21" % self._ctarget |
asharif | ca3c6c1 | 2013-02-15 23:17:54 +0000 | [diff] [blame] | 142 | self._ce.ChrootRunCommand(self._chromeos_root, command) |
asharif | c97199a | 2013-02-15 22:48:45 +0000 | [diff] [blame] | 143 | |
| 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) |
bjanakiraman | 7f4a485 | 2013-02-15 04:35:28 +0000 | [diff] [blame] | 158 | |
bjanakiraman | 7f4a485 | 2013-02-15 04:35:28 +0000 | [diff] [blame] | 159 | |
asharif | 0d3535a | 2013-02-15 04:50:33 +0000 | [diff] [blame] | 160 | def Main(argv): |
asharif | 19c73dd | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 161 | """The main function.""" |
asharif | 5a9bb46 | 2013-02-15 04:50:57 +0000 | [diff] [blame] | 162 | # Common initializations |
asharif | 19c73dd | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 163 | parser = optparse.OptionParser() |
asharif | c97199a | 2013-02-15 22:48:45 +0000 | [diff] [blame] | 164 | 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.") |
cmtice | 80d257f | 2013-02-15 23:44:51 +0000 | [diff] [blame^] | 173 | parser.add_option("-x", |
| 174 | "--gdb_dir", |
| 175 | dest="gdb_dir", |
| 176 | help="The directory where gdb resides.") |
asharif | c97199a | 2013-02-15 22:48:45 +0000 | [diff] [blame] | 177 | 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, |
asharif | d751e25 | 2013-02-15 04:35:52 +0000 | [diff] [blame] | 186 | action="store_true", |
asharif | c97199a | 2013-02-15 22:48:45 +0000 | [diff] [blame] | 187 | help="Use FEATURES=keepwork to do incremental builds.") |
| 188 | parser.add_option("-d", |
| 189 | "--debug", |
| 190 | dest="debug", |
| 191 | default=False, |
asharif | d751e25 | 2013-02-15 04:35:52 +0000 | [diff] [blame] | 192 | action="store_true", |
asharif | c97199a | 2013-02-15 22:48:45 +0000 | [diff] [blame] | 193 | help="Build a compiler with -g3 -O0.") |
asharif | 86968c4 | 2013-02-15 23:44:37 +0000 | [diff] [blame] | 194 | 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.") |
cmtice | 80d257f | 2013-02-15 23:44:51 +0000 | [diff] [blame^] | 200 | 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.") |
asharif | 86968c4 | 2013-02-15 23:44:37 +0000 | [diff] [blame] | 206 | |
bjanakiraman | 7f4a485 | 2013-02-15 04:35:28 +0000 | [diff] [blame] | 207 | |
asharif | c97199a | 2013-02-15 22:48:45 +0000 | [diff] [blame] | 208 | options, _ = parser.parse_args(argv) |
asharif | 1762130 | 2013-02-15 04:46:35 +0000 | [diff] [blame] | 209 | |
asharif | c97199a | 2013-02-15 22:48:45 +0000 | [diff] [blame] | 210 | chromeos_root = utils.CanonicalizePath(options.chromeos_root) |
cmtice | 80d257f | 2013-02-15 23:44:51 +0000 | [diff] [blame^] | 211 | 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 |
asharif | c97199a | 2013-02-15 22:48:45 +0000 | [diff] [blame] | 219 | build_env = {} |
| 220 | if options.debug: |
| 221 | debug_flags = "-g3 -O0" |
| 222 | build_env["CFLAGS"] = debug_flags |
| 223 | build_env["CXXFLAGS"] = debug_flags |
bjanakiraman | 7f4a485 | 2013-02-15 04:35:28 +0000 | [diff] [blame] | 224 | |
asharif | 86968c4 | 2013-02-15 23:44:37 +0000 | [diff] [blame] | 225 | # Create toolchain parts |
| 226 | toolchain_parts = [] |
| 227 | for board in options.board.split(","): |
| 228 | if options.gcc_dir: |
asharif | c97199a | 2013-02-15 22:48:45 +0000 | [diff] [blame] | 229 | tp = ToolchainPart("gcc", gcc_dir, chromeos_root, board, |
| 230 | not options.noincremental, build_env) |
asharif | 86968c4 | 2013-02-15 23:44:37 +0000 | [diff] [blame] | 231 | toolchain_parts.append(tp) |
cmtice | 80d257f | 2013-02-15 23:44:51 +0000 | [diff] [blame^] | 232 | if options.gdb_dir: |
| 233 | tp = ToolchainPart("gdb", gdb_dir, chromeos_root, board, |
| 234 | not options.noincremental, build_env) |
| 235 | toolchain_parts.append(tp) |
asharif | 86968c4 | 2013-02-15 23:44:37 +0000 | [diff] [blame] | 236 | |
| 237 | try: |
| 238 | for tp in toolchain_parts: |
cmtice | 80d257f | 2013-02-15 23:44:51 +0000 | [diff] [blame^] | 239 | if options.mount_only or options.unmount_only: |
| 240 | tp.MountSources(options.unmount_only) |
asharif | 86968c4 | 2013-02-15 23:44:37 +0000 | [diff] [blame] | 241 | else: |
| 242 | tp.Build() |
asharif | c97199a | 2013-02-15 22:48:45 +0000 | [diff] [blame] | 243 | finally: |
| 244 | print "Exiting..." |
| 245 | return 0 |
asharif | 0d3535a | 2013-02-15 04:50:33 +0000 | [diff] [blame] | 246 | |
asharif | 19c73dd | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 247 | |
| 248 | if __name__ == "__main__": |
asharif | 2198c51 | 2013-02-15 09:21:35 +0000 | [diff] [blame] | 249 | retval = Main(sys.argv) |
| 250 | sys.exit(retval) |