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