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() |
| 58 | self.MountSources() |
| 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 | |
| 75 | def MountSources(self): |
| 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 | |
| 107 | mount_statuses = [mp.DoMount() == 0 for mp in mount_points] |
| 108 | |
| 109 | if not all(mount_statuses): |
| 110 | mounted = [mp for mp, status in zip(mount_points, mount_statuses) if status] |
| 111 | unmount_statuses = [mp.UnMount() == 0 for mp in mounted] |
| 112 | assert all(unmount_statuses), "Could not unmount all mount points!" |
| 113 | |
asharif | 7dd6d86 | 2013-02-15 23:17:46 +0000 | [diff] [blame] | 114 | def UninstallTool(self): |
| 115 | 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] | 116 | self._ce.ChrootRunCommand(self._chromeos_root, command) |
asharif | 7dd6d86 | 2013-02-15 23:17:46 +0000 | [diff] [blame] | 117 | |
asharif | c97199a | 2013-02-15 22:48:45 +0000 | [diff] [blame] | 118 | def BuildTool(self): |
| 119 | env = self._build_env |
| 120 | features = "nostrip userpriv userfetch -sandbox noclean" |
| 121 | env["FEATURES"] = features |
| 122 | |
| 123 | if self._incremental: |
| 124 | env["FEATURES"] += " keepwork" |
| 125 | |
| 126 | env["USE"] = "multislot mounted_%s" % self._name |
| 127 | env["%s_SOURCE_PATH" % self._name.upper()] = ( |
| 128 | os.path.join("/", self._chroot_source_path)) |
| 129 | env["ACCEPT_KEYWORDS"] = "~*" |
| 130 | env_string = " ".join(["%s=\"%s\"" % var for var in env.items()]) |
| 131 | command = "emerge =cross-%s/%s-9999" % (self._ctarget, self._name) |
| 132 | full_command = "sudo %s %s" % (env_string, command) |
asharif | ca3c6c1 | 2013-02-15 23:17:54 +0000 | [diff] [blame] | 133 | self._ce.ChrootRunCommand(self._chromeos_root, full_command) |
asharif | c97199a | 2013-02-15 22:48:45 +0000 | [diff] [blame] | 134 | |
| 135 | def SwitchToBFD(self): |
| 136 | command = "sudo binutils-config %s-2.21" % self._ctarget |
asharif | ca3c6c1 | 2013-02-15 23:17:54 +0000 | [diff] [blame] | 137 | self._ce.ChrootRunCommand(self._chromeos_root, command) |
asharif | c97199a | 2013-02-15 22:48:45 +0000 | [diff] [blame] | 138 | |
| 139 | def SwitchToOriginalLD(self): |
| 140 | pass |
| 141 | |
| 142 | def MoveMaskFile(self): |
| 143 | self._new_mask_file = None |
| 144 | if os.path.isfile(self._mask_file): |
| 145 | self._new_mask_file = tempfile.mktemp() |
| 146 | command = "sudo mv %s %s" % (self._mask_file, self._new_mask_file) |
| 147 | self._ce.RunCommand(command) |
| 148 | |
| 149 | def UnMoveMaskFile(self): |
| 150 | if self._new_mask_file: |
| 151 | command = "sudo mv %s %s" % (self._new_mask_file, self._mask_file) |
| 152 | self._ce.RunCommand(command) |
bjanakiraman | 7f4a485 | 2013-02-15 04:35:28 +0000 | [diff] [blame] | 153 | |
bjanakiraman | 7f4a485 | 2013-02-15 04:35:28 +0000 | [diff] [blame] | 154 | |
asharif | 0d3535a | 2013-02-15 04:50:33 +0000 | [diff] [blame] | 155 | def Main(argv): |
asharif | 19c73dd | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 156 | """The main function.""" |
asharif | 5a9bb46 | 2013-02-15 04:50:57 +0000 | [diff] [blame] | 157 | # Common initializations |
asharif | 19c73dd | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 158 | parser = optparse.OptionParser() |
asharif | c97199a | 2013-02-15 22:48:45 +0000 | [diff] [blame] | 159 | parser.add_option("-c", |
| 160 | "--chromeos_root", |
| 161 | dest="chromeos_root", |
| 162 | help=("ChromeOS root checkout directory" |
| 163 | " uses ../.. if none given.")) |
| 164 | parser.add_option("-g", |
| 165 | "--gcc_dir", |
| 166 | dest="gcc_dir", |
| 167 | help="The directory where gcc resides.") |
| 168 | parser.add_option("-b", |
| 169 | "--board", |
| 170 | dest="board", |
| 171 | default="x86-agz", |
| 172 | help="The target board.") |
| 173 | parser.add_option("-n", |
| 174 | "--noincremental", |
| 175 | dest="noincremental", |
| 176 | default=False, |
asharif | d751e25 | 2013-02-15 04:35:52 +0000 | [diff] [blame] | 177 | action="store_true", |
asharif | c97199a | 2013-02-15 22:48:45 +0000 | [diff] [blame] | 178 | help="Use FEATURES=keepwork to do incremental builds.") |
| 179 | parser.add_option("-d", |
| 180 | "--debug", |
| 181 | dest="debug", |
| 182 | default=False, |
asharif | d751e25 | 2013-02-15 04:35:52 +0000 | [diff] [blame] | 183 | action="store_true", |
asharif | c97199a | 2013-02-15 22:48:45 +0000 | [diff] [blame] | 184 | help="Build a compiler with -g3 -O0.") |
asharif | 86968c4 | 2013-02-15 23:44:37 +0000 | [diff] [blame^] | 185 | parser.add_option("-m", |
| 186 | "--mount_only", |
| 187 | dest="mount_only", |
| 188 | default=False, |
| 189 | action="store_true", |
| 190 | help="Just mount the tool directories.") |
| 191 | |
bjanakiraman | 7f4a485 | 2013-02-15 04:35:28 +0000 | [diff] [blame] | 192 | |
asharif | c97199a | 2013-02-15 22:48:45 +0000 | [diff] [blame] | 193 | options, _ = parser.parse_args(argv) |
asharif | 1762130 | 2013-02-15 04:46:35 +0000 | [diff] [blame] | 194 | |
asharif | c97199a | 2013-02-15 22:48:45 +0000 | [diff] [blame] | 195 | chromeos_root = utils.CanonicalizePath(options.chromeos_root) |
| 196 | gcc_dir = utils.CanonicalizePath(options.gcc_dir) |
| 197 | build_env = {} |
| 198 | if options.debug: |
| 199 | debug_flags = "-g3 -O0" |
| 200 | build_env["CFLAGS"] = debug_flags |
| 201 | build_env["CXXFLAGS"] = debug_flags |
bjanakiraman | 7f4a485 | 2013-02-15 04:35:28 +0000 | [diff] [blame] | 202 | |
asharif | 86968c4 | 2013-02-15 23:44:37 +0000 | [diff] [blame^] | 203 | # Create toolchain parts |
| 204 | toolchain_parts = [] |
| 205 | for board in options.board.split(","): |
| 206 | if options.gcc_dir: |
asharif | c97199a | 2013-02-15 22:48:45 +0000 | [diff] [blame] | 207 | tp = ToolchainPart("gcc", gcc_dir, chromeos_root, board, |
| 208 | not options.noincremental, build_env) |
asharif | 86968c4 | 2013-02-15 23:44:37 +0000 | [diff] [blame^] | 209 | toolchain_parts.append(tp) |
| 210 | |
| 211 | try: |
| 212 | for tp in toolchain_parts: |
| 213 | if options.mount_only: |
| 214 | tp.MountSources() |
| 215 | else: |
| 216 | tp.Build() |
asharif | c97199a | 2013-02-15 22:48:45 +0000 | [diff] [blame] | 217 | finally: |
| 218 | print "Exiting..." |
| 219 | return 0 |
asharif | 0d3535a | 2013-02-15 04:50:33 +0000 | [diff] [blame] | 220 | |
asharif | 19c73dd | 2013-02-15 04:35:37 +0000 | [diff] [blame] | 221 | |
| 222 | if __name__ == "__main__": |
asharif | 2198c51 | 2013-02-15 09:21:35 +0000 | [diff] [blame] | 223 | retval = Main(sys.argv) |
| 224 | sys.exit(retval) |