summaryrefslogtreecommitdiffstats
path: root/scripts/lib/devtool/build_image.py
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/lib/devtool/build_image.py')
-rw-r--r--scripts/lib/devtool/build_image.py119
1 files changed, 119 insertions, 0 deletions
diff --git a/scripts/lib/devtool/build_image.py b/scripts/lib/devtool/build_image.py
new file mode 100644
index 0000000000..ff764fa833
--- /dev/null
+++ b/scripts/lib/devtool/build_image.py
@@ -0,0 +1,119 @@
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
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
28def _get_packages(tinfoil, workspace, config):
29 """Get list of packages from recipes in the workspace."""
30 result = []
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:
37 logger.warning("Skipping recipe %s as it doesn't produce a "
38 "package with the same name", recipe)
39 return result
40
41def build_image(args, config, basepath, workspace):
42 """Entry point for the devtool 'build-image' subcommand."""
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
54 appendfile = os.path.join(config.workspace_path, 'appends',
55 '%s.bbappend' % image)
56
57 # remove <image>.bbappend to make sure setup_tinfoil doesn't
58 # break because of it
59 if os.path.isfile(appendfile):
60 os.unlink(appendfile)
61
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)
72
73 try:
74 if workspace or args.add_packages:
75 if args.add_packages:
76 packages = args.add_packages.split(',')
77 else:
78 packages = _get_packages(tinfoil, workspace, config)
79 if packages:
80 with open(appendfile, 'w') as afile:
81 # include packages from workspace recipes into the image
82 afile.write('IMAGE_INSTALL_append = " %s"\n' % ' '.join(packages))
83 logger.info('Building image %s with the following '
84 'additional packages: %s', image, ' '.join(packages))
85 else:
86 logger.warning('No packages to add, building image %s unmodified', image)
87 else:
88 logger.warning('No recipes in workspace, building image %s unmodified', image)
89
90 deploy_dir_image = tinfoil.config_data.getVar('DEPLOY_DIR_IMAGE', True)
91
92 tinfoil.shutdown()
93
94 # run bitbake to build image
95 try:
96 exec_build_env_command(config.init_path, basepath,
97 'bitbake %s' % image, watch=True)
98 except ExecutionError as err:
99 return err.exitcode
100 finally:
101 if os.path.isfile(appendfile):
102 os.unlink(appendfile)
103
104 logger.info('Successfully built %s. You can find output files in %s'
105 % (image, deploy_dir_image))
106
107def register_commands(subparsers, context):
108 """Register devtool subcommands from the build-image plugin"""
109 parser = subparsers.add_parser('build-image',
110 help='Build image including workspace recipe packages',
111 description='Builds an image, extending it to include '
112 'packages from recipes in the workspace',
113 group='testbuild', order=-10)
114 parser.add_argument('imagename', help='Image recipe to build', nargs='?')
115 parser.add_argument('-p', '--add-packages', help='Instead of adding packages for the '
116 'entire workspace, specify packages to be added to the image '
117 '(separate multiple packages by commas)',
118 metavar='PACKAGES')
119 parser.set_defaults(func=build_image)