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