rm_work.bbclass 6.5 KB

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