1# $FreeBSD$
2# $Id: progs.mk,v 1.11 2012/11/06 17:18:54 sjg Exp $
3#
4#	@(#) Copyright (c) 2006, 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.MAIN: all
18
19.if defined(PROGS) || defined(PROGS_CXX)
20# we really only use PROGS below...
21PROGS += ${PROGS_CXX}
22
23.if defined(PROG)
24# just one of many
25PROG_OVERRIDE_VARS +=	BINDIR BINGRP BINOWN BINMODE CSTD CXXSTD DPSRCS MAN \
26			NO_SHARED NO_WERROR PROGNAME SRCS STRIP WARNS
27PROG_VARS +=	CFLAGS CXXFLAGS DEBUG_FLAGS DPADD INTERNALPROG LDADD LIBADD \
28		LINKS LDFLAGS MLINKS ${PROG_OVERRIDE_VARS}
29.for v in ${PROG_VARS:O:u}
30.if empty(${PROG_OVERRIDE_VARS:M$v})
31.if defined(${v}.${PROG})
32$v += ${${v}.${PROG}}
33.elif defined(${v}_${PROG})
34$v += ${${v}_${PROG}}
35.endif
36.else
37.if defined(${v}.${PROG})
38$v = ${${v}.${PROG}}
39.elif defined(${v}_${PROG})
40$v = ${${v}_${PROG}}
41.endif
42$v ?=
43.endif
44.endfor
45
46.if ${MK_DIRDEPS_BUILD} == "yes"
47# Leave updating the Makefile.depend to the parent.
48UPDATE_DEPENDFILE = NO
49
50# Record our meta files for the parent to use.
51CLEANFILES+= ${PROG}.meta_files
52${PROG}.meta_files: .NOMETA $${.MAKE.META.CREATED} ${_this}
53	@echo "Updating ${.TARGET}: ${.OODATE:T:[1..8]}"
54	@echo ${.MAKE.META.FILES} > ${.TARGET}
55
56.if !defined(_SKIP_BUILD)
57.END: ${PROG}.meta_files
58.endif
59.endif	# ${MK_DIRDEPS_BUILD} == "yes"
60
61# prog.mk will do the rest
62.else # !defined(PROG)
63.if !defined(_SKIP_BUILD)
64all: ${PROGS}
65.endif
66
67META_XTRAS+=	${cat ${PROGS:S/$/*.meta_files/} 2>/dev/null || true:L:sh}
68
69.if ${MK_STAGING} != "no" && !empty(PROGS)
70# Stage from parent while respecting PROGNAME and BINDIR overrides.
71.for _prog in ${PROGS}
72STAGE_DIR.prog.${_prog}= ${STAGE_OBJTOP}${BINDIR.${_prog}:UBINDIR_${_prog}:U${BINDIR}}
73STAGE_AS_SETS+=	prog.${_prog}
74STAGE_AS_prog.${_prog}=	${PROGNAME.${_prog}:UPROGNAME_${_prog}:U${_prog}}
75stage_as.prog.${_prog}: ${_prog}
76.endfor
77.endif	# ${MK_STAGING} != "no" && !empty(PROGS)
78.endif
79.endif	# PROGS || PROGS_CXX
80
81# These are handled by the main make process.
82.ifdef _RECURSING_PROGS
83MK_STAGING= no
84
85_PROGS_GLOBAL_VARS= CLEANFILES CLEANDIRS CONFGROUPS DIRS FILESGROUPS INCSGROUPS \
86		    SCRIPTS
87.for v in ${_PROGS_GLOBAL_VARS}
88$v =
89.endfor
90.endif
91
92# handle being called [bsd.]progs.mk
93.include <bsd.prog.mk>
94
95# Find common sources among the PROGS to depend on them before building
96# anything.  This allows parallelization without them each fighting over
97# the same objects.
98_PROGS_COMMON_SRCS=
99_PROGS_ALL_SRCS=
100.for p in ${PROGS}
101.for s in ${SRCS.${p}}
102.if ${_PROGS_ALL_SRCS:M${s}} && !${_PROGS_COMMON_SRCS:M${s}}
103_PROGS_COMMON_SRCS+=	${s}
104.else
105_PROGS_ALL_SRCS+=	${s}
106.endif
107.endfor
108.endfor
109.if !empty(_PROGS_COMMON_SRCS)
110_PROGS_COMMON_OBJS=	${_PROGS_COMMON_SRCS:M*.[dhly]}
111.if !empty(_PROGS_COMMON_SRCS:N*.[dhly])
112_PROGS_COMMON_OBJS+=	${_PROGS_COMMON_SRCS:N*.[dhly]:${OBJS_SRCS_FILTER:ts:}:S/$/.o/g}
113.endif
114.endif
115
116# When recursing, ensure common sources are not rebuilt in META_MODE.
117.if defined(_RECURSING_PROGS) && !empty(_PROGS_COMMON_OBJS) && \
118    !empty(.MAKE.MODE:Mmeta)
119${_PROGS_COMMON_OBJS}: .NOMETA
120.endif
121
122.if !empty(PROGS) && !defined(_RECURSING_PROGS) && !defined(PROG)
123# tell progs.mk we might want to install things
124PROGS_TARGETS+= checkdpadd clean depend install
125# Only handle removing depend files from the main process.
126_PROG_MK.cleandir=	CLEANDEPENDFILES= CLEANDEPENDDIRS=
127_PROG_MK.cleanobj=	CLEANDEPENDFILES= CLEANDEPENDDIRS=
128# Only recurse on these if there is no objdir, meaning a normal
129# 'clean' gets ran via the target defined in bsd.obj.mk.
130# Same check from cleanobj: in bsd.obj.mk
131.if ${CANONICALOBJDIR} == ${.CURDIR} || !exists(${CANONICALOBJDIR}/)
132PROGS_TARGETS+=	cleandir cleanobj
133.endif
134
135# Ensure common objects are built before recursing.
136.if !empty(_PROGS_COMMON_OBJS)
137${PROGS}: ${_PROGS_COMMON_OBJS}
138.endif
139
140.for p in ${PROGS}
141.if defined(PROGS_CXX) && !empty(PROGS_CXX:M$p)
142# bsd.prog.mk may need to know this
143x.$p= PROG_CXX=$p
144.endif
145
146# Main PROG target
147$p ${p}_p: .PHONY .MAKE
148	(cd ${.CURDIR} && \
149	    DEPENDFILE=.depend.$p \
150	    NO_SUBDIR=1 ${MAKE} -f ${MAKEFILE} _RECURSING_PROGS=t \
151	    PROG=$p ${x.$p})
152
153# Pseudo targets for PROG, such as 'install'.
154.for t in ${PROGS_TARGETS:O:u}
155$p.$t: .PHONY .MAKE
156	(cd ${.CURDIR} && \
157	    DEPENDFILE=.depend.$p \
158	    NO_SUBDIR=1 ${MAKE} -f ${MAKEFILE} _RECURSING_PROGS=t \
159	    ${_PROG_MK.${t}} PROG=$p ${x.$p} ${@:E})
160.endfor
161.endfor
162
163# Depend main pseudo targets on all PROG.pseudo targets too.
164.for t in ${PROGS_TARGETS:O:u}
165.if make(${t})
166$t: ${PROGS:%=%.$t}
167.endif
168.endfor
169.endif	# !empty(PROGS) && !defined(_RECURSING_PROGS) && !defined(PROG)
170