meta.stage.mk revision 288475
1284345Ssjg# $FreeBSD: head/share/mk/meta.stage.mk 288475 2015-10-02 06:24:09Z bdrewery $
2284345Ssjg# $Id: meta.stage.mk,v 1.35 2015/05/20 06:40:33 sjg Exp $
3284345Ssjg#
4284345Ssjg#	@(#) Copyright (c) 2011, Simon J. Gerraty
5284345Ssjg#
6284345Ssjg#	This file is provided in the hope that it will
7284345Ssjg#	be of use.  There is absolutely NO WARRANTY.
8284345Ssjg#	Permission to copy, redistribute or otherwise
9284345Ssjg#	use this file is hereby granted provided that 
10284345Ssjg#	the above copyright notice and this notice are
11284345Ssjg#	left intact. 
12284345Ssjg#      
13284345Ssjg#	Please send copies of changes and bug-fixes to:
14284345Ssjg#	sjg@crufty.net
15284345Ssjg#
16284345Ssjg
17284345Ssjg.if !target(__${.PARSEFILE}__)
18284345Ssjg__${.PARSEFILE}__:
19284345Ssjg
20284345Ssjg.if ${.MAKE.DEPENDFILE_PREFERENCE:U${.MAKE.DEPENDFILE}:M*.${MACHINE}} != ""
21284345Ssjg# this is generally safer anyway
22284345Ssjg_dirdep = ${RELDIR}.${MACHINE}
23284345Ssjg.else
24284345Ssjg_dirdep = ${RELDIR}
25284345Ssjg.endif
26284345Ssjg
27284345Ssjg# this allows us to trace dependencies back to their src dir
28284345Ssjg.dirdep:
29284345Ssjg	@echo '${_dirdep}' > $@
30284345Ssjg
31284345Ssjg.if defined(NO_POSIX_SHELL) || ${type printf:L:sh:Mbuiltin} == ""
32284345Ssjg_stage_file_basename = `basename $$f`
33284345Ssjg_stage_target_dirname = `dirname $$t`
34284345Ssjg.else
35284345Ssjg_stage_file_basename = $${f\#\#*/}
36284345Ssjg_stage_target_dirname = $${t%/*}
37284345Ssjg.endif
38284345Ssjg
39284345Ssjg_OBJROOT ?= ${OBJROOT:U${OBJTOP:H}}
40284345Ssjg.if ${_OBJROOT:M*/} != ""
41284345Ssjg_objroot ?= ${_OBJROOT:tA}/
42284345Ssjg.else
43284345Ssjg_objroot ?= ${_OBJROOT:tA}
44284345Ssjg.endif
45284345Ssjg
46284345Ssjg# make sure this is global
47284345Ssjg_STAGED_DIRS ?=
48284345Ssjg.export _STAGED_DIRS
49284345Ssjg# add each dir we stage to to _STAGED_DIRS
50284345Ssjg# and make sure we have absolute paths so that bmake
51284345Ssjg# will match against .MAKE.META.BAILIWICK
52284345SsjgSTAGE_DIR_FILTER = tA:@d@$${_STAGED_DIRS::+=$$d}$$d@
53284345Ssjg# convert _STAGED_DIRS into suitable filters
54284345SsjgGENDIRDEPS_FILTER += Nnot-empty-is-important \
55284345Ssjg	${_STAGED_DIRS:O:u:M${OBJTOP}*:S,${OBJTOP}/,N,} \
56284345Ssjg	${_STAGED_DIRS:O:u:M${_objroot}*:N${OBJTOP}*:S,${_objroot},,:C,^([^/]+)/(.*),N\2.\1,:S,${HOST_TARGET},.host,}
57284345Ssjg
58284345SsjgLN_CP_SCRIPT = LnCp() { \
59284345Ssjg  rm -f $$2 2> /dev/null; \
60284345Ssjg  ln $$1 $$2 2> /dev/null || \
61284345Ssjg  cp -p $$1 $$2; }
62284345Ssjg
63284478Ssjg# a staging conflict should cause an error
64284478Ssjg# a warning is handy when bootstapping different options.
65284478SsjgSTAGE_CONFLICT?= ERROR
66284478Ssjg.if ${STAGE_CONFLICT:tl} == "error"
67284478SsjgSTAGE_CONFLICT_ACTION= exit 1;
68284478Ssjg.else
69284478SsjgSTAGE_CONFLICT_ACTION=
70284478Ssjg.endif
71284478Ssjg
72284345Ssjg# it is an error for more than one src dir to try and stage
73284345Ssjg# the same file
74284345SsjgSTAGE_DIRDEP_SCRIPT = ${LN_CP_SCRIPT}; StageDirdep() { \
75284345Ssjg  t=$$1; \
76284345Ssjg  if [ -s $$t.dirdep ]; then \
77284345Ssjg	cmp -s .dirdep $$t.dirdep && return; \
78284478Ssjg	echo "${STAGE_CONFLICT}: $$t installed by `cat $$t.dirdep` not ${_dirdep}" >&2; \
79284478Ssjg	${STAGE_CONFLICT_ACTION} \
80284345Ssjg  fi; \
81284345Ssjg  LnCp .dirdep $$t.dirdep || exit 1; }
82284345Ssjg
83284345Ssjg# common logic for staging files
84284345Ssjg# this all relies on RELDIR being set to a subdir of SRCTOP
85284345Ssjg# we use ln(1) if we can, else cp(1)
86284345SsjgSTAGE_FILE_SCRIPT = ${STAGE_DIRDEP_SCRIPT}; StageFiles() { \
87284345Ssjg  case "$$1" in "") return;; -m) mode=$$2; shift 2;; *) mode=;; esac; \
88284345Ssjg  dest=$$1; shift; \
89284345Ssjg  mkdir -p $$dest; \
90284345Ssjg  [ -s .dirdep ] || echo '${_dirdep}' > .dirdep; \
91284345Ssjg  for f in "$$@"; do \
92284345Ssjg	case "$$f" in */*) t=$$dest/${_stage_file_basename};; *) t=$$dest/$$f;; esac; \
93284345Ssjg	StageDirdep $$t; \
94284345Ssjg	LnCp $$f $$t || exit 1; \
95284345Ssjg	[ -z "$$mode" ] || chmod $$mode $$t; \
96284345Ssjg  done; :; }
97284345Ssjg
98284345SsjgSTAGE_LINKS_SCRIPT = ${STAGE_DIRDEP_SCRIPT}; StageLinks() { \
99284345Ssjg  case "$$1" in "") return;; --) shift;; -*) ldest= lnf=$$1; shift;; /*) ldest=$$1/;; esac; \
100284345Ssjg  dest=$$1; shift; \
101284345Ssjg  mkdir -p $$dest; \
102284345Ssjg  [ -s .dirdep ] || echo '${_dirdep}' > .dirdep; \
103284345Ssjg  while test $$\# -ge 2; do \
104284345Ssjg	l=$$ldest$$1; shift; \
105284345Ssjg	t=$$dest/$$1; \
106284345Ssjg	case "$$1" in */*) mkdir -p ${_stage_target_dirname};; esac; \
107284345Ssjg	shift; \
108284345Ssjg	StageDirdep $$t; \
109284345Ssjg	rm -f $$t 2>/dev/null; \
110284345Ssjg	ln $$lnf $$l $$t || exit 1; \
111284345Ssjg  done; :; }
112284345Ssjg
113284345SsjgSTAGE_AS_SCRIPT = ${STAGE_DIRDEP_SCRIPT}; StageAs() { \
114284345Ssjg  case "$$1" in "") return;; -m) mode=$$2; shift 2;; *) mode=;; esac; \
115284345Ssjg  dest=$$1; shift; \
116284345Ssjg  mkdir -p $$dest; \
117284345Ssjg  [ -s .dirdep ] || echo '${_dirdep}' > .dirdep; \
118284345Ssjg  while test $$\# -ge 2; do \
119284345Ssjg	s=$$1; shift; \
120284345Ssjg	t=$$dest/$$1; \
121284345Ssjg	case "$$1" in */*) mkdir -p ${_stage_target_dirname};; esac; \
122284345Ssjg	shift; \
123284345Ssjg	StageDirdep $$t; \
124284345Ssjg	LnCp $$s $$t || exit 1; \
125284345Ssjg	[ -z "$$mode" ] || chmod $$mode $$t; \
126284345Ssjg  done; :; }
127284345Ssjg
128284345Ssjg# this is simple, a list of the "staged" files depends on this,
129284345Ssjg_STAGE_BASENAME_USE:	.USE ${.TARGET:T}
130284345Ssjg	@${STAGE_FILE_SCRIPT}; StageFiles ${.TARGET:H:${STAGE_DIR_FILTER}} ${.TARGET:T}
131284345Ssjg
132284345Ssjg_STAGE_AS_BASENAME_USE:        .USE ${.TARGET:T}
133284345Ssjg	@${STAGE_AS_SCRIPT}; StageAs ${.TARGET:H:${STAGE_DIR_FILTER}} ${.TARGET:T} ${STAGE_AS_${.TARGET:T}:U${.TARGET:T}}
134284345Ssjg
135284345Ssjg.if !empty(STAGE_INCSDIR)
136284345SsjgSTAGE_TARGETS += stage_incs
137284345SsjgSTAGE_INCS ?= ${.ALLSRC:N.dirdep}
138284345Ssjg
139284345Ssjgstage_includes: stage_incs
140284345Ssjgstage_incs:	.dirdep
141284345Ssjg	@${STAGE_FILE_SCRIPT}; StageFiles ${STAGE_INCSDIR:${STAGE_DIR_FILTER}} ${STAGE_INCS}
142284345Ssjg	@touch $@
143284345Ssjg.endif
144284345Ssjg
145284345Ssjg.if !empty(STAGE_LIBDIR)
146284345SsjgSTAGE_TARGETS += stage_libs
147284345Ssjg
148284345SsjgSTAGE_LIBS ?= ${.ALLSRC:N.dirdep}
149284345Ssjg
150284345Ssjgstage_libs:	.dirdep
151284345Ssjg	@${STAGE_FILE_SCRIPT}; StageFiles ${STAGE_LIBDIR:${STAGE_DIR_FILTER}} ${STAGE_LIBS}
152284345Ssjg.if !defined(NO_SHLIB_LINKS)
153284345Ssjg.if !empty(SHLIB_LINKS)
154284345Ssjg	@${STAGE_LINKS_SCRIPT}; StageLinks -s ${STAGE_LIBDIR:${STAGE_DIR_FILTER}} \
155284345Ssjg	${SHLIB_LINKS:@t@${STAGE_LIBS:T:M$t.*} $t@}
156284345Ssjg.elif !empty(SHLIB_LINK) && !empty(SHLIB_NAME)
157284345Ssjg	@${STAGE_LINKS_SCRIPT}; StageLinks -s ${STAGE_LIBDIR:${STAGE_DIR_FILTER}} ${SHLIB_NAME} ${SHLIB_LINK} ${SYMLINKS:T}
158284345Ssjg.endif
159284345Ssjg.endif
160284345Ssjg	@touch $@
161284345Ssjg.endif
162284345Ssjg
163284345Ssjg.if !empty(STAGE_DIR)
164284345SsjgSTAGE_SETS += _default
165284345SsjgSTAGE_DIR._default = ${STAGE_DIR}
166284345SsjgSTAGE_LINKS_DIR._default = ${STAGE_LINKS_DIR:U${STAGE_OBJTOP}}
167284345SsjgSTAGE_SYMLINKS_DIR._default = ${STAGE_SYMLINKS_DIR:U${STAGE_OBJTOP}}
168284345SsjgSTAGE_FILES._default = ${STAGE_FILES}
169284345SsjgSTAGE_LINKS._default = ${STAGE_LINKS}
170284345SsjgSTAGE_SYMLINKS._default = ${STAGE_SYMLINKS}
171284345SsjgSTAGE_FILES ?= ${.ALLSRC:N.dirdep:Nstage_*}
172284345SsjgSTAGE_SYMLINKS ?= ${.ALLSRC:T:N.dirdep:Nstage_*}
173284345Ssjg.endif
174284345Ssjg
175284345Ssjg.if !empty(STAGE_SETS)
176284345SsjgCLEANFILES += ${STAGE_SETS:@s@stage*$s@}
177284345Ssjg
178284345Ssjg# some makefiles need to populate multiple directories
179284345Ssjg.for s in ${STAGE_SETS:O:u}
180284345SsjgSTAGE_FILES.$s ?= ${.ALLSRC:N.dirdep}
181284345SsjgSTAGE_SYMLINKS.$s ?= ${.ALLSRC:N.dirdep}
182284345SsjgSTAGE_LINKS_DIR.$s ?= ${STAGE_OBJTOP}
183284345SsjgSTAGE_SYMLINKS_DIR.$s ?= ${STAGE_OBJTOP}
184284345Ssjg
185284345SsjgSTAGE_TARGETS += stage_files
186284345Ssjg.if $s != "_default"
187284345Ssjgstage_files:	stage_files.$s
188284345Ssjgstage_files.$s:	.dirdep
189284345Ssjg.else
190284345Ssjgstage_files:	.dirdep
191284345Ssjg.endif
192284345Ssjg	@${STAGE_FILE_SCRIPT}; StageFiles ${FLAGS.$@} ${STAGE_FILES_DIR.$s:U${STAGE_DIR.$s}:${STAGE_DIR_FILTER}} ${STAGE_FILES.$s}
193284345Ssjg	@touch $@
194284345Ssjg
195284345SsjgSTAGE_TARGETS += stage_links
196284345Ssjg.if $s != "_default"
197284345Ssjgstage_links:	stage_links.$s
198284345Ssjgstage_links.$s:	.dirdep
199284345Ssjg.else
200284345Ssjgstage_links:	.dirdep
201284345Ssjg.endif
202284345Ssjg	@${STAGE_LINKS_SCRIPT}; StageLinks ${STAGE_LINKS_DIR.$s:U${STAGE_DIR.$s}:${STAGE_DIR_FILTER}} ${STAGE_LINKS.$s}
203284345Ssjg	@touch $@
204284345Ssjg
205284345SsjgSTAGE_TARGETS += stage_symlinks
206284345Ssjg.if $s != "_default"
207284345Ssjgstage_symlinks:	stage_symlinks.$s
208284345Ssjgstage_symlinks.$s:	.dirdep
209284345Ssjg.else
210284345Ssjgstage_symlinks:	.dirdep
211284345Ssjg.endif
212284345Ssjg	@${STAGE_LINKS_SCRIPT}; StageLinks -s ${STAGE_SYMLINKS_DIR.$s:U${STAGE_DIR.$s}:${STAGE_DIR_FILTER}} ${STAGE_SYMLINKS.$s}
213284345Ssjg	@touch $@
214284345Ssjg
215284345Ssjg.endfor
216284345Ssjg.endif
217284345Ssjg
218284345Ssjg.if !empty(STAGE_AS_SETS)
219284345SsjgCLEANFILES += ${STAGE_AS_SETS:@s@stage*$s@}
220284345Ssjg
221284345SsjgSTAGE_TARGETS += stage_as
222284345Ssjg
223284345Ssjg# sometimes things need to be renamed as they are staged
224284345Ssjg# each ${file} will be staged as ${STAGE_AS_${file:T}}
225284345Ssjg# one could achieve the same with SYMLINKS
226284345Ssjg.for s in ${STAGE_AS_SETS:O:u}
227284345SsjgSTAGE_AS.$s ?= ${.ALLSRC:N.dirdep}
228284345Ssjg
229284345Ssjgstage_as:	stage_as.$s
230284345Ssjgstage_as.$s:	.dirdep
231284345Ssjg	@${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}}}@}
232284345Ssjg	@touch $@
233284345Ssjg
234284345Ssjg.endfor
235284345Ssjg.endif
236284345Ssjg
237284345SsjgCLEANFILES += ${STAGE_TARGETS} stage_incs stage_includes
238284345Ssjg
239284345Ssjg# stage_*links usually needs to follow any others.
240288475Sbdrewery.if !empty(STAGE_SETS) && !empty(STAGE_TARGETS:Nstage_links)
241288475Sbdrewery.for s in ${STAGE_SETS:O:u}
242288475Sbdrewerystage_links.$s: ${STAGE_TARGETS:Nstage_links:O:u}
243284345Ssjg.endfor
244288475Sbdrewery.endif
245284345Ssjg
246284345Ssjg# make sure this exists
247284345Ssjgstaging:
248284345Ssjg
249284345Ssjg# generally we want staging to wait until everything else is done
250284345SsjgSTAGING_WAIT ?= .WAIT
251284345Ssjg
252284345Ssjg.if ${.MAKE.LEVEL} > 0
253284345Ssjgall: ${STAGING_WAIT} staging
254284345Ssjg.endif
255284345Ssjg
256284345Ssjg.if exists(${.PARSEDIR}/stage-install.sh) && !defined(STAGE_INSTALL)
257284345Ssjg# this will run install(1) and then followup with .dirdep files.
258284345SsjgSTAGE_INSTALL := sh ${.PARSEDIR:tA}/stage-install.sh INSTALL="${INSTALL}" OBJDIR=${.OBJDIR:tA}
259284345Ssjg.endif
260284345Ssjg
261284345Ssjg# if ${INSTALL} gets run during 'all' assume it is for staging?
262284345Ssjg.if ${.TARGETS:Nall} == "" && defined(STAGE_INSTALL)
263284345SsjgINSTALL := ${STAGE_INSTALL}
264284345Ssjg.if target(beforeinstall)
265284345Ssjgbeforeinstall: .dirdep
266284345Ssjg.endif
267284345Ssjg.endif
268284345Ssjg.NOPATH: ${STAGE_FILES}
269284345Ssjg
270284345Ssjg.if !empty(STAGE_TARGETS)
271284345SsjgMK_STALE_STAGED?= no
272284345Ssjg.if ${MK_STALE_STAGED} == "yes"
273284345Ssjgall: stale_staged
274284345Ssjg# get a list of paths that we have just staged
275284345Ssjg# get a list of paths that we have previously staged to those same dirs
276284345Ssjg# anything in the 2nd list but not the first is stale - remove it.
277284345Ssjgstale_staged: staging .NOMETA
278284345Ssjg	@egrep '^[WL] .*${STAGE_OBJTOP}' /dev/null ${.MAKE.META.FILES:M*stage_*} | \
279284345Ssjg	sed "/\.dirdep/d;s,.* '*\(${STAGE_OBJTOP}/[^ '][^ ']*\).*,\1," | \
280284345Ssjg	sort > ${.TARGET}.staged1
281284345Ssjg	@grep -l '${_dirdep}' /dev/null ${_STAGED_DIRS:M${STAGE_OBJTOP}*:O:u:@d@$d/*.dirdep@} | \
282284345Ssjg	sed 's,\.dirdep,,' | sort > ${.TARGET}.staged2
283284345Ssjg	@comm -13 ${.TARGET}.staged1 ${.TARGET}.staged2 > ${.TARGET}.stale
284284345Ssjg	@test ! -s ${.TARGET}.stale || { \
285284345Ssjg		echo "Removing stale staged files..."; \
286284345Ssjg		sed 's,.*,& &.dirdep,' ${.TARGET}.stale | xargs rm -f; }
287284345Ssjg
288284345Ssjg.endif
289284345Ssjg.endif
290284345Ssjg.endif
291