devtool-source.bbclass 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. #
  2. # Copyright OpenEmbedded Contributors
  3. #
  4. # SPDX-License-Identifier: MIT
  5. #
  6. # Development tool - source extraction helper class
  7. #
  8. # NOTE: this class is intended for use by devtool and should not be
  9. # inherited manually.
  10. #
  11. # Copyright (C) 2014-2017 Intel Corporation
  12. #
  13. # This program is free software; you can redistribute it and/or modify
  14. # it under the terms of the GNU General Public License version 2 as
  15. # published by the Free Software Foundation.
  16. #
  17. # This program is distributed in the hope that it will be useful,
  18. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. # GNU General Public License for more details.
  21. #
  22. # You should have received a copy of the GNU General Public License along
  23. # with this program; if not, write to the Free Software Foundation, Inc.,
  24. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  25. DEVTOOL_TEMPDIR ?= ""
  26. DEVTOOL_PATCH_SRCDIR = "${DEVTOOL_TEMPDIR}/patchworkdir"
  27. python() {
  28. tempdir = d.getVar('DEVTOOL_TEMPDIR')
  29. if not tempdir:
  30. bb.fatal('devtool-source class is for internal use by devtool only')
  31. # Make a subdir so we guard against WORKDIR==S
  32. workdir = os.path.join(tempdir, 'workdir')
  33. d.setVar('WORKDIR', workdir)
  34. if not d.getVar('S').startswith(workdir):
  35. # Usually a shared workdir recipe (kernel, gcc)
  36. # Try to set a reasonable default
  37. if bb.data.inherits_class('kernel', d):
  38. d.setVar('S', '${WORKDIR}/source')
  39. else:
  40. d.setVar('S', '${WORKDIR}/%s' % os.path.basename(d.getVar('S')))
  41. if bb.data.inherits_class('kernel', d):
  42. # We don't want to move the source to STAGING_KERNEL_DIR here
  43. d.setVar('STAGING_KERNEL_DIR', '${S}')
  44. d.setVar('STAMPS_DIR', os.path.join(tempdir, 'stamps'))
  45. d.setVar('T', os.path.join(tempdir, 'temp'))
  46. # Hook in pre/postfuncs
  47. is_kernel_yocto = bb.data.inherits_class('kernel-yocto', d)
  48. if is_kernel_yocto:
  49. unpacktask = 'do_kernel_checkout'
  50. d.appendVarFlag('do_configure', 'postfuncs', ' devtool_post_configure')
  51. else:
  52. unpacktask = 'do_unpack'
  53. d.appendVarFlag(unpacktask, 'postfuncs', ' devtool_post_unpack')
  54. d.prependVarFlag('do_patch', 'prefuncs', ' devtool_pre_patch')
  55. d.appendVarFlag('do_patch', 'postfuncs', ' devtool_post_patch')
  56. # NOTE: in order for the patch stuff to be fully functional,
  57. # PATCHTOOL and PATCH_COMMIT_FUNCTIONS need to be set; we can't
  58. # do that here because we can't guarantee the order of the anonymous
  59. # functions, so it gets done in the bbappend we create.
  60. }
  61. python devtool_post_unpack() {
  62. import oe.recipeutils
  63. import shutil
  64. sys.path.insert(0, os.path.join(d.getVar('COREBASE'), 'scripts', 'lib'))
  65. import scriptutils
  66. from devtool import setup_git_repo
  67. tempdir = d.getVar('DEVTOOL_TEMPDIR')
  68. workdir = d.getVar('WORKDIR')
  69. srcsubdir = d.getVar('S')
  70. def _move_file(src, dst):
  71. """Move a file. Creates all the directory components of destination path."""
  72. dst_d = os.path.dirname(dst)
  73. if dst_d:
  74. bb.utils.mkdirhier(dst_d)
  75. shutil.move(src, dst)
  76. def _ls_tree(directory):
  77. """Recursive listing of files in a directory"""
  78. ret = []
  79. for root, dirs, files in os.walk(directory):
  80. ret.extend([os.path.relpath(os.path.join(root, fname), directory) for
  81. fname in files])
  82. return ret
  83. is_kernel_yocto = bb.data.inherits_class('kernel-yocto', d)
  84. # Move local source files into separate subdir
  85. recipe_patches = [os.path.basename(patch) for patch in
  86. oe.recipeutils.get_recipe_patches(d)]
  87. local_files = oe.recipeutils.get_recipe_local_files(d)
  88. if is_kernel_yocto:
  89. for key in [f for f in local_files if f.endswith('scc')]:
  90. with open(local_files[key], 'r') as sccfile:
  91. for l in sccfile:
  92. line = l.split()
  93. if line and line[0] in ('kconf', 'patch'):
  94. cfg = os.path.join(os.path.dirname(local_files[key]), line[-1])
  95. if cfg not in local_files.values():
  96. local_files[line[-1]] = cfg
  97. shutil.copy2(cfg, workdir)
  98. # Ignore local files with subdir={BP}
  99. srcabspath = os.path.abspath(srcsubdir)
  100. local_files = [fname for fname in local_files if
  101. os.path.exists(os.path.join(workdir, fname)) and
  102. (srcabspath == workdir or not
  103. os.path.join(workdir, fname).startswith(srcabspath +
  104. os.sep))]
  105. if local_files:
  106. for fname in local_files:
  107. _move_file(os.path.join(workdir, fname),
  108. os.path.join(tempdir, 'oe-local-files', fname))
  109. with open(os.path.join(tempdir, 'oe-local-files', '.gitignore'),
  110. 'w') as f:
  111. f.write('# Ignore local files, by default. Remove this file '
  112. 'if you want to commit the directory to Git\n*\n')
  113. if srcsubdir == workdir:
  114. # Find non-patch non-local sources that were "unpacked" to srctree
  115. # directory
  116. src_files = [fname for fname in _ls_tree(workdir) if
  117. os.path.basename(fname) not in recipe_patches]
  118. srcsubdir = d.getVar('DEVTOOL_PATCH_SRCDIR')
  119. # Move source files to S
  120. for path in src_files:
  121. _move_file(os.path.join(workdir, path),
  122. os.path.join(srcsubdir, path))
  123. elif os.path.dirname(srcsubdir) != workdir:
  124. # Handle if S is set to a subdirectory of the source
  125. srcsubdir = os.path.join(workdir, os.path.relpath(srcsubdir, workdir).split(os.sep)[0])
  126. scriptutils.git_convert_standalone_clone(srcsubdir)
  127. # Make sure that srcsubdir exists
  128. bb.utils.mkdirhier(srcsubdir)
  129. if not os.listdir(srcsubdir):
  130. bb.warn("No source unpacked to S - either the %s recipe "
  131. "doesn't use any source or the correct source "
  132. "directory could not be determined" % d.getVar('PN'))
  133. devbranch = d.getVar('DEVTOOL_DEVBRANCH')
  134. setup_git_repo(srcsubdir, d.getVar('PV'), devbranch, d=d)
  135. (stdout, _) = bb.process.run('git rev-parse HEAD', cwd=srcsubdir)
  136. initial_rev = stdout.rstrip()
  137. with open(os.path.join(tempdir, 'initial_rev'), 'w') as f:
  138. f.write(initial_rev)
  139. with open(os.path.join(tempdir, 'srcsubdir'), 'w') as f:
  140. f.write(srcsubdir)
  141. }
  142. python devtool_pre_patch() {
  143. if d.getVar('S') == d.getVar('WORKDIR'):
  144. d.setVar('S', '${DEVTOOL_PATCH_SRCDIR}')
  145. }
  146. python devtool_post_patch() {
  147. import shutil
  148. tempdir = d.getVar('DEVTOOL_TEMPDIR')
  149. with open(os.path.join(tempdir, 'srcsubdir'), 'r') as f:
  150. srcsubdir = f.read()
  151. with open(os.path.join(tempdir, 'initial_rev'), 'r') as f:
  152. initial_rev = f.read()
  153. def rm_patches():
  154. patches_dir = os.path.join(srcsubdir, 'patches')
  155. if os.path.exists(patches_dir):
  156. shutil.rmtree(patches_dir)
  157. # Restore any "patches" directory that was actually part of the source tree
  158. try:
  159. bb.process.run('git checkout -- patches', cwd=srcsubdir)
  160. except bb.process.ExecutionError:
  161. pass
  162. extra_overrides = d.getVar('DEVTOOL_EXTRA_OVERRIDES')
  163. if extra_overrides:
  164. extra_overrides = set(extra_overrides.split(':'))
  165. devbranch = d.getVar('DEVTOOL_DEVBRANCH')
  166. default_overrides = d.getVar('OVERRIDES').split(':')
  167. no_overrides = []
  168. # First, we may have some overrides that are referred to in the recipe set in
  169. # our configuration, so we need to make a branch that excludes those
  170. for override in default_overrides:
  171. if override not in extra_overrides:
  172. no_overrides.append(override)
  173. if default_overrides != no_overrides:
  174. # Some overrides are active in the current configuration, so
  175. # we need to create a branch where none of the overrides are active
  176. bb.process.run('git checkout %s -b devtool-no-overrides' % initial_rev, cwd=srcsubdir)
  177. # Run do_patch function with the override applied
  178. localdata = bb.data.createCopy(d)
  179. localdata.setVar('OVERRIDES', ':'.join(no_overrides))
  180. localdata.setVar('FILESOVERRIDES', ':'.join(no_overrides))
  181. bb.build.exec_func('do_patch', localdata)
  182. rm_patches()
  183. # Now we need to reconcile the dev branch with the no-overrides one
  184. # (otherwise we'd likely be left with identical commits that have different hashes)
  185. bb.process.run('git checkout %s' % devbranch, cwd=srcsubdir)
  186. bb.process.run('git rebase devtool-no-overrides', cwd=srcsubdir)
  187. else:
  188. bb.process.run('git checkout %s -b devtool-no-overrides' % devbranch, cwd=srcsubdir)
  189. for override in extra_overrides:
  190. localdata = bb.data.createCopy(d)
  191. if override in default_overrides:
  192. bb.process.run('git branch devtool-override-%s %s' % (override, devbranch), cwd=srcsubdir)
  193. else:
  194. # Reset back to the initial commit on a new branch
  195. bb.process.run('git checkout %s -b devtool-override-%s' % (initial_rev, override), cwd=srcsubdir)
  196. # Run do_patch function with the override applied
  197. localdata.setVar('OVERRIDES', ':'.join(no_overrides + [override]))
  198. localdata.setVar('FILESOVERRIDES', ':'.join(no_overrides + [override]))
  199. bb.build.exec_func('do_patch', localdata)
  200. rm_patches()
  201. # Now we need to reconcile the new branch with the no-overrides one
  202. # (otherwise we'd likely be left with identical commits that have different hashes)
  203. bb.process.run('git rebase devtool-no-overrides', cwd=srcsubdir)
  204. bb.process.run('git checkout %s' % devbranch, cwd=srcsubdir)
  205. bb.process.run('git tag -f devtool-patched', cwd=srcsubdir)
  206. if os.path.exists(os.path.join(srcsubdir, '.gitmodules')):
  207. bb.process.run('git submodule foreach --recursive "git tag -f devtool-patched"', cwd=srcsubdir)
  208. }
  209. python devtool_post_configure() {
  210. import shutil
  211. tempdir = d.getVar('DEVTOOL_TEMPDIR')
  212. shutil.copy2(os.path.join(d.getVar('B'), '.config'), tempdir)
  213. }