blob: e53239dd285833f10273df364cfb4e36752a60d8 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001# 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
20import os
21import logging
22
23from bb.process import ExecutionError
Patrick Williamsf1e5d692016-03-30 15:21:19 -050024from devtool import exec_build_env_command, setup_tinfoil, parse_recipe, DevtoolError
Patrick Williamsc124f4f2015-09-15 14:41:29 -050025
26logger = logging.getLogger('devtool')
27
Patrick Williamsf1e5d692016-03-30 15:21:19 -050028def _get_packages(tinfoil, workspace, config):
29 """Get list of packages from recipes in the workspace."""
Patrick Williamsc124f4f2015-09-15 14:41:29 -050030 result = []
Patrick Williamsc124f4f2015-09-15 14:41:29 -050031 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 Williamsf1e5d692016-03-30 15:21:19 -050037 logger.warning("Skipping recipe %s as it doesn't produce a "
Patrick Williamsc124f4f2015-09-15 14:41:29 -050038 "package with the same name", recipe)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050039 return result
40
41def build_image(args, config, basepath, workspace):
42 """Entry point for the devtool 'build-image' subcommand."""
Patrick Williamsf1e5d692016-03-30 15:21:19 -050043
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 Williamsc124f4f2015-09-15 14:41:29 -050054 appendfile = os.path.join(config.workspace_path, 'appends',
55 '%s.bbappend' % image)
56
Patrick Williamsf1e5d692016-03-30 15:21:19 -050057 # remove <image>.bbappend to make sure setup_tinfoil doesn't
58 # break because of it
Patrick Williamsc124f4f2015-09-15 14:41:29 -050059 if os.path.isfile(appendfile):
60 os.unlink(appendfile)
61
Patrick Williamsf1e5d692016-03-30 15:21:19 -050062 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 Williamsc124f4f2015-09-15 14:41:29 -050072
Patrick Williamsc124f4f2015-09-15 14:41:29 -050073 try:
Patrick Williamsf1e5d692016-03-30 15:21:19 -050074 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 Williamsc124f4f2015-09-15 14:41:29 -050086
Patrick Williamsf1e5d692016-03-30 15:21:19 -050087 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 Williamsc124f4f2015-09-15 14:41:29 -0500103
104def 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 Williamsf1e5d692016-03-30 15:21:19 -0500110 parser.add_argument('imagename', help='Image recipe to build', nargs='?')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500111 parser.set_defaults(func=build_image)