1250837Ssjg# $Id: meta.stage.mk,v 1.30 2013/04/19 16:32:57 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
26246149Ssjg# this allows us to trace dependencies back to their src dir
27246149Ssjg.dirdep:
28246149Ssjg	@echo '${_dirdep}' > $@
29246149Ssjg
30246149Ssjg.if defined(NO_POSIX_SHELL) || ${type printf:L:sh:Mbuiltin} == ""
31246149Ssjg_stage_file_basename = `basename $$f`
32246149Ssjg_stage_target_dirname = `dirname $$t`
33246149Ssjg.else
34246149Ssjg_stage_file_basename = $${f\#\#*/}
35246149Ssjg_stage_target_dirname = $${t%/*}
36246149Ssjg.endif
37246149Ssjg
38246149Ssjg_objroot ?= ${_OBJROOT:tA}
39246149Ssjg# make sure this is global
40246149Ssjg_STAGED_DIRS ?=
41246149Ssjg.export _STAGED_DIRS
42246149Ssjg# add each dir we stage to to _STAGED_DIRS
43246149Ssjg# and make sure we have absolute paths so that bmake
44246149Ssjg# will match against .MAKE.META.BAILIWICK
45246149SsjgSTAGE_DIR_FILTER = tA:@d@$${_STAGED_DIRS::+=$$d}$$d@
46246149Ssjg# convert _STAGED_DIRS into suitable filters
47246149SsjgGENDIRDEPS_FILTER += Nnot-empty-is-important \
48246149Ssjg	${_STAGED_DIRS:O:u:M${OBJTOP}*:S,${OBJTOP}/,N,} \
49246149Ssjg	${_STAGED_DIRS:O:u:N${OBJTOP}*:S,${_objroot},,:C,^([^/]+)/(.*),N\2.\1,:S,${HOST_TARGET},.host,}
50246149Ssjg
51249033SsjgLN_CP_SCRIPT = LnCp() { \
52249033Ssjg  rm -f $$2 2> /dev/null; \
53249033Ssjg  ln $$1 $$2 2> /dev/null || \
54249033Ssjg  cp -p $$1 $$2; }
55249033Ssjg
56246149Ssjg# it is an error for more than one src dir to try and stage
57246149Ssjg# the same file
58249033SsjgSTAGE_DIRDEP_SCRIPT = ${LN_CP_SCRIPT}; StageDirdep() { \
59246149Ssjg  t=$$1; \
60246149Ssjg  if [ -s $$t.dirdep ]; then \
61246149Ssjg	cmp -s .dirdep $$t.dirdep && return; \
62246149Ssjg	echo "ERROR: $$t installed by `cat $$t.dirdep` not ${_dirdep}" >&2; \
63246149Ssjg	exit 1; \
64246149Ssjg  fi; \
65249033Ssjg  LnCp .dirdep $$t.dirdep || exit 1; }
66246149Ssjg
67246149Ssjg# common logic for staging files
68246149Ssjg# this all relies on RELDIR being set to a subdir of SRCTOP
69246149Ssjg# we use ln(1) if we can, else cp(1)
70246149SsjgSTAGE_FILE_SCRIPT = ${STAGE_DIRDEP_SCRIPT}; StageFiles() { \
71249033Ssjg  case "$$1" in "") return;; -m) mode=$$2; shift 2;; *) mode=;; esac; \
72246149Ssjg  dest=$$1; shift; \
73246149Ssjg  mkdir -p $$dest; \
74246149Ssjg  [ -s .dirdep ] || echo '${_dirdep}' > .dirdep; \
75246149Ssjg  for f in "$$@"; do \
76246149Ssjg	case "$$f" in */*) t=$$dest/${_stage_file_basename};; *) t=$$dest/$$f;; esac; \
77246149Ssjg	StageDirdep $$t; \
78249033Ssjg	LnCp $$f $$t || exit 1; \
79249033Ssjg	[ -z "$$mode" ] || chmod $$mode $$t; \
80246149Ssjg  done; :; }
81246149Ssjg
82246149SsjgSTAGE_LINKS_SCRIPT = ${STAGE_DIRDEP_SCRIPT}; StageLinks() { \
83249033Ssjg  case "$$1" in "") return;; --) shift;; -*) ldest= lnf=$$1; shift;; /*) ldest=$$1/;; esac; \
84246149Ssjg  dest=$$1; shift; \
85246149Ssjg  mkdir -p $$dest; \
86246149Ssjg  [ -s .dirdep ] || echo '${_dirdep}' > .dirdep; \
87246149Ssjg  while test $$\# -ge 2; do \
88246149Ssjg	l=$$ldest$$1; shift; \
89246149Ssjg	t=$$dest/$$1; \
90246149Ssjg	case "$$1" in */*) mkdir -p ${_stage_target_dirname};; esac; \
91246149Ssjg	shift; \
92246149Ssjg	StageDirdep $$t; \
93246149Ssjg	rm -f $$t 2>/dev/null; \
94249033Ssjg	ln $$lnf $$l $$t || exit 1; \
95246149Ssjg  done; :; }
96246149Ssjg
97246149SsjgSTAGE_AS_SCRIPT = ${STAGE_DIRDEP_SCRIPT}; StageAs() { \
98249033Ssjg  case "$$1" in "") return;; -m) mode=$$2; shift 2;; *) mode=;; esac; \
99246149Ssjg  dest=$$1; shift; \
100246149Ssjg  mkdir -p $$dest; \
101246149Ssjg  [ -s .dirdep ] || echo '${_dirdep}' > .dirdep; \
102246149Ssjg  while test $$\# -ge 2; do \
103246149Ssjg	s=$$1; shift; \
104246149Ssjg	t=$$dest/$$1; \
105246149Ssjg	case "$$1" in */*) mkdir -p ${_stage_target_dirname};; esac; \
106246149Ssjg	shift; \
107246149Ssjg	StageDirdep $$t; \
108249033Ssjg	LnCp $$s $$t || exit 1; \
109249033Ssjg	[ -z "$$mode" ] || chmod $$mode $$t; \
110246149Ssjg  done; :; }
111246149Ssjg
112246149Ssjg# this is simple, a list of the "staged" files depends on this,
113246149Ssjg_STAGE_BASENAME_USE:	.USE ${.TARGET:T}
114246149Ssjg	@${STAGE_FILE_SCRIPT}; StageFiles ${.TARGET:H:${STAGE_DIR_FILTER}} ${.TARGET:T}
115246149Ssjg
116246149Ssjg.if !empty(STAGE_INCSDIR)
117249033SsjgSTAGE_TARGETS += stage_incs
118246149SsjgSTAGE_INCS ?= ${.ALLSRC:N.dirdep}
119246149Ssjg
120246149Ssjgstage_incs:	.dirdep
121246149Ssjg	@${STAGE_FILE_SCRIPT}; StageFiles ${STAGE_INCSDIR:${STAGE_DIR_FILTER}} ${STAGE_INCS}
122246149Ssjg	@touch $@
123246149Ssjg.endif
124246149Ssjg
125246149Ssjg.if !empty(STAGE_LIBDIR)
126249033SsjgSTAGE_TARGETS += stage_libs
127246149Ssjg
128246149SsjgSTAGE_LIBS ?= ${.ALLSRC:N.dirdep}
129246149Ssjg
130246149Ssjgstage_libs:	.dirdep
131246149Ssjg	@${STAGE_FILE_SCRIPT}; StageFiles ${STAGE_LIBDIR:${STAGE_DIR_FILTER}} ${STAGE_LIBS}
132246149Ssjg.if !empty(SHLIB_LINKS)
133246149Ssjg	@${STAGE_LINKS_SCRIPT}; StageLinks -s ${STAGE_LIBDIR:${STAGE_DIR_FILTER}} \
134246149Ssjg	${SHLIB_LINKS:@t@${STAGE_LIBS:T:M$t.*} $t@}
135246149Ssjg.elif !empty(SHLIB_LINK) && !empty(SHLIB_NAME)
136246149Ssjg	@${STAGE_LINKS_SCRIPT}; StageLinks -s ${STAGE_LIBDIR:${STAGE_DIR_FILTER}} ${SHLIB_NAME} ${SHLIB_LINK} ${SYMLINKS:T}
137246149Ssjg.endif
138246149Ssjg	@touch $@
139246149Ssjg.endif
140246149Ssjg
141246149Ssjg.if !empty(STAGE_DIR)
142246149SsjgSTAGE_SETS += _default
143246149SsjgSTAGE_DIR._default = ${STAGE_DIR}
144246149SsjgSTAGE_LINKS_DIR._default = ${STAGE_LINKS_DIR:U${STAGE_OBJTOP}}
145246149SsjgSTAGE_SYMLINKS_DIR._default = ${STAGE_SYMLINKS_DIR:U${STAGE_OBJTOP}}
146246149SsjgSTAGE_FILES._default = ${STAGE_FILES}
147246149SsjgSTAGE_LINKS._default = ${STAGE_LINKS}
148246149SsjgSTAGE_SYMLINKS._default = ${STAGE_SYMLINKS}
149246149SsjgSTAGE_FILES ?= ${.ALLSRC:N.dirdep:Nstage_*}
150246149SsjgSTAGE_SYMLINKS ?= ${.ALLSRC:T:N.dirdep:Nstage_*}
151246149Ssjg.endif
152246149Ssjg
153246149Ssjg.if !empty(STAGE_SETS)
154246149SsjgCLEANFILES += ${STAGE_SETS:@s@stage*$s@}
155246149Ssjg
156246149Ssjg# some makefiles need to populate multiple directories
157246149Ssjg.for s in ${STAGE_SETS:O:u}
158246149SsjgSTAGE_FILES.$s ?= ${.ALLSRC:N.dirdep}
159246149SsjgSTAGE_SYMLINKS.$s ?= ${.ALLSRC:N.dirdep}
160246149SsjgSTAGE_LINKS_DIR.$s ?= ${STAGE_OBJTOP}
161246149SsjgSTAGE_SYMLINKS_DIR.$s ?= ${STAGE_OBJTOP}
162246149Ssjg
163249033SsjgSTAGE_TARGETS += stage_files
164246149Ssjg.if $s != "_default"
165246149Ssjgstage_files:	stage_files.$s
166246149Ssjgstage_files.$s:	.dirdep
167246149Ssjg.else
168246149Ssjgstage_files:	.dirdep
169246149Ssjg.endif
170246149Ssjg	@${STAGE_FILE_SCRIPT}; StageFiles ${FLAGS.$@} ${STAGE_FILES_DIR.$s:U${STAGE_DIR.$s}:${STAGE_DIR_FILTER}} ${STAGE_FILES.$s}
171246149Ssjg	@touch $@
172246149Ssjg
173249033SsjgSTAGE_TARGETS += stage_links
174246149Ssjg.if $s != "_default"
175246149Ssjgstage_links:	stage_links.$s
176246149Ssjgstage_links.$s:	.dirdep
177246149Ssjg.else
178246149Ssjgstage_links:	.dirdep
179246149Ssjg.endif
180246149Ssjg	@${STAGE_LINKS_SCRIPT}; StageLinks ${STAGE_LINKS_DIR.$s:U${STAGE_DIR.$s}:${STAGE_DIR_FILTER}} ${STAGE_LINKS.$s}
181246149Ssjg	@touch $@
182246149Ssjg
183249033SsjgSTAGE_TARGETS += stage_symlinks
184246149Ssjg.if $s != "_default"
185246149Ssjgstage_symlinks:	stage_symlinks.$s
186246149Ssjgstage_symlinks.$s:	.dirdep
187246149Ssjg.else
188246149Ssjgstage_symlinks:	.dirdep
189246149Ssjg.endif
190246149Ssjg	@${STAGE_LINKS_SCRIPT}; StageLinks -s ${STAGE_SYMLINKS_DIR.$s:U${STAGE_DIR.$s}:${STAGE_DIR_FILTER}} ${STAGE_SYMLINKS.$s}
191246149Ssjg	@touch $@
192246149Ssjg
193246149Ssjg.endfor
194246149Ssjg.endif
195246149Ssjg
196246149Ssjg.if !empty(STAGE_AS_SETS)
197246149SsjgCLEANFILES += ${STAGE_AS_SETS:@s@stage*$s@}
198246149Ssjg
199249033SsjgSTAGE_TARGETS += stage_as
200249033Ssjg
201246149Ssjg# sometimes things need to be renamed as they are staged
202246149Ssjg# each ${file} will be staged as ${STAGE_AS_${file:T}}
203246149Ssjg# one could achieve the same with SYMLINKS
204246149Ssjg.for s in ${STAGE_AS_SETS:O:u}
205246149SsjgSTAGE_AS.$s ?= ${.ALLSRC:N.dirdep}
206246149Ssjg
207246149Ssjgstage_as:	stage_as.$s
208246149Ssjgstage_as.$s:	.dirdep
209246149Ssjg	@${STAGE_AS_SCRIPT}; StageAs ${FLAGS.$@} ${STAGE_FILES_DIR.$s:U${STAGE_DIR.$s}:${STAGE_DIR_FILTER}} ${STAGE_AS.$s:@f@$f ${STAGE_AS_${f:T}:U${f:T}}@}
210246149Ssjg	@touch $@
211246149Ssjg
212246149Ssjg.endfor
213246149Ssjg.endif
214246149Ssjg
215249033SsjgCLEANFILES += ${STAGE_TARGETS}
216249033Ssjg
217249033Ssjg# stage_*links usually needs to follow any others.
218249033Ssjg.for t in ${STAGE_TARGETS:N*links:O:u}
219249033Ssjg.ORDER: $t stage_links
220249033Ssjg.ORDER: $t stage_symlinks
221249033Ssjg.endfor
222249033Ssjg
223249033Ssjg# make sure this exists
224249033Ssjgstaging:
225249033Ssjg
226249033Ssjg# generally we want staging to wait until everything else is done
227249033SsjgSTAGING_WAIT ?= .WAIT
228249033Ssjg
229249033Ssjgall: ${STAGING_WAIT} staging
230249033Ssjg
231250837Ssjg.if exists(${.PARSEDIR}/stage-install.sh) && !defined(STAGE_INSTALL)
232250837Ssjg# this will run install(1) and then followup with .dirdep files.
233250837SsjgSTAGE_INSTALL := sh ${.PARSEDIR:tA}/stage-install.sh INSTALL="${INSTALL}" OBJDIR=${.OBJDIR:tA}
234246149Ssjg.endif
235250837Ssjg
236250837Ssjg# if ${INSTALL} gets run during 'all' assume it is for staging?
237250837Ssjg.if ${.TARGETS:Nall} == "" && defined(STAGE_INSTALL)
238250837SsjgINSTALL := ${STAGE_INSTALL}
239250837Ssjg.if target(beforeinstall)
240250837Ssjgbeforeinstall: .dirdep
241250837Ssjg.endif
242250837Ssjg.endif
243250837Ssjg
244250837Ssjg.endif
245