blob: cf0b800e0b20a5d9347ca709227acbcfeb5cab13 [file] [log] [blame]
bjanakiraman7f4a4852013-02-15 04:35:28 +00001#!/usr/bin/python2.6
2#
3# Copyright 2010 Google Inc. All Rights Reserved.
4
5"""Utilities for toolchain build."""
6
bjanakiraman7f4a4852013-02-15 04:35:28 +00007__author__ = "asharif@google.com (Ahmad Sharif)"
8
asharif4d2b7162013-02-15 05:14:57 +00009import hashlib
kbaclawski6999ada2013-02-15 19:57:09 +000010import os
11import re
asharif9d671b92013-02-15 22:39:35 +000012import stat
13import command_executer
kbaclawski6999ada2013-02-15 19:57:09 +000014import logger
asharifa5689fd2013-02-15 23:17:53 +000015import tempfile
asharif7aad14d2013-02-15 21:15:13 +000016from contextlib import contextmanager
bjanakiraman7f4a4852013-02-15 04:35:28 +000017
18
19def 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
asharifcefd1382013-02-15 04:49:12 +000025def FormatQuotedCommand(command):
26 return command.replace("\"", "\\\"")
27
kbaclawski5fec5502013-02-15 19:55:56 +000028
asharif0e0e2682013-02-15 05:15:29 +000029def FormatCommands(commands):
kbaclawski08b1e152013-02-15 20:00:19 +000030 output = str(commands)
kbaclawski5fec5502013-02-15 19:55:56 +000031 output = re.sub("&&", "&&\n", output)
32 output = re.sub(";", ";\n", output)
33 output = re.sub("\n+\s*", "\n", output)
34 return output
asharif0e0e2682013-02-15 05:15:29 +000035
kbaclawski6999ada2013-02-15 19:57:09 +000036
asharife0cc3052013-02-15 05:20:48 +000037def GetBuildPackagesCommand(board):
raymesfc4e4f12013-02-15 19:55:58 +000038 return "./build_packages --nousepkg --withdev --withtest --withautotest " \
asharif852482f2013-02-15 21:19:59 +000039 "--skip_toolchain_update --nowithdebug --board=%s" % board
kbaclawski5fec5502013-02-15 19:55:56 +000040
asharife0cc3052013-02-15 05:20:48 +000041
42def GetBuildImageCommand(board):
kbaclawski5fec5502013-02-15 19:55:56 +000043 return "./build_image --withdev --board=%s" % board
44
asharife0cc3052013-02-15 05:20:48 +000045
46def GetModImageForTestCommand(board):
kbaclawski5fec5502013-02-15 19:55:56 +000047 return "./mod_image_for_test.sh --yes --board=%s" % board
asharife0cc3052013-02-15 05:20:48 +000048
kbaclawski5fec5502013-02-15 19:55:56 +000049
50def 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")
asharife0cc3052013-02-15 05:20:48 +000062 else:
kbaclawski5fec5502013-02-15 19:55:56 +000063 options.append("--nousepkg")
64
65 if force:
66 options.append("--force")
67
68 return "./setup_board --board=%s %s" % (board, " ".join(options))
asharife0cc3052013-02-15 05:20:48 +000069
asharif0e0e2682013-02-15 05:15:29 +000070
asharifc97199a2013-02-15 22:48:45 +000071def CanonicalizePath(path):
72 path = os.path.expanduser(path)
73 path = os.path.realpath(path)
74 return path
75
76
asharif77bd80d2013-02-15 22:49:32 +000077def GetCtargetFromBoard(board, chromeos_root):
78 base_board = board.split("_")[0]
asharif5859e1b2013-02-16 01:05:20 +000079 command = ("source "
80 "../platform/dev/toolchain_utils.sh; get_ctarget_from_board %s" %
81 base_board)
asharifca3c6c12013-02-15 23:17:54 +000082 ce = command_executer.GetCommandExecuter()
83 ret, out, err = ce.ChrootRunCommand(chromeos_root,
84 command,
85 return_output=True)
asharif77bd80d2013-02-15 22:49:32 +000086 if ret != 0:
87 raise ValueError("Board %s is invalid!" % board)
88 return out.strip()
asharifc97199a2013-02-15 22:48:45 +000089
90
asharif180f31a2013-02-16 00:28:06 +000091def GetChromeSrcDir():
92 return "var/cache/chromeos-chrome/chrome-src/src"
93
94
asharif7aad14d2013-02-15 21:15:13 +000095@contextmanager
96def WorkingDirectory(new_dir):
97 old_dir = os.getcwd()
ashariff6e91952013-02-15 22:36:16 +000098 if old_dir != new_dir:
99 msg = "cd %s" % new_dir
100 logger.GetLogger().LogCmd(msg)
asharif7aad14d2013-02-15 21:15:13 +0000101 os.chdir(new_dir)
102 yield new_dir
ashariff6e91952013-02-15 22:36:16 +0000103 if old_dir != new_dir:
104 msg = "cd %s" % old_dir
105 logger.GetLogger().LogCmd(msg)
asharif7aad14d2013-02-15 21:15:13 +0000106 os.chdir(old_dir)