meta.stage.mk revision 284345
1284345Ssjg# $FreeBSD: head/share/mk/meta.stage.mk 284345 2015-06-13 19:20:56Z sjg $
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
63284345Ssjg# it is an error for more than one src dir to try and stage
64284345Ssjg# the same file
65284345SsjgSTAGE_DIRDEP_SCRIPT = ${LN_CP_SCRIPT}; StageDirdep() { \
66284345Ssjg  t=$$1; \
67284345Ssjg  if [ -s $$t.dirdep ]; then \
68284345Ssjg	cmp -s .dirdep $$t.dirdep && return; \
69284345Ssjg	echo "ERROR: $$t installed by `cat $$t.dirdep` not ${_dirdep}" >&2; \
70284345Ssjg	exit 1; \
71284345Ssjg  fi; \
72284345Ssjg  LnCp .dirdep $$t.dirdep || exit 1; }
73284345Ssjg
74284345Ssjg# common logic for staging files
75284345Ssjg# this all relies on RELDIR being set to a subdir of SRCTOP
76284345Ssjg# we use ln(1) if we can, else cp(1)
77284345SsjgSTAGE_FILE_SCRIPT = ${STAGE_DIRDEP_SCRIPT}; StageFiles() { \
78284345Ssjg  case "$$1" in "") return;; -m) mode=$$2; shift 2;; *) mode=;; esac; \
79284345Ssjg  dest=$$1; shift; \
80284345Ssjg  mkdir -p $$dest; \
81284345Ssjg  [ -s .dirdep ] || echo '${_dirdep}' > .dirdep; \
82284345Ssjg  for f in "$$@"; do \
83284345Ssjg	case "$$f" in */*) t=$$dest/${_stage_file_basename};; *) t=$$dest/$$f;; esac; \
84284345Ssjg	StageDirdep $$t; \
85284345Ssjg	LnCp $$f $$t || exit 1; \
86284345Ssjg	[ -z "$$mode" ] || chmod $$mode $$t; \
87284345Ssjg  done; :; }
88284345Ssjg
89284345SsjgSTAGE_LINKS_SCRIPT = ${STAGE_DIRDEP_SCRIPT}; StageLinks() { \
90284345Ssjg  case "$$1" in "") return;; --) shift;; -*) ldest= lnf=$$1; shift;; /*) ldest=$$1/;; esac; \
91284345Ssjg  dest=$$1; shift; \
92284345Ssjg  mkdir -p $$dest; \
93284345Ssjg  [ -s .dirdep ] || echo '${_dirdep}' > .dirdep; \
94284345Ssjg  while test $$\# -ge 2; do \
95284345Ssjg	l=$$ldest$$1; shift; \
96284345Ssjg	t=$$dest/$$1; \
97284345Ssjg	case "$$1" in */*) mkdir -p ${_stage_target_dirname};; esac; \
98284345Ssjg	shift; \
99284345Ssjg	StageDirdep $$t; \
100284345Ssjg	rm -f $$t 2>/dev/null; \
101284345Ssjg	ln $$lnf $$l $$t || exit 1; \
102284345Ssjg  done; :; }
103284345Ssjg
104284345SsjgSTAGE_AS_SCRIPT = ${STAGE_DIRDEP_SCRIPT}; StageAs() { \
105284345Ssjg  case "$$1" in "") return;; -m) mode=$$2; shift 2;; *) mode=;; esac; \
106284345Ssjg  dest=$$1; shift; \
107284345Ssjg  mkdir -p $$dest; \
108284345Ssjg  [ -s .dirdep ] || echo '${_dirdep}' > .dirdep; \
109284345Ssjg  while test $$\# -ge 2; do \
110284345Ssjg	s=$$1; shift; \
111284345Ssjg	t=$$dest/$$1; \
112284345Ssjg	case "$$1" in */*) mkdir -p ${_stage_target_dirname};; esac; \
113284345Ssjg	shift; \
114284345Ssjg	StageDirdep $$t; \
115284345Ssjg	LnCp $$s $$t || exit 1; \
116284345Ssjg	[ -z "$$mode" ] || chmod $$mode $$t; \
117284345Ssjg  done; :; }
118284345Ssjg
119284345Ssjg# this is simple, a list of the "staged" files depends on this,
120284345Ssjg_STAGE_BASENAME_USE:	.USE ${.TARGET:T}
121284345Ssjg	@${STAGE_FILE_SCRIPT}; StageFiles ${.TARGET:H:${STAGE_DIR_FILTER}} ${.TARGET:T}
122284345Ssjg
123284345Ssjg_STAGE_AS_BASENAME_USE:        .USE ${.TARGET:T}
124284345Ssjg	@${STAGE_AS_SCRIPT}; StageAs ${.TARGET:H:${STAGE_DIR_FILTER}} ${.TARGET:T} ${STAGE_AS_${.TARGET:T}:U${.TARGET:T}}
125284345Ssjg
126284345Ssjg.if !empty(STAGE_INCSDIR)
127284345SsjgSTAGE_TARGETS += stage_incs
128284345SsjgSTAGE_INCS ?= ${.ALLSRC:N.dirdep}
129284345Ssjg
130284345Ssjgstage_includes: stage_incs
131284345Ssjgstage_incs:	.dirdep
132284345Ssjg	@${STAGE_FILE_SCRIPT}; StageFiles ${STAGE_INCSDIR:${STAGE_DIR_FILTER}} ${STAGE_INCS}
133284345Ssjg	@touch $@
134284345Ssjg.endif
135284345Ssjg
136284345Ssjg.if !empty(STAGE_LIBDIR)
137284345SsjgSTAGE_TARGETS += stage_libs
138284345Ssjg
139284345SsjgSTAGE_LIBS ?= ${.ALLSRC:N.dirdep}
140284345Ssjg
141284345Ssjgstage_libs:	.dirdep
142284345Ssjg	@${STAGE_FILE_SCRIPT}; StageFiles ${STAGE_LIBDIR:${STAGE_DIR_FILTER}} ${STAGE_LIBS}
143284345Ssjg.if !defined(NO_SHLIB_LINKS)
144284345Ssjg.if !empty(SHLIB_LINKS)
145284345Ssjg	@${STAGE_LINKS_SCRIPT}; StageLinks -s ${STAGE_LIBDIR:${STAGE_DIR_FILTER}} \
146284345Ssjg	${SHLIB_LINKS:@t@${STAGE_LIBS:T:M$t.*} $t@}
147284345Ssjg.elif !empty(SHLIB_LINK) && !empty(SHLIB_NAME)
148284345Ssjg	@${STAGE_LINKS_SCRIPT}; StageLinks -s ${STAGE_LIBDIR:${STAGE_DIR_FILTER}} ${SHLIB_NAME} ${SHLIB_LINK} ${SYMLINKS:T}
149284345Ssjg.endif
150284345Ssjg.endif
151284345Ssjg	@touch $@
152284345Ssjg.endif
153284345Ssjg
154284345Ssjg.if !empty(STAGE_DIR)
155284345SsjgSTAGE_SETS += _default
156284345SsjgSTAGE_DIR._default = ${STAGE_DIR}
157284345SsjgSTAGE_LINKS_DIR._default = ${STAGE_LINKS_DIR:U${STAGE_OBJTOP}}
158284345SsjgSTAGE_SYMLINKS_DIR._default = ${STAGE_SYMLINKS_DIR:U${STAGE_OBJTOP}}
159284345SsjgSTAGE_FILES._default = ${STAGE_FILES}
160284345SsjgSTAGE_LINKS._default = ${STAGE_LINKS}
161284345SsjgSTAGE_SYMLINKS._default = ${STAGE_SYMLINKS}
162284345SsjgSTAGE_FILES ?= ${.ALLSRC:N.dirdep:Nstage_*}
163284345SsjgSTAGE_SYMLINKS ?= ${.ALLSRC:T:N.dirdep:Nstage_*}
164284345Ssjg.endif
165284345Ssjg
166284345Ssjg.if !empty(STAGE_SETS)
167284345SsjgCLEANFILES += ${STAGE_SETS:@s@stage*$s@}
168284345Ssjg
169284345Ssjg# some makefiles need to populate multiple directories
170284345Ssjg.for s in ${STAGE_SETS:O:u}
171284345SsjgSTAGE_FILES.$s ?= ${.ALLSRC:N.dirdep}
172284345SsjgSTAGE_SYMLINKS.$s ?= ${.ALLSRC:N.dirdep}
173284345SsjgSTAGE_LINKS_DIR.$s ?= ${STAGE_OBJTOP}
174284345SsjgSTAGE_SYMLINKS_DIR.$s ?= ${STAGE_OBJTOP}
175284345Ssjg
176284345SsjgSTAGE_TARGETS += stage_files
177284345Ssjg.if $s != "_default"
178284345Ssjgstage_files:	stage_files.$s
179284345Ssjgstage_files.$s:	.dirdep
180284345Ssjg.else
181284345Ssjgstage_files:	.dirdep
182284345Ssjg.endif
183284345Ssjg	@${STAGE_FILE_SCRIPT}; StageFiles ${FLAGS.$@} ${STAGE_FILES_DIR.$s:U${STAGE_DIR.$s}:${STAGE_DIR_FILTER}} ${STAGE_FILES.$s}
184284345Ssjg	@touch $@
185284345Ssjg
186284345SsjgSTAGE_TARGETS += stage_links
187284345Ssjg.if $s != "_default"
188284345Ssjgstage_links:	stage_links.$s
189284345Ssjgstage_links.$s:	.dirdep
190284345Ssjg.else
191284345Ssjgstage_links:	.dirdep
192284345Ssjg.endif
193284345Ssjg	@${STAGE_LINKS_SCRIPT}; StageLinks ${STAGE_LINKS_DIR.$s:U${STAGE_DIR.$s}:${STAGE_DIR_FILTER}} ${STAGE_LINKS.$s}
194284345Ssjg	@touch $@
195284345Ssjg
196284345SsjgSTAGE_TARGETS += stage_symlinks
197284345Ssjg.if $s != "_default"
198284345Ssjgstage_symlinks:	stage_symlinks.$s
199284345Ssjgstage_symlinks.$s:	.dirdep
200284345Ssjg.else
201284345Ssjgstage_symlinks:	.dirdep
202284345Ssjg.endif
203284345Ssjg	@${STAGE_LINKS_SCRIPT}; StageLinks -s ${STAGE_SYMLINKS_DIR.$s:U${STAGE_DIR.$s}:${STAGE_DIR_FILTER}} ${STAGE_SYMLINKS.$s}
204284345Ssjg	@touch $@
205284345Ssjg
206284345Ssjg.endfor
207284345Ssjg.endif
208284345Ssjg
209284345Ssjg.if !empty(STAGE_AS_SETS)
210284345SsjgCLEANFILES += ${STAGE_AS_SETS:@s@stage*$s@}
211284345Ssjg
212284345SsjgSTAGE_TARGETS += stage_as
213284345Ssjg
214284345Ssjg# sometimes things need to be renamed as they are staged
215284345Ssjg# each ${file} will be staged as ${STAGE_AS_${file:T}}
216284345Ssjg# one could achieve the same with SYMLINKS
217284345Ssjg.for s in ${STAGE_AS_SETS:O:u}
218284345SsjgSTAGE_AS.$s ?= ${.ALLSRC:N.dirdep}
219284345Ssjg
220284345Ssjgstage_as:	stage_as.$s
221284345Ssjgstage_as.$s:	.dirdep
222284345Ssjg	@${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}}}@}
223284345Ssjg	@touch $@
224284345Ssjg
225284345Ssjg.endfor
226284345Ssjg.endif
227284345Ssjg
228284345SsjgCLEANFILES += ${STAGE_TARGETS} stage_incs stage_includes
229284345Ssjg
230284345Ssjg# stage_*links usually needs to follow any others.
231284345Ssjg.for t in ${STAGE_TARGETS:N*links:O:u}
232284345Ssjg.ORDER: $t stage_links
233284345Ssjg.ORDER: $t stage_symlinks
234284345Ssjg.endfor
235284345Ssjg
236284345Ssjg# make sure this exists
237284345Ssjgstaging:
238284345Ssjg
239284345Ssjg# generally we want staging to wait until everything else is done
240284345SsjgSTAGING_WAIT ?= .WAIT
241284345Ssjg
242284345Ssjg.if ${.MAKE.LEVEL} > 0
243284345Ssjgall: ${STAGING_WAIT} staging
244284345Ssjg.endif
245284345Ssjg
246284345Ssjg.if exists(${.PARSEDIR}/stage-install.sh) && !defined(STAGE_INSTALL)
247284345Ssjg# this will run install(1) and then followup with .dirdep files.
248284345SsjgSTAGE_INSTALL := sh ${.PARSEDIR:tA}/stage-install.sh INSTALL="${INSTALL}" OBJDIR=${.OBJDIR:tA}
249284345Ssjg.endif
250284345Ssjg
251284345Ssjg# if ${INSTALL} gets run during 'all' assume it is for staging?
252284345Ssjg.if ${.TARGETS:Nall} == "" && defined(STAGE_INSTALL)
253284345SsjgINSTALL := ${STAGE_INSTALL}
254284345Ssjg.if target(beforeinstall)
255284345Ssjgbeforeinstall: .dirdep
256284345Ssjg.endif
257284345Ssjg.endif
258284345Ssjg.NOPATH: ${STAGE_FILES}
259284345Ssjg
260284345Ssjg.if !empty(STAGE_TARGETS)
261284345SsjgMK_STALE_STAGED?= no
262284345Ssjg.if ${MK_STALE_STAGED} == "yes"
263284345Ssjgall: stale_staged
264284345Ssjg# get a list of paths that we have just staged
265284345Ssjg# get a list of paths that we have previously staged to those same dirs
266284345Ssjg# anything in the 2nd list but not the first is stale - remove it.
267284345Ssjgstale_staged: staging .NOMETA
268284345Ssjg	@egrep '^[WL] .*${STAGE_OBJTOP}' /dev/null ${.MAKE.META.FILES:M*stage_*} | \
269284345Ssjg	sed "/\.dirdep/d;s,.* '*\(${STAGE_OBJTOP}/[^ '][^ ']*\).*,\1," | \
270284345Ssjg	sort > ${.TARGET}.staged1
271284345Ssjg	@grep -l '${_dirdep}' /dev/null ${_STAGED_DIRS:M${STAGE_OBJTOP}*:O:u:@d@$d/*.dirdep@} | \
272284345Ssjg	sed 's,\.dirdep,,' | sort > ${.TARGET}.staged2
273284345Ssjg	@comm -13 ${.TARGET}.staged1 ${.TARGET}.staged2 > ${.TARGET}.stale
274284345Ssjg	@test ! -s ${.TARGET}.stale || { \
275284345Ssjg		echo "Removing stale staged files..."; \
276284345Ssjg		sed 's,.*,& &.dirdep,' ${.TARGET}.stale | xargs rm -f; }
277284345Ssjg
278284345Ssjg.endif
279284345Ssjg.endif
280284345Ssjg.endif
281