| OLD | NEW |
| 1 # Copyright (c) 2011 The Chromium OS Authors. All rights reserved. | 1 # 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 """Implementation of the 'build' chromite command.""" | 5 """Implementation of the 'build' chromite command.""" |
| 6 | 6 |
| 7 # Python imports | 7 # Python imports |
| 8 import optparse | 8 import optparse |
| 9 import os | 9 import os |
| 10 | 10 |
| 11 | 11 |
| 12 # Local imports | 12 # Local imports |
| 13 import chromite.lib.cros_build_lib as cros_lib | 13 import chromite.lib.cros_build_lib as cros_lib |
| 14 from chromite.shell import utils | 14 from chromite.shell import utils |
| 15 from chromite.shell import subcmd | 15 from chromite.shell import subcmd |
| 16 | 16 |
| 17 # TODO(sjg): I would prefer that these methods be inside the BuildCmd() class. | |
| 18 | 17 |
| 19 | 18 def _DoMakeChroot(chroot_config, clean_first): |
| 20 def _DoMakeChroot(cros_env, chroot_config, clean_first): | |
| 21 """Build the chroot, if needed. | 19 """Build the chroot, if needed. |
| 22 | 20 |
| 23 Args: | 21 Args: |
| 24 cros_env: Chromite environment to use for this command. | |
| 25 chroot_config: A SafeConfigParser representing the config for the chroot. | 22 chroot_config: A SafeConfigParser representing the config for the chroot. |
| 26 clean_first: Delete any old chroot first. | 23 clean_first: Delete any old chroot first. |
| 27 """ | 24 """ |
| 28 # Skip this whole command if things already exist. | 25 # Skip this whole command if things already exist. |
| 29 # TODO(dianders): Theoretically, calling make_chroot a second time is OK | 26 # TODO(dianders): Theoretically, calling make_chroot a second time is OK |
| 30 # and "fixes up" the chroot. ...but build_packages will do the fixups | 27 # and "fixes up" the chroot. ...but build_packages will do the fixups |
| 31 # anyway (I think), so this isn't that important. | 28 # anyway (I think), so this isn't that important. |
| 32 chroot_dir = utils.GetChrootAbsDir(chroot_config) | 29 chroot_dir = utils.GetChrootAbsDir(chroot_config) |
| 33 if (not clean_first) and utils.DoesChrootExist(chroot_config): | 30 if (not clean_first) and utils.DoesChrootExist(chroot_config): |
| 34 cros_env.GetOperation().Info('%s already exists, skipping make_chroot.' % | 31 cros_lib.Info('%s already exists, skipping make_chroot.' % chroot_dir) |
| 35 chroot_dir) | |
| 36 return | 32 return |
| 37 | 33 |
| 34 cros_lib.Info('MAKING THE CHROOT') |
| 35 |
| 38 # Put together command. | 36 # Put together command. |
| 39 arg_list = [ | 37 cmd_list = [ |
| 38 './make_chroot', |
| 40 '--chroot="%s"' % chroot_dir, | 39 '--chroot="%s"' % chroot_dir, |
| 41 chroot_config.get('CHROOT', 'make_chroot_flags'), | 40 chroot_config.get('CHROOT', 'make_chroot_flags'), |
| 42 ] | 41 ] |
| 43 if clean_first: | 42 if clean_first: |
| 44 arg_list.insert(0, '--replace') | 43 cmd_list.insert(1, '--replace') |
| 45 | 44 |
| 46 cros_env.RunScript('MAKING THE CHROOT', './make_chroot', arg_list) | 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) |
| 47 | 56 |
| 48 | 57 |
| 49 def _DoSetupBoard(cros_env, build_config, clean_first): | 58 def _DoSetupBoard(build_config, clean_first): |
| 50 """Setup the board, if needed. | 59 """Setup the board, if needed. |
| 51 | 60 |
| 52 This just runs the setup_board command with the proper args, if needed. | 61 This just runs the setup_board command with the proper args, if needed. |
| 53 | 62 |
| 54 Args: | 63 Args: |
| 55 cros_env: Chromite environment to use for this command. | |
| 56 build_config: A SafeConfigParser representing the build config. | 64 build_config: A SafeConfigParser representing the build config. |
| 57 clean_first: Delete any old board config first. | 65 clean_first: Delete any old board config first. |
| 58 """ | 66 """ |
| 59 # Skip this whole command if things already exist. | 67 # Skip this whole command if things already exist. |
| 60 board_dir = utils.GetBoardDir(build_config) | 68 board_dir = utils.GetBoardDir(build_config) |
| 61 if (not clean_first) and os.path.isdir(board_dir): | 69 if (not clean_first) and os.path.isdir(board_dir): |
| 62 cros_env.GetOperation().Info('%s already exists, skipping setup_board.' % | 70 cros_lib.Info('%s already exists, skipping setup_board.' % board_dir) |
| 63 board_dir) | |
| 64 return | 71 return |
| 65 | 72 |
| 73 cros_lib.Info('SETTING UP THE BOARD') |
| 74 |
| 66 # Put together command. | 75 # Put together command. |
| 67 cmd_list = [ | 76 cmd_list = [ |
| 77 './setup_board', |
| 68 '--board="%s"' % build_config.get('DEFAULT', 'target'), | 78 '--board="%s"' % build_config.get('DEFAULT', 'target'), |
| 69 build_config.get('BUILD', 'setup_board_flags'), | 79 build_config.get('BUILD', 'setup_board_flags'), |
| 70 ] | 80 ] |
| 71 if clean_first: | 81 if clean_first: |
| 72 arg_list.insert(0, '--force') | 82 cmd_list.insert(1, '--force') |
| 73 | 83 |
| 74 cros_env.RunScript('SETTING UP THE BOARD', './setup_board', arg_list) | 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) |
| 75 | 95 |
| 76 | 96 |
| 77 def _DoBuildPackages(cros_env, build_config): | 97 def _DoBuildPackages(build_config): |
| 78 """Build packages. | 98 """Build packages. |
| 79 | 99 |
| 80 This just runs the build_packages command with the proper args. | 100 This just runs the build_packages command with the proper args. |
| 81 | 101 |
| 82 Args: | 102 Args: |
| 83 cros_env: Chromite environment to use for this command. | |
| 84 build_config: A SafeConfigParser representing the build config. | 103 build_config: A SafeConfigParser representing the build config. |
| 85 """ | 104 """ |
| 105 cros_lib.Info('BUILDING PACKAGES') |
| 86 | 106 |
| 87 # Put together command. We're going to force the shell to do all of the | 107 # Put together command. We're going to force the shell to do all of the |
| 88 # splitting of arguments, since we're throwing all of the flags from the | 108 # splitting of arguments, since we're throwing all of the flags from the |
| 89 # config file in there. | 109 # config file in there. |
| 90 arg_list = ['--board="%s"' % build_config.get('DEFAULT', 'target'), | 110 cmd = '%s ./build_packages --board="%s" %s' % ( |
| 111 build_config.get('BUILD', 'build_packages_environ'), |
| 112 build_config.get('DEFAULT', 'target'), |
| 91 build_config.get('BUILD', 'build_packages_flags') | 113 build_config.get('BUILD', 'build_packages_flags') |
| 92 ] | 114 ) |
| 93 | 115 |
| 94 cros_env.RunScript('BUILDING PACKAGES', './build_packages', arg_list, | 116 # We'll put CWD as src/scripts when running the command. Since everyone |
| 95 shell_vars=build_config.get('BUILD', 'build_packages_environ')) | 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) |
| 96 | 122 |
| 97 | 123 |
| 98 def _DoBuildImage(cros_env, build_config): | 124 def _DoBuildImage(build_config): |
| 99 """Build an image. | 125 """Build an image. |
| 100 | 126 |
| 101 This just runs the build_image command with the proper args. | 127 This just runs the build_image command with the proper args. |
| 102 | 128 |
| 103 Args: | 129 Args: |
| 104 build_config: A SafeConfigParser representing the build config. | 130 build_config: A SafeConfigParser representing the build config. |
| 105 """ | 131 """ |
| 132 cros_lib.Info('BUILDING THE IMAGE') |
| 106 | 133 |
| 107 # Put together command. We're going to force the shell to do all of the | 134 # 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 | 135 # splitting of arguments, since we're throwing all of the flags from the |
| 109 # config file in there. | 136 # config file in there. |
| 110 arg_list = ['--board="%s"' % build_config.get('DEFAULT', 'target'), | 137 cmd = '%s ./build_image --board="%s" %s' % ( |
| 138 build_config.get('IMAGE', 'build_image_environ'), |
| 139 build_config.get('DEFAULT', 'target'), |
| 111 build_config.get('IMAGE', 'build_image_flags') | 140 build_config.get('IMAGE', 'build_image_flags') |
| 112 ] | 141 ) |
| 113 | 142 |
| 114 cros_env.RunScript('BUILDING THE IMAGE', './build_image', arg_list, | 143 # We'll put CWD as src/scripts when running the command. Since everyone |
| 115 shell_vars=build_config.get('IMAGE', 'build_image_environ')) | 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) |
| 116 | 149 |
| 117 | 150 |
| 118 def _DoImagePostProcessing(cros_env, build_config): | 151 def _DoImagePostProcessing(build_config): |
| 119 """Do post processing steps after the build image runs. | 152 """Do post processing steps after the build image runs. |
| 120 | 153 |
| 121 Args: | 154 Args: |
| 122 build_config: A SafeConfigParser representing the build config. | 155 build_config: A SafeConfigParser representing the build config. |
| 123 """ | 156 """ |
| 157 # We'll put CWD as src/scripts when running the commands, since many of these |
| 158 # legacy commands live in src/scripts. |
| 159 # TODO(dianders): Don't set CWD once crosutils are properly installed. |
| 160 cwd = os.path.join(utils.SRCROOT_PATH, 'src', 'scripts') |
| 161 |
| 124 # The user specifies a list of "steps" in this space-separated variable. | 162 # The user specifies a list of "steps" in this space-separated variable. |
| 125 # We'll use each step name to construct other variable names to look for | 163 # We'll use each step name to construct other variable names to look for |
| 126 # the actual commands. | 164 # the actual commands. |
| 127 steps = build_config.get('IMAGE', 'post_process_steps') | 165 steps = build_config.get('IMAGE', 'post_process_steps') |
| 128 for step_name in steps.split(): | 166 for step_name in steps.split(): |
| 167 cros_lib.Info('IMAGING POST-PROCESS: %s' % step_name) |
| 168 |
| 129 # Get the name of the variable that the user stored the cmd in. | 169 # Get the name of the variable that the user stored the cmd in. |
| 130 cmd_var_name = 'post_process_%s_cmd' % step_name | 170 cmd_var_name = 'post_process_%s_cmd' % step_name |
| 131 | 171 |
| 132 # Run the command. Exceptions will cause the program to exit. | 172 # Run the command. Exceptions will cause the program to exit. |
| 133 cmd = build_config.get('IMAGE', cmd_var_name) | 173 cmd = build_config.get('IMAGE', cmd_var_name) |
| 134 cros_env.RunScript('IMAGING POST-PROCESS: %s' % step_name, '', [cmd]) | 174 cros_lib.RunCommand(cmd, shell=True, cwd=cwd, ignore_sigint=True) |
| 135 | 175 |
| 136 | 176 |
| 137 class BuildCmd(subcmd.ChromiteCmd): | 177 class BuildCmd(subcmd.ChromiteCmd): |
| 138 """Build the chroot (if needed), the packages for a target, and the image.""" | 178 """Build the chroot (if needed), the packages for a target, and the image.""" |
| 139 | 179 |
| 140 def Run(self, raw_argv, chroot_config=None, | 180 def Run(self, raw_argv, chroot_config=None, |
| 141 loaded_config=False, build_config=None): | 181 loaded_config=False, build_config=None): |
| 142 """Run the command. | 182 """Run the command. |
| 143 | 183 |
| 144 Args: | 184 Args: |
| (...skipping 20 matching lines...) Expand all Loading... |
| 165 argv, build_config = utils.GetBuildConfigFromArgs(argv) | 205 argv, build_config = utils.GetBuildConfigFromArgs(argv) |
| 166 if argv: | 206 if argv: |
| 167 cros_lib.Die('Unknown arguments: %s' % ' '.join(argv)) | 207 cros_lib.Die('Unknown arguments: %s' % ' '.join(argv)) |
| 168 | 208 |
| 169 if not cros_lib.IsInsideChroot(): | 209 if not cros_lib.IsInsideChroot(): |
| 170 # Note: we only want to clean the chroot if they do --clean and have the | 210 # Note: we only want to clean the chroot if they do --clean and have the |
| 171 # host target. If they do --clean and have a board target, it means | 211 # host target. If they do --clean and have a board target, it means |
| 172 # that they just want to clean the board... | 212 # that they just want to clean the board... |
| 173 want_clean_chroot = options.clean and build_config is None | 213 want_clean_chroot = options.clean and build_config is None |
| 174 | 214 |
| 175 _DoMakeChroot(self.cros_env, chroot_config, want_clean_chroot) | 215 _DoMakeChroot(chroot_config, want_clean_chroot) |
| 176 | 216 |
| 177 if build_config is not None: | 217 if build_config is not None: |
| 178 self._oper.Info('ENTERING THE CHROOT') | |
| 179 utils.EnterChroot(chroot_config, (self, 'Run'), raw_argv, | 218 utils.EnterChroot(chroot_config, (self, 'Run'), raw_argv, |
| 180 build_config=build_config, loaded_config=True) | 219 build_config=build_config, loaded_config=True) |
| 181 | 220 |
| 182 self._oper.Info('Done building.') | 221 cros_lib.Info('Done building.') |
| 183 else: | 222 else: |
| 184 if build_config is None: | 223 if build_config is None: |
| 185 cros_lib.Die("You can't build the host chroot from within the chroot.") | 224 cros_lib.Die("You can't build the host chroot from within the chroot.") |
| 186 | 225 |
| 187 _DoSetupBoard(self.cros_env, build_config, options.clean) | 226 _DoSetupBoard(build_config, options.clean) |
| 188 _DoBuildPackages(self.cros_env, build_config) | 227 _DoBuildPackages(build_config) |
| 189 _DoBuildImage(self.cros_env, build_config) | 228 _DoBuildImage(build_config) |
| 190 _DoImagePostProcessing(self.cros_env, build_config) | 229 _DoImagePostProcessing(build_config) |
| OLD | NEW |