blob: 5ae9512e9fdb645eb185d86c9f0d9adfa89c79b0 [file] [log] [blame]
Doug Andersona9b10902011-02-01 17:54:31 -08001# Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Implementation of the 'build' chromite command."""
6
7# Python imports
8import optparse
9import os
10
11
12# Local imports
13import chromite.lib.cros_build_lib as cros_lib
14from chromite.shell import utils
15from chromite.shell import subcmd
16
17
18def _DoMakeChroot(chroot_config, clean_first):
19 """Build the chroot, if needed.
20
21 Args:
22 chroot_config: A SafeConfigParser representing the config for the chroot.
23 clean_first: Delete any old chroot first.
24 """
25 # Skip this whole command if things already exist.
26 # TODO(dianders): Theoretically, calling make_chroot a second time is OK
27 # and "fixes up" the chroot. ...but build_packages will do the fixups
28 # anyway (I think), so this isn't that important.
29 chroot_dir = utils.GetChrootAbsDir(chroot_config)
30 if (not clean_first) and utils.DoesChrootExist(chroot_config):
31 cros_lib.Info('%s already exists, skipping make_chroot.' % chroot_dir)
32 return
33
34 cros_lib.Info('MAKING THE CHROOT')
35
36 # Put together command.
37 cmd_list = [
38 './make_chroot',
39 '--chroot="%s"' % chroot_dir,
40 chroot_config.get('CHROOT', 'make_chroot_flags'),
41 ]
42 if clean_first:
43 cmd_list.insert(1, '--replace')
44
45 # We're going convert to a string and force the shell to do all of the
46 # splitting of arguments, since we're throwing all of the flags from the
47 # config file in there.
48 cmd = ' '.join(cmd_list)
49
50 # We'll put CWD as src/scripts when running the command. Since everyone
51 # running by hand has their cwd there, it is probably the safest.
52 cwd = os.path.join(utils.SRCROOT_PATH, 'src', 'scripts')
53
54 # Run it. Exceptions will cause the program to exit.
55 cros_lib.RunCommand(cmd, shell=True, cwd=cwd, ignore_sigint=True)
56
57
58def _DoSetupBoard(build_config, clean_first):
59 """Setup the board, if needed.
60
61 This just runs the setup_board command with the proper args, if needed.
62
63 Args:
64 build_config: A SafeConfigParser representing the build config.
65 clean_first: Delete any old board config first.
66 """
67 # Skip this whole command if things already exist.
68 board_dir = utils.GetBoardDir(build_config)
69 if (not clean_first) and os.path.isdir(board_dir):
70 cros_lib.Info('%s already exists, skipping setup_board.' % board_dir)
71 return
72
73 cros_lib.Info('SETTING UP THE BOARD')
74
75 # Put together command.
76 cmd_list = [
77 './setup_board',
78 '--board="%s"' % build_config.get('BUILD', 'target'),
79 build_config.get('BUILD', 'setup_board_flags'),
80 ]
81 if clean_first:
82 cmd_list.insert(1, '--force')
83
84 # We're going convert to a string and force the shell to do all of the
85 # splitting of arguments, since we're throwing all of the flags from the
86 # config file in there.
87 cmd = ' '.join(cmd_list)
88
89 # We'll put CWD as src/scripts when running the command. Since everyone
90 # running by hand has their cwd there, it is probably the safest.
91 cwd = os.path.join(utils.SRCROOT_PATH, 'src', 'scripts')
92
93 # Run it. Exceptions will cause the program to exit.
94 cros_lib.RunCommand(cmd, shell=True, cwd=cwd, ignore_sigint=True)
95
96
97def _DoBuildPackages(build_config):
98 """Build packages.
99
100 This just runs the build_packages command with the proper args.
101
102 Args:
103 build_config: A SafeConfigParser representing the build config.
104 """
105 cros_lib.Info('BUILDING PACKAGES')
106
107 # Put together command. We're going to force the shell to do all of the
108 # splitting of arguments, since we're throwing all of the flags from the
109 # config file in there.
Doug Andersonf8ac9442011-02-07 09:11:36 -0800110 cmd = '%s ./build_packages --board="%s" %s' % (
111 build_config.get('BUILD', 'build_packages_environ'),
Doug Andersona9b10902011-02-01 17:54:31 -0800112 build_config.get('BUILD', 'target'),
113 build_config.get('BUILD', 'build_packages_flags')
114 )
115
116 # We'll put CWD as src/scripts when running the command. Since everyone
117 # running by hand has their cwd there, it is probably the safest.
118 cwd = os.path.join(utils.SRCROOT_PATH, 'src', 'scripts')
119
120 # Run it. Exceptions will cause the program to exit.
121 cros_lib.RunCommand(cmd, shell=True, cwd=cwd, ignore_sigint=True)
122
123
124def _DoBuildImage(build_config):
125 """Build an image.
126
127 This just runs the build_image command with the proper args.
128
129 Args:
130 build_config: A SafeConfigParser representing the build config.
131 """
132 cros_lib.Info('BUILDING THE IMAGE')
133
134 # Put together command. We're going to force the shell to do all of the
135 # splitting of arguments, since we're throwing all of the flags from the
136 # config file in there.
Doug Andersonf8ac9442011-02-07 09:11:36 -0800137 cmd = '%s ./build_image --board="%s" %s' % (
138 build_config.get('IMAGE', 'build_image_environ'),
Doug Andersona9b10902011-02-01 17:54:31 -0800139 build_config.get('BUILD', 'target'),
140 build_config.get('IMAGE', 'build_image_flags')
141 )
142
143 # We'll put CWD as src/scripts when running the command. Since everyone
144 # running by hand has their cwd there, it is probably the safest.
145 cwd = os.path.join(utils.SRCROOT_PATH, 'src', 'scripts')
146
147 # Run it. Exceptions will cause the program to exit.
148 cros_lib.RunCommand(cmd, shell=True, cwd=cwd, ignore_sigint=True)
149
150
151class BuildCmd(subcmd.ChromiteCmd):
152 """Build the chroot (if needed), the packages for a target, and the image."""
153
154 def Run(self, raw_argv, chroot_config=None,
155 loaded_config=False, build_config=None):
156 """Run the command.
157
158 Args:
159 raw_argv: Command line arguments, including this command's name, but not
160 the chromite command name or chromite options.
161 chroot_config: A SafeConfigParser for the chroot config; or None chromite
162 was called from within the chroot.
163 loaded_config: If True, we've already loaded the config.
164 build_config: None when called normally, but contains the SafeConfigParser
165 for the build config if we call ourselves with _DoEnterChroot(). Note
166 that even when called through _DoEnterChroot(), could still be None
167 if user chose 'HOST' as the target.
168 """
169 # Parse options for command...
170 usage_str = ('usage: %%prog [chromite_options] %s [options] [target]' %
171 raw_argv[0])
172 parser = optparse.OptionParser(usage=usage_str)
173 parser.add_option('--clean', default=False, action='store_true',
174 help='Clean before building.')
175 (options, argv) = parser.parse_args(raw_argv[1:])
176
177 # Load the build config if needed...
178 if not loaded_config:
179 argv, build_config = utils.GetBuildConfigFromArgs(argv)
180 if argv:
181 cros_lib.Die('Unknown arguments: %s' % ' '.join(argv))
182
183 if not cros_lib.IsInsideChroot():
184 # Note: we only want to clean the chroot if they do --clean and have the
185 # host target. If they do --clean and have a board target, it means
186 # that they just want to clean the board...
187 want_clean_chroot = options.clean and build_config is None
188
189 _DoMakeChroot(chroot_config, want_clean_chroot)
190
191 if build_config is not None:
192 utils.EnterChroot(chroot_config, (self, 'Run'), raw_argv,
193 build_config=build_config, loaded_config=True)
194
195 cros_lib.Info('Done building.')
196 else:
197 if build_config is None:
198 cros_lib.Die("You can't build the host chroot from within the chroot.")
199
200 _DoSetupBoard(build_config, options.clean)
201 _DoBuildPackages(build_config)
202 _DoBuildImage(build_config)