Ahmad Sharif | fd356fb | 2012-05-07 12:02:16 -0700 | [diff] [blame] | 1 | #!/usr/bin/python2.6 |
| 2 | # |
| 3 | # Copyright 2010 Google Inc. All Rights Reserved. |
| 4 | |
| 5 | """Utilities for toolchain build.""" |
| 6 | |
| 7 | __author__ = "asharif@google.com (Ahmad Sharif)" |
| 8 | |
| 9 | import hashlib |
| 10 | import os |
| 11 | import re |
| 12 | import stat |
| 13 | import command_executer |
| 14 | import logger |
| 15 | import tempfile |
| 16 | from contextlib import contextmanager |
| 17 | |
| 18 | |
| 19 | def ApplySubs(string, *substitutions): |
| 20 | for pattern, replacement in substitutions: |
| 21 | string = re.sub(pattern, replacement, string) |
| 22 | return string |
| 23 | |
| 24 | |
| 25 | def GetFilenameFromString(string): |
| 26 | return ApplySubs(string, |
| 27 | ("/", "__"), |
| 28 | ("\s", "_"), |
| 29 | ("=", ""), |
| 30 | ("\"", "")) |
| 31 | |
| 32 | |
| 33 | def GetRoot(scr_name): |
| 34 | """Break up pathname into (dir+name).""" |
| 35 | abs_path = os.path.abspath(scr_name) |
| 36 | return (os.path.dirname(abs_path), os.path.basename(abs_path)) |
| 37 | |
| 38 | |
| 39 | def FormatQuotedCommand(command): |
| 40 | return ApplySubs(command, |
| 41 | ("\"", "\\\"")) |
| 42 | |
| 43 | |
| 44 | def FormatCommands(commands): |
| 45 | return ApplySubs(str(commands), |
| 46 | ("&&", "&&\n"), |
| 47 | (";", ";\n"), |
| 48 | ("\n+\s*", "\n")) |
| 49 | |
| 50 | |
| 51 | def GetImageDir(chromeos_root, board): |
| 52 | return os.path.join(chromeos_root, |
| 53 | "src", |
| 54 | "build", |
| 55 | "images", |
| 56 | board) |
| 57 | |
| 58 | |
| 59 | def LabelLatestImage(chromeos_root, board, label): |
| 60 | image_dir = GetImageDir(chromeos_root, board) |
| 61 | latest_image_dir = os.path.join(image_dir, "latest") |
| 62 | latest_image_dir = os.path.realpath(latest_image_dir) |
| 63 | latest_image_dir = os.path.basename(latest_image_dir) |
| 64 | with WorkingDirectory(image_dir): |
| 65 | command = "ln -sf -T %s %s" % (latest_image_dir, label) |
| 66 | ce = command_executer.GetCommandExecuter() |
| 67 | return ce.RunCommand(command) |
| 68 | |
| 69 | |
| 70 | def DoesLabelExist(chromeos_root, board, label): |
| 71 | image_label = os.path.join(GetImageDir(chromeos_root, board), |
| 72 | label) |
| 73 | return os.path.exists(image_label) |
| 74 | |
| 75 | |
| 76 | def GetBuildPackagesCommand(board, usepkg=False): |
| 77 | if usepkg: |
| 78 | usepkg_flag = "--usepkg" |
| 79 | else: |
| 80 | usepkg_flag = "--nousepkg" |
| 81 | return ("./build_packages %s --withdev --withtest --withautotest " |
| 82 | "--skip_toolchain_update --nowithdebug --board=%s" % |
| 83 | (usepkg_flag, board)) |
| 84 | |
| 85 | |
| 86 | def GetBuildImageCommand(board): |
| 87 | return "./build_image --board=%s test" % board |
| 88 | |
| 89 | |
| 90 | def GetSetupBoardCommand(board, gcc_version=None, binutils_version=None, |
| 91 | usepkg=None, force=None): |
| 92 | options = [] |
| 93 | |
| 94 | if gcc_version: |
| 95 | options.append("--gcc_version=%s" % gcc_version) |
| 96 | |
| 97 | if binutils_version: |
| 98 | options.append("--binutils_version=%s" % binutils_version) |
| 99 | |
| 100 | if usepkg: |
| 101 | options.append("--usepkg") |
| 102 | else: |
| 103 | options.append("--nousepkg") |
| 104 | |
| 105 | if force: |
| 106 | options.append("--force") |
| 107 | |
| 108 | return "./setup_board --board=%s %s" % (board, " ".join(options)) |
| 109 | |
| 110 | |
| 111 | def CanonicalizePath(path): |
| 112 | path = os.path.expanduser(path) |
| 113 | path = os.path.realpath(path) |
| 114 | return path |
| 115 | |
| 116 | |
| 117 | def GetCtargetFromBoard(board, chromeos_root): |
| 118 | base_board = board.split("_")[0] |
| 119 | command = ("source " |
| 120 | "../platform/dev/toolchain_utils.sh; get_ctarget_from_board %s" % |
| 121 | base_board) |
| 122 | ce = command_executer.GetCommandExecuter() |
| 123 | ret, out, err = ce.ChrootRunCommand(chromeos_root, |
| 124 | command, |
| 125 | return_output=True) |
| 126 | if ret != 0: |
| 127 | raise ValueError("Board %s is invalid!" % board) |
| 128 | return out.strip() |
| 129 | |
| 130 | |
| 131 | def GetChromeSrcDir(): |
| 132 | return "var/cache/distfiles/target/chrome-src/src" |
| 133 | |
| 134 | |
| 135 | def GetEnvStringFromDict(env_dict): |
| 136 | return " ".join(["%s=\"%s\"" % var for var in env_dict.items()]) |
| 137 | |
| 138 | |
| 139 | def GetAllImages(chromeos_root, board): |
| 140 | ce = command_executer.GetCommandExecuter() |
| 141 | command = ("find %s/src/build/images/%s -name chromiumos_test_image.bin" % |
| 142 | (chromeos_root, board)) |
| 143 | ret, out, err = ce.RunCommand(command, return_output=True) |
| 144 | return out.splitlines() |
| 145 | |
| 146 | |
| 147 | @contextmanager |
| 148 | def WorkingDirectory(new_dir): |
| 149 | old_dir = os.getcwd() |
| 150 | if old_dir != new_dir: |
| 151 | msg = "cd %s" % new_dir |
| 152 | logger.GetLogger().LogCmd(msg) |
| 153 | os.chdir(new_dir) |
| 154 | yield new_dir |
| 155 | if old_dir != new_dir: |
| 156 | msg = "cd %s" % old_dir |
| 157 | logger.GetLogger().LogCmd(msg) |
| 158 | os.chdir(old_dir) |