build_image.py 6.6 KB

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