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