Add files from parent branch HEAD:
[pkgsrc.git] / mk / bsd.pkg.clean.mk
1 # $NetBSD: bsd.pkg.clean.mk,v 1.12 2007/11/15 12:05:09 rillig Exp $
2 #
3 # This Makefile fragment is included to bsd.pkg.mk and defines the
4 # relevant variables and targets for the "clean" phase.
5 #
6 # The following variables may be set by the package Makefile and
7 # specify how cleaning happens:
8 #
9 #    CLEANDEPENDS specifies the whether "cleaning" will also clean
10 #       in all dependencies, implied and direct.  CLEANDEPENDS
11 #       defaults to "no".
12 #
13 # The following targets are defined by bsd.pkg.clean.mk:
14 #
15 #    clean-depends is the target which descends into dependencies'
16 #       package directories and invokes the "clean" action.
17 #
18 #    do-clean is the target that does the actual cleaning, which
19 #       involves removing the work directory and other temporary
20 #       files used by the package.
21 #
22 #    clean is the target that is invoked by the user to perform
23 #       the "clean" action.
24 #
25 #    cleandir is an alias for "clean".
26 #
27
28 CLEANDEPENDS?=  no
29
30 .if defined(PRIVILEGED_STAGES) && !empty(PRIVILEGED_STAGES:Mclean)
31 _MAKE_CLEAN_AS_ROOT=yes
32 .endif
33
34 .PHONY: clean-depends
35 clean-depends:
36         ${RUN}                                                          \
37         ${_DEPENDS_WALK_CMD} ${PKGPATH} |                               \
38         while read dir; do                                              \
39                 cd ${.CURDIR}/../../$$dir &&                            \
40                 ${RECURSIVE_MAKE} ${MAKEFLAGS} CLEANDEPENDS=no clean;   \
41         done
42
43 .PHONY: pre-clean
44 .if !target(pre-clean)
45 pre-clean:
46         @${DO_NADA}
47 .endif
48
49 .PHONY: post-clean
50 .if !target(post-clean)
51 post-clean:
52         @${DO_NADA}
53 .endif
54
55 .PHONY: do-clean
56 .if !target(do-clean)
57 .  if !empty(_MAKE_CLEAN_AS_ROOT:M[Yy][Ee][Ss])
58 do-clean: su-target
59 .  else
60 do-clean: su-do-clean
61 .  endif
62 .endif
63
64 su-do-clean: .PHONY
65         @${PHASE_MSG} "Cleaning for ${PKGNAME}"
66         ${RUN}                                                          \
67         if ${TEST} -d ${WRKDIR}; then                                   \
68                 if ${TEST} -w ${WRKDIR}; then                           \
69                         ${RM} -fr ${WRKDIR};                            \
70                 else                                                    \
71                         ${STEP_MSG} ${WRKDIR}" not writable, skipping"; \
72                 fi;                                                     \
73         fi
74 .if defined(WRKOBJDIR)
75         ${RUN}                                                          \
76         ${RMDIR} ${BUILD_DIR} 2>/dev/null || ${TRUE};                   \
77         ${RMDIR} ${BUILD_DIR:H} 2>/dev/null || ${TRUE};                 \
78         ${RM} -f ${WRKDIR_BASENAME} 2>/dev/null || ${TRUE}
79 .endif
80
81 _CLEAN_TARGETS+=        pre-clean
82 .if empty(CLEANDEPENDS:M[nN][oO])
83 _CLEAN_TARGETS+=        clean-depends
84 .endif
85 _CLEAN_TARGETS+=        do-clean
86 _CLEAN_TARGETS+=        post-clean
87
88 .PHONY: clean
89 .if !target(clean)
90 clean: ${_CLEAN_TARGETS}
91 .endif
92
93 .PHONY: cleandir
94 cleandir: clean