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