blob: c493efff2fcac7edb04448a88151600d61f0ab9d [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 Williamsc124f4f2015-09-15 14:41:29 -0500115do_rm_work_all () {
116 :
117}
118do_rm_work_all[recrdeptask] = "do_rm_work"
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500119do_rm_work_all[noexec] = "1"
Brad Bishop79641f22019-09-10 07:20:22 -0400120addtask rm_work_all before do_build
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500121
122do_populate_sdk[postfuncs] += "rm_work_populatesdk"
123rm_work_populatesdk () {
124 :
125}
126rm_work_populatesdk[cleandirs] = "${WORKDIR}/sdk"
127
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500128do_image_complete[postfuncs] += "rm_work_rootfs"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500129rm_work_rootfs () {
130 :
131}
132rm_work_rootfs[cleandirs] = "${WORKDIR}/rootfs"
133
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500134# This task can be used instead of do_build to trigger building
135# without also invoking do_rm_work. It only exists when rm_work.bbclass
136# is active, otherwise do_build needs to be used.
137#
138# The intended usage is
139# ${@ d.getVar('RM_WORK_BUILD_WITHOUT') or 'do_build'}
140# in places that previously used just 'do_build'.
141RM_WORK_BUILD_WITHOUT = "do_build_without_rm_work"
142do_build_without_rm_work () {
143 :
144}
145do_build_without_rm_work[noexec] = "1"
146
147# We have to add these tasks already now, because all tasks are
148# meant to be defined before the RecipeTaskPreProcess event triggers.
149# The inject_rm_work event handler then merely changes task dependencies.
150addtask do_rm_work
151addtask do_build_without_rm_work
152addhandler inject_rm_work
153inject_rm_work[eventmask] = "bb.event.RecipeTaskPreProcess"
154python inject_rm_work() {
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600155 if bb.data.inherits_class('kernel', d):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500156 d.appendVar("RM_WORK_EXCLUDE", ' ' + d.getVar("PN"))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500157 # If the recipe name is in the RM_WORK_EXCLUDE, skip the recipe.
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500158 excludes = (d.getVar("RM_WORK_EXCLUDE") or "").split()
159 pn = d.getVar("PN")
160
161 # Determine what do_build depends upon, without including do_build
162 # itself or our own special do_rm_work_all.
Brad Bishopf8caae32019-03-25 13:13:56 -0400163 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 -0500164
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500165 # deps can be empty if do_build doesn't exist, e.g. *-inital recipes
166 if not deps:
167 deps = ["do_populate_sysroot", "do_populate_lic"]
168
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500169 if pn in excludes:
170 d.delVarFlag('rm_work_rootfs', 'cleandirs')
171 d.delVarFlag('rm_work_populatesdk', 'cleandirs')
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500172 else:
173 # Inject do_rm_work into the tasks of the current recipe such that do_build
174 # depends on it and that it runs after all other tasks that block do_build,
175 # i.e. after all work on the current recipe is done. The reason for taking
176 # this approach instead of making do_rm_work depend on do_build is that
177 # do_build inherits additional runtime dependencies on
178 # other recipes and thus will typically run much later than completion of
179 # work in the recipe itself.
180 # In practice, addtask() here merely updates the dependencies.
181 bb.build.addtask('do_rm_work', 'do_build', ' '.join(deps), d)
182
183 # Always update do_build_without_rm_work dependencies.
184 bb.build.addtask('do_build_without_rm_work', '', ' '.join(deps), d)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500185}