rm_work.bbclass 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #
  2. # Copyright OpenEmbedded Contributors
  3. #
  4. # SPDX-License-Identifier: MIT
  5. #
  6. #
  7. # Removes source after build
  8. #
  9. # To use it add that line to conf/local.conf:
  10. #
  11. # INHERIT += "rm_work"
  12. #
  13. # To inhibit rm_work for some recipes, specify them in RM_WORK_EXCLUDE.
  14. # For example, in conf/local.conf:
  15. #
  16. # RM_WORK_EXCLUDE += "icu-native icu busybox"
  17. #
  18. # Recipes can also configure which entries in their ${WORKDIR}
  19. # are preserved besides temp, which already gets excluded by default
  20. # because it contains logs:
  21. # do_install:append () {
  22. # echo "bar" >${WORKDIR}/foo
  23. # }
  24. # RM_WORK_EXCLUDE_ITEMS += "foo"
  25. RM_WORK_EXCLUDE_ITEMS = "temp"
  26. # Use the completion scheduler by default when rm_work is active
  27. # to try and reduce disk usage
  28. BB_SCHEDULER ?= "completion"
  29. # Run the rm_work task in the idle scheduling class
  30. BB_TASK_IONICE_LEVEL:task-rm_work = "3.0"
  31. do_rm_work () {
  32. # Force using the HOSTTOOLS 'rm' - otherwise the SYSROOT_NATIVE 'rm' can be selected depending on PATH
  33. # Avoids race-condition accessing 'rm' when deleting WORKDIR folders at the end of this function
  34. RM_BIN="$(PATH=${HOSTTOOLS_DIR} command -v rm)"
  35. if [ -z "${RM_BIN}" ]; then
  36. bbfatal "Binary 'rm' not found in HOSTTOOLS_DIR, cannot remove WORKDIR data."
  37. fi
  38. # If the recipe name is in the RM_WORK_EXCLUDE, skip the recipe.
  39. for p in ${RM_WORK_EXCLUDE}; do
  40. if [ "$p" = "${PN}" ]; then
  41. bbnote "rm_work: Skipping ${PN} since it is in RM_WORK_EXCLUDE"
  42. exit 0
  43. fi
  44. done
  45. # Need to add pseudo back or subsqeuent work in this workdir
  46. # might fail since setscene may not rerun to recreate it
  47. mkdir -p ${WORKDIR}/pseudo/
  48. excludes='${RM_WORK_EXCLUDE_ITEMS}'
  49. # Change normal stamps into setscene stamps as they better reflect the
  50. # fact WORKDIR is now empty
  51. # Also leave noexec stamps since setscene stamps don't cover them
  52. STAMPDIR=`dirname ${STAMP}`
  53. if test -d $STAMPDIR; then
  54. cd $STAMPDIR
  55. for i in `basename ${STAMP}`*
  56. do
  57. case $i in
  58. *sigdata*|*sigbasedata*)
  59. # Save/skip anything that looks like a signature data file.
  60. ;;
  61. *do_image_complete_setscene*|*do_image_qa_setscene*)
  62. # Ensure we don't 'stack' setscene extensions to these stamps with the sections below
  63. ;;
  64. *do_image_complete*)
  65. # Promote do_image_complete stamps to setscene versions (ahead of *do_image* below)
  66. mv $i `echo $i | sed -e "s#do_image_complete#do_image_complete_setscene#"`
  67. ;;
  68. *do_image_qa*)
  69. # Promote do_image_qa stamps to setscene versions (ahead of *do_image* below)
  70. mv $i `echo $i | sed -e "s#do_image_qa#do_image_qa_setscene#"`
  71. ;;
  72. *do_package_write*|*do_rootfs*|*do_image*|*do_bootimg*|*do_write_qemuboot_conf*|*do_build*)
  73. ;;
  74. *do_addto_recipe_sysroot*)
  75. # Preserve recipe-sysroot-native if do_addto_recipe_sysroot has been used
  76. excludes="$excludes recipe-sysroot-native"
  77. ;;
  78. *do_package|*do_package.*|*do_package_setscene.*)
  79. # We remove do_package entirely, including any
  80. # sstate version since otherwise we'd need to leave 'plaindirs' around
  81. # such as 'packages' and 'packages-split' and these can be large. No end
  82. # of chain tasks depend directly on do_package anymore.
  83. "${RM_BIN}" -f -- $i;
  84. ;;
  85. *_setscene*)
  86. # Skip stamps which are already setscene versions
  87. ;;
  88. *)
  89. # For everything else: if suitable, promote the stamp to a setscene
  90. # version, otherwise remove it
  91. for j in ${SSTATETASKS} do_shared_workdir
  92. do
  93. case $i in
  94. *$j|*$j.*)
  95. mv $i `echo $i | sed -e "s#${j}#${j}_setscene#"`
  96. break
  97. ;;
  98. esac
  99. done
  100. "${RM_BIN}" -f -- $i
  101. esac
  102. done
  103. fi
  104. cd ${WORKDIR}
  105. for dir in *
  106. do
  107. # Retain only logs and other files in temp, safely ignore
  108. # failures of removing pseudo folers on NFS2/3 server.
  109. if [ $dir = 'pseudo' ]; then
  110. "${RM_BIN}" -rf -- $dir 2> /dev/null || true
  111. elif ! echo "$excludes" | grep -q -w "$dir"; then
  112. "${RM_BIN}" -rf -- $dir
  113. fi
  114. done
  115. }
  116. do_rm_work[vardepsexclude] += "SSTATETASKS"
  117. do_rm_work_all () {
  118. :
  119. }
  120. do_rm_work_all[recrdeptask] = "do_rm_work"
  121. do_rm_work_all[noexec] = "1"
  122. addtask rm_work_all before do_build
  123. do_populate_sdk[postfuncs] += "rm_work_populatesdk"
  124. rm_work_populatesdk () {
  125. :
  126. }
  127. rm_work_populatesdk[cleandirs] = "${WORKDIR}/sdk"
  128. do_image_complete[postfuncs] += "rm_work_rootfs"
  129. rm_work_rootfs () {
  130. :
  131. }
  132. rm_work_rootfs[cleandirs] = "${WORKDIR}/rootfs"
  133. # This task can be used instead of do_build to trigger building
  134. # without also invoking do_rm_work. It only exists when rm_work.bbclass
  135. # is active, otherwise do_build needs to be used.
  136. #
  137. # The intended usage is
  138. # ${@ d.getVar('RM_WORK_BUILD_WITHOUT') or 'do_build'}
  139. # in places that previously used just 'do_build'.
  140. RM_WORK_BUILD_WITHOUT = "do_build_without_rm_work"
  141. do_build_without_rm_work () {
  142. :
  143. }
  144. do_build_without_rm_work[noexec] = "1"
  145. # We have to add these tasks already now, because all tasks are
  146. # meant to be defined before the RecipeTaskPreProcess event triggers.
  147. # The inject_rm_work event handler then merely changes task dependencies.
  148. addtask do_rm_work
  149. addtask do_build_without_rm_work
  150. addhandler inject_rm_work
  151. inject_rm_work[eventmask] = "bb.event.RecipeTaskPreProcess"
  152. python inject_rm_work() {
  153. if bb.data.inherits_class('kernel', d):
  154. d.appendVar("RM_WORK_EXCLUDE", ' ' + d.getVar("PN"))
  155. # If the recipe name is in the RM_WORK_EXCLUDE, skip the recipe.
  156. excludes = (d.getVar("RM_WORK_EXCLUDE") or "").split()
  157. pn = d.getVar("PN")
  158. # Determine what do_build depends upon, without including do_build
  159. # itself or our own special do_rm_work_all.
  160. deps = sorted((set(bb.build.preceedtask('do_build', True, d))).difference(('do_build', 'do_rm_work_all')) or "")
  161. # deps can be empty if do_build doesn't exist, e.g. *-inital recipes
  162. if not deps:
  163. deps = ["do_populate_sysroot", "do_populate_lic"]
  164. if pn in excludes:
  165. d.delVarFlag('rm_work_rootfs', 'cleandirs')
  166. d.delVarFlag('rm_work_populatesdk', 'cleandirs')
  167. else:
  168. # Inject do_rm_work into the tasks of the current recipe such that do_build
  169. # depends on it and that it runs after all other tasks that block do_build,
  170. # i.e. after all work on the current recipe is done. The reason for taking
  171. # this approach instead of making do_rm_work depend on do_build is that
  172. # do_build inherits additional runtime dependencies on
  173. # other recipes and thus will typically run much later than completion of
  174. # work in the recipe itself.
  175. # In practice, addtask() here merely updates the dependencies.
  176. bb.build.addtask('do_rm_work', 'do_rm_work_all do_build', ' '.join(deps), d)
  177. # Always update do_build_without_rm_work dependencies.
  178. bb.build.addtask('do_build_without_rm_work', '', ' '.join(deps), d)
  179. }