blob: 52ecfafb72d66e4ea4dbbcb231388dff96a23756 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001#
Patrick Williams92b42cb2022-09-03 06:53:57 -05002# Copyright OpenEmbedded Contributors
3#
4# SPDX-License-Identifier: MIT
5#
6
7#
Patrick Williamsc124f4f2015-09-15 14:41:29 -05008# 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 Bishop6e60e8b2018-02-01 10:27:11 -050019# 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 Williams213cb262021-08-07 19:21:33 -050022# do_install:append () {
Brad Bishop6e60e8b2018-02-01 10:27:11 -050023# echo "bar" >${WORKDIR}/foo
24# }
25# RM_WORK_EXCLUDE_ITEMS += "foo"
26RM_WORK_EXCLUDE_ITEMS = "temp"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050027
28# Use the completion scheduler by default when rm_work is active
29# to try and reduce disk usage
30BB_SCHEDULER ?= "completion"
31
Patrick Williamsc0f7c042017-02-23 20:41:17 -060032# Run the rm_work task in the idle scheduling class
Patrick Williams213cb262021-08-07 19:21:33 -050033BB_TASK_IONICE_LEVEL:task-rm_work = "3.0"
Patrick Williamsc0f7c042017-02-23 20:41:17 -060034
Patrick Williamsc124f4f2015-09-15 14:41:29 -050035do_rm_work () {
Andrew Geissler517393d2023-01-13 08:55:19 -060036 # 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 Williamsc124f4f2015-09-15 14:41:29 -050043 # 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 Williamsc124f4f2015-09-15 14:41:29 -050051 # 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 Bishopd7bf8c12018-02-25 22:55:05 -050055 excludes='${RM_WORK_EXCLUDE_ITEMS}'
56
Patrick Williamsc124f4f2015-09-15 14:41:29 -050057 # 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 Geissler517393d2023-01-13 08:55:19 -060060 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 Bishopd7bf8c12018-02-25 22:55:05 -0500112
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 Geissler517393d2023-01-13 08:55:19 -0600119 "${RM_BIN}" -rf -- $dir 2> /dev/null || true
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500120 elif ! echo "$excludes" | grep -q -w "$dir"; then
Andrew Geissler517393d2023-01-13 08:55:19 -0600121 "${RM_BIN}" -rf -- $dir
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500122 fi
123 done
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500124}
Patrick Williams7784c422022-11-17 07:29:11 -0600125do_rm_work[vardepsexclude] += "SSTATETASKS"
126
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500127do_rm_work_all () {
128 :
129}
130do_rm_work_all[recrdeptask] = "do_rm_work"
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500131do_rm_work_all[noexec] = "1"
Brad Bishop79641f22019-09-10 07:20:22 -0400132addtask rm_work_all before do_build
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500133
134do_populate_sdk[postfuncs] += "rm_work_populatesdk"
135rm_work_populatesdk () {
136 :
137}
138rm_work_populatesdk[cleandirs] = "${WORKDIR}/sdk"
139
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500140do_image_complete[postfuncs] += "rm_work_rootfs"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500141rm_work_rootfs () {
142 :
143}
144rm_work_rootfs[cleandirs] = "${WORKDIR}/rootfs"
145
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500146# 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() {
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600167 if bb.data.inherits_class('kernel', d):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500168 d.appendVar("RM_WORK_EXCLUDE", ' ' + d.getVar("PN"))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500169 # If the recipe name is in the RM_WORK_EXCLUDE, skip the recipe.
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500170 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 Bishopf8caae32019-03-25 13:13:56 -0400175 deps = sorted((set(bb.build.preceedtask('do_build', True, d))).difference(('do_build', 'do_rm_work_all')) or "")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500176
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500177 # 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 Williamsc124f4f2015-09-15 14:41:29 -0500181 if pn in excludes:
182 d.delVarFlag('rm_work_rootfs', 'cleandirs')
183 d.delVarFlag('rm_work_populatesdk', 'cleandirs')
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500184 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 Geissler517393d2023-01-13 08:55:19 -0600193 bb.build.addtask('do_rm_work', 'do_rm_work_all do_build', ' '.join(deps), d)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500194
195 # Always update do_build_without_rm_work dependencies.
196 bb.build.addtask('do_build_without_rm_work', '', ' '.join(deps), d)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500197}