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