1 # $Id: meta.stage.mk,v 1.35 2015/05/20 06:40:33 sjg Exp $
3 # @(#) Copyright (c) 2011, Simon J. Gerraty
5 # This file is provided in the hope that it will
6 # be of use. There is absolutely NO WARRANTY.
7 # Permission to copy, redistribute or otherwise
8 # use this file is hereby granted provided that
9 # the above copyright notice and this notice are
12 # Please send copies of changes and bug-fixes to:
16 .if !target(__${.PARSEFILE}__)
19 .if ${.MAKE.DEPENDFILE_PREFERENCE:U${.MAKE.DEPENDFILE}:M*.${MACHINE}} != ""
20 # this is generally safer anyway
21 _dirdep = ${RELDIR}.${MACHINE}
26 # this allows us to trace dependencies back to their src dir
28 @echo '${_dirdep}' > $@
30 .if defined(NO_POSIX_SHELL) || ${type printf:L:sh:Mbuiltin} == ""
31 _stage_file_basename = `basename $$f`
32 _stage_target_dirname = `dirname $$t`
34 _stage_file_basename = $${f\#\#*/}
35 _stage_target_dirname = $${t%/*}
38 _OBJROOT ?= ${OBJROOT:U${OBJTOP:H}}
39 .if ${_OBJROOT:M*/} != ""
40 _objroot ?= ${_OBJROOT:tA}/
42 _objroot ?= ${_OBJROOT:tA}
45 # make sure this is global
48 # add each dir we stage to to _STAGED_DIRS
49 # and make sure we have absolute paths so that bmake
50 # will match against .MAKE.META.BAILIWICK
51 STAGE_DIR_FILTER = tA:@d@$${_STAGED_DIRS::+=$$d}$$d@
52 # convert _STAGED_DIRS into suitable filters
53 GENDIRDEPS_FILTER += Nnot-empty-is-important \
54 ${_STAGED_DIRS:O:u:M${OBJTOP}*:S,${OBJTOP}/,N,} \
55 ${_STAGED_DIRS:O:u:M${_objroot}*:N${OBJTOP}*:S,${_objroot},,:C,^([^/]+)/(.*),N\2.\1,:S,${HOST_TARGET},.host,}
57 LN_CP_SCRIPT = LnCp() { \
58 rm -f $$2 2> /dev/null; \
59 ln $$1 $$2 2> /dev/null || \
62 # it is an error for more than one src dir to try and stage
64 STAGE_DIRDEP_SCRIPT = ${LN_CP_SCRIPT}; StageDirdep() { \
66 if [ -s $$t.dirdep ]; then \
67 cmp -s .dirdep $$t.dirdep && return; \
68 echo "ERROR: $$t installed by `cat $$t.dirdep` not ${_dirdep}" >&2; \
71 LnCp .dirdep $$t.dirdep || exit 1; }
73 # common logic for staging files
74 # this all relies on RELDIR being set to a subdir of SRCTOP
75 # we use ln(1) if we can, else cp(1)
76 STAGE_FILE_SCRIPT = ${STAGE_DIRDEP_SCRIPT}; StageFiles() { \
77 case "$$1" in "") return;; -m) mode=$$2; shift 2;; *) mode=;; esac; \
80 [ -s .dirdep ] || echo '${_dirdep}' > .dirdep; \
82 case "$$f" in */*) t=$$dest/${_stage_file_basename};; *) t=$$dest/$$f;; esac; \
84 LnCp $$f $$t || exit 1; \
85 [ -z "$$mode" ] || chmod $$mode $$t; \
88 STAGE_LINKS_SCRIPT = ${STAGE_DIRDEP_SCRIPT}; StageLinks() { \
89 case "$$1" in "") return;; --) shift;; -*) ldest= lnf=$$1; shift;; /*) ldest=$$1/;; esac; \
92 [ -s .dirdep ] || echo '${_dirdep}' > .dirdep; \
93 while test $$\# -ge 2; do \
94 l=$$ldest$$1; shift; \
96 case "$$1" in */*) mkdir -p ${_stage_target_dirname};; esac; \
99 rm -f $$t 2>/dev/null; \
100 ln $$lnf $$l $$t || exit 1; \
103 STAGE_AS_SCRIPT = ${STAGE_DIRDEP_SCRIPT}; StageAs() { \
104 case "$$1" in "") return;; -m) mode=$$2; shift 2;; *) mode=;; esac; \
107 [ -s .dirdep ] || echo '${_dirdep}' > .dirdep; \
108 while test $$\# -ge 2; do \
111 case "$$1" in */*) mkdir -p ${_stage_target_dirname};; esac; \
114 LnCp $$s $$t || exit 1; \
115 [ -z "$$mode" ] || chmod $$mode $$t; \
118 # this is simple, a list of the "staged" files depends on this,
119 _STAGE_BASENAME_USE: .USE ${.TARGET:T}
120 @${STAGE_FILE_SCRIPT}; StageFiles ${.TARGET:H:${STAGE_DIR_FILTER}} ${.TARGET:T}
122 _STAGE_AS_BASENAME_USE: .USE ${.TARGET:T}
123 @${STAGE_AS_SCRIPT}; StageAs ${.TARGET:H:${STAGE_DIR_FILTER}} ${.TARGET:T} ${STAGE_AS_${.TARGET:T}:U${.TARGET:T}}
125 .if !empty(STAGE_INCSDIR)
126 STAGE_TARGETS += stage_incs
127 STAGE_INCS ?= ${.ALLSRC:N.dirdep}
129 stage_includes: stage_incs
131 @${STAGE_FILE_SCRIPT}; StageFiles ${STAGE_INCSDIR:${STAGE_DIR_FILTER}} ${STAGE_INCS}
135 .if !empty(STAGE_LIBDIR)
136 STAGE_TARGETS += stage_libs
138 STAGE_LIBS ?= ${.ALLSRC:N.dirdep}
141 @${STAGE_FILE_SCRIPT}; StageFiles ${STAGE_LIBDIR:${STAGE_DIR_FILTER}} ${STAGE_LIBS}
142 .if !defined(NO_SHLIB_LINKS)
143 .if !empty(SHLIB_LINKS)
144 @${STAGE_LINKS_SCRIPT}; StageLinks -s ${STAGE_LIBDIR:${STAGE_DIR_FILTER}} \
145 ${SHLIB_LINKS:@t@${STAGE_LIBS:T:M$t.*} $t@}
146 .elif !empty(SHLIB_LINK) && !empty(SHLIB_NAME)
147 @${STAGE_LINKS_SCRIPT}; StageLinks -s ${STAGE_LIBDIR:${STAGE_DIR_FILTER}} ${SHLIB_NAME} ${SHLIB_LINK} ${SYMLINKS:T}
153 .if !empty(STAGE_DIR)
154 STAGE_SETS += _default
155 STAGE_DIR._default = ${STAGE_DIR}
156 STAGE_LINKS_DIR._default = ${STAGE_LINKS_DIR:U${STAGE_OBJTOP}}
157 STAGE_SYMLINKS_DIR._default = ${STAGE_SYMLINKS_DIR:U${STAGE_OBJTOP}}
158 STAGE_FILES._default = ${STAGE_FILES}
159 STAGE_LINKS._default = ${STAGE_LINKS}
160 STAGE_SYMLINKS._default = ${STAGE_SYMLINKS}
161 STAGE_FILES ?= ${.ALLSRC:N.dirdep:Nstage_*}
162 STAGE_SYMLINKS ?= ${.ALLSRC:T:N.dirdep:Nstage_*}
165 .if !empty(STAGE_SETS)
166 CLEANFILES += ${STAGE_SETS:@s@stage*$s@}
168 # some makefiles need to populate multiple directories
169 .for s in ${STAGE_SETS:O:u}
170 STAGE_FILES.$s ?= ${.ALLSRC:N.dirdep}
171 STAGE_SYMLINKS.$s ?= ${.ALLSRC:N.dirdep}
172 STAGE_LINKS_DIR.$s ?= ${STAGE_OBJTOP}
173 STAGE_SYMLINKS_DIR.$s ?= ${STAGE_OBJTOP}
175 STAGE_TARGETS += stage_files
177 stage_files: stage_files.$s
178 stage_files.$s: .dirdep
182 @${STAGE_FILE_SCRIPT}; StageFiles ${FLAGS.$@} ${STAGE_FILES_DIR.$s:U${STAGE_DIR.$s}:${STAGE_DIR_FILTER}} ${STAGE_FILES.$s}
185 STAGE_TARGETS += stage_links
187 stage_links: stage_links.$s
188 stage_links.$s: .dirdep
192 @${STAGE_LINKS_SCRIPT}; StageLinks ${STAGE_LINKS_DIR.$s:U${STAGE_DIR.$s}:${STAGE_DIR_FILTER}} ${STAGE_LINKS.$s}
195 STAGE_TARGETS += stage_symlinks
197 stage_symlinks: stage_symlinks.$s
198 stage_symlinks.$s: .dirdep
200 stage_symlinks: .dirdep
202 @${STAGE_LINKS_SCRIPT}; StageLinks -s ${STAGE_SYMLINKS_DIR.$s:U${STAGE_DIR.$s}:${STAGE_DIR_FILTER}} ${STAGE_SYMLINKS.$s}
208 .if !empty(STAGE_AS_SETS)
209 CLEANFILES += ${STAGE_AS_SETS:@s@stage*$s@}
211 STAGE_TARGETS += stage_as
213 # sometimes things need to be renamed as they are staged
214 # each ${file} will be staged as ${STAGE_AS_${file:T}}
215 # one could achieve the same with SYMLINKS
216 .for s in ${STAGE_AS_SETS:O:u}
217 STAGE_AS.$s ?= ${.ALLSRC:N.dirdep}
219 stage_as: stage_as.$s
221 @${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}}}@}
227 CLEANFILES += ${STAGE_TARGETS} stage_incs stage_includes
229 # stage_*links usually needs to follow any others.
230 .for t in ${STAGE_TARGETS:N*links:O:u}
231 .ORDER: $t stage_links
232 .ORDER: $t stage_symlinks
235 # make sure this exists
238 # generally we want staging to wait until everything else is done
239 STAGING_WAIT ?= .WAIT
241 .if ${.MAKE.LEVEL} > 0
242 all: ${STAGING_WAIT} staging
245 .if exists(${.PARSEDIR}/stage-install.sh) && !defined(STAGE_INSTALL)
246 # this will run install(1) and then followup with .dirdep files.
247 STAGE_INSTALL := sh ${.PARSEDIR:tA}/stage-install.sh INSTALL="${INSTALL}" OBJDIR=${.OBJDIR:tA}
250 # if ${INSTALL} gets run during 'all' assume it is for staging?
251 .if ${.TARGETS:Nall} == "" && defined(STAGE_INSTALL)
252 INSTALL := ${STAGE_INSTALL}
253 .if target(beforeinstall)
254 beforeinstall: .dirdep
257 .NOPATH: ${STAGE_FILES}
259 .if !empty(STAGE_TARGETS)
261 .if ${MK_STALE_STAGED} == "yes"
263 # get a list of paths that we have just staged
264 # get a list of paths that we have previously staged to those same dirs
265 # anything in the 2nd list but not the first is stale - remove it.
266 stale_staged: staging .NOMETA
267 @egrep '^[WL] .*${STAGE_OBJTOP}' /dev/null ${.MAKE.META.FILES:M*stage_*} | \
268 sed "/\.dirdep/d;s,.* '*\(${STAGE_OBJTOP}/[^ '][^ ']*\).*,\1," | \
269 sort > ${.TARGET}.staged1
270 @grep -l '${_dirdep}' /dev/null ${_STAGED_DIRS:M${STAGE_OBJTOP}*:O:u:@d@$d/*.dirdep@} | \
271 sed 's,\.dirdep,,' | sort > ${.TARGET}.staged2
272 @comm -13 ${.TARGET}.staged1 ${.TARGET}.staged2 > ${.TARGET}.stale
273 @test ! -s ${.TARGET}.stale || { \
274 echo "Removing stale staged files..."; \
275 sed 's,.*,& &.dirdep,' ${.TARGET}.stale | xargs rm -f; }