blob: 9f96c631b7fa7ada18a9518b6a51e05b7b13a5cb [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"""Script to build the ChromeOS toolchain.
6
7This script sets up the toolchain if you give it the gcctools directory.
8"""
9
10__author__ = "asharif@google.com (Ahmad Sharif)"
11
asharif80c6e552013-02-15 04:35:40 +000012import getpass
bjanakiraman7f4a4852013-02-15 04:35:28 +000013import optparse
asharif17621302013-02-15 04:46:35 +000014import os
bjanakiraman7f4a4852013-02-15 04:35:28 +000015import sys
asharifc97199a2013-02-15 22:48:45 +000016import tempfile
asharif252df0f2013-02-15 04:46:28 +000017import tc_enter_chroot
raymes01959ae2013-02-15 04:50:07 +000018from utils import command_executer
bjanakiraman7f4a4852013-02-15 04:35:28 +000019from utils import utils
20
asharif5a9bb462013-02-15 04:50:57 +000021
asharifc97199a2013-02-15 22:48:45 +000022class 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
asharif77bd80d2013-02-15 22:49:32 +000029 self._ctarget = utils.GetCtargetFromBoard(self._board,
30 self._chromeos_root)
asharifc97199a2013-02-15 22:48:45 +000031 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
49 utils.ExecuteCommandInChroot(self._chromeos_root, command)
50
51 def Build(self):
52 self.RunSetupBoardIfNecessary()
53
54 try:
55 self.MoveMaskFile()
56 self.SwitchToBFD()
57 self.MountSources()
58 if not self._incremental:
59 self.RemoveCompiledFile()
60 self.BuildTool()
61 finally:
62 self.UnMoveMaskFile()
63 self.SwitchToOriginalLD()
64
65 def RemoveCompiledFile(self):
66 compiled_file = os.path.join(self._chromeos_root,
67 "chroot",
68 "var/tmp/portage/cross-%s" % self._ctarget,
69 "%s-9999" % self._name,
70 ".compiled")
71 command = "rm -rf %s" % compiled_file
72 self._ce.RunCommand(command)
73
74 def MountSources(self):
75 mount_points = []
76 mounted_source_path = os.path.join(self._chromeos_root,
77 "chroot",
78 self._chroot_source_path)
79 src_mp = tc_enter_chroot.MountPoint(
80 self._source_path,
81 mounted_source_path,
82 getpass.getuser(),
83 "ro")
84 mount_points.append(src_mp)
85
86 build_suffix = "build-%s" % self._ctarget
87 build_dir = "%s-%s" % (self._source_path, build_suffix)
88
89 if not self._incremental and os.path.exists(build_dir):
90 command = "rm -rf %s/*" % build_dir
91 self._ce.RunCommand(command)
92
93 # Create a -build directory for the objects.
94 command = "mkdir -p %s" % build_dir
95 self._ce.RunCommand(command)
96
97 mounted_build_dir = os.path.join(
98 self._chromeos_root, "chroot", "%s-%s" %
99 (self._chroot_source_path, build_suffix))
100 build_mp = tc_enter_chroot.MountPoint(
101 build_dir,
102 mounted_build_dir,
103 getpass.getuser())
104 mount_points.append(build_mp)
105
106 mount_statuses = [mp.DoMount() == 0 for mp in mount_points]
107
108 if not all(mount_statuses):
109 mounted = [mp for mp, status in zip(mount_points, mount_statuses) if status]
110 unmount_statuses = [mp.UnMount() == 0 for mp in mounted]
111 assert all(unmount_statuses), "Could not unmount all mount points!"
112
113 def BuildTool(self):
114 env = self._build_env
115 features = "nostrip userpriv userfetch -sandbox noclean"
116 env["FEATURES"] = features
117
118 if self._incremental:
119 env["FEATURES"] += " keepwork"
120
121 env["USE"] = "multislot mounted_%s" % self._name
122 env["%s_SOURCE_PATH" % self._name.upper()] = (
123 os.path.join("/", self._chroot_source_path))
124 env["ACCEPT_KEYWORDS"] = "~*"
125 env_string = " ".join(["%s=\"%s\"" % var for var in env.items()])
126 command = "emerge =cross-%s/%s-9999" % (self._ctarget, self._name)
127 full_command = "sudo %s %s" % (env_string, command)
128 utils.ExecuteCommandInChroot(self._chromeos_root, full_command)
129
130 def SwitchToBFD(self):
131 command = "sudo binutils-config %s-2.21" % self._ctarget
132 utils.ExecuteCommandInChroot(self._chromeos_root, command)
133
134 def SwitchToOriginalLD(self):
135 pass
136
137 def MoveMaskFile(self):
138 self._new_mask_file = None
139 if os.path.isfile(self._mask_file):
140 self._new_mask_file = tempfile.mktemp()
141 command = "sudo mv %s %s" % (self._mask_file, self._new_mask_file)
142 self._ce.RunCommand(command)
143
144 def UnMoveMaskFile(self):
145 if self._new_mask_file:
146 command = "sudo mv %s %s" % (self._new_mask_file, self._mask_file)
147 self._ce.RunCommand(command)
bjanakiraman7f4a4852013-02-15 04:35:28 +0000148
bjanakiraman7f4a4852013-02-15 04:35:28 +0000149
asharif0d3535a2013-02-15 04:50:33 +0000150def Main(argv):
asharif19c73dd2013-02-15 04:35:37 +0000151 """The main function."""
asharif5a9bb462013-02-15 04:50:57 +0000152 # Common initializations
asharif19c73dd2013-02-15 04:35:37 +0000153 parser = optparse.OptionParser()
asharifc97199a2013-02-15 22:48:45 +0000154 parser.add_option("-c",
155 "--chromeos_root",
156 dest="chromeos_root",
157 help=("ChromeOS root checkout directory"
158 " uses ../.. if none given."))
159 parser.add_option("-g",
160 "--gcc_dir",
161 dest="gcc_dir",
162 help="The directory where gcc resides.")
163 parser.add_option("-b",
164 "--board",
165 dest="board",
166 default="x86-agz",
167 help="The target board.")
168 parser.add_option("-n",
169 "--noincremental",
170 dest="noincremental",
171 default=False,
asharifd751e252013-02-15 04:35:52 +0000172 action="store_true",
asharifc97199a2013-02-15 22:48:45 +0000173 help="Use FEATURES=keepwork to do incremental builds.")
174 parser.add_option("-d",
175 "--debug",
176 dest="debug",
177 default=False,
asharifd751e252013-02-15 04:35:52 +0000178 action="store_true",
asharifc97199a2013-02-15 22:48:45 +0000179 help="Build a compiler with -g3 -O0.")
bjanakiraman7f4a4852013-02-15 04:35:28 +0000180
asharifc97199a2013-02-15 22:48:45 +0000181 options, _ = parser.parse_args(argv)
asharif17621302013-02-15 04:46:35 +0000182
asharifc97199a2013-02-15 22:48:45 +0000183 chromeos_root = utils.CanonicalizePath(options.chromeos_root)
184 gcc_dir = utils.CanonicalizePath(options.gcc_dir)
185 build_env = {}
186 if options.debug:
187 debug_flags = "-g3 -O0"
188 build_env["CFLAGS"] = debug_flags
189 build_env["CXXFLAGS"] = debug_flags
bjanakiraman7f4a4852013-02-15 04:35:28 +0000190
asharifc97199a2013-02-15 22:48:45 +0000191 try:
192 for board in options.board.split(","):
193 tp = ToolchainPart("gcc", gcc_dir, chromeos_root, board,
194 not options.noincremental, build_env)
195 return tp.Build()
196 finally:
197 print "Exiting..."
198 return 0
asharif0d3535a2013-02-15 04:50:33 +0000199
asharif19c73dd2013-02-15 04:35:37 +0000200
201if __name__ == "__main__":
asharif2198c512013-02-15 09:21:35 +0000202 retval = Main(sys.argv)
203 sys.exit(retval)