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 |
| 12 | import logger |
asharif | 7aad14d | 2013-02-15 21:15:13 +0000 | [diff] [blame] | 13 | from contextlib import contextmanager |
bjanakiraman | 7f4a485 | 2013-02-15 04:35:28 +0000 | [diff] [blame] | 14 | |
| 15 | |
| 16 | def GetRoot(scr_name): |
| 17 | """Break up pathname into (dir+name).""" |
| 18 | abs_path = os.path.abspath(scr_name) |
| 19 | return (os.path.dirname(abs_path), os.path.basename(abs_path)) |
| 20 | |
| 21 | |
asharif | cefd138 | 2013-02-15 04:49:12 +0000 | [diff] [blame] | 22 | def FormatQuotedCommand(command): |
| 23 | return command.replace("\"", "\\\"") |
| 24 | |
kbaclawski | 5fec550 | 2013-02-15 19:55:56 +0000 | [diff] [blame] | 25 | |
asharif | 0e0e268 | 2013-02-15 05:15:29 +0000 | [diff] [blame] | 26 | def FormatCommands(commands): |
kbaclawski | 08b1e15 | 2013-02-15 20:00:19 +0000 | [diff] [blame] | 27 | output = str(commands) |
kbaclawski | 5fec550 | 2013-02-15 19:55:56 +0000 | [diff] [blame] | 28 | output = re.sub("&&", "&&\n", output) |
| 29 | output = re.sub(";", ";\n", output) |
| 30 | output = re.sub("\n+\s*", "\n", output) |
| 31 | return output |
asharif | 0e0e268 | 2013-02-15 05:15:29 +0000 | [diff] [blame] | 32 | |
kbaclawski | 6999ada | 2013-02-15 19:57:09 +0000 | [diff] [blame] | 33 | |
asharif | e0cc305 | 2013-02-15 05:20:48 +0000 | [diff] [blame] | 34 | def GetBuildPackagesCommand(board): |
raymes | fc4e4f1 | 2013-02-15 19:55:58 +0000 | [diff] [blame] | 35 | return "./build_packages --nousepkg --withdev --withtest --withautotest " \ |
asharif | 852482f | 2013-02-15 21:19:59 +0000 | [diff] [blame] | 36 | "--skip_toolchain_update --nowithdebug --board=%s" % board |
kbaclawski | 5fec550 | 2013-02-15 19:55:56 +0000 | [diff] [blame] | 37 | |
asharif | e0cc305 | 2013-02-15 05:20:48 +0000 | [diff] [blame] | 38 | |
| 39 | def GetBuildImageCommand(board): |
kbaclawski | 5fec550 | 2013-02-15 19:55:56 +0000 | [diff] [blame] | 40 | return "./build_image --withdev --board=%s" % board |
| 41 | |
asharif | e0cc305 | 2013-02-15 05:20:48 +0000 | [diff] [blame] | 42 | |
| 43 | def GetModImageForTestCommand(board): |
kbaclawski | 5fec550 | 2013-02-15 19:55:56 +0000 | [diff] [blame] | 44 | return "./mod_image_for_test.sh --yes --board=%s" % board |
asharif | e0cc305 | 2013-02-15 05:20:48 +0000 | [diff] [blame] | 45 | |
kbaclawski | 5fec550 | 2013-02-15 19:55:56 +0000 | [diff] [blame] | 46 | |
| 47 | def GetSetupBoardCommand(board, gcc_version=None, binutils_version=None, |
| 48 | usepkg=None, force=None): |
| 49 | options = [] |
| 50 | |
| 51 | if gcc_version: |
| 52 | options.append("--gcc_version=%s" % gcc_version) |
| 53 | |
| 54 | if binutils_version: |
| 55 | options.append("--binutils_version=%s" % binutils_version) |
| 56 | |
| 57 | if usepkg: |
| 58 | options.append("--usepkg") |
asharif | e0cc305 | 2013-02-15 05:20:48 +0000 | [diff] [blame] | 59 | else: |
kbaclawski | 5fec550 | 2013-02-15 19:55:56 +0000 | [diff] [blame] | 60 | options.append("--nousepkg") |
| 61 | |
| 62 | if force: |
| 63 | options.append("--force") |
| 64 | |
| 65 | return "./setup_board --board=%s %s" % (board, " ".join(options)) |
asharif | e0cc305 | 2013-02-15 05:20:48 +0000 | [diff] [blame] | 66 | |
asharif | 0e0e268 | 2013-02-15 05:15:29 +0000 | [diff] [blame] | 67 | |
asharif | 7aad14d | 2013-02-15 21:15:13 +0000 | [diff] [blame] | 68 | @contextmanager |
| 69 | def WorkingDirectory(new_dir): |
| 70 | old_dir = os.getcwd() |
asharif | f6e9195 | 2013-02-15 22:36:16 +0000 | [diff] [blame^] | 71 | if old_dir != new_dir: |
| 72 | msg = "cd %s" % new_dir |
| 73 | logger.GetLogger().LogCmd(msg) |
asharif | 7aad14d | 2013-02-15 21:15:13 +0000 | [diff] [blame] | 74 | os.chdir(new_dir) |
| 75 | yield new_dir |
asharif | f6e9195 | 2013-02-15 22:36:16 +0000 | [diff] [blame^] | 76 | if old_dir != new_dir: |
| 77 | msg = "cd %s" % old_dir |
| 78 | logger.GetLogger().LogCmd(msg) |
asharif | 7aad14d | 2013-02-15 21:15:13 +0000 | [diff] [blame] | 79 | os.chdir(old_dir) |