1# 2# Copyright OpenEmbedded Contributors 3# 4# SPDX-License-Identifier: MIT 5# 6 7# 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# 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: 22# do_install:append () { 23# echo "bar" >${WORKDIR}/foo 24# } 25# RM_WORK_EXCLUDE_ITEMS += "foo" 26RM_WORK_EXCLUDE_ITEMS = "temp" 27 28# Use the completion scheduler by default when rm_work is active 29# to try and reduce disk usage 30BB_SCHEDULER ?= "completion" 31 32# Run the rm_work task in the idle scheduling class 33BB_TASK_IONICE_LEVEL:task-rm_work = "3.0" 34 35do_rm_work () { 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 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 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 55 excludes='${RM_WORK_EXCLUDE_ITEMS}' 56 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 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 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 119 "${RM_BIN}" -rf -- $dir 2> /dev/null || true 120 elif ! echo "$excludes" | grep -q -w "$dir"; then 121 "${RM_BIN}" -rf -- $dir 122 fi 123 done 124} 125do_rm_work[vardepsexclude] += "SSTATETASKS" 126 127do_rm_work_all () { 128 : 129} 130do_rm_work_all[recrdeptask] = "do_rm_work" 131do_rm_work_all[noexec] = "1" 132addtask rm_work_all before do_build 133 134do_populate_sdk[postfuncs] += "rm_work_populatesdk" 135rm_work_populatesdk () { 136 : 137} 138rm_work_populatesdk[cleandirs] = "${WORKDIR}/sdk" 139 140do_image_complete[postfuncs] += "rm_work_rootfs" 141rm_work_rootfs () { 142 : 143} 144rm_work_rootfs[cleandirs] = "${WORKDIR}/rootfs" 145 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'. 153RM_WORK_BUILD_WITHOUT = "do_build_without_rm_work" 154do_build_without_rm_work () { 155 : 156} 157do_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. 162addtask do_rm_work 163addtask do_build_without_rm_work 164addhandler inject_rm_work 165inject_rm_work[eventmask] = "bb.event.RecipeTaskPreProcess" 166python inject_rm_work() { 167 if bb.data.inherits_class('kernel', d): 168 d.appendVar("RM_WORK_EXCLUDE", ' ' + d.getVar("PN")) 169 # If the recipe name is in the RM_WORK_EXCLUDE, skip the recipe. 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. 175 deps = sorted((set(bb.build.preceedtask('do_build', True, d))).difference(('do_build', 'do_rm_work_all')) or "") 176 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 181 if pn in excludes: 182 d.delVarFlag('rm_work_rootfs', 'cleandirs') 183 d.delVarFlag('rm_work_populatesdk', 'cleandirs') 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. 193 bb.build.addtask('do_rm_work', 'do_rm_work_all do_build', ' '.join(deps), d) 194 195 # Always update do_build_without_rm_work dependencies. 196 bb.build.addtask('do_build_without_rm_work', '', ' '.join(deps), d) 197} 198