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 | """Utilities for toolchain build.""" |
| 6 | |
bjanakiraman | 7f4a485 | 2013-02-15 04:35:28 +0000 | [diff] [blame] | 7 | __author__ = "asharif@google.com (Ahmad Sharif)" |
| 8 | |
asharif | 4d2b716 | 2013-02-15 05:14:57 +0000 | [diff] [blame] | 9 | import hashlib |
kbaclawski | 6999ada | 2013-02-15 19:57:09 +0000 | [diff] [blame] | 10 | import os |
| 11 | import re |
asharif | 9d671b9 | 2013-02-15 22:39:35 +0000 | [diff] [blame] | 12 | import stat |
| 13 | import command_executer |
kbaclawski | 6999ada | 2013-02-15 19:57:09 +0000 | [diff] [blame] | 14 | import logger |
asharif | a5689fd | 2013-02-15 23:17:53 +0000 | [diff] [blame] | 15 | import tempfile |
asharif | 7aad14d | 2013-02-15 21:15:13 +0000 | [diff] [blame] | 16 | from contextlib import contextmanager |
bjanakiraman | 7f4a485 | 2013-02-15 04:35:28 +0000 | [diff] [blame] | 17 | |
| 18 | |
| 19 | def GetRoot(scr_name): |
| 20 | """Break up pathname into (dir+name).""" |
| 21 | abs_path = os.path.abspath(scr_name) |
| 22 | return (os.path.dirname(abs_path), os.path.basename(abs_path)) |
| 23 | |
| 24 | |
asharif | cefd138 | 2013-02-15 04:49:12 +0000 | [diff] [blame] | 25 | def FormatQuotedCommand(command): |
| 26 | return command.replace("\"", "\\\"") |
| 27 | |
kbaclawski | 5fec550 | 2013-02-15 19:55:56 +0000 | [diff] [blame] | 28 | |
asharif | 0e0e268 | 2013-02-15 05:15:29 +0000 | [diff] [blame] | 29 | def FormatCommands(commands): |
kbaclawski | 08b1e15 | 2013-02-15 20:00:19 +0000 | [diff] [blame] | 30 | output = str(commands) |
kbaclawski | 5fec550 | 2013-02-15 19:55:56 +0000 | [diff] [blame] | 31 | output = re.sub("&&", "&&\n", output) |
| 32 | output = re.sub(";", ";\n", output) |
| 33 | output = re.sub("\n+\s*", "\n", output) |
| 34 | return output |
asharif | 0e0e268 | 2013-02-15 05:15:29 +0000 | [diff] [blame] | 35 | |
kbaclawski | 6999ada | 2013-02-15 19:57:09 +0000 | [diff] [blame] | 36 | |
asharif | e0cc305 | 2013-02-15 05:20:48 +0000 | [diff] [blame] | 37 | def GetBuildPackagesCommand(board): |
raymes | fc4e4f1 | 2013-02-15 19:55:58 +0000 | [diff] [blame] | 38 | return "./build_packages --nousepkg --withdev --withtest --withautotest " \ |
asharif | 852482f | 2013-02-15 21:19:59 +0000 | [diff] [blame] | 39 | "--skip_toolchain_update --nowithdebug --board=%s" % board |
kbaclawski | 5fec550 | 2013-02-15 19:55:56 +0000 | [diff] [blame] | 40 | |
asharif | e0cc305 | 2013-02-15 05:20:48 +0000 | [diff] [blame] | 41 | |
| 42 | def GetBuildImageCommand(board): |
kbaclawski | 5fec550 | 2013-02-15 19:55:56 +0000 | [diff] [blame] | 43 | return "./build_image --withdev --board=%s" % board |
| 44 | |
asharif | e0cc305 | 2013-02-15 05:20:48 +0000 | [diff] [blame] | 45 | |
| 46 | def GetModImageForTestCommand(board): |
kbaclawski | 5fec550 | 2013-02-15 19:55:56 +0000 | [diff] [blame] | 47 | return "./mod_image_for_test.sh --yes --board=%s" % board |
asharif | e0cc305 | 2013-02-15 05:20:48 +0000 | [diff] [blame] | 48 | |
kbaclawski | 5fec550 | 2013-02-15 19:55:56 +0000 | [diff] [blame] | 49 | |
| 50 | def GetSetupBoardCommand(board, gcc_version=None, binutils_version=None, |
| 51 | usepkg=None, force=None): |
| 52 | options = [] |
| 53 | |
| 54 | if gcc_version: |
| 55 | options.append("--gcc_version=%s" % gcc_version) |
| 56 | |
| 57 | if binutils_version: |
| 58 | options.append("--binutils_version=%s" % binutils_version) |
| 59 | |
| 60 | if usepkg: |
| 61 | options.append("--usepkg") |
asharif | e0cc305 | 2013-02-15 05:20:48 +0000 | [diff] [blame] | 62 | else: |
kbaclawski | 5fec550 | 2013-02-15 19:55:56 +0000 | [diff] [blame] | 63 | options.append("--nousepkg") |
| 64 | |
| 65 | if force: |
| 66 | options.append("--force") |
| 67 | |
| 68 | return "./setup_board --board=%s %s" % (board, " ".join(options)) |
asharif | e0cc305 | 2013-02-15 05:20:48 +0000 | [diff] [blame] | 69 | |
asharif | 0e0e268 | 2013-02-15 05:15:29 +0000 | [diff] [blame] | 70 | |
asharif | c97199a | 2013-02-15 22:48:45 +0000 | [diff] [blame] | 71 | def CanonicalizePath(path): |
| 72 | path = os.path.expanduser(path) |
| 73 | path = os.path.realpath(path) |
| 74 | return path |
| 75 | |
| 76 | |
asharif | 77bd80d | 2013-02-15 22:49:32 +0000 | [diff] [blame] | 77 | def GetCtargetFromBoard(board, chromeos_root): |
| 78 | base_board = board.split("_")[0] |
| 79 | command = ("cat" |
| 80 | " $(cros_overlay_list --board=%s --primary_only)/toolchain.conf" % |
| 81 | (base_board)) |
asharif | ca3c6c1 | 2013-02-15 23:17:54 +0000 | [diff] [blame^] | 82 | ce = command_executer.GetCommandExecuter() |
| 83 | ret, out, err = ce.ChrootRunCommand(chromeos_root, |
| 84 | command, |
| 85 | return_output=True) |
asharif | 77bd80d | 2013-02-15 22:49:32 +0000 | [diff] [blame] | 86 | if ret != 0: |
| 87 | raise ValueError("Board %s is invalid!" % board) |
| 88 | return out.strip() |
asharif | c97199a | 2013-02-15 22:48:45 +0000 | [diff] [blame] | 89 | |
| 90 | |
asharif | 7aad14d | 2013-02-15 21:15:13 +0000 | [diff] [blame] | 91 | @contextmanager |
| 92 | def WorkingDirectory(new_dir): |
| 93 | old_dir = os.getcwd() |
asharif | f6e9195 | 2013-02-15 22:36:16 +0000 | [diff] [blame] | 94 | if old_dir != new_dir: |
| 95 | msg = "cd %s" % new_dir |
| 96 | logger.GetLogger().LogCmd(msg) |
asharif | 7aad14d | 2013-02-15 21:15:13 +0000 | [diff] [blame] | 97 | os.chdir(new_dir) |
| 98 | yield new_dir |
asharif | f6e9195 | 2013-02-15 22:36:16 +0000 | [diff] [blame] | 99 | if old_dir != new_dir: |
| 100 | msg = "cd %s" % old_dir |
| 101 | logger.GetLogger().LogCmd(msg) |
asharif | 7aad14d | 2013-02-15 21:15:13 +0000 | [diff] [blame] | 102 | os.chdir(old_dir) |