blob: e51d766474f3ed4fab000119ad6ff2091bcad338 [file] [log] [blame]
Patrick Williamsd8c66bc2016-06-20 12:57:21 -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
24from devtool import exec_build_env_command, setup_tinfoil, parse_recipe, DevtoolError
25
26logger = logging.getLogger('devtool')
27
28class TargetNotImageError(Exception):
29 pass
30
31def _get_packages(tinfoil, workspace, config):
32 """Get list of packages from recipes in the workspace."""
33 result = []
34 for recipe in workspace:
35 data = parse_recipe(config, tinfoil, recipe, True)
36 if 'class-target' in data.getVar('OVERRIDES', True).split(':'):
37 if recipe in data.getVar('PACKAGES', True):
38 result.append(recipe)
39 else:
40 logger.warning("Skipping recipe %s as it doesn't produce a "
41 "package with the same name", recipe)
42 return result
43
44def build_image(args, config, basepath, workspace):
45 """Entry point for the devtool 'build-image' subcommand."""
46
47 image = args.imagename
48 auto_image = False
49 if not image:
50 sdk_targets = config.get('SDK', 'sdk_targets', '').split()
51 if sdk_targets:
52 image = sdk_targets[0]
53 auto_image = True
54 if not image:
55 raise DevtoolError('Unable to determine image to build, please specify one')
56
57 try:
58 if args.add_packages:
59 add_packages = args.add_packages.split(',')
60 else:
61 add_packages = None
62 result, outputdir = build_image_task(config, basepath, workspace, image, add_packages)
63 except TargetNotImageError:
64 if auto_image:
65 raise DevtoolError('Unable to determine image to build, please specify one')
66 else:
67 raise DevtoolError('Specified recipe %s is not an image recipe' % image)
68
69 if result == 0:
70 logger.info('Successfully built %s. You can find output files in %s'
71 % (image, outputdir))
72 return result
73
74def build_image_task(config, basepath, workspace, image, add_packages=None, task=None, extra_append=None):
75 appendfile = os.path.join(config.workspace_path, 'appends',
76 '%s.bbappend' % image)
77
78 # remove <image>.bbappend to make sure setup_tinfoil doesn't
79 # break because of it
80 if os.path.isfile(appendfile):
81 os.unlink(appendfile)
82
83 tinfoil = setup_tinfoil(basepath=basepath)
84 rd = parse_recipe(config, tinfoil, image, True)
85 if not rd:
86 # Error already shown
87 return (1, None)
88 if not bb.data.inherits_class('image', rd):
89 raise TargetNotImageError()
90
91 outputdir = None
92 try:
93 if workspace or add_packages:
94 if add_packages:
95 packages = add_packages
96 else:
97 packages = _get_packages(tinfoil, workspace, config)
98 else:
99 packages = None
100 if not task:
101 if not packages and not add_packages and workspace:
102 logger.warning('No recipes in workspace, building image %s unmodified', image)
103 elif not packages:
104 logger.warning('No packages to add, building image %s unmodified', image)
105
106 if packages or extra_append:
107 bb.utils.mkdirhier(os.path.dirname(appendfile))
108 with open(appendfile, 'w') as afile:
109 if packages:
110 # include packages from workspace recipes into the image
111 afile.write('IMAGE_INSTALL_append = " %s"\n' % ' '.join(packages))
112 if not task:
113 logger.info('Building image %s with the following '
114 'additional packages: %s', image, ' '.join(packages))
115 if extra_append:
116 for line in extra_append:
117 afile.write('%s\n' % line)
118
119 if task in ['populate_sdk', 'populate_sdk_ext']:
120 outputdir = rd.getVar('SDK_DEPLOY', True)
121 else:
122 outputdir = rd.getVar('DEPLOY_DIR_IMAGE', True)
123
124 tinfoil.shutdown()
125
126 options = ''
127 if task:
128 options += '-c %s' % task
129
130 # run bitbake to build image (or specified task)
131 try:
132 exec_build_env_command(config.init_path, basepath,
133 'bitbake %s %s' % (options, image), watch=True)
134 except ExecutionError as err:
135 return (err.exitcode, None)
136 finally:
137 if os.path.isfile(appendfile):
138 os.unlink(appendfile)
139 return (0, outputdir)
140
141
142def register_commands(subparsers, context):
143 """Register devtool subcommands from the build-image plugin"""
144 parser = subparsers.add_parser('build-image',
145 help='Build image including workspace recipe packages',
146 description='Builds an image, extending it to include '
147 'packages from recipes in the workspace',
148 group='testbuild', order=-10)
149 parser.add_argument('imagename', help='Image recipe to build', nargs='?')
150 parser.add_argument('-p', '--add-packages', help='Instead of adding packages for the '
151 'entire workspace, specify packages to be added to the image '
152 '(separate multiple packages by commas)',
153 metavar='PACKAGES')
154 parser.set_defaults(func=build_image)