blob: c478f4a1870c665a2c084035d68d3309d820bc05 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001#
2# Removes source after build
3#
4# To use it add that line to conf/local.conf:
5#
6# INHERIT += "rm_work"
7#
8# To inhibit rm_work for some recipes, specify them in RM_WORK_EXCLUDE.
9# For example, in conf/local.conf:
10#
11# RM_WORK_EXCLUDE += "icu-native icu busybox"
12#
Brad Bishop6e60e8b2018-02-01 10:27:11 -050013# Recipes can also configure which entries in their ${WORKDIR}
14# are preserved besides temp, which already gets excluded by default
15# because it contains logs:
16# do_install_append () {
17# echo "bar" >${WORKDIR}/foo
18# }
19# RM_WORK_EXCLUDE_ITEMS += "foo"
20RM_WORK_EXCLUDE_ITEMS = "temp"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050021
22# Use the completion scheduler by default when rm_work is active
23# to try and reduce disk usage
24BB_SCHEDULER ?= "completion"
25
Patrick Williamsc0f7c042017-02-23 20:41:17 -060026# Run the rm_work task in the idle scheduling class
27BB_TASK_IONICE_LEVEL_task-rm_work = "3.0"
28
Patrick Williamsc124f4f2015-09-15 14:41:29 -050029do_rm_work () {
30 # If the recipe name is in the RM_WORK_EXCLUDE, skip the recipe.
31 for p in ${RM_WORK_EXCLUDE}; do
32 if [ "$p" = "${PN}" ]; then
33 bbnote "rm_work: Skipping ${PN} since it is in RM_WORK_EXCLUDE"
34 exit 0
35 fi
36 done
37
Patrick Williamsc124f4f2015-09-15 14:41:29 -050038 # Need to add pseudo back or subsqeuent work in this workdir
39 # might fail since setscene may not rerun to recreate it
40 mkdir -p ${WORKDIR}/pseudo/
41
Brad Bishopd7bf8c12018-02-25 22:55:05 -050042 excludes='${RM_WORK_EXCLUDE_ITEMS}'
43
Patrick Williamsc124f4f2015-09-15 14:41:29 -050044 # Change normal stamps into setscene stamps as they better reflect the
45 # fact WORKDIR is now empty
46 # Also leave noexec stamps since setscene stamps don't cover them
47 cd `dirname ${STAMP}`
48 for i in `basename ${STAMP}`*
49 do
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080050 # By default we'll delete the stamp, unless $i is changed by the inner loop
51 # (i=dummy does this)
52
53 case $i in
54 *sigdata*|*sigbasedata*)
55 # Save/skip anything that looks like a signature data file.
56 i=dummy
57 ;;
58 *do_image_complete_setscene*)
59 # Ensure we don't 'stack' setscene extensions to this stamp with the section below
60 i=dummy
61 ;;
62 *do_image_complete*)
63 # Promote do_image_complete stamps to setscene versions (ahead of *do_image* below)
64 mv $i `echo $i | sed -e "s#do_image_complete#do_image_complete_setscene#"`
65 i=dummy
66 ;;
67 *do_package_write*|*do_rootfs*|*do_image*|*do_bootimg*|*do_write_qemuboot_conf*|*do_build*)
68 i=dummy
69 ;;
70 *do_addto_recipe_sysroot*)
71 # Preserve recipe-sysroot-native if do_addto_recipe_sysroot has been used
72 excludes="$excludes recipe-sysroot-native"
73 i=dummy
74 ;;
75 *do_package|*do_package.*|*do_package_setscene.*)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050076 # We remove do_package entirely, including any
77 # sstate version since otherwise we'd need to leave 'plaindirs' around
78 # such as 'packages' and 'packages-split' and these can be large. No end
79 # of chain tasks depend directly on do_package anymore.
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080080 rm -f $i;
81 i=dummy
82 ;;
83 *_setscene*)
84 # Skip stamps which are already setscene versions
85 i=dummy
86 ;;
87 esac
88
89 for j in ${SSTATETASKS} do_shared_workdir
90 do
91 case $i in
92 dummy)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050093 break
94 ;;
95 *$j|*$j.*)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080096 # Promote the stamp to a setscene version
Patrick Williamsc124f4f2015-09-15 14:41:29 -050097 mv $i `echo $i | sed -e "s#${j}#${j}_setscene#"`
98 i=dummy
99 break
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800100 ;;
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500101 esac
102 done
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800103
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500104 rm -f $i
105 done
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500106
107 cd ${WORKDIR}
108 for dir in *
109 do
110 # Retain only logs and other files in temp, safely ignore
111 # failures of removing pseudo folers on NFS2/3 server.
112 if [ $dir = 'pseudo' ]; then
113 rm -rf $dir 2> /dev/null || true
114 elif ! echo "$excludes" | grep -q -w "$dir"; then
115 rm -rf $dir
116 fi
117 done
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500118}
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500119do_rm_work_all () {
120 :
121}
122do_rm_work_all[recrdeptask] = "do_rm_work"
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500123do_rm_work_all[noexec] = "1"
124addtask rm_work_all after before do_build
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500125
126do_populate_sdk[postfuncs] += "rm_work_populatesdk"
127rm_work_populatesdk () {
128 :
129}
130rm_work_populatesdk[cleandirs] = "${WORKDIR}/sdk"
131
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500132do_image_complete[postfuncs] += "rm_work_rootfs"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500133rm_work_rootfs () {
134 :
135}
136rm_work_rootfs[cleandirs] = "${WORKDIR}/rootfs"
137
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500138# This task can be used instead of do_build to trigger building
139# without also invoking do_rm_work. It only exists when rm_work.bbclass
140# is active, otherwise do_build needs to be used.
141#
142# The intended usage is
143# ${@ d.getVar('RM_WORK_BUILD_WITHOUT') or 'do_build'}
144# in places that previously used just 'do_build'.
145RM_WORK_BUILD_WITHOUT = "do_build_without_rm_work"
146do_build_without_rm_work () {
147 :
148}
149do_build_without_rm_work[noexec] = "1"
150
151# We have to add these tasks already now, because all tasks are
152# meant to be defined before the RecipeTaskPreProcess event triggers.
153# The inject_rm_work event handler then merely changes task dependencies.
154addtask do_rm_work
155addtask do_build_without_rm_work
156addhandler inject_rm_work
157inject_rm_work[eventmask] = "bb.event.RecipeTaskPreProcess"
158python inject_rm_work() {
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600159 if bb.data.inherits_class('kernel', d):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500160 d.appendVar("RM_WORK_EXCLUDE", ' ' + d.getVar("PN"))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500161 # If the recipe name is in the RM_WORK_EXCLUDE, skip the recipe.
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500162 excludes = (d.getVar("RM_WORK_EXCLUDE") or "").split()
163 pn = d.getVar("PN")
164
165 # Determine what do_build depends upon, without including do_build
166 # itself or our own special do_rm_work_all.
Brad Bishopf8caae32019-03-25 13:13:56 -0400167 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 -0500168
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500169 # deps can be empty if do_build doesn't exist, e.g. *-inital recipes
170 if not deps:
171 deps = ["do_populate_sysroot", "do_populate_lic"]
172
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500173 if pn in excludes:
174 d.delVarFlag('rm_work_rootfs', 'cleandirs')
175 d.delVarFlag('rm_work_populatesdk', 'cleandirs')
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500176 else:
177 # Inject do_rm_work into the tasks of the current recipe such that do_build
178 # depends on it and that it runs after all other tasks that block do_build,
179 # i.e. after all work on the current recipe is done. The reason for taking
180 # this approach instead of making do_rm_work depend on do_build is that
181 # do_build inherits additional runtime dependencies on
182 # other recipes and thus will typically run much later than completion of
183 # work in the recipe itself.
184 # In practice, addtask() here merely updates the dependencies.
185 bb.build.addtask('do_rm_work', 'do_build', ' '.join(deps), d)
186
187 # Always update do_build_without_rm_work dependencies.
188 bb.build.addtask('do_build_without_rm_work', '', ' '.join(deps), d)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500189}