xref: /dragonfly/contrib/bmake/mk/meta.stage.mk (revision 0ca59c34)
1# $Id: meta.stage.mk,v 1.32 2014/10/18 05:48:02 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:M${_objroot}*: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_STAGE_AS_BASENAME_USE:        .USE ${.TARGET:T}
117	@${STAGE_AS_SCRIPT}; StageAs ${.TARGET:H:${STAGE_DIR_FILTER}} ${.TARGET:T} ${STAGE_AS_${.TARGET:T}:U${.TARGET:T}}
118
119.if !empty(STAGE_INCSDIR)
120STAGE_TARGETS += stage_incs
121STAGE_INCS ?= ${.ALLSRC:N.dirdep}
122
123stage_incs:	.dirdep
124	@${STAGE_FILE_SCRIPT}; StageFiles ${STAGE_INCSDIR:${STAGE_DIR_FILTER}} ${STAGE_INCS}
125	@touch $@
126.endif
127
128.if !empty(STAGE_LIBDIR)
129STAGE_TARGETS += stage_libs
130
131STAGE_LIBS ?= ${.ALLSRC:N.dirdep}
132
133stage_libs:	.dirdep
134	@${STAGE_FILE_SCRIPT}; StageFiles ${STAGE_LIBDIR:${STAGE_DIR_FILTER}} ${STAGE_LIBS}
135.if !empty(SHLIB_LINKS)
136	@${STAGE_LINKS_SCRIPT}; StageLinks -s ${STAGE_LIBDIR:${STAGE_DIR_FILTER}} \
137	${SHLIB_LINKS:@t@${STAGE_LIBS:T:M$t.*} $t@}
138.elif !empty(SHLIB_LINK) && !empty(SHLIB_NAME)
139	@${STAGE_LINKS_SCRIPT}; StageLinks -s ${STAGE_LIBDIR:${STAGE_DIR_FILTER}} ${SHLIB_NAME} ${SHLIB_LINK} ${SYMLINKS:T}
140.endif
141	@touch $@
142.endif
143
144.if !empty(STAGE_DIR)
145STAGE_SETS += _default
146STAGE_DIR._default = ${STAGE_DIR}
147STAGE_LINKS_DIR._default = ${STAGE_LINKS_DIR:U${STAGE_OBJTOP}}
148STAGE_SYMLINKS_DIR._default = ${STAGE_SYMLINKS_DIR:U${STAGE_OBJTOP}}
149STAGE_FILES._default = ${STAGE_FILES}
150STAGE_LINKS._default = ${STAGE_LINKS}
151STAGE_SYMLINKS._default = ${STAGE_SYMLINKS}
152STAGE_FILES ?= ${.ALLSRC:N.dirdep:Nstage_*}
153STAGE_SYMLINKS ?= ${.ALLSRC:T:N.dirdep:Nstage_*}
154.endif
155
156.if !empty(STAGE_SETS)
157CLEANFILES += ${STAGE_SETS:@s@stage*$s@}
158
159# some makefiles need to populate multiple directories
160.for s in ${STAGE_SETS:O:u}
161STAGE_FILES.$s ?= ${.ALLSRC:N.dirdep}
162STAGE_SYMLINKS.$s ?= ${.ALLSRC:N.dirdep}
163STAGE_LINKS_DIR.$s ?= ${STAGE_OBJTOP}
164STAGE_SYMLINKS_DIR.$s ?= ${STAGE_OBJTOP}
165
166STAGE_TARGETS += stage_files
167.if $s != "_default"
168stage_files:	stage_files.$s
169stage_files.$s:	.dirdep
170.else
171stage_files:	.dirdep
172.endif
173	@${STAGE_FILE_SCRIPT}; StageFiles ${FLAGS.$@} ${STAGE_FILES_DIR.$s:U${STAGE_DIR.$s}:${STAGE_DIR_FILTER}} ${STAGE_FILES.$s}
174	@touch $@
175
176STAGE_TARGETS += stage_links
177.if $s != "_default"
178stage_links:	stage_links.$s
179stage_links.$s:	.dirdep
180.else
181stage_links:	.dirdep
182.endif
183	@${STAGE_LINKS_SCRIPT}; StageLinks ${STAGE_LINKS_DIR.$s:U${STAGE_DIR.$s}:${STAGE_DIR_FILTER}} ${STAGE_LINKS.$s}
184	@touch $@
185
186STAGE_TARGETS += stage_symlinks
187.if $s != "_default"
188stage_symlinks:	stage_symlinks.$s
189stage_symlinks.$s:	.dirdep
190.else
191stage_symlinks:	.dirdep
192.endif
193	@${STAGE_LINKS_SCRIPT}; StageLinks -s ${STAGE_SYMLINKS_DIR.$s:U${STAGE_DIR.$s}:${STAGE_DIR_FILTER}} ${STAGE_SYMLINKS.$s}
194	@touch $@
195
196.endfor
197.endif
198
199.if !empty(STAGE_AS_SETS)
200CLEANFILES += ${STAGE_AS_SETS:@s@stage*$s@}
201
202STAGE_TARGETS += stage_as
203
204# sometimes things need to be renamed as they are staged
205# each ${file} will be staged as ${STAGE_AS_${file:T}}
206# one could achieve the same with SYMLINKS
207.for s in ${STAGE_AS_SETS:O:u}
208STAGE_AS.$s ?= ${.ALLSRC:N.dirdep}
209
210stage_as:	stage_as.$s
211stage_as.$s:	.dirdep
212	@${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}}@}
213	@touch $@
214
215.endfor
216.endif
217
218CLEANFILES += ${STAGE_TARGETS}
219
220# stage_*links usually needs to follow any others.
221.for t in ${STAGE_TARGETS:N*links:O:u}
222.ORDER: $t stage_links
223.ORDER: $t stage_symlinks
224.endfor
225
226# make sure this exists
227staging:
228
229# generally we want staging to wait until everything else is done
230STAGING_WAIT ?= .WAIT
231
232all: ${STAGING_WAIT} staging
233
234.if exists(${.PARSEDIR}/stage-install.sh) && !defined(STAGE_INSTALL)
235# this will run install(1) and then followup with .dirdep files.
236STAGE_INSTALL := sh ${.PARSEDIR:tA}/stage-install.sh INSTALL="${INSTALL}" OBJDIR=${.OBJDIR:tA}
237.endif
238
239# if ${INSTALL} gets run during 'all' assume it is for staging?
240.if ${.TARGETS:Nall} == "" && defined(STAGE_INSTALL)
241INSTALL := ${STAGE_INSTALL}
242.if target(beforeinstall)
243beforeinstall: .dirdep
244.endif
245.endif
246.NOPATH: ${STAGE_FILES}
247
248.endif
249