xref: /OK3568_Linux_fs/yocto/poky/meta/classes/rm_work.bbclass (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1#
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#
13# 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"
21
22# Use the completion scheduler by default when rm_work is active
23# to try and reduce disk usage
24BB_SCHEDULER ?= "completion"
25
26# Run the rm_work task in the idle scheduling class
27BB_TASK_IONICE_LEVEL:task-rm_work = "3.0"
28
29do_rm_work () {
30    # Force using the HOSTTOOLS 'rm' - otherwise the SYSROOT_NATIVE 'rm' can be selected depending on PATH
31    # Avoids race-condition accessing 'rm' when deleting WORKDIR folders at the end of this function
32    RM_BIN="$(PATH=${HOSTTOOLS_DIR} command -v rm)"
33    if [ -z "${RM_BIN}" ]; then
34        bbfatal "Binary 'rm' not found in HOSTTOOLS_DIR, cannot remove WORKDIR data."
35    fi
36
37    # If the recipe name is in the RM_WORK_EXCLUDE, skip the recipe.
38    for p in ${RM_WORK_EXCLUDE}; do
39        if [ "$p" = "${PN}" ]; then
40            bbnote "rm_work: Skipping ${PN} since it is in RM_WORK_EXCLUDE"
41            exit 0
42        fi
43    done
44
45    # Need to add pseudo back or subsqeuent work in this workdir
46    # might fail since setscene may not rerun to recreate it
47    mkdir -p ${WORKDIR}/pseudo/
48
49    excludes='${RM_WORK_EXCLUDE_ITEMS}'
50
51    # Change normal stamps into setscene stamps as they better reflect the
52    # fact WORKDIR is now empty
53    # Also leave noexec stamps since setscene stamps don't cover them
54    cd `dirname ${STAMP}`
55    for i in `basename ${STAMP}`*
56    do
57        case $i in
58        *sigdata*|*sigbasedata*)
59            # Save/skip anything that looks like a signature data file.
60            ;;
61        *do_image_complete_setscene*|*do_image_qa_setscene*)
62            # Ensure we don't 'stack' setscene extensions to these stamps with the sections below
63            ;;
64        *do_image_complete*)
65            # Promote do_image_complete stamps to setscene versions (ahead of *do_image* below)
66            mv $i `echo $i | sed -e "s#do_image_complete#do_image_complete_setscene#"`
67            ;;
68        *do_image_qa*)
69            # Promote do_image_qa stamps to setscene versions (ahead of *do_image* below)
70            mv $i `echo $i | sed -e "s#do_image_qa#do_image_qa_setscene#"`
71            ;;
72        *do_package_write*|*do_rootfs*|*do_image*|*do_bootimg*|*do_write_qemuboot_conf*|*do_build*)
73            ;;
74        *do_addto_recipe_sysroot*)
75            # Preserve recipe-sysroot-native if do_addto_recipe_sysroot has been used
76            excludes="$excludes recipe-sysroot-native"
77            ;;
78        *do_package|*do_package.*|*do_package_setscene.*)
79            # We remove do_package entirely, including any
80            # sstate version since otherwise we'd need to leave 'plaindirs' around
81            # such as 'packages' and 'packages-split' and these can be large. No end
82            # of chain tasks depend directly on do_package anymore.
83            "${RM_BIN}" -f -- $i;
84            ;;
85        *_setscene*)
86            # Skip stamps which are already setscene versions
87            ;;
88        *)
89            # For everything else: if suitable, promote the stamp to a setscene
90            # version, otherwise remove it
91            for j in ${SSTATETASKS} do_shared_workdir
92            do
93                case $i in
94                *$j|*$j.*)
95                    mv $i `echo $i | sed -e "s#${j}#${j}_setscene#"`
96                    break
97                    ;;
98                esac
99            done
100            "${RM_BIN}" -f -- $i
101        esac
102    done
103
104    cd ${WORKDIR}
105    for dir in *
106    do
107        # Retain only logs and other files in temp, safely ignore
108        # failures of removing pseudo folers on NFS2/3 server.
109        if [ $dir = 'pseudo' ]; then
110            "${RM_BIN}" -rf -- $dir 2> /dev/null || true
111        elif ! echo "$excludes" | grep -q -w "$dir"; then
112            "${RM_BIN}" -rf -- $dir
113        fi
114    done
115}
116do_rm_work[vardepsexclude] += "SSTATETASKS"
117
118do_rm_work_all () {
119    :
120}
121do_rm_work_all[recrdeptask] = "do_rm_work"
122do_rm_work_all[noexec] = "1"
123addtask rm_work_all before do_build
124
125do_populate_sdk[postfuncs] += "rm_work_populatesdk"
126rm_work_populatesdk () {
127    :
128}
129rm_work_populatesdk[cleandirs] = "${WORKDIR}/sdk"
130
131do_image_complete[postfuncs] += "rm_work_rootfs"
132rm_work_rootfs () {
133    :
134}
135rm_work_rootfs[cleandirs] = "${WORKDIR}/rootfs"
136
137# This task can be used instead of do_build to trigger building
138# without also invoking do_rm_work. It only exists when rm_work.bbclass
139# is active, otherwise do_build needs to be used.
140#
141# The intended usage is
142# ${@ d.getVar('RM_WORK_BUILD_WITHOUT') or 'do_build'}
143# in places that previously used just 'do_build'.
144RM_WORK_BUILD_WITHOUT = "do_build_without_rm_work"
145do_build_without_rm_work () {
146    :
147}
148do_build_without_rm_work[noexec] = "1"
149
150# We have to add these tasks already now, because all tasks are
151# meant to be defined before the RecipeTaskPreProcess event triggers.
152# The inject_rm_work event handler then merely changes task dependencies.
153addtask do_rm_work
154addtask do_build_without_rm_work
155addhandler inject_rm_work
156inject_rm_work[eventmask] = "bb.event.RecipeTaskPreProcess"
157python inject_rm_work() {
158    if bb.data.inherits_class('kernel', d):
159        d.appendVar("RM_WORK_EXCLUDE", ' ' + d.getVar("PN"))
160    # If the recipe name is in the RM_WORK_EXCLUDE, skip the recipe.
161    excludes = (d.getVar("RM_WORK_EXCLUDE") or "").split()
162    pn = d.getVar("PN")
163
164    # Determine what do_build depends upon, without including do_build
165    # itself or our own special do_rm_work_all.
166    deps = sorted((set(bb.build.preceedtask('do_build', True, d))).difference(('do_build', 'do_rm_work_all')) or "")
167
168    # deps can be empty if do_build doesn't exist, e.g. *-inital recipes
169    if not deps:
170        deps = ["do_populate_sysroot", "do_populate_lic"]
171
172    if pn in excludes:
173        d.delVarFlag('rm_work_rootfs', 'cleandirs')
174        d.delVarFlag('rm_work_populatesdk', 'cleandirs')
175    else:
176        # Inject do_rm_work into the tasks of the current recipe such that do_build
177        # depends on it and that it runs after all other tasks that block do_build,
178        # i.e. after all work on the current recipe is done. The reason for taking
179        # this approach instead of making do_rm_work depend on do_build is that
180        # do_build inherits additional runtime dependencies on
181        # other recipes and thus will typically run much later than completion of
182        # work in the recipe itself.
183        # In practice, addtask() here merely updates the dependencies.
184        bb.build.addtask('do_rm_work', 'do_rm_work_all do_build', ' '.join(deps), d)
185
186    # Always update do_build_without_rm_work dependencies.
187    bb.build.addtask('do_build_without_rm_work', '', ' '.join(deps), d)
188}
189