1 # $Id: meta.stage.mk,v 1.30 2013/04/19 16:32:57 sjg Exp $
3 # @(#) Copyright (c) 2011, Simon J. Gerraty
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
12 # Please send copies of changes and bug-fixes to:
16 .if !target(__${.PARSEFILE}__)
19 .if ${.MAKE.DEPENDFILE_PREFERENCE:U${.MAKE.DEPENDFILE}:M*.${MACHINE}} != ""
20 # this is generally safer anyway
21 _dirdep = ${RELDIR}.${MACHINE}
26 # this allows us to trace dependencies back to their src dir
28 @echo '${_dirdep}' > $@
30 .if defined(NO_POSIX_SHELL) || ${type printf:L:sh:Mbuiltin} == ""
31 _stage_file_basename = `basename $$f`
32 _stage_target_dirname = `dirname $$t`
34 _stage_file_basename = $${f\#\#*/}
35 _stage_target_dirname = $${t%/*}
38 _objroot ?= ${_OBJROOT:tA}
39 # make sure this is global
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
45 STAGE_DIR_FILTER = tA:@d@$${_STAGED_DIRS::+=$$d}$$d@
46 # convert _STAGED_DIRS into suitable filters
47 GENDIRDEPS_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,}
51 LN_CP_SCRIPT = LnCp() { \
52 rm -f $$2 2> /dev/null; \
53 ln $$1 $$2 2> /dev/null || \
56 # it is an error for more than one src dir to try and stage
58 STAGE_DIRDEP_SCRIPT = ${LN_CP_SCRIPT}; StageDirdep() { \
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; \
65 LnCp .dirdep $$t.dirdep || exit 1; }
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)
70 STAGE_FILE_SCRIPT = ${STAGE_DIRDEP_SCRIPT}; StageFiles() { \
71 case "$$1" in "") return;; -m) mode=$$2; shift 2;; *) mode=;; esac; \
74 [ -s .dirdep ] || echo '${_dirdep}' > .dirdep; \
76 case "$$f" in */*) t=$$dest/${_stage_file_basename};; *) t=$$dest/$$f;; esac; \
78 LnCp $$f $$t || exit 1; \
79 [ -z "$$mode" ] || chmod $$mode $$t; \
82 STAGE_LINKS_SCRIPT = ${STAGE_DIRDEP_SCRIPT}; StageLinks() { \
83 case "$$1" in "") return;; --) shift;; -*) ldest= lnf=$$1; shift;; /*) ldest=$$1/;; esac; \
86 [ -s .dirdep ] || echo '${_dirdep}' > .dirdep; \
87 while test $$\# -ge 2; do \
88 l=$$ldest$$1; shift; \
90 case "$$1" in */*) mkdir -p ${_stage_target_dirname};; esac; \
93 rm -f $$t 2>/dev/null; \
94 ln $$lnf $$l $$t || exit 1; \
97 STAGE_AS_SCRIPT = ${STAGE_DIRDEP_SCRIPT}; StageAs() { \
98 case "$$1" in "") return;; -m) mode=$$2; shift 2;; *) mode=;; esac; \
101 [ -s .dirdep ] || echo '${_dirdep}' > .dirdep; \
102 while test $$\# -ge 2; do \
105 case "$$1" in */*) mkdir -p ${_stage_target_dirname};; esac; \
108 LnCp $$s $$t || exit 1; \
109 [ -z "$$mode" ] || chmod $$mode $$t; \
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}
116 .if !empty(STAGE_INCSDIR)
117 STAGE_TARGETS += stage_incs
118 STAGE_INCS ?= ${.ALLSRC:N.dirdep}
121 @${STAGE_FILE_SCRIPT}; StageFiles ${STAGE_INCSDIR:${STAGE_DIR_FILTER}} ${STAGE_INCS}
125 .if !empty(STAGE_LIBDIR)
126 STAGE_TARGETS += stage_libs
128 STAGE_LIBS ?= ${.ALLSRC:N.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}
141 .if !empty(STAGE_DIR)
142 STAGE_SETS += _default
143 STAGE_DIR._default = ${STAGE_DIR}
144 STAGE_LINKS_DIR._default = ${STAGE_LINKS_DIR:U${STAGE_OBJTOP}}
145 STAGE_SYMLINKS_DIR._default = ${STAGE_SYMLINKS_DIR:U${STAGE_OBJTOP}}
146 STAGE_FILES._default = ${STAGE_FILES}
147 STAGE_LINKS._default = ${STAGE_LINKS}
148 STAGE_SYMLINKS._default = ${STAGE_SYMLINKS}
149 STAGE_FILES ?= ${.ALLSRC:N.dirdep:Nstage_*}
150 STAGE_SYMLINKS ?= ${.ALLSRC:T:N.dirdep:Nstage_*}
153 .if !empty(STAGE_SETS)
154 CLEANFILES += ${STAGE_SETS:@s@stage*$s@}
156 # some makefiles need to populate multiple directories
157 .for s in ${STAGE_SETS:O:u}
158 STAGE_FILES.$s ?= ${.ALLSRC:N.dirdep}
159 STAGE_SYMLINKS.$s ?= ${.ALLSRC:N.dirdep}
160 STAGE_LINKS_DIR.$s ?= ${STAGE_OBJTOP}
161 STAGE_SYMLINKS_DIR.$s ?= ${STAGE_OBJTOP}
163 STAGE_TARGETS += stage_files
165 stage_files: stage_files.$s
166 stage_files.$s: .dirdep
170 @${STAGE_FILE_SCRIPT}; StageFiles ${FLAGS.$@} ${STAGE_FILES_DIR.$s:U${STAGE_DIR.$s}:${STAGE_DIR_FILTER}} ${STAGE_FILES.$s}
173 STAGE_TARGETS += stage_links
175 stage_links: stage_links.$s
176 stage_links.$s: .dirdep
180 @${STAGE_LINKS_SCRIPT}; StageLinks ${STAGE_LINKS_DIR.$s:U${STAGE_DIR.$s}:${STAGE_DIR_FILTER}} ${STAGE_LINKS.$s}
183 STAGE_TARGETS += stage_symlinks
185 stage_symlinks: stage_symlinks.$s
186 stage_symlinks.$s: .dirdep
188 stage_symlinks: .dirdep
190 @${STAGE_LINKS_SCRIPT}; StageLinks -s ${STAGE_SYMLINKS_DIR.$s:U${STAGE_DIR.$s}:${STAGE_DIR_FILTER}} ${STAGE_SYMLINKS.$s}
196 .if !empty(STAGE_AS_SETS)
197 CLEANFILES += ${STAGE_AS_SETS:@s@stage*$s@}
199 STAGE_TARGETS += stage_as
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}
205 STAGE_AS.$s ?= ${.ALLSRC:N.dirdep}
207 stage_as: stage_as.$s
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}}@}
215 CLEANFILES += ${STAGE_TARGETS}
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
223 # make sure this exists
226 # generally we want staging to wait until everything else is done
227 STAGING_WAIT ?= .WAIT
229 all: ${STAGING_WAIT} staging
231 .if exists(${.PARSEDIR}/stage-install.sh) && !defined(STAGE_INSTALL)
232 # this will run install(1) and then followup with .dirdep files.
233 STAGE_INSTALL := sh ${.PARSEDIR:tA}/stage-install.sh INSTALL="${INSTALL}" OBJDIR=${.OBJDIR:tA}
236 # if ${INSTALL} gets run during 'all' assume it is for staging?
237 .if ${.TARGETS:Nall} == "" && defined(STAGE_INSTALL)
238 INSTALL := ${STAGE_INSTALL}
239 .if target(beforeinstall)
240 beforeinstall: .dirdep