meta.stage.mk revision 291978
190075Sobrien# $Id: meta.stage.mk,v 1.41 2015/11/13 17:34:04 sjg Exp $
290075Sobrien#
390075Sobrien#	@(#) Copyright (c) 2011, Simon J. Gerraty
490075Sobrien#
590075Sobrien#	This file is provided in the hope that it will
690075Sobrien#	be of use.  There is absolutely NO WARRANTY.
790075Sobrien#	Permission to copy, redistribute or otherwise
890075Sobrien#	use this file is hereby granted provided that 
990075Sobrien#	the above copyright notice and this notice are
1090075Sobrien#	left intact. 
1190075Sobrien#      
1290075Sobrien#	Please send copies of changes and bug-fixes to:
1390075Sobrien#	sjg@crufty.net
1490075Sobrien#
1590075Sobrien
1690075Sobrien.if !target(__${.PARSEFILE}__)
1790075Sobrien__${.PARSEFILE}__:
1890075Sobrien
1990075Sobrien.if ${.MAKE.DEPENDFILE_PREFERENCE:U${.MAKE.DEPENDFILE}:M*.${MACHINE}} != ""
2090075Sobrien# this is generally safer anyway
2190075Sobrien_dirdep = ${RELDIR}.${MACHINE}
2290075Sobrien.else
2390075Sobrien_dirdep = ${RELDIR}
2490075Sobrien.endif
2590075Sobrien
2690075SobrienCLEANFILES+= .dirdep
2790075Sobrien
2890075Sobrien# this allows us to trace dependencies back to their src dir
2990075Sobrien.dirdep:
3090075Sobrien	@echo '${_dirdep}' > $@
3190075Sobrien
3290075Sobrien.if defined(NO_POSIX_SHELL) || ${type printf:L:sh:Mbuiltin} == ""
3390075Sobrien_stage_file_basename = `basename $$f`
3490075Sobrien_stage_target_dirname = `dirname $$t`
3590075Sobrien.else
3690075Sobrien_stage_file_basename = $${f\#\#*/}
3790075Sobrien_stage_target_dirname = $${t%/*}
3890075Sobrien.endif
3990075Sobrien
4090075Sobrien_OBJROOT ?= ${OBJROOT:U${OBJTOP:H}}
4190075Sobrien.if ${_OBJROOT:M*/} != ""
4290075Sobrien_objroot ?= ${_OBJROOT:tA}/
4390075Sobrien.else
4490075Sobrien_objroot ?= ${_OBJROOT:tA}
4590075Sobrien.endif
4690075Sobrien
4790075Sobrien# make sure this is global
4890075Sobrien_STAGED_DIRS ?=
4990075Sobrien.export _STAGED_DIRS
5090075Sobrien# add each dir we stage to to _STAGED_DIRS
5190075Sobrien# and make sure we have absolute paths so that bmake
5290075Sobrien# will match against .MAKE.META.BAILIWICK
5390075SobrienSTAGE_DIR_FILTER = tA:@d@$${_STAGED_DIRS::+=$$d}$$d@
5490075Sobrien# convert _STAGED_DIRS into suitable filters
5590075SobrienGENDIRDEPS_FILTER += Nnot-empty-is-important \
5690075Sobrien	${_STAGED_DIRS:O:u:M${OBJTOP}*:S,${OBJTOP}/,N,} \
5790075Sobrien	${_STAGED_DIRS:O:u:M${_objroot}*:N${OBJTOP}*:S,${_objroot},,:C,^([^/]+)/(.*),N\2.\1,:S,${HOST_TARGET},.host,}
5890075Sobrien
5990075SobrienLN_CP_SCRIPT = LnCp() { \
6090075Sobrien  rm -f $$2 2> /dev/null; \
6190075Sobrien  ln $$1 $$2 2> /dev/null || \
6290075Sobrien  cp -p $$1 $$2; }
6390075Sobrien
6490075Sobrien# a staging conflict should cause an error
6590075Sobrien# a warning is handy when bootstapping different options.
6690075SobrienSTAGE_CONFLICT?= ERROR
6790075Sobrien.if ${STAGE_CONFLICT:tl} == "error"
6890075SobrienSTAGE_CONFLICT_ACTION= exit 1;
6990075Sobrien.else
7090075SobrienSTAGE_CONFLICT_ACTION=
7190075Sobrien.endif
7290075Sobrien
7390075Sobrien# it is an error for more than one src dir to try and stage
7490075Sobrien# the same file
7590075SobrienSTAGE_DIRDEP_SCRIPT = ${LN_CP_SCRIPT}; StageDirdep() { \
7690075Sobrien  t=$$1; \
7790075Sobrien  if [ -s $$t.dirdep ]; then \
7890075Sobrien	cmp -s .dirdep $$t.dirdep && return; \
7990075Sobrien	echo "${STAGE_CONFLICT}: $$t installed by `cat $$t.dirdep` not ${_dirdep}" >&2; \
8090075Sobrien	${STAGE_CONFLICT_ACTION} \
8190075Sobrien  fi; \
8290075Sobrien  LnCp .dirdep $$t.dirdep || exit 1; }
8390075Sobrien
8490075Sobrien# common logic for staging files
8590075Sobrien# this all relies on RELDIR being set to a subdir of SRCTOP
8690075Sobrien# we use ln(1) if we can, else cp(1)
8790075SobrienSTAGE_FILE_SCRIPT = ${STAGE_DIRDEP_SCRIPT}; StageFiles() { \
8890075Sobrien  case "$$1" in "") return;; -m) mode=$$2; shift 2;; *) mode=;; esac; \
8990075Sobrien  dest=$$1; shift; \
9090075Sobrien  mkdir -p $$dest; \
9190075Sobrien  [ -s .dirdep ] || echo '${_dirdep}' > .dirdep; \
9290075Sobrien  for f in "$$@"; do \
9390075Sobrien	case "$$f" in */*) t=$$dest/${_stage_file_basename};; *) t=$$dest/$$f;; esac; \
9490075Sobrien	StageDirdep $$t; \
9590075Sobrien	LnCp $$f $$t || exit 1; \
9690075Sobrien	[ -z "$$mode" ] || chmod $$mode $$t; \
9790075Sobrien  done; :; }
9890075Sobrien
9990075SobrienSTAGE_LINKS_SCRIPT = ${STAGE_DIRDEP_SCRIPT}; StageLinks() { \
10090075Sobrien  case "$$1" in "") return;; --) shift;; -*) ldest= lnf=$$1; shift;; /*) ldest=$$1/;; esac; \
10190075Sobrien  dest=$$1; shift; \
10290075Sobrien  mkdir -p $$dest; \
10390075Sobrien  [ -s .dirdep ] || echo '${_dirdep}' > .dirdep; \
10490075Sobrien  while test $$\# -ge 2; do \
10590075Sobrien	l=$$ldest$$1; shift; \
10690075Sobrien	t=$$dest/$$1; \
10790075Sobrien	case "$$1" in */*) mkdir -p ${_stage_target_dirname};; esac; \
10890075Sobrien	shift; \
10990075Sobrien	StageDirdep $$t; \
11090075Sobrien	rm -f $$t 2>/dev/null; \
11190075Sobrien	ln $$lnf $$l $$t || exit 1; \
11290075Sobrien  done; :; }
11390075Sobrien
11490075SobrienSTAGE_AS_SCRIPT = ${STAGE_DIRDEP_SCRIPT}; StageAs() { \
11590075Sobrien  case "$$1" in "") return;; -m) mode=$$2; shift 2;; *) mode=;; esac; \
11690075Sobrien  dest=$$1; shift; \
11790075Sobrien  mkdir -p $$dest; \
11890075Sobrien  [ -s .dirdep ] || echo '${_dirdep}' > .dirdep; \
11990075Sobrien  while test $$\# -ge 2; do \
12090075Sobrien	s=$$1; shift; \
12190075Sobrien	t=$$dest/$$1; \
12290075Sobrien	case "$$1" in */*) mkdir -p ${_stage_target_dirname};; esac; \
12390075Sobrien	shift; \
12490075Sobrien	StageDirdep $$t; \
12590075Sobrien	LnCp $$s $$t || exit 1; \
12690075Sobrien	[ -z "$$mode" ] || chmod $$mode $$t; \
12790075Sobrien  done; :; }
12890075Sobrien
12990075Sobrien# this is simple, a list of the "staged" files depends on this,
13090075Sobrien_STAGE_BASENAME_USE:	.USE ${.TARGET:T}
13190075Sobrien	@${STAGE_FILE_SCRIPT}; StageFiles ${.TARGET:H:${STAGE_DIR_FILTER}} ${.TARGET:T}
13290075Sobrien
13390075Sobrien_STAGE_AS_BASENAME_USE:        .USE ${.TARGET:T}
13490075Sobrien	@${STAGE_AS_SCRIPT}; StageAs ${.TARGET:H:${STAGE_DIR_FILTER}} ${.TARGET:T} ${STAGE_AS_${.TARGET:T}:U${.TARGET:T}}
13590075Sobrien
13690075Sobrien.if !empty(STAGE_INCSDIR)
13790075SobrienSTAGE_TARGETS += stage_incs
13890075SobrienSTAGE_INCS ?= ${.ALLSRC:N.dirdep:Nstage_*}
13990075Sobrien
14090075Sobrienstage_includes: stage_incs
14190075Sobrienstage_incs:	.dirdep
14290075Sobrien	@${STAGE_FILE_SCRIPT}; StageFiles ${STAGE_INCSDIR:${STAGE_DIR_FILTER}} ${STAGE_INCS}
14390075Sobrien	@touch $@
14490075Sobrien.endif
14590075Sobrien
14690075Sobrien.if !empty(STAGE_LIBDIR)
14790075SobrienSTAGE_TARGETS += stage_libs
14890075Sobrien
14990075SobrienSTAGE_LIBS ?= ${.ALLSRC:N.dirdep:Nstage_*}
15090075Sobrien
15190075Sobrienstage_libs:	.dirdep
15290075Sobrien	@${STAGE_FILE_SCRIPT}; StageFiles ${STAGE_LIBDIR:${STAGE_DIR_FILTER}} ${STAGE_LIBS}
15390075Sobrien.if !defined(NO_SHLIB_LINKS)
15490075Sobrien.if !empty(SHLIB_LINKS)
15590075Sobrien	@${STAGE_LINKS_SCRIPT}; StageLinks -s ${STAGE_LIBDIR:${STAGE_DIR_FILTER}} \
15690075Sobrien	${SHLIB_LINKS:@t@${STAGE_LIBS:T:M$t.*} $t@}
15790075Sobrien.elif !empty(SHLIB_LINK) && !empty(SHLIB_NAME)
158	@${STAGE_LINKS_SCRIPT}; StageLinks -s ${STAGE_LIBDIR:${STAGE_DIR_FILTER}} ${SHLIB_NAME} ${SHLIB_LINK}
159.endif
160.endif
161	@touch $@
162.endif
163
164.if !empty(STAGE_DIR)
165STAGE_SETS += _default
166STAGE_DIR._default = ${STAGE_DIR}
167STAGE_LINKS_DIR._default = ${STAGE_LINKS_DIR:U${STAGE_OBJTOP}}
168STAGE_SYMLINKS_DIR._default = ${STAGE_SYMLINKS_DIR:U${STAGE_OBJTOP}}
169STAGE_FILES._default = ${STAGE_FILES}
170STAGE_LINKS._default = ${STAGE_LINKS}
171STAGE_SYMLINKS._default = ${STAGE_SYMLINKS}
172STAGE_FILES ?= ${.ALLSRC:N.dirdep:Nstage_*}
173STAGE_SYMLINKS ?= ${.ALLSRC:T:N.dirdep:Nstage_*}
174.endif
175
176.if !empty(STAGE_SETS)
177CLEANFILES += ${STAGE_SETS:@s@stage*$s@}
178
179# some makefiles need to populate multiple directories
180.for s in ${STAGE_SETS:O:u}
181STAGE_FILES.$s ?= ${.ALLSRC:N.dirdep:Nstage_*}
182STAGE_SYMLINKS.$s ?= ${.ALLSRC:N.dirdep:Nstage_*}
183STAGE_LINKS_DIR.$s ?= ${STAGE_OBJTOP}
184STAGE_SYMLINKS_DIR.$s ?= ${STAGE_OBJTOP}
185
186STAGE_TARGETS += stage_files
187.if $s != "_default"
188stage_files:	stage_files.$s
189stage_files.$s:	.dirdep
190.else
191stage_files:	.dirdep
192.endif
193	@${STAGE_FILE_SCRIPT}; StageFiles ${FLAGS.$@} ${STAGE_FILES_DIR.$s:U${STAGE_DIR.$s}:${STAGE_DIR_FILTER}} ${STAGE_FILES.$s}
194	@touch $@
195
196STAGE_TARGETS += stage_links
197.if $s != "_default"
198stage_links:	stage_links.$s
199stage_links.$s:	.dirdep
200.else
201stage_links:	.dirdep
202.endif
203	@${STAGE_LINKS_SCRIPT}; StageLinks ${STAGE_LINKS_DIR.$s:U${STAGE_DIR.$s}:${STAGE_DIR_FILTER}} ${STAGE_LINKS.$s}
204	@touch $@
205
206STAGE_TARGETS += stage_symlinks
207.if $s != "_default"
208stage_symlinks:	stage_symlinks.$s
209stage_symlinks.$s:	.dirdep
210.else
211stage_symlinks:	.dirdep
212.endif
213	@${STAGE_LINKS_SCRIPT}; StageLinks -s ${STAGE_SYMLINKS_DIR.$s:U${STAGE_DIR.$s}:${STAGE_DIR_FILTER}} ${STAGE_SYMLINKS.$s}
214	@touch $@
215
216.endfor
217.endif
218
219.if !empty(STAGE_AS_SETS)
220CLEANFILES += ${STAGE_AS_SETS:@s@stage*$s@}
221
222STAGE_TARGETS += stage_as
223
224# sometimes things need to be renamed as they are staged
225# each ${file} will be staged as ${STAGE_AS_${file:T}}
226# one could achieve the same with SYMLINKS
227.for s in ${STAGE_AS_SETS:O:u}
228STAGE_AS.$s ?= ${.ALLSRC:N.dirdep:Nstage_*}
229
230stage_as:	stage_as.$s
231stage_as.$s:	.dirdep
232	@${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}}}@}
233	@touch $@
234
235.endfor
236.endif
237
238CLEANFILES += ${STAGE_TARGETS} stage_incs stage_includes
239
240# stage_*links usually needs to follow any others.
241# for non-jobs mode the order here matters
242staging: ${STAGE_TARGETS:N*_links} ${STAGE_TARGETS:M*_links}
243
244.if ${.MAKE.JOBS:U0} > 0 && ${STAGE_TARGETS:M*_links} != ""
245# the above isn't sufficient
246.for t in ${STAGE_TARGETS:N*links:O:u}
247.ORDER: $t stage_links
248.endfor
249.endif
250
251# generally we want staging to wait until everything else is done
252STAGING_WAIT ?= .WAIT
253
254.if ${.MAKE.LEVEL} > 0
255all: ${STAGING_WAIT} staging
256.endif
257
258.if exists(${.PARSEDIR}/stage-install.sh) && !defined(STAGE_INSTALL)
259# this will run install(1) and then followup with .dirdep files.
260STAGE_INSTALL := sh ${.PARSEDIR:tA}/stage-install.sh INSTALL="${INSTALL}" OBJDIR=${.OBJDIR:tA}
261.endif
262
263# if ${INSTALL} gets run during 'all' assume it is for staging?
264.if ${.TARGETS:Nall} == "" && defined(STAGE_INSTALL)
265INSTALL := ${STAGE_INSTALL}
266.if target(beforeinstall)
267beforeinstall: .dirdep
268.endif
269.endif
270.NOPATH: ${STAGE_FILES}
271
272.if !empty(STAGE_TARGETS)
273MK_STALE_STAGED?= no
274.if ${MK_STALE_STAGED} == "yes"
275all: stale_staged
276# get a list of paths that we have just staged
277# get a list of paths that we have previously staged to those same dirs
278# anything in the 2nd list but not the first is stale - remove it.
279stale_staged: staging .NOMETA
280	@egrep '^[WL] .*${STAGE_OBJTOP}' /dev/null ${.MAKE.META.FILES:M*stage_*} | \
281	sed "/\.dirdep/d;s,.* '*\(${STAGE_OBJTOP}/[^ '][^ ']*\).*,\1," | \
282	sort > ${.TARGET}.staged1
283	@grep -l '${_dirdep}' /dev/null ${_STAGED_DIRS:M${STAGE_OBJTOP}*:O:u:@d@$d/*.dirdep@} | \
284	sed 's,\.dirdep,,' | sort > ${.TARGET}.staged2
285	@comm -13 ${.TARGET}.staged1 ${.TARGET}.staged2 > ${.TARGET}.stale
286	@test ! -s ${.TARGET}.stale || { \
287		echo "Removing stale staged files..."; \
288		sed 's,.*,& &.dirdep,' ${.TARGET}.stale | xargs rm -f; }
289
290.endif
291.endif
292.endif
293