blob: 4121a1327911a9c905dd973fbec81f86b7785208 [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 () {
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 Williamsc124f4f2015-09-15 14:41:29 -050044 # 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 Bishopd7bf8c12018-02-25 22:55:05 -050048 excludes='${RM_WORK_EXCLUDE_ITEMS}'
49
Patrick Williamsc124f4f2015-09-15 14:41:29 -050050 # 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 Bishop1a4b7ee2018-12-16 17:11:34 -080056 case $i in
57 *sigdata*|*sigbasedata*)
58 # Save/skip anything that looks like a signature data file.
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080059 ;;
Brad Bishop1d80a2e2019-11-15 16:35:03 -050060 *do_image_complete_setscene*|*do_image_qa_setscene*)
61 # Ensure we don't 'stack' setscene extensions to these stamps with the sections below
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080062 ;;
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 Bishop1d80a2e2019-11-15 16:35:03 -050066 ;;
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 Bishop1a4b7ee2018-12-16 17:11:34 -080070 ;;
71 *do_package_write*|*do_rootfs*|*do_image*|*do_bootimg*|*do_write_qemuboot_conf*|*do_build*)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080072 ;;
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 Bishop1a4b7ee2018-12-16 17:11:34 -080076 ;;
77 *do_package|*do_package.*|*do_package_setscene.*)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050078 # 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 Williams93c203f2021-10-06 16:15:23 -050082 rm -f -- $i;
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080083 ;;
84 *_setscene*)
85 # Skip stamps which are already setscene versions
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080086 ;;
Brad Bishop1d80a2e2019-11-15 16:35:03 -050087 *)
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 Williams93c203f2021-10-06 16:15:23 -050099 rm -f -- $i
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800100 esac
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500101 done
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500102
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 Williams93c203f2021-10-06 16:15:23 -0500109 rm -rf -- $dir 2> /dev/null || true
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500110 elif ! echo "$excludes" | grep -q -w "$dir"; then
Patrick Williams93c203f2021-10-06 16:15:23 -0500111 rm -rf -- $dir
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500112 fi
113 done
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500114}
Patrick Williams7784c422022-11-17 07:29:11 -0600115do_rm_work[vardepsexclude] += "SSTATETASKS"
116
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500117do_rm_work_all () {
118 :
119}
120do_rm_work_all[recrdeptask] = "do_rm_work"
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500121do_rm_work_all[noexec] = "1"
Brad Bishop79641f22019-09-10 07:20:22 -0400122addtask rm_work_all before do_build
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500123
124do_populate_sdk[postfuncs] += "rm_work_populatesdk"
125rm_work_populatesdk () {
126 :
127}
128rm_work_populatesdk[cleandirs] = "${WORKDIR}/sdk"
129
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500130do_image_complete[postfuncs] += "rm_work_rootfs"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500131rm_work_rootfs () {
132 :
133}
134rm_work_rootfs[cleandirs] = "${WORKDIR}/rootfs"
135
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500136# 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'.
143RM_WORK_BUILD_WITHOUT = "do_build_without_rm_work"
144do_build_without_rm_work () {
145 :
146}
147do_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.
152addtask do_rm_work
153addtask do_build_without_rm_work
154addhandler inject_rm_work
155inject_rm_work[eventmask] = "bb.event.RecipeTaskPreProcess"
156python inject_rm_work() {
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600157 if bb.data.inherits_class('kernel', d):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500158 d.appendVar("RM_WORK_EXCLUDE", ' ' + d.getVar("PN"))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500159 # If the recipe name is in the RM_WORK_EXCLUDE, skip the recipe.
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500160 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 Bishopf8caae32019-03-25 13:13:56 -0400165 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 -0500166
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500167 # 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 Williamsc124f4f2015-09-15 14:41:29 -0500171 if pn in excludes:
172 d.delVarFlag('rm_work_rootfs', 'cleandirs')
173 d.delVarFlag('rm_work_populatesdk', 'cleandirs')
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500174 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 Williamsc124f4f2015-09-15 14:41:29 -0500187}