xref: /freebsd/share/mk/meta.stage.mk (revision 9268022b74279434ed6300244e3f977e56a8ceb5)
1# $Id: meta.stage.mk,v 1.30 2013/04/19 16:32:57 sjg Exp $
2#
3#	@(#) Copyright (c) 2011, 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__${.PARSEFILE}__:
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
26# this allows us to trace dependencies back to their src dir
27.dirdep:
28	@echo '${_dirdep}' > $@
29
30.if defined(NO_POSIX_SHELL) || ${type printf:L:sh:Mbuiltin} == ""
31_stage_file_basename = `basename $$f`
32_stage_target_dirname = `dirname $$t`
33.else
34_stage_file_basename = $${f\#\#*/}
35_stage_target_dirname = $${t%/*}
36.endif
37
38_OBJROOT ?= ${OBJROOT:U${OBJTOP:H}}
39.if ${_OBJROOT:M*/} != ""
40_objroot ?= ${_OBJROOT:tA}/
41.else
42_objroot ?= ${_OBJROOT:tA}
43.endif
44
45# make sure this is global
46_STAGED_DIRS ?=
47.export _STAGED_DIRS
48# add each dir we stage to to _STAGED_DIRS
49# and make sure we have absolute paths so that bmake
50# will match against .MAKE.META.BAILIWICK
51STAGE_DIR_FILTER = tA:@d@$${_STAGED_DIRS::+=$$d}$$d@
52# convert _STAGED_DIRS into suitable filters
53GENDIRDEPS_FILTER += Nnot-empty-is-important \
54	${_STAGED_DIRS:O:u:M${OBJTOP}*:S,${OBJTOP}/,N,} \
55	${_STAGED_DIRS:O:u:N${OBJTOP}*:S,${_objroot},,:C,^([^/]+)/(.*),N\2.\1,:S,${HOST_TARGET},.host,}
56
57LN_CP_SCRIPT = LnCp() { \
58  rm -f $$2 2> /dev/null; \
59  ln $$1 $$2 2> /dev/null || \
60  cp -p $$1 $$2; }
61
62# it is an error for more than one src dir to try and stage
63# the same file
64STAGE_DIRDEP_SCRIPT = ${LN_CP_SCRIPT}; StageDirdep() { \
65  t=$$1; \
66  if [ -s $$t.dirdep ]; then \
67	cmp -s .dirdep $$t.dirdep && return; \
68	echo "ERROR: $$t installed by `cat $$t.dirdep` not ${_dirdep}" >&2; \
69	exit 1; \
70  fi; \
71  LnCp .dirdep $$t.dirdep || exit 1; }
72
73# common logic for staging files
74# this all relies on RELDIR being set to a subdir of SRCTOP
75# we use ln(1) if we can, else cp(1)
76STAGE_FILE_SCRIPT = ${STAGE_DIRDEP_SCRIPT}; StageFiles() { \
77  case "$$1" in "") return;; -m) mode=$$2; shift 2;; *) mode=;; esac; \
78  dest=$$1; shift; \
79  mkdir -p $$dest; \
80  [ -s .dirdep ] || echo '${_dirdep}' > .dirdep; \
81  for f in "$$@"; do \
82	case "$$f" in */*) t=$$dest/${_stage_file_basename};; *) t=$$dest/$$f;; esac; \
83	StageDirdep $$t; \
84	LnCp $$f $$t || exit 1; \
85	[ -z "$$mode" ] || chmod $$mode $$t; \
86  done; :; }
87
88STAGE_LINKS_SCRIPT = ${STAGE_DIRDEP_SCRIPT}; StageLinks() { \
89  case "$$1" in "") return;; --) shift;; -*) ldest= lnf=$$1; shift;; /*) ldest=$$1/;; esac; \
90  dest=$$1; shift; \
91  mkdir -p $$dest; \
92  [ -s .dirdep ] || echo '${_dirdep}' > .dirdep; \
93  while test $$\# -ge 2; do \
94	l=$$ldest$$1; shift; \
95	t=$$dest/$$1; \
96	case "$$1" in */*) mkdir -p ${_stage_target_dirname};; esac; \
97	shift; \
98	StageDirdep $$t; \
99	rm -f $$t 2>/dev/null; \
100	ln $$lnf $$l $$t || exit 1; \
101  done; :; }
102
103STAGE_AS_SCRIPT = ${STAGE_DIRDEP_SCRIPT}; StageAs() { \
104  case "$$1" in "") return;; -m) mode=$$2; shift 2;; *) mode=;; esac; \
105  dest=$$1; shift; \
106  mkdir -p $$dest; \
107  [ -s .dirdep ] || echo '${_dirdep}' > .dirdep; \
108  while test $$\# -ge 2; do \
109	s=$$1; shift; \
110	t=$$dest/$$1; \
111	case "$$1" in */*) mkdir -p ${_stage_target_dirname};; esac; \
112	shift; \
113	StageDirdep $$t; \
114	LnCp $$s $$t || exit 1; \
115	[ -z "$$mode" ] || chmod $$mode $$t; \
116  done; :; }
117
118# this is simple, a list of the "staged" files depends on this,
119_STAGE_BASENAME_USE:	.USE ${.TARGET:T}
120	@${STAGE_FILE_SCRIPT}; StageFiles ${.TARGET:H:${STAGE_DIR_FILTER}} ${.TARGET:T}
121
122.if !empty(STAGE_INCSDIR)
123STAGE_TARGETS += stage_incs
124STAGE_INCS ?= ${.ALLSRC:N.dirdep}
125
126stage_includes: stage_incs
127stage_incs:	.dirdep
128	@${STAGE_FILE_SCRIPT}; StageFiles ${STAGE_INCSDIR:${STAGE_DIR_FILTER}} ${STAGE_INCS}
129	@touch $@
130.endif
131
132.if !empty(STAGE_LIBDIR)
133STAGE_TARGETS += stage_libs
134
135STAGE_LIBS ?= ${.ALLSRC:N.dirdep}
136
137stage_libs:	.dirdep
138	@${STAGE_FILE_SCRIPT}; StageFiles ${STAGE_LIBDIR:${STAGE_DIR_FILTER}} ${STAGE_LIBS}
139.if !defined(NO_SHLIB_LINKS)
140.if !empty(SHLIB_LINKS)
141	@${STAGE_LINKS_SCRIPT}; StageLinks -s ${STAGE_LIBDIR:${STAGE_DIR_FILTER}} \
142	${SHLIB_LINKS:@t@${STAGE_LIBS:T:M$t.*} $t@}
143.elif !empty(SHLIB_LINK) && !empty(SHLIB_NAME)
144	@${STAGE_LINKS_SCRIPT}; StageLinks -s ${STAGE_LIBDIR:${STAGE_DIR_FILTER}} ${SHLIB_NAME} ${SHLIB_LINK} ${SYMLINKS:T}
145.endif
146.endif
147	@touch $@
148.endif
149
150.if !empty(STAGE_DIR)
151STAGE_SETS += _default
152STAGE_DIR._default = ${STAGE_DIR}
153STAGE_LINKS_DIR._default = ${STAGE_LINKS_DIR:U${STAGE_OBJTOP}}
154STAGE_SYMLINKS_DIR._default = ${STAGE_SYMLINKS_DIR:U${STAGE_OBJTOP}}
155STAGE_FILES._default = ${STAGE_FILES}
156STAGE_LINKS._default = ${STAGE_LINKS}
157STAGE_SYMLINKS._default = ${STAGE_SYMLINKS}
158STAGE_FILES ?= ${.ALLSRC:N.dirdep:Nstage_*}
159STAGE_SYMLINKS ?= ${.ALLSRC:T:N.dirdep:Nstage_*}
160.endif
161
162.if !empty(STAGE_SETS)
163CLEANFILES += ${STAGE_SETS:@s@stage*$s@}
164
165# some makefiles need to populate multiple directories
166.for s in ${STAGE_SETS:O:u}
167STAGE_FILES.$s ?= ${.ALLSRC:N.dirdep}
168STAGE_SYMLINKS.$s ?= ${.ALLSRC:N.dirdep}
169STAGE_LINKS_DIR.$s ?= ${STAGE_OBJTOP}
170STAGE_SYMLINKS_DIR.$s ?= ${STAGE_OBJTOP}
171
172STAGE_TARGETS += stage_files
173.if $s != "_default"
174stage_files:	stage_files.$s
175stage_files.$s:	.dirdep
176.else
177stage_files:	.dirdep
178.endif
179	@${STAGE_FILE_SCRIPT}; StageFiles ${FLAGS.$@} ${STAGE_FILES_DIR.$s:U${STAGE_DIR.$s}:${STAGE_DIR_FILTER}} ${STAGE_FILES.$s}
180	@touch $@
181
182STAGE_TARGETS += stage_links
183.if $s != "_default"
184stage_links:	stage_links.$s
185stage_links.$s:	.dirdep
186.else
187stage_links:	.dirdep
188.endif
189	@${STAGE_LINKS_SCRIPT}; StageLinks ${STAGE_LINKS_DIR.$s:U${STAGE_DIR.$s}:${STAGE_DIR_FILTER}} ${STAGE_LINKS.$s}
190	@touch $@
191
192STAGE_TARGETS += stage_symlinks
193.if $s != "_default"
194stage_symlinks:	stage_symlinks.$s
195stage_symlinks.$s:	.dirdep
196.else
197stage_symlinks:	.dirdep
198.endif
199	@${STAGE_LINKS_SCRIPT}; StageLinks -s ${STAGE_SYMLINKS_DIR.$s:U${STAGE_DIR.$s}:${STAGE_DIR_FILTER}} ${STAGE_SYMLINKS.$s}
200	@touch $@
201
202.endfor
203.endif
204
205.if !empty(STAGE_AS_SETS)
206CLEANFILES += ${STAGE_AS_SETS:@s@stage*$s@}
207
208STAGE_TARGETS += stage_as
209
210# sometimes things need to be renamed as they are staged
211# each ${file} will be staged as ${STAGE_AS_${file:T}}
212# one could achieve the same with SYMLINKS
213.for s in ${STAGE_AS_SETS:O:u}
214STAGE_AS.$s ?= ${.ALLSRC:N.dirdep}
215
216stage_as:	stage_as.$s
217stage_as.$s:	.dirdep
218	@${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}}@}
219	@touch $@
220
221.endfor
222.endif
223
224CLEANFILES += ${STAGE_TARGETS} stage_incs stage_includes
225
226# stage_*links usually needs to follow any others.
227.for t in ${STAGE_TARGETS:N*links:O:u}
228.ORDER: $t stage_links
229.ORDER: $t stage_symlinks
230.endfor
231
232# make sure this exists
233staging:
234
235# generally we want staging to wait until everything else is done
236STAGING_WAIT ?= .WAIT
237
238all: ${STAGING_WAIT} staging
239
240.if exists(${.PARSEDIR}/stage-install.sh) && !defined(STAGE_INSTALL)
241# this will run install(1) and then followup with .dirdep files.
242STAGE_INSTALL := sh ${.PARSEDIR:tA}/stage-install.sh INSTALL="${INSTALL}" OBJDIR=${.OBJDIR:tA}
243.endif
244
245# if ${INSTALL} gets run during 'all' assume it is for staging?
246.if ${.TARGETS:Nall} == "" && defined(STAGE_INSTALL)
247INSTALL := ${STAGE_INSTALL}
248.if target(beforeinstall)
249beforeinstall: .dirdep
250.endif
251.endif
252
253.endif
254