2 # $Id: meta.stage.mk,v 1.45 2016/05/26 03:59:09 sjg Exp $
4 # @(#) Copyright (c) 2011, Simon J. Gerraty
6 # This file is provided in the hope that it will
7 # be of use. There is absolutely NO WARRANTY.
8 # Permission to copy, redistribute or otherwise
9 # use this file is hereby granted provided that
10 # the above copyright notice and this notice are
13 # Please send copies of changes and bug-fixes to:
17 .if !target(__${.PARSEFILE}__)
20 .if ${.MAKE.DEPENDFILE_PREFERENCE:U${.MAKE.DEPENDFILE}:M*.${MACHINE}} != ""
21 # this is generally safer anyway
22 _dirdep = ${RELDIR}.${MACHINE}
29 # this allows us to trace dependencies back to their src dir
31 @echo '${_dirdep}' > $@
33 .if defined(NO_POSIX_SHELL) || ${type printf:L:sh:Mbuiltin} == ""
34 _stage_file_basename = `basename $$f`
35 _stage_target_dirname = `dirname $$t`
37 _stage_file_basename = $${f\#\#*/}
38 _stage_target_dirname = $${t%/*}
41 _OBJROOT ?= ${OBJROOT:U${OBJTOP:H}}
42 .if ${_OBJROOT:M*/} != ""
43 _objroot ?= ${_OBJROOT:tA}/
45 _objroot ?= ${_OBJROOT:tA}
48 # make sure this is global
51 # add each dir we stage to to _STAGED_DIRS
52 # and make sure we have absolute paths so that bmake
53 # will match against .MAKE.META.BAILIWICK
54 STAGE_DIR_FILTER = tA:@d@$${_STAGED_DIRS::+=$$d}$$d@
55 # convert _STAGED_DIRS into suitable filters
56 GENDIRDEPS_FILTER += Nnot-empty-is-important \
57 ${_STAGED_DIRS:O:u:M${OBJTOP}*:S,${OBJTOP}/,N,} \
58 ${_STAGED_DIRS:O:u:M${_objroot}*:N${OBJTOP}*:S,${_objroot},,:C,^([^/]+)/(.*),N\2.\1,:S,${HOST_TARGET},.host,}
60 LN_CP_SCRIPT = LnCp() { \
61 rm -f $$2 2> /dev/null; \
62 { [ -z "$$mode" ] && ln $$1 $$2 2> /dev/null; } || \
65 # a staging conflict should cause an error
66 # a warning is handy when bootstapping different options.
67 STAGE_CONFLICT?= ERROR
68 .if ${STAGE_CONFLICT:tl} == "error"
69 STAGE_CONFLICT_ACTION= exit 1;
71 STAGE_CONFLICT_ACTION=
74 # it is an error for more than one src dir to try and stage
76 STAGE_DIRDEP_SCRIPT = ${LN_CP_SCRIPT}; StageDirdep() { \
78 if [ -s $$t.dirdep ]; then \
79 cmp -s .dirdep $$t.dirdep && return; \
80 echo "${STAGE_CONFLICT}: $$t installed by `cat $$t.dirdep` not ${_dirdep}" >&2; \
81 ${STAGE_CONFLICT_ACTION} \
83 LnCp .dirdep $$t.dirdep || exit 1; }
85 # common logic for staging files
86 # this all relies on RELDIR being set to a subdir of SRCTOP
87 # we use ln(1) if we can, else cp(1)
88 STAGE_FILE_SCRIPT = ${STAGE_DIRDEP_SCRIPT}; StageFiles() { \
89 case "$$1" in "") return;; -m) mode=$$2; shift 2;; *) mode=;; esac; \
92 [ -s .dirdep ] || echo '${_dirdep}' > .dirdep; \
94 case "$$f" in */*) t=$$dest/${_stage_file_basename};; *) t=$$dest/$$f;; esac; \
96 LnCp $$f $$t || exit 1; \
97 [ -z "$$mode" ] || chmod $$mode $$t; \
100 STAGE_LINKS_SCRIPT = ${STAGE_DIRDEP_SCRIPT}; StageLinks() { \
101 case "$$1" in "") return;; --) shift;; -*) ldest= lnf=$$1; shift;; /*) ldest=$$1/;; esac; \
104 [ -s .dirdep ] || echo '${_dirdep}' > .dirdep; \
105 while test $$\# -ge 2; do \
106 l=$$ldest$$1; shift; \
108 case "$$1" in */*) mkdir -p ${_stage_target_dirname};; esac; \
111 rm -f $$t 2>/dev/null; \
112 ln $$lnf $$l $$t || exit 1; \
115 STAGE_AS_SCRIPT = ${STAGE_DIRDEP_SCRIPT}; StageAs() { \
116 case "$$1" in "") return;; -m) mode=$$2; shift 2;; *) mode=;; esac; \
119 [ -s .dirdep ] || echo '${_dirdep}' > .dirdep; \
120 while test $$\# -ge 2; do \
123 case "$$1" in */*) mkdir -p ${_stage_target_dirname};; esac; \
126 LnCp $$s $$t || exit 1; \
127 [ -z "$$mode" ] || chmod $$mode $$t; \
130 # this is simple, a list of the "staged" files depends on this,
131 _STAGE_BASENAME_USE: .USE ${.TARGET:T}
132 @${STAGE_FILE_SCRIPT}; StageFiles ${.TARGET:H:${STAGE_DIR_FILTER}} ${.TARGET:T}
134 _STAGE_AS_BASENAME_USE: .USE ${.TARGET:T}
135 @${STAGE_AS_SCRIPT}; StageAs ${.TARGET:H:${STAGE_DIR_FILTER}} ${.TARGET:T} ${STAGE_AS_${.TARGET:T}:U${.TARGET:T}}
137 .if !empty(STAGE_INCSDIR)
138 STAGE_TARGETS += stage_incs
139 STAGE_INCS ?= ${.ALLSRC:N.dirdep:Nstage_*}
141 stage_includes: stage_incs
143 @${STAGE_FILE_SCRIPT}; StageFiles ${STAGE_INCSDIR:${STAGE_DIR_FILTER}} ${STAGE_INCS}
147 .if !empty(STAGE_LIBDIR)
148 STAGE_TARGETS += stage_libs
150 STAGE_LIBS ?= ${.ALLSRC:N.dirdep:Nstage_*}
153 @${STAGE_FILE_SCRIPT}; StageFiles ${STAGE_LIBDIR:${STAGE_DIR_FILTER}} ${STAGE_LIBS}
154 .if !defined(NO_SHLIB_LINKS)
155 .if !empty(SHLIB_LINKS)
156 @${STAGE_LINKS_SCRIPT}; StageLinks -s ${STAGE_LIBDIR:${STAGE_DIR_FILTER}} \
157 ${SHLIB_LINKS:@t@${STAGE_LIBS:T:M$t.*} $t@}
158 .elif !empty(SHLIB_LINK) && !empty(SHLIB_NAME)
159 @${STAGE_LINKS_SCRIPT}; StageLinks -s ${STAGE_LIBDIR:${STAGE_DIR_FILTER}} ${SHLIB_NAME} ${SHLIB_LINK}
165 .if !empty(STAGE_DIR)
166 STAGE_SETS += _default
167 STAGE_DIR._default = ${STAGE_DIR}
168 STAGE_LINKS_DIR._default = ${STAGE_LINKS_DIR:U${STAGE_OBJTOP}}
169 STAGE_SYMLINKS_DIR._default = ${STAGE_SYMLINKS_DIR:U${STAGE_OBJTOP}}
170 STAGE_FILES._default = ${STAGE_FILES}
171 STAGE_LINKS._default = ${STAGE_LINKS}
172 STAGE_SYMLINKS._default = ${STAGE_SYMLINKS}
173 STAGE_FILES ?= ${.ALLSRC:N.dirdep:Nstage_*}
174 STAGE_SYMLINKS ?= ${.ALLSRC:T:N.dirdep:Nstage_*}
177 .if !empty(STAGE_SETS)
178 CLEANFILES += ${STAGE_SETS:@s@stage*$s@}
180 # some makefiles need to populate multiple directories
181 .for s in ${STAGE_SETS:O:u}
182 STAGE_FILES.$s ?= ${.ALLSRC:N.dirdep:Nstage_*}
183 STAGE_SYMLINKS.$s ?= ${.ALLSRC:N.dirdep:Nstage_*}
184 STAGE_LINKS_DIR.$s ?= ${STAGE_OBJTOP}
185 STAGE_SYMLINKS_DIR.$s ?= ${STAGE_OBJTOP}
187 STAGE_TARGETS += stage_files
189 stage_files: stage_files.$s
190 stage_files.$s: .dirdep
194 @${STAGE_FILE_SCRIPT}; StageFiles ${FLAGS.$@} ${STAGE_FILES_DIR.$s:U${STAGE_DIR.$s}:${STAGE_DIR_FILTER}} ${STAGE_FILES.$s}
197 STAGE_TARGETS += stage_links
199 stage_links: stage_links.$s
200 stage_links.$s: .dirdep
204 @${STAGE_LINKS_SCRIPT}; StageLinks ${STAGE_LINKS_DIR.$s:U${STAGE_DIR.$s}:${STAGE_DIR_FILTER}} ${STAGE_LINKS.$s}
207 STAGE_TARGETS += stage_symlinks
209 stage_symlinks: stage_symlinks.$s
210 stage_symlinks.$s: .dirdep
212 stage_symlinks: .dirdep
214 @${STAGE_LINKS_SCRIPT}; StageLinks -s ${STAGE_SYMLINKS_DIR.$s:U${STAGE_DIR.$s}:${STAGE_DIR_FILTER}} ${STAGE_SYMLINKS.$s}
220 .if !empty(STAGE_AS_SETS)
221 CLEANFILES += ${STAGE_AS_SETS:@s@stage*$s@}
223 STAGE_TARGETS += stage_as
225 # sometimes things need to be renamed as they are staged
226 # each ${file} will be staged as ${STAGE_AS_${file:T}}
227 # one could achieve the same with SYMLINKS
228 .for s in ${STAGE_AS_SETS:O:u}
229 STAGE_AS.$s ?= ${.ALLSRC:N.dirdep:Nstage_*}
231 stage_as: stage_as.$s
233 @${STAGE_AS_SCRIPT}; StageAs ${FLAGS.$@} ${STAGE_FILES_DIR.$s:U${STAGE_DIR.$s}:${STAGE_DIR_FILTER}} ${STAGE_AS.$s:@f@$f ${STAGE_AS_${f:tA}:U${STAGE_AS_${f:T}:U${f:T}}}@}
239 CLEANFILES += ${STAGE_TARGETS} stage_incs stage_includes
241 # stage_*links usually needs to follow any others.
242 # for non-jobs mode the order here matters
243 staging: ${STAGE_TARGETS:N*_links} ${STAGE_TARGETS:M*_links}
245 .if ${.MAKE.JOBS:U0} > 0 && ${STAGE_TARGETS:U:M*_links} != ""
246 # the above isn't sufficient
247 .for t in ${STAGE_TARGETS:N*links:O:u}
248 .ORDER: $t stage_links
252 # generally we want staging to wait until everything else is done
253 STAGING_WAIT ?= .WAIT
255 .if ${.MAKE.LEVEL} > 0
256 all: ${STAGING_WAIT} staging
259 .if exists(${.PARSEDIR}/stage-install.sh) && !defined(STAGE_INSTALL)
260 # this will run install(1) and then followup with .dirdep files.
261 STAGE_INSTALL := sh ${.PARSEDIR:tA}/stage-install.sh INSTALL="${INSTALL}" OBJDIR=${.OBJDIR:tA}
264 # if ${INSTALL} gets run during 'all' assume it is for staging?
265 .if ${.TARGETS:Nall} == "" && defined(STAGE_INSTALL)
266 INSTALL := ${STAGE_INSTALL}
267 .if target(beforeinstall)
268 beforeinstall: .dirdep
271 .NOPATH: ${STAGE_FILES}
273 .if !empty(STAGE_TARGETS)
274 .NOPATH: ${CLEANFILES}
277 .if ${MK_STALE_STAGED} == "yes"
279 # get a list of paths that we have just staged
280 # get a list of paths that we have previously staged to those same dirs
281 # anything in the 2nd list but not the first is stale - remove it.
282 stale_staged: staging .NOMETA
283 @egrep '^[WL] .*${STAGE_OBJTOP}' /dev/null ${.MAKE.META.FILES:M*stage_*} | \
284 sed "/\.dirdep/d;s,.* '*\(${STAGE_OBJTOP}/[^ '][^ ']*\).*,\1," | \
285 sort > ${.TARGET}.staged1
286 @grep -l '${_dirdep}' /dev/null ${_STAGED_DIRS:M${STAGE_OBJTOP}*:O:u:@d@$d/*.dirdep@} | \
287 sed 's,\.dirdep,,' | sort > ${.TARGET}.staged2
288 @comm -13 ${.TARGET}.staged1 ${.TARGET}.staged2 > ${.TARGET}.stale
289 @test ! -s ${.TARGET}.stale || { \
290 echo "Removing stale staged files..."; \
291 sed 's,.*,& &.dirdep,' ${.TARGET}.stale | xargs rm -f; }