build_image.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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').split(':'):
  32. if recipe in data.getVar('PACKAGES').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. try:
  78. rd = parse_recipe(config, tinfoil, image, True)
  79. if not rd:
  80. # Error already shown
  81. return (1, None)
  82. if not bb.data.inherits_class('image', rd):
  83. raise TargetNotImageError()
  84. # Get the actual filename used and strip the .bb and full path
  85. target_basename = rd.getVar('FILE')
  86. target_basename = os.path.splitext(os.path.basename(target_basename))[0]
  87. config.set('SDK', 'target_basename', target_basename)
  88. config.write()
  89. appendfile = os.path.join(config.workspace_path, 'appends',
  90. '%s.bbappend' % target_basename)
  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. if packages or extra_append:
  106. bb.utils.mkdirhier(os.path.dirname(appendfile))
  107. with open(appendfile, 'w') as afile:
  108. if packages:
  109. # include packages from workspace recipes into the image
  110. afile.write('IMAGE_INSTALL_append = " %s"\n' % ' '.join(packages))
  111. if not task:
  112. logger.info('Building image %s with the following '
  113. 'additional packages: %s', image, ' '.join(packages))
  114. if extra_append:
  115. for line in extra_append:
  116. afile.write('%s\n' % line)
  117. if task in ['populate_sdk', 'populate_sdk_ext']:
  118. outputdir = rd.getVar('SDK_DEPLOY')
  119. else:
  120. outputdir = rd.getVar('DEPLOY_DIR_IMAGE')
  121. tmp_tinfoil = tinfoil
  122. tinfoil = None
  123. tmp_tinfoil.shutdown()
  124. options = ''
  125. if task:
  126. options += '-c %s' % task
  127. # run bitbake to build image (or specified task)
  128. try:
  129. exec_build_env_command(config.init_path, basepath,
  130. 'bitbake %s %s' % (options, image), watch=True)
  131. except ExecutionError as err:
  132. return (err.exitcode, None)
  133. finally:
  134. if os.path.isfile(appendfile):
  135. os.unlink(appendfile)
  136. finally:
  137. if tinfoil:
  138. tinfoil.shutdown()
  139. return (0, outputdir)
  140. def register_commands(subparsers, context):
  141. """Register devtool subcommands from the build-image plugin"""
  142. parser = subparsers.add_parser('build-image',
  143. help='Build image including workspace recipe packages',
  144. description='Builds an image, extending it to include '
  145. 'packages from recipes in the workspace',
  146. group='testbuild', order=-10)
  147. parser.add_argument('imagename', help='Image recipe to build', nargs='?')
  148. parser.add_argument('-p', '--add-packages', help='Instead of adding packages for the '
  149. 'entire workspace, specify packages to be added to the image '
  150. '(separate multiple packages by commas)',
  151. metavar='PACKAGES')
  152. parser.set_defaults(func=build_image)