xref: /freebsd/contrib/bmake/mk/meta.stage.mk (revision b0b1dbdd)
1# $Id: meta.stage.mk,v 1.48 2017/03/01 22:48:07 sjg Exp $
2#
3#	@(#) Copyright (c) 2011-2017, Simon J. Gerraty
4#
5#	This file is provided in the hope that it will
6#	be of use.  There is absolutely NO WARRANTY.
7#	Permission to copy, redistribute or otherwise
8#	use this file is hereby granted provided that
9#	the above copyright notice and this notice are
10#	left intact.
11#
12#	Please send copies of changes and bug-fixes to:
13#	sjg@crufty.net
14#
15
16.if !target(__${.PARSEFILE}__)
17# the guard target is defined later
18
19.if ${.MAKE.DEPENDFILE_PREFERENCE:U${.MAKE.DEPENDFILE}:M*.${MACHINE}} != ""
20# this is generally safer anyway
21_dirdep = ${RELDIR}.${MACHINE}
22.else
23_dirdep = ${RELDIR}
24.endif
25
26CLEANFILES+= .dirdep
27
28# this allows us to trace dependencies back to their src dir
29.dirdep:	.NOPATH
30	@echo '${_dirdep}' > $@
31
32.if defined(NO_POSIX_SHELL) || ${type printf:L:sh:Mbuiltin} == ""
33_stage_file_basename = `basename $$f`
34_stage_target_dirname = `dirname $$t`
35.else
36_stage_file_basename = $${f\#\#*/}
37_stage_target_dirname = $${t%/*}
38.endif
39
40_OBJROOT ?= ${OBJROOT:U${OBJTOP:H}}
41.if ${_OBJROOT:M*/} != ""
42_objroot ?= ${_OBJROOT:tA}/
43.else
44_objroot ?= ${_OBJROOT:tA}
45.endif
46
47# make sure this is global
48_STAGED_DIRS ?=
49.export _STAGED_DIRS
50# add each dir we stage to to _STAGED_DIRS
51# and make sure we have absolute paths so that bmake
52# will match against .MAKE.META.BAILIWICK
53STAGE_DIR_FILTER = tA:@d@$${_STAGED_DIRS::+=$$d}$$d@
54# convert _STAGED_DIRS into suitable filters
55GENDIRDEPS_FILTER += Nnot-empty-is-important \
56	${_STAGED_DIRS:O:u:M${OBJTOP}*:S,${OBJTOP}/,N,} \
57	${_STAGED_DIRS:O:u:M${_objroot}*:N${OBJTOP}*:S,${_objroot},,:C,^([^/]+)/(.*),N\2.\1,:S,${HOST_TARGET},.host,}
58
59LN_CP_SCRIPT = LnCp() { \
60  rm -f $$2 2> /dev/null; \
61  { [ -z "$$mode" ] && ${LN:Uln} $$1 $$2 2> /dev/null; } || \
62  cp -p $$1 $$2; }
63
64# a staging conflict should cause an error
65# a warning is handy when bootstapping different options.
66STAGE_CONFLICT?= ERROR
67.if ${STAGE_CONFLICT:tl} == "error"
68STAGE_CONFLICT_ACTION= exit 1;
69.else
70STAGE_CONFLICT_ACTION=
71.endif
72
73# it is an error for more than one src dir to try and stage
74# the same file
75STAGE_DIRDEP_SCRIPT = ${LN_CP_SCRIPT}; StageDirdep() { \
76  t=$$1; \
77  if [ -s $$t.dirdep ]; then \
78	cmp -s .dirdep $$t.dirdep && return; \
79	echo "${STAGE_CONFLICT}: $$t installed by `cat $$t.dirdep` not ${_dirdep}" >&2; \
80	${STAGE_CONFLICT_ACTION} \
81  fi; \
82  LnCp .dirdep $$t.dirdep || exit 1; }
83
84# common logic for staging files
85# this all relies on RELDIR being set to a subdir of SRCTOP
86# we use ln(1) if we can, else cp(1)
87STAGE_FILE_SCRIPT = ${STAGE_DIRDEP_SCRIPT}; StageFiles() { \
88  case "$$1" in "") return;; -m) mode=$$2; shift 2;; *) mode=;; esac; \
89  dest=$$1; shift; \
90  mkdir -p $$dest; \
91  [ -s .dirdep ] || echo '${_dirdep}' > .dirdep; \
92  for f in "$$@"; do \
93	case "$$f" in */*) t=$$dest/${_stage_file_basename};; *) t=$$dest/$$f;; esac; \
94	StageDirdep $$t; \
95	LnCp $$f $$t || exit 1; \
96	[ -z "$$mode" ] || chmod $$mode $$t; \
97  done; :; }
98
99STAGE_LINKS_SCRIPT = ${STAGE_DIRDEP_SCRIPT}; StageLinks() { \
100  case "$$1" in "") return;; --) shift;; -*) ldest= lnf=$$1; shift;; /*) ldest=$$1/;; esac; \
101  dest=$$1; shift; \
102  mkdir -p $$dest; \
103  [ -s .dirdep ] || echo '${_dirdep}' > .dirdep; \
104  while test $$\# -ge 2; do \
105	l=$$ldest$$1; shift; \
106	t=$$dest/$$1; \
107	case "$$1" in */*) mkdir -p ${_stage_target_dirname};; esac; \
108	shift; \
109	StageDirdep $$t; \
110	rm -f $$t 2>/dev/null; \
111	ln $$lnf $$l $$t || exit 1; \
112  done; :; }
113
114STAGE_AS_SCRIPT = ${STAGE_DIRDEP_SCRIPT}; StageAs() { \
115  case "$$1" in "") return;; -m) mode=$$2; shift 2;; *) mode=;; esac; \
116  dest=$$1; shift; \
117  mkdir -p $$dest; \
118  [ -s .dirdep ] || echo '${_dirdep}' > .dirdep; \
119  while test $$\# -ge 2; do \
120	s=$$1; shift; \
121	t=$$dest/$$1; \
122	case "$$1" in */*) mkdir -p ${_stage_target_dirname};; esac; \
123	shift; \
124	StageDirdep $$t; \
125	LnCp $$s $$t || exit 1; \
126	[ -z "$$mode" ] || chmod $$mode $$t; \
127  done; :; }
128
129# this is simple, a list of the "staged" files depends on this,
130_STAGE_BASENAME_USE:	.USE .dirdep ${.TARGET:T}
131	@${STAGE_FILE_SCRIPT}; StageFiles ${.TARGET:H:${STAGE_DIR_FILTER}} ${.TARGET:T}
132
133_STAGE_AS_BASENAME_USE:        .USE .dirdep ${.TARGET:T}
134	@${STAGE_AS_SCRIPT}; StageAs ${.TARGET:H:${STAGE_DIR_FILTER}} ${.TARGET:T} ${STAGE_AS_${.TARGET:T}:U${.TARGET:T}}
135
136
137stage_includes: stage_incs
138stage_incs:	.dirdep
139	@${STAGE_FILE_SCRIPT}; StageFiles ${STAGE_INCSDIR:${STAGE_DIR_FILTER}} ${STAGE_INCS}
140	@touch $@
141
142stage_libs:	.dirdep
143	@${STAGE_FILE_SCRIPT}; StageFiles ${STAGE_LIBDIR:${STAGE_DIR_FILTER}} ${STAGE_LIBS}
144.if !defined(NO_SHLIB_LINKS)
145.if !empty(SHLIB_LINKS)
146	@${STAGE_LINKS_SCRIPT}; StageLinks -s ${STAGE_LIBDIR:${STAGE_DIR_FILTER}} \
147	${SHLIB_LINKS:@t@${STAGE_LIBS:T:M$t.*} $t@}
148.elif !empty(SHLIB_LINK) && !empty(SHLIB_NAME)
149	@${STAGE_LINKS_SCRIPT}; StageLinks -s ${STAGE_LIBDIR:${STAGE_DIR_FILTER}} ${SHLIB_NAME} ${SHLIB_LINK}
150.endif
151.endif
152	@touch $@
153
154.endif				# first time
155
156
157.if !empty(STAGE_INCSDIR)
158STAGE_TARGETS += stage_incs
159STAGE_INCS ?= ${.ALLSRC:N.dirdep:Nstage_*}
160.endif
161
162.if !empty(STAGE_LIBDIR)
163STAGE_TARGETS += stage_libs
164STAGE_LIBS ?= ${.ALLSRC:N.dirdep:Nstage_*}
165.endif
166
167.if !empty(STAGE_DIR)
168STAGE_SETS += _default
169STAGE_DIR._default = ${STAGE_DIR}
170STAGE_LINKS_DIR._default = ${STAGE_LINKS_DIR:U${STAGE_OBJTOP}}
171STAGE_SYMLINKS_DIR._default = ${STAGE_SYMLINKS_DIR:U${STAGE_OBJTOP}}
172STAGE_FILES._default = ${STAGE_FILES}
173STAGE_LINKS._default = ${STAGE_LINKS}
174STAGE_SYMLINKS._default = ${STAGE_SYMLINKS}
175STAGE_FILES ?= ${.ALLSRC:N.dirdep:Nstage_*}
176STAGE_SYMLINKS ?= ${.ALLSRC:T:N.dirdep:Nstage_*}
177.endif
178
179.if !empty(STAGE_SETS)
180CLEANFILES += ${STAGE_SETS:@s@stage*$s@}
181
182# some makefiles need to populate multiple directories
183.for s in ${STAGE_SETS:O:u}
184STAGE_FILES.$s ?= ${.ALLSRC:N.dirdep:Nstage_*}
185STAGE_SYMLINKS.$s ?= ${.ALLSRC:N.dirdep:Nstage_*}
186STAGE_LINKS_DIR.$s ?= ${STAGE_OBJTOP}
187STAGE_SYMLINKS_DIR.$s ?= ${STAGE_OBJTOP}
188
189STAGE_TARGETS += stage_files
190.if !target(.stage_files.$s)
191.stage_files.$s:
192.if $s != "_default"
193stage_files:	stage_files.$s
194stage_files.$s:	.dirdep
195.else
196stage_files:	.dirdep
197.endif
198	@${STAGE_FILE_SCRIPT}; StageFiles ${FLAGS.$@} ${STAGE_FILES_DIR.$s:U${STAGE_DIR.$s}:${STAGE_DIR_FILTER}} ${STAGE_FILES.$s}
199	@touch $@
200.endif
201
202STAGE_TARGETS += stage_links
203.if !target(.stage_links.$s)
204.stage_links.$s:
205.if $s != "_default"
206stage_links:	stage_links.$s
207stage_links.$s:	.dirdep
208.else
209stage_links:	.dirdep
210.endif
211	@${STAGE_LINKS_SCRIPT}; StageLinks ${STAGE_LINKS_DIR.$s:U${STAGE_DIR.$s}:${STAGE_DIR_FILTER}} ${STAGE_LINKS.$s}
212	@touch $@
213.endif
214
215STAGE_TARGETS += stage_symlinks
216.if !target(.stage_symlinks.$s)
217.stage_symlinks.$s:
218.if $s != "_default"
219stage_symlinks:	stage_symlinks.$s
220stage_symlinks.$s:	.dirdep
221.else
222stage_symlinks:	.dirdep
223.endif
224	@${STAGE_LINKS_SCRIPT}; StageLinks -s ${STAGE_SYMLINKS_DIR.$s:U${STAGE_DIR.$s}:${STAGE_DIR_FILTER}} ${STAGE_SYMLINKS.$s}
225	@touch $@
226.endif
227
228.endfor
229.endif
230
231.if !empty(STAGE_AS_SETS)
232CLEANFILES += ${STAGE_AS_SETS:@s@stage*$s@}
233
234STAGE_TARGETS += stage_as stage_as_and_symlink
235
236# sometimes things need to be renamed as they are staged
237# each ${file} will be staged as ${STAGE_AS_${file:T}}
238# one could achieve the same with SYMLINKS
239# stage_as_and_symlink makes the original name a symlink to the new name
240# it is the same as using stage_as and stage_symlinks but ensures
241# both operations happen together
242.for s in ${STAGE_AS_SETS:O:u}
243STAGE_AS.$s ?= ${.ALLSRC:N.dirdep:Nstage_*}
244STAGE_AS_AND_SYMLINK.$s ?= ${.ALLSRC:N.dirdep:Nstage_*}
245
246.if !target(.stage_as.$s)
247.stage_as.$s:
248stage_as:	stage_as.$s
249stage_as.$s:	.dirdep
250	@${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}}}@}
251	@touch $@
252.endif
253
254.if !target(.stage_as_and_symlink.$s)
255.stage_as_and_symlink.$s:
256stage_as_and_symlink:	stage_as_and_symlink.$s
257stage_as_and_symlink.$s:	.dirdep
258	@${STAGE_AS_SCRIPT}; StageAs ${FLAGS.$@} ${STAGE_FILES_DIR.$s:U${STAGE_DIR.$s}:${STAGE_DIR_FILTER}} ${STAGE_AS_AND_SYMLINK.$s:@f@$f ${STAGE_AS_${f:tA}:U${STAGE_AS_${f:T}:U${f:T}}}@}
259	@${STAGE_LINKS_SCRIPT}; StageLinks -s ${STAGE_FILES_DIR.$s:U${STAGE_DIR.$s}:${STAGE_DIR_FILTER}} ${STAGE_AS_AND_SYMLINK.$s:@f@${STAGE_AS_${f:tA}:U${STAGE_AS_${f:T}:U${f:T}}} $f@}
260	@touch $@
261.endif
262
263.endfor
264.endif
265
266CLEANFILES += ${STAGE_TARGETS} stage_incs stage_includes
267
268# this lot also only makes sense the first time...
269.if !target(__${.PARSEFILE}__)
270__${.PARSEFILE}__:
271
272# stage_*links usually needs to follow any others.
273# for non-jobs mode the order here matters
274staging: ${STAGE_TARGETS:N*_links} ${STAGE_TARGETS:M*_links}
275
276.if ${.MAKE.JOBS:U0} > 0 && ${STAGE_TARGETS:U:M*_links} != ""
277# the above isn't sufficient
278.for t in ${STAGE_TARGETS:N*links:O:u}
279.ORDER: $t stage_links
280.endfor
281.endif
282
283# generally we want staging to wait until everything else is done
284STAGING_WAIT ?= .WAIT
285
286.if ${.MAKE.LEVEL} > 0
287all: ${STAGING_WAIT} staging
288.endif
289
290.if exists(${.PARSEDIR}/stage-install.sh) && !defined(STAGE_INSTALL)
291# this will run install(1) and then followup with .dirdep files.
292STAGE_INSTALL := sh ${.PARSEDIR:tA}/stage-install.sh INSTALL="${INSTALL}" OBJDIR=${.OBJDIR:tA}
293.endif
294
295# if ${INSTALL} gets run during 'all' assume it is for staging?
296.if ${.TARGETS:Nall} == "" && defined(STAGE_INSTALL)
297INSTALL := ${STAGE_INSTALL}
298.if target(beforeinstall)
299beforeinstall: .dirdep
300.endif
301.endif
302.NOPATH: ${STAGE_FILES}
303
304.if !empty(STAGE_TARGETS)
305.NOPATH: ${CLEANFILES}
306
307MK_STALE_STAGED?= no
308.if ${MK_STALE_STAGED} == "yes"
309all: stale_staged
310# get a list of paths that we have just staged
311# get a list of paths that we have previously staged to those same dirs
312# anything in the 2nd list but not the first is stale - remove it.
313stale_staged: staging .NOMETA
314	@egrep '^[WL] .*${STAGE_OBJTOP}' /dev/null ${.MAKE.META.FILES:M*stage_*} | \
315	sed "/\.dirdep/d;s,.* '*\(${STAGE_OBJTOP}/[^ '][^ ']*\).*,\1," | \
316	sort > ${.TARGET}.staged1
317	@grep -l '${_dirdep}' /dev/null ${_STAGED_DIRS:M${STAGE_OBJTOP}*:O:u:@d@$d/*.dirdep@} | \
318	sed 's,\.dirdep,,' | sort > ${.TARGET}.staged2
319	@comm -13 ${.TARGET}.staged1 ${.TARGET}.staged2 > ${.TARGET}.stale
320	@test ! -s ${.TARGET}.stale || { \
321		echo "Removing stale staged files..."; \
322		sed 's,.*,& &.dirdep,' ${.TARGET}.stale | xargs rm -f; }
323
324.endif
325.endif
326.endif
327