1301462Ssjg# $Id: meta.stage.mk,v 1.45 2016/05/26 03:59:09 sjg Exp $
2246149Ssjg#
3246149Ssjg#	@(#) Copyright (c) 2011, Simon J. Gerraty
4246149Ssjg#
5246149Ssjg#	This file is provided in the hope that it will
6246149Ssjg#	be of use.  There is absolutely NO WARRANTY.
7246149Ssjg#	Permission to copy, redistribute or otherwise
8246149Ssjg#	use this file is hereby granted provided that 
9246149Ssjg#	the above copyright notice and this notice are
10246149Ssjg#	left intact. 
11246149Ssjg#      
12246149Ssjg#	Please send copies of changes and bug-fixes to:
13246149Ssjg#	sjg@crufty.net
14246149Ssjg#
15246149Ssjg
16246149Ssjg.if !target(__${.PARSEFILE}__)
17246149Ssjg__${.PARSEFILE}__:
18246149Ssjg
19246149Ssjg.if ${.MAKE.DEPENDFILE_PREFERENCE:U${.MAKE.DEPENDFILE}:M*.${MACHINE}} != ""
20246149Ssjg# this is generally safer anyway
21246149Ssjg_dirdep = ${RELDIR}.${MACHINE}
22246149Ssjg.else
23246149Ssjg_dirdep = ${RELDIR}
24246149Ssjg.endif
25246149Ssjg
26289842SsjgCLEANFILES+= .dirdep
27289842Ssjg
28246149Ssjg# this allows us to trace dependencies back to their src dir
29296637Ssjg.dirdep:	.NOPATH
30246149Ssjg	@echo '${_dirdep}' > $@
31246149Ssjg
32246149Ssjg.if defined(NO_POSIX_SHELL) || ${type printf:L:sh:Mbuiltin} == ""
33246149Ssjg_stage_file_basename = `basename $$f`
34246149Ssjg_stage_target_dirname = `dirname $$t`
35246149Ssjg.else
36246149Ssjg_stage_file_basename = $${f\#\#*/}
37246149Ssjg_stage_target_dirname = $${t%/*}
38246149Ssjg.endif
39246149Ssjg
40281812Ssjg_OBJROOT ?= ${OBJROOT:U${OBJTOP:H}}
41281812Ssjg.if ${_OBJROOT:M*/} != ""
42281812Ssjg_objroot ?= ${_OBJROOT:tA}/
43281812Ssjg.else
44246149Ssjg_objroot ?= ${_OBJROOT:tA}
45281812Ssjg.endif
46281812Ssjg
47246149Ssjg# make sure this is global
48246149Ssjg_STAGED_DIRS ?=
49246149Ssjg.export _STAGED_DIRS
50246149Ssjg# add each dir we stage to to _STAGED_DIRS
51246149Ssjg# and make sure we have absolute paths so that bmake
52246149Ssjg# will match against .MAKE.META.BAILIWICK
53246149SsjgSTAGE_DIR_FILTER = tA:@d@$${_STAGED_DIRS::+=$$d}$$d@
54246149Ssjg# convert _STAGED_DIRS into suitable filters
55246149SsjgGENDIRDEPS_FILTER += Nnot-empty-is-important \
56246149Ssjg	${_STAGED_DIRS:O:u:M${OBJTOP}*:S,${OBJTOP}/,N,} \
57281812Ssjg	${_STAGED_DIRS:O:u:M${_objroot}*:N${OBJTOP}*:S,${_objroot},,:C,^([^/]+)/(.*),N\2.\1,:S,${HOST_TARGET},.host,}
58246149Ssjg
59249033SsjgLN_CP_SCRIPT = LnCp() { \
60249033Ssjg  rm -f $$2 2> /dev/null; \
61297040Ssjg  { [ -z "$$mode" ] && ln $$1 $$2 2> /dev/null; } || \
62249033Ssjg  cp -p $$1 $$2; }
63249033Ssjg
64289842Ssjg# a staging conflict should cause an error
65289842Ssjg# a warning is handy when bootstapping different options.
66289842SsjgSTAGE_CONFLICT?= ERROR
67289842Ssjg.if ${STAGE_CONFLICT:tl} == "error"
68289842SsjgSTAGE_CONFLICT_ACTION= exit 1;
69289842Ssjg.else
70289842SsjgSTAGE_CONFLICT_ACTION=
71289842Ssjg.endif
72289842Ssjg
73246149Ssjg# it is an error for more than one src dir to try and stage
74246149Ssjg# the same file
75249033SsjgSTAGE_DIRDEP_SCRIPT = ${LN_CP_SCRIPT}; StageDirdep() { \
76246149Ssjg  t=$$1; \
77246149Ssjg  if [ -s $$t.dirdep ]; then \
78246149Ssjg	cmp -s .dirdep $$t.dirdep && return; \
79289842Ssjg	echo "${STAGE_CONFLICT}: $$t installed by `cat $$t.dirdep` not ${_dirdep}" >&2; \
80289842Ssjg	${STAGE_CONFLICT_ACTION} \
81246149Ssjg  fi; \
82249033Ssjg  LnCp .dirdep $$t.dirdep || exit 1; }
83246149Ssjg
84246149Ssjg# common logic for staging files
85246149Ssjg# this all relies on RELDIR being set to a subdir of SRCTOP
86246149Ssjg# we use ln(1) if we can, else cp(1)
87246149SsjgSTAGE_FILE_SCRIPT = ${STAGE_DIRDEP_SCRIPT}; StageFiles() { \
88249033Ssjg  case "$$1" in "") return;; -m) mode=$$2; shift 2;; *) mode=;; esac; \
89246149Ssjg  dest=$$1; shift; \
90246149Ssjg  mkdir -p $$dest; \
91246149Ssjg  [ -s .dirdep ] || echo '${_dirdep}' > .dirdep; \
92246149Ssjg  for f in "$$@"; do \
93246149Ssjg	case "$$f" in */*) t=$$dest/${_stage_file_basename};; *) t=$$dest/$$f;; esac; \
94246149Ssjg	StageDirdep $$t; \
95249033Ssjg	LnCp $$f $$t || exit 1; \
96249033Ssjg	[ -z "$$mode" ] || chmod $$mode $$t; \
97246149Ssjg  done; :; }
98246149Ssjg
99246149SsjgSTAGE_LINKS_SCRIPT = ${STAGE_DIRDEP_SCRIPT}; StageLinks() { \
100249033Ssjg  case "$$1" in "") return;; --) shift;; -*) ldest= lnf=$$1; shift;; /*) ldest=$$1/;; esac; \
101246149Ssjg  dest=$$1; shift; \
102246149Ssjg  mkdir -p $$dest; \
103246149Ssjg  [ -s .dirdep ] || echo '${_dirdep}' > .dirdep; \
104246149Ssjg  while test $$\# -ge 2; do \
105246149Ssjg	l=$$ldest$$1; shift; \
106246149Ssjg	t=$$dest/$$1; \
107246149Ssjg	case "$$1" in */*) mkdir -p ${_stage_target_dirname};; esac; \
108246149Ssjg	shift; \
109246149Ssjg	StageDirdep $$t; \
110246149Ssjg	rm -f $$t 2>/dev/null; \
111249033Ssjg	ln $$lnf $$l $$t || exit 1; \
112246149Ssjg  done; :; }
113246149Ssjg
114246149SsjgSTAGE_AS_SCRIPT = ${STAGE_DIRDEP_SCRIPT}; StageAs() { \
115249033Ssjg  case "$$1" in "") return;; -m) mode=$$2; shift 2;; *) mode=;; esac; \
116246149Ssjg  dest=$$1; shift; \
117246149Ssjg  mkdir -p $$dest; \
118246149Ssjg  [ -s .dirdep ] || echo '${_dirdep}' > .dirdep; \
119246149Ssjg  while test $$\# -ge 2; do \
120246149Ssjg	s=$$1; shift; \
121246149Ssjg	t=$$dest/$$1; \
122246149Ssjg	case "$$1" in */*) mkdir -p ${_stage_target_dirname};; esac; \
123246149Ssjg	shift; \
124246149Ssjg	StageDirdep $$t; \
125249033Ssjg	LnCp $$s $$t || exit 1; \
126249033Ssjg	[ -z "$$mode" ] || chmod $$mode $$t; \
127246149Ssjg  done; :; }
128246149Ssjg
129246149Ssjg# this is simple, a list of the "staged" files depends on this,
130246149Ssjg_STAGE_BASENAME_USE:	.USE ${.TARGET:T}
131246149Ssjg	@${STAGE_FILE_SCRIPT}; StageFiles ${.TARGET:H:${STAGE_DIR_FILTER}} ${.TARGET:T}
132246149Ssjg
133281812Ssjg_STAGE_AS_BASENAME_USE:        .USE ${.TARGET:T}
134281812Ssjg	@${STAGE_AS_SCRIPT}; StageAs ${.TARGET:H:${STAGE_DIR_FILTER}} ${.TARGET:T} ${STAGE_AS_${.TARGET:T}:U${.TARGET:T}}
135281812Ssjg
136246149Ssjg.if !empty(STAGE_INCSDIR)
137249033SsjgSTAGE_TARGETS += stage_incs
138289842SsjgSTAGE_INCS ?= ${.ALLSRC:N.dirdep:Nstage_*}
139246149Ssjg
140281812Ssjgstage_includes: stage_incs
141246149Ssjgstage_incs:	.dirdep
142246149Ssjg	@${STAGE_FILE_SCRIPT}; StageFiles ${STAGE_INCSDIR:${STAGE_DIR_FILTER}} ${STAGE_INCS}
143246149Ssjg	@touch $@
144246149Ssjg.endif
145246149Ssjg
146246149Ssjg.if !empty(STAGE_LIBDIR)
147249033SsjgSTAGE_TARGETS += stage_libs
148246149Ssjg
149289842SsjgSTAGE_LIBS ?= ${.ALLSRC:N.dirdep:Nstage_*}
150246149Ssjg
151246149Ssjgstage_libs:	.dirdep
152246149Ssjg	@${STAGE_FILE_SCRIPT}; StageFiles ${STAGE_LIBDIR:${STAGE_DIR_FILTER}} ${STAGE_LIBS}
153281812Ssjg.if !defined(NO_SHLIB_LINKS)
154246149Ssjg.if !empty(SHLIB_LINKS)
155246149Ssjg	@${STAGE_LINKS_SCRIPT}; StageLinks -s ${STAGE_LIBDIR:${STAGE_DIR_FILTER}} \
156246149Ssjg	${SHLIB_LINKS:@t@${STAGE_LIBS:T:M$t.*} $t@}
157246149Ssjg.elif !empty(SHLIB_LINK) && !empty(SHLIB_NAME)
158291978Ssjg	@${STAGE_LINKS_SCRIPT}; StageLinks -s ${STAGE_LIBDIR:${STAGE_DIR_FILTER}} ${SHLIB_NAME} ${SHLIB_LINK}
159246149Ssjg.endif
160281812Ssjg.endif
161246149Ssjg	@touch $@
162246149Ssjg.endif
163246149Ssjg
164246149Ssjg.if !empty(STAGE_DIR)
165246149SsjgSTAGE_SETS += _default
166246149SsjgSTAGE_DIR._default = ${STAGE_DIR}
167246149SsjgSTAGE_LINKS_DIR._default = ${STAGE_LINKS_DIR:U${STAGE_OBJTOP}}
168246149SsjgSTAGE_SYMLINKS_DIR._default = ${STAGE_SYMLINKS_DIR:U${STAGE_OBJTOP}}
169246149SsjgSTAGE_FILES._default = ${STAGE_FILES}
170246149SsjgSTAGE_LINKS._default = ${STAGE_LINKS}
171246149SsjgSTAGE_SYMLINKS._default = ${STAGE_SYMLINKS}
172246149SsjgSTAGE_FILES ?= ${.ALLSRC:N.dirdep:Nstage_*}
173246149SsjgSTAGE_SYMLINKS ?= ${.ALLSRC:T:N.dirdep:Nstage_*}
174246149Ssjg.endif
175246149Ssjg
176246149Ssjg.if !empty(STAGE_SETS)
177246149SsjgCLEANFILES += ${STAGE_SETS:@s@stage*$s@}
178246149Ssjg
179246149Ssjg# some makefiles need to populate multiple directories
180246149Ssjg.for s in ${STAGE_SETS:O:u}
181289842SsjgSTAGE_FILES.$s ?= ${.ALLSRC:N.dirdep:Nstage_*}
182289842SsjgSTAGE_SYMLINKS.$s ?= ${.ALLSRC:N.dirdep:Nstage_*}
183246149SsjgSTAGE_LINKS_DIR.$s ?= ${STAGE_OBJTOP}
184246149SsjgSTAGE_SYMLINKS_DIR.$s ?= ${STAGE_OBJTOP}
185246149Ssjg
186249033SsjgSTAGE_TARGETS += stage_files
187246149Ssjg.if $s != "_default"
188246149Ssjgstage_files:	stage_files.$s
189246149Ssjgstage_files.$s:	.dirdep
190246149Ssjg.else
191246149Ssjgstage_files:	.dirdep
192246149Ssjg.endif
193246149Ssjg	@${STAGE_FILE_SCRIPT}; StageFiles ${FLAGS.$@} ${STAGE_FILES_DIR.$s:U${STAGE_DIR.$s}:${STAGE_DIR_FILTER}} ${STAGE_FILES.$s}
194246149Ssjg	@touch $@
195246149Ssjg
196249033SsjgSTAGE_TARGETS += stage_links
197246149Ssjg.if $s != "_default"
198246149Ssjgstage_links:	stage_links.$s
199246149Ssjgstage_links.$s:	.dirdep
200246149Ssjg.else
201246149Ssjgstage_links:	.dirdep
202246149Ssjg.endif
203246149Ssjg	@${STAGE_LINKS_SCRIPT}; StageLinks ${STAGE_LINKS_DIR.$s:U${STAGE_DIR.$s}:${STAGE_DIR_FILTER}} ${STAGE_LINKS.$s}
204246149Ssjg	@touch $@
205246149Ssjg
206249033SsjgSTAGE_TARGETS += stage_symlinks
207246149Ssjg.if $s != "_default"
208246149Ssjgstage_symlinks:	stage_symlinks.$s
209246149Ssjgstage_symlinks.$s:	.dirdep
210246149Ssjg.else
211246149Ssjgstage_symlinks:	.dirdep
212246149Ssjg.endif
213246149Ssjg	@${STAGE_LINKS_SCRIPT}; StageLinks -s ${STAGE_SYMLINKS_DIR.$s:U${STAGE_DIR.$s}:${STAGE_DIR_FILTER}} ${STAGE_SYMLINKS.$s}
214246149Ssjg	@touch $@
215246149Ssjg
216246149Ssjg.endfor
217246149Ssjg.endif
218246149Ssjg
219246149Ssjg.if !empty(STAGE_AS_SETS)
220246149SsjgCLEANFILES += ${STAGE_AS_SETS:@s@stage*$s@}
221246149Ssjg
222249033SsjgSTAGE_TARGETS += stage_as
223249033Ssjg
224246149Ssjg# sometimes things need to be renamed as they are staged
225246149Ssjg# each ${file} will be staged as ${STAGE_AS_${file:T}}
226246149Ssjg# one could achieve the same with SYMLINKS
227246149Ssjg.for s in ${STAGE_AS_SETS:O:u}
228289842SsjgSTAGE_AS.$s ?= ${.ALLSRC:N.dirdep:Nstage_*}
229246149Ssjg
230246149Ssjgstage_as:	stage_as.$s
231246149Ssjgstage_as.$s:	.dirdep
232284254Ssjg	@${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}}}@}
233246149Ssjg	@touch $@
234246149Ssjg
235246149Ssjg.endfor
236246149Ssjg.endif
237246149Ssjg
238281812SsjgCLEANFILES += ${STAGE_TARGETS} stage_incs stage_includes
239249033Ssjg
240249033Ssjg# stage_*links usually needs to follow any others.
241289842Ssjg# for non-jobs mode the order here matters
242289842Ssjgstaging: ${STAGE_TARGETS:N*_links} ${STAGE_TARGETS:M*_links}
243289842Ssjg
244296637Ssjg.if ${.MAKE.JOBS:U0} > 0 && ${STAGE_TARGETS:U:M*_links} != ""
245289842Ssjg# the above isn't sufficient
246249033Ssjg.for t in ${STAGE_TARGETS:N*links:O:u}
247249033Ssjg.ORDER: $t stage_links
248249033Ssjg.endfor
249289842Ssjg.endif
250249033Ssjg
251249033Ssjg# generally we want staging to wait until everything else is done
252249033SsjgSTAGING_WAIT ?= .WAIT
253249033Ssjg
254284254Ssjg.if ${.MAKE.LEVEL} > 0
255249033Ssjgall: ${STAGING_WAIT} staging
256284254Ssjg.endif
257249033Ssjg
258250837Ssjg.if exists(${.PARSEDIR}/stage-install.sh) && !defined(STAGE_INSTALL)
259250837Ssjg# this will run install(1) and then followup with .dirdep files.
260250837SsjgSTAGE_INSTALL := sh ${.PARSEDIR:tA}/stage-install.sh INSTALL="${INSTALL}" OBJDIR=${.OBJDIR:tA}
261246149Ssjg.endif
262250837Ssjg
263250837Ssjg# if ${INSTALL} gets run during 'all' assume it is for staging?
264250837Ssjg.if ${.TARGETS:Nall} == "" && defined(STAGE_INSTALL)
265250837SsjgINSTALL := ${STAGE_INSTALL}
266250837Ssjg.if target(beforeinstall)
267250837Ssjgbeforeinstall: .dirdep
268250837Ssjg.endif
269250837Ssjg.endif
270281812Ssjg.NOPATH: ${STAGE_FILES}
271250837Ssjg
272281812Ssjg.if !empty(STAGE_TARGETS)
273301462Ssjg.NOPATH: ${CLEANFILES}
274301462Ssjg
275281812SsjgMK_STALE_STAGED?= no
276281812Ssjg.if ${MK_STALE_STAGED} == "yes"
277281812Ssjgall: stale_staged
278281812Ssjg# get a list of paths that we have just staged
279281812Ssjg# get a list of paths that we have previously staged to those same dirs
280281812Ssjg# anything in the 2nd list but not the first is stale - remove it.
281281812Ssjgstale_staged: staging .NOMETA
282281812Ssjg	@egrep '^[WL] .*${STAGE_OBJTOP}' /dev/null ${.MAKE.META.FILES:M*stage_*} | \
283281812Ssjg	sed "/\.dirdep/d;s,.* '*\(${STAGE_OBJTOP}/[^ '][^ ']*\).*,\1," | \
284281812Ssjg	sort > ${.TARGET}.staged1
285281812Ssjg	@grep -l '${_dirdep}' /dev/null ${_STAGED_DIRS:M${STAGE_OBJTOP}*:O:u:@d@$d/*.dirdep@} | \
286281812Ssjg	sed 's,\.dirdep,,' | sort > ${.TARGET}.staged2
287281812Ssjg	@comm -13 ${.TARGET}.staged1 ${.TARGET}.staged2 > ${.TARGET}.stale
288281812Ssjg	@test ! -s ${.TARGET}.stale || { \
289281812Ssjg		echo "Removing stale staged files..."; \
290281812Ssjg		sed 's,.*,& &.dirdep,' ${.TARGET}.stale | xargs rm -f; }
291281812Ssjg
292250837Ssjg.endif
293281812Ssjg.endif
294281812Ssjg.endif
295