Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | # Development tool - build-image plugin |
| 2 | # |
| 3 | # Copyright (C) 2015 Intel Corporation |
| 4 | # |
| 5 | # This program is free software; you can redistribute it and/or modify |
| 6 | # it under the terms of the GNU General Public License version 2 as |
| 7 | # published by the Free Software Foundation. |
| 8 | # |
| 9 | # This program is distributed in the hope that it will be useful, |
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | # GNU General Public License for more details. |
| 13 | # |
| 14 | # You should have received a copy of the GNU General Public License along |
| 15 | # with this program; if not, write to the Free Software Foundation, Inc., |
| 16 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 17 | |
| 18 | """Devtool plugin containing the build-image subcommand.""" |
| 19 | |
| 20 | import os |
| 21 | import logging |
| 22 | |
| 23 | from bb.process import ExecutionError |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 24 | from devtool import exec_build_env_command, setup_tinfoil, parse_recipe, DevtoolError |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 25 | |
| 26 | logger = logging.getLogger('devtool') |
| 27 | |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 28 | def _get_packages(tinfoil, workspace, config): |
| 29 | """Get list of packages from recipes in the workspace.""" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 30 | result = [] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 31 | for recipe in workspace: |
| 32 | data = parse_recipe(config, tinfoil, recipe, True) |
| 33 | if 'class-target' in data.getVar('OVERRIDES', True).split(':'): |
| 34 | if recipe in data.getVar('PACKAGES', True): |
| 35 | result.append(recipe) |
| 36 | else: |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 37 | logger.warning("Skipping recipe %s as it doesn't produce a " |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 38 | "package with the same name", recipe) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 39 | return result |
| 40 | |
| 41 | def build_image(args, config, basepath, workspace): |
| 42 | """Entry point for the devtool 'build-image' subcommand.""" |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 43 | |
| 44 | image = args.imagename |
| 45 | auto_image = False |
| 46 | if not image: |
| 47 | sdk_targets = config.get('SDK', 'sdk_targets', '').split() |
| 48 | if sdk_targets: |
| 49 | image = sdk_targets[0] |
| 50 | auto_image = True |
| 51 | if not image: |
| 52 | raise DevtoolError('Unable to determine image to build, please specify one') |
| 53 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 54 | appendfile = os.path.join(config.workspace_path, 'appends', |
| 55 | '%s.bbappend' % image) |
| 56 | |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 57 | # remove <image>.bbappend to make sure setup_tinfoil doesn't |
| 58 | # break because of it |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 59 | if os.path.isfile(appendfile): |
| 60 | os.unlink(appendfile) |
| 61 | |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 62 | tinfoil = setup_tinfoil(basepath=basepath) |
| 63 | rd = parse_recipe(config, tinfoil, image, True) |
| 64 | if not rd: |
| 65 | # Error already shown |
| 66 | return 1 |
| 67 | if not bb.data.inherits_class('image', rd): |
| 68 | if auto_image: |
| 69 | raise DevtoolError('Unable to determine image to build, please specify one') |
| 70 | else: |
| 71 | raise DevtoolError('Specified recipe %s is not an image recipe' % image) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 72 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 73 | try: |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 74 | if workspace: |
| 75 | packages = _get_packages(tinfoil, workspace, config) |
| 76 | if packages: |
| 77 | with open(appendfile, 'w') as afile: |
| 78 | # include packages from workspace recipes into the image |
| 79 | afile.write('IMAGE_INSTALL_append = " %s"\n' % ' '.join(packages)) |
| 80 | logger.info('Building image %s with the following ' |
| 81 | 'additional packages: %s', image, ' '.join(packages)) |
| 82 | else: |
| 83 | logger.warning('No packages to add, building image %s unmodified', image) |
| 84 | else: |
| 85 | logger.warning('No recipes in workspace, building image %s unmodified', image) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 86 | |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 87 | deploy_dir_image = tinfoil.config_data.getVar('DEPLOY_DIR_IMAGE', True) |
| 88 | |
| 89 | tinfoil.shutdown() |
| 90 | |
| 91 | # run bitbake to build image |
| 92 | try: |
| 93 | exec_build_env_command(config.init_path, basepath, |
| 94 | 'bitbake %s' % image, watch=True) |
| 95 | except ExecutionError as err: |
| 96 | return err.exitcode |
| 97 | finally: |
| 98 | if os.path.isfile(appendfile): |
| 99 | os.unlink(appendfile) |
| 100 | |
| 101 | logger.info('Successfully built %s. You can find output files in %s' |
| 102 | % (image, deploy_dir_image)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 103 | |
| 104 | def register_commands(subparsers, context): |
| 105 | """Register devtool subcommands from the build-image plugin""" |
| 106 | parser = subparsers.add_parser('build-image', |
| 107 | help='Build image including workspace recipe packages', |
| 108 | description='Builds an image, extending it to include ' |
| 109 | 'packages from recipes in the workspace') |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 110 | parser.add_argument('imagename', help='Image recipe to build', nargs='?') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 111 | parser.set_defaults(func=build_image) |