1 # $Id: meta.stage.mk,v 1.67 2023/04/17 01:22:10 sjg Exp $
3 # @(#) Copyright (c) 2011-2017, 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:
18 .if !target(__${.PARSEFILE}__)
19 # the guard target is defined later
21 .-include <local.meta.stage.mk>
23 .if ${.MAKE.DEPENDFILE_PREFERENCE:U${.MAKE.DEPENDFILE}:M*.${MACHINE}} != ""
24 # this is generally safer anyway
25 _dirdep ?= ${RELDIR}.${TARGET_SPEC:U${MACHINE}}
32 # this allows us to trace dependencies back to their src dir
34 .if !commands(.dirdep)
36 @echo '${_dirdep}' > $@
39 .if defined(NO_POSIX_SHELL) || ${type printf:L:sh:Mbuiltin} == ""
40 _stage_file_basename = `basename $$f`
41 _stage_target_dirname = `dirname $$t`
43 _stage_file_basename = $${f\#\#*/}
44 _stage_target_dirname = $${t%/*}
47 _OBJROOT ?= ${OBJROOT:U${OBJTOP:H}}
48 .if ${_OBJROOT:M*/} != ""
49 _objroot ?= ${_OBJROOT:tA}/
51 _objroot ?= ${_OBJROOT:tA}
54 # make sure this is global
57 # add each dir we stage to _STAGED_DIRS
58 # and make sure we have absolute paths so that bmake
59 # will match against .MAKE.META.BAILIWICK
60 STAGE_DIR_FILTER = tA:@d@$${_STAGED_DIRS::+=$$d}$$d@
61 # convert _STAGED_DIRS into suitable filters
62 GENDIRDEPS_FILTER += Nnot-empty-is-important \
63 ${_STAGED_DIRS:O:u:M${OBJTOP}*:S,${OBJTOP}/,N,} \
64 ${_STAGED_DIRS:O:u:M${_objroot}*:N${OBJTOP}*:S,${_objroot},,:C,^([^/]+)/(.*),N\2.\1,:S,${HOST_TARGET},.host,}
66 LN_CP_SCRIPT = LnCp() { \
67 rm -f $$2 2> /dev/null; \
68 { [ -z "$$mode" ] && ${LN:Uln} $$1 $$2 2> /dev/null; } || \
69 cp -p $$1 $$2 2> /dev/null || cp $$1 $$2; }
71 # a staging conflict should cause an error
72 # a warning is handy when bootstapping different options.
73 STAGE_CONFLICT?= ERROR
74 .if ${STAGE_CONFLICT:tl} == "error"
75 STAGE_CONFLICT_ACTION= exit 1
77 STAGE_CONFLICT_ACTION=
80 # it is an error for more than one src dir to try and stage
82 STAGE_DIRDEP_SCRIPT = ${LN_CP_SCRIPT}; StageDirdep() { \
84 if [ -s $$t.dirdep ]; then \
85 cmp -s .dirdep $$t.dirdep && return; \
87 case "${RELDIR}:${_dirdep}" in $${x%.*}:$${x}*) ;; \
88 *) echo "${STAGE_CONFLICT}: $$t installed by $$x not ${_dirdep}" >&2; \
89 ${STAGE_CONFLICT_ACTION} ;; esac; \
91 LnCp .dirdep $$t.dirdep || exit 1; }
93 # common logic for staging files
94 # this all relies on RELDIR being set to a subdir of SRCTOP
95 # we use ln(1) if we can, else cp(1)
96 STAGE_FILE_SCRIPT = ${STAGE_DIRDEP_SCRIPT}; StageFiles() { \
97 case "$$1" in "") return;; -m) mode=$$2; shift 2;; *) mode=;; esac; \
100 [ -s .dirdep ] || echo '${_dirdep}' > .dirdep; \
102 case "$$f" in */*) t=$$dest/${_stage_file_basename};; *) t=$$dest/$$f;; esac; \
104 LnCp $$f $$t || exit 1; \
105 [ -z "$$mode" ] || chmod $$mode $$t; \
108 STAGE_LINKS_SCRIPT = ${STAGE_DIRDEP_SCRIPT}; StageLinks() { \
109 case "$$1" in "") return;; --) shift;; -*) ldest= lnf=$$1; shift;; /*) ldest=$$1/;; esac; \
112 [ -s .dirdep ] || echo '${_dirdep}' > .dirdep; \
113 while test $$\# -ge 2; do \
114 l=$$ldest$$1; shift; \
116 case "$$1" in */*) mkdir -p ${_stage_target_dirname};; esac; \
119 rm -f $$t 2>/dev/null; \
120 ln $$lnf $$l $$t || exit 1; \
123 STAGE_AS_SCRIPT = ${STAGE_DIRDEP_SCRIPT}; StageAs() { \
124 case "$$1" in "") return;; -m) mode=$$2; shift 2;; *) mode=;; esac; \
127 [ -s .dirdep ] || echo '${_dirdep}' > .dirdep; \
128 while test $$\# -ge 2; do \
131 case "$$1" in */*) mkdir -p ${_stage_target_dirname};; esac; \
134 LnCp $$s $$t || exit 1; \
135 [ -z "$$mode" ] || chmod $$mode $$t; \
138 # this is simple, a list of the "staged" files depends on this,
139 _STAGE_BASENAME_USE: .USE .dirdep ${.TARGET:T}
140 @${STAGE_FILE_SCRIPT}; StageFiles ${.TARGET:H:${STAGE_DIR_FILTER}} ${.TARGET:T}
142 _STAGE_AS_BASENAME_USE: .USE .dirdep ${.TARGET:T}
143 @${STAGE_AS_SCRIPT}; StageAs ${.TARGET:H:${STAGE_DIR_FILTER}} ${.TARGET:T} ${STAGE_AS_${.TARGET:T}:U${.TARGET:T}}
149 .if !empty(STAGE_INCSDIR)
150 .if !empty(STAGE_INCS)
151 stage_incs: ${STAGE_INCS:N*\**}
153 .if target(stage_incs) || !empty(.ALLTARGETS:Mstage_includes)
154 STAGE_TARGETS += stage_incs
155 STAGE_INCS ?= ${.ALLSRC:N.dirdep:Nstage_*}
156 stage_includes: stage_incs
158 @${STAGE_FILE_SCRIPT}; StageFiles ${STAGE_INCSDIR:${STAGE_DIR_FILTER}} ${STAGE_INCS}
164 .if !empty(STAGE_LIBDIR)
165 .if !empty(STAGE_LIBS)
166 stage_libs: ${STAGE_LIBS:N*\**}
168 .if target(stage_libs)
169 STAGE_TARGETS += stage_libs
170 STAGE_LIBS ?= ${.ALLSRC:N.dirdep:Nstage_*}
172 @${STAGE_FILE_SCRIPT}; StageFiles ${STAGE_LIBDIR:${STAGE_DIR_FILTER}} ${STAGE_LIBS}
173 .if !defined(NO_SHLIB_LINKS)
174 .if !empty(SHLIB_LINKS)
175 @${STAGE_LINKS_SCRIPT}; StageLinks -s ${STAGE_LIBDIR:${STAGE_DIR_FILTER}} \
176 ${SHLIB_LINKS:@t@${STAGE_LIBS:T:M$t.*:${STAGE_SHLIB_LINKS_FILTER:U}} $t@}
177 .elif !empty(SHLIB_LINK) && !empty(SHLIB_NAME)
178 @${STAGE_LINKS_SCRIPT}; StageLinks -s ${STAGE_LIBDIR:${STAGE_DIR_FILTER}} ${SHLIB_NAME} ${SHLIB_LINK}
185 .if !empty(STAGE_DIR)
186 STAGE_SETS += _default
187 STAGE_DIR._default = ${STAGE_DIR}
188 STAGE_LINKS_DIR._default = ${STAGE_LINKS_DIR:U${STAGE_OBJTOP}}
189 STAGE_SYMLINKS_DIR._default = ${STAGE_SYMLINKS_DIR:U${STAGE_OBJTOP}}
190 STAGE_FILES._default = ${STAGE_FILES}
191 STAGE_LINKS._default = ${STAGE_LINKS}
192 STAGE_SYMLINKS._default = ${STAGE_SYMLINKS}
195 .if !empty(STAGE_SETS)
196 CLEANFILES += ${STAGE_SETS:@s@stage*$s@}
198 # some makefiles need to populate multiple directories
199 .for s in ${STAGE_SETS:O:u}
200 .if !empty(STAGE_FILES.$s)
201 stage_files.$s: ${STAGE_FILES.$s:N*\**}
203 .if target(stage_files.$s) || target(stage_files${s:S,^,.,:N._default})
204 STAGE_TARGETS += stage_files
205 STAGE_FILES.$s ?= ${.ALLSRC:N.dirdep:Nstage_*}
206 .if !target(.stage_files.$s)
209 stage_files: stage_files.$s
210 stage_files.$s: .dirdep
212 STAGE_FILES ?= ${.ALLSRC:N.dirdep:Nstage_*}
215 @${STAGE_FILE_SCRIPT}; StageFiles ${FLAGS.$@} ${STAGE_FILES_DIR.$s:U${STAGE_DIR.$s}:${STAGE_DIR_FILTER}} ${STAGE_FILES.$s:O}
220 .if !empty(STAGE_LINKS.$s)
223 .if target(stage_links.$s) || target(stage_links${s:S,^,.,:N._default})
224 STAGE_LINKS_DIR.$s ?= ${STAGE_OBJTOP}
225 STAGE_TARGETS += stage_links
226 .if !target(.stage_links.$s)
229 stage_links: stage_links.$s
230 stage_links.$s: .dirdep
234 @${STAGE_LINKS_SCRIPT}; StageLinks ${STAGE_LINKS_DIR.$s:U${STAGE_DIR.$s}:${STAGE_DIR_FILTER}} ${STAGE_LINKS.$s}
239 .if !empty(STAGE_SYMLINKS.$s)
242 .if target(stage_symlinks.$s) || target(stage_symlinks${s:S,^,.,:N._default})
243 STAGE_SYMLINKS_DIR.$s ?= ${STAGE_OBJTOP}
244 STAGE_TARGETS += stage_symlinks
245 .if !target(.stage_symlinks.$s)
248 stage_symlinks: stage_symlinks.$s
249 stage_symlinks.$s: .dirdep
251 stage_symlinks: .dirdep
253 @${STAGE_LINKS_SCRIPT}; StageLinks -s ${STAGE_SYMLINKS_DIR.$s:U${STAGE_DIR.$s}:${STAGE_DIR_FILTER}} ${STAGE_SYMLINKS.$s}
261 .if !empty(STAGE_AS_SETS)
262 CLEANFILES += ${STAGE_AS_SETS:@s@stage*$s@}
264 # sometimes things need to be renamed as they are staged
265 # each ${file} will be staged as ${STAGE_AS_${file:T}}
266 # one could achieve the same with SYMLINKS
267 # stage_as_and_symlink makes the original name (or ${STAGE_LINK_AS_${name}})
268 # a symlink to the new name
269 # it is the same as using stage_as and stage_symlinks but ensures
270 # both operations happen together
271 .for s in ${STAGE_AS_SETS:O:u}
272 .if !empty(STAGE_AS.$s)
273 stage_as.$s: ${STAGE_AS.$s:N*\**}
275 .if target(stage_as.$s)
276 STAGE_TARGETS += stage_as
277 STAGE_AS.$s ?= ${.ALLSRC:N.dirdep:Nstage_*}
278 .if !target(.stage_as.$s)
280 stage_as: stage_as.$s
282 @${STAGE_AS_SCRIPT}; StageAs ${FLAGS.$@} ${STAGE_FILES_DIR.$s:U${STAGE_DIR.$s}:${STAGE_DIR_FILTER}} ${STAGE_AS.$s:O:@f@$f ${STAGE_AS_${f:tA}:U${STAGE_AS_${f:T}:U${f:T}}}@}
287 .if !empty(STAGE_AS_AND_SYMLINK.$s)
288 stage_as_and_symlink.$s: ${STAGE_AS_AND_SYMLINK.$s:N*\**}
290 .if target(stage_as_and_symlink.$s)
291 STAGE_TARGETS += stage_as_and_symlink
292 STAGE_AS_AND_SYMLINK.$s ?= ${.ALLSRC:N.dirdep:Nstage_*}
293 .if !target(.stage_as_and_symlink.$s)
294 .stage_as_and_symlink.$s:
295 stage_as_and_symlink: stage_as_and_symlink.$s
296 stage_as_and_symlink.$s: .dirdep
297 @${STAGE_AS_SCRIPT}; StageAs ${FLAGS.$@} ${STAGE_FILES_DIR.$s:U${STAGE_DIR.$s}:${STAGE_DIR_FILTER}} ${STAGE_AS_AND_SYMLINK.$s:O:@f@$f ${STAGE_AS_${f:tA}:U${STAGE_AS_${f:T}:U${f:T}}}@}
298 @${STAGE_LINKS_SCRIPT}; StageLinks -s ${STAGE_FILES_DIR.$s:U${STAGE_DIR.$s}:${STAGE_DIR_FILTER}} ${STAGE_AS_AND_SYMLINK.$s:O:@f@${STAGE_AS_${f:tA}:U${STAGE_AS_${f:T}:U${f:T}}} ${STAGE_LINK_AS_${f}:U$f}@}
306 CLEANFILES += ${STAGE_TARGETS} stage_incs stage_includes
308 # this lot also only makes sense the first time...
309 .if !target(__${.PARSEFILE}__)
310 __${.PARSEFILE}__: .NOTMAIN
312 # stage_*links usually needs to follow any others.
313 # for non-jobs mode the order here matters
314 staging: ${STAGE_TARGETS:N*_links} ${STAGE_TARGETS:M*_links}
316 .if ${.MAKE.JOBS:U0} > 0 && ${STAGE_TARGETS:U:M*_links} != ""
317 # the above isn't sufficient
318 .for t in ${STAGE_TARGETS:N*links:O:u}
319 .ORDER: $t stage_links
323 # generally we want staging to wait until everything else is done
324 STAGING_WAIT ?= .WAIT
326 .if ${.MAKE.LEVEL} > 0
327 all: ${STAGING_WAIT} staging
330 .if exists(${.PARSEDIR}/stage-install.sh) && !defined(STAGE_INSTALL)
331 # this will run install(1) and then followup with .dirdep files.
332 STAGE_INSTALL := sh ${.PARSEDIR:tA}/stage-install.sh INSTALL="${INSTALL}" OBJDIR=${.OBJDIR:tA}
335 # if ${INSTALL} gets run during 'all' assume it is for staging?
336 .if ${.TARGETS:Nall} == "" && defined(STAGE_INSTALL)
337 INSTALL := ${STAGE_INSTALL}
338 .if target(beforeinstall)
339 beforeinstall: .dirdep
342 .NOPATH: ${STAGE_FILES}
344 .if !empty(STAGE_TARGETS)
345 # for backwards compat make sure they exist
348 .NOPATH: ${CLEANFILES}
351 .if ${MK_STALE_STAGED} == "yes"
353 # get a list of paths that we have just staged
354 # get a list of paths that we have previously staged to those same dirs
355 # anything in the 2nd list but not the first is stale - remove it.
356 stale_staged: staging .NOMETA
357 @${EGREP:Uegrep} '^[WL] .*${STAGE_OBJTOP}' /dev/null ${.MAKE.META.FILES:M*stage_*} | \
358 sed "/\.dirdep/d;s,.* '*\(${STAGE_OBJTOP}/[^ '][^ ']*\).*,\1," | \
359 sort > ${.TARGET}.staged1
360 @grep -l '${_dirdep}' /dev/null ${_STAGED_DIRS:M${STAGE_OBJTOP}*:O:u:@d@$d/*.dirdep@} | \
361 sed 's,\.dirdep,,' | sort > ${.TARGET}.staged2
362 @comm -13 ${.TARGET}.staged1 ${.TARGET}.staged2 > ${.TARGET}.stale
363 @test ! -s ${.TARGET}.stale || { \
364 echo "Removing stale staged files..."; \
365 sed 's,.*,& &.dirdep,' ${.TARGET}.stale | xargs rm -f; }