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