xref: /freebsd/share/mk/bsd.prog.mk (revision 273c26a3c3bea87a241d6879abd4f991db180bf0)
1#	from: @(#)bsd.prog.mk	5.26 (Berkeley) 6/25/91
2# $FreeBSD$
3
4.include <bsd.init.mk>
5.include <bsd.compiler.mk>
6
7.SUFFIXES: .out .o .c .cc .cpp .cxx .C .m .y .l .ln .s .S .asm
8
9# XXX The use of COPTS in modern makefiles is discouraged.
10.if defined(COPTS)
11.warning ${.CURDIR}: COPTS should be CFLAGS.
12CFLAGS+=${COPTS}
13.endif
14
15.if ${MK_ASSERT_DEBUG} == "no"
16CFLAGS+= -DNDEBUG
17NO_WERROR=
18.endif
19
20.if defined(DEBUG_FLAGS)
21CFLAGS+=${DEBUG_FLAGS}
22CXXFLAGS+=${DEBUG_FLAGS}
23
24.if ${MK_CTF} != "no" && ${DEBUG_FLAGS:M-g} != ""
25CTFFLAGS+= -g
26.endif
27.endif
28
29.if defined(PROG_CXX)
30PROG=	${PROG_CXX}
31.endif
32
33.if !empty(LDFLAGS:M-Wl,*--oformat,*) || !empty(LDFLAGS:M-static)
34MK_DEBUG_FILES=	no
35.endif
36
37.if defined(CRUNCH_CFLAGS)
38CFLAGS+=${CRUNCH_CFLAGS}
39.else
40.if ${MK_DEBUG_FILES} != "no" && empty(DEBUG_FLAGS:M-g) && \
41    empty(DEBUG_FLAGS:M-gdwarf-*)
42CFLAGS+= -g
43CTFFLAGS+= -g
44.endif
45.endif
46
47.if !defined(DEBUG_FLAGS)
48STRIP?=	-s
49.endif
50
51.if defined(NO_ROOT)
52.if !defined(TAGS) || ! ${TAGS:Mpackage=*}
53TAGS+=		package=${PACKAGE:Uruntime}
54.endif
55TAG_ARGS=	-T ${TAGS:[*]:S/ /,/g}
56.endif
57
58.if defined(NO_SHARED) && (${NO_SHARED} != "no" && ${NO_SHARED} != "NO")
59LDFLAGS+= -static
60.endif
61
62.if ${MK_DEBUG_FILES} != "no"
63PROG_FULL=${PROG}.full
64# Use ${DEBUGDIR} for base system debug files, else .debug subdirectory
65.if defined(BINDIR) && (\
66    ${BINDIR} == "/bin" ||\
67    ${BINDIR:C%/libexec(/.*)?%/libexec%} == "/libexec" ||\
68    ${BINDIR} == "/sbin" ||\
69    ${BINDIR:C%/usr/(bin|bsdinstall|libexec|lpr|sendmail|sm.bin|sbin|tests)(/.*)?%/usr/bin%} == "/usr/bin" ||\
70    ${BINDIR} == "/usr/lib" \
71     )
72DEBUGFILEDIR=	${DEBUGDIR}${BINDIR}
73.else
74DEBUGFILEDIR?=	${BINDIR}/.debug
75.endif
76.if !exists(${DESTDIR}${DEBUGFILEDIR})
77DEBUGMKDIR=
78.endif
79.else
80PROG_FULL=	${PROG}
81.endif
82
83.if defined(PROG)
84PROGNAME?=	${PROG}
85
86.if defined(SRCS)
87
88OBJS+=  ${SRCS:N*.h:R:S/$/.o/g}
89
90.if target(beforelinking)
91beforelinking: ${OBJS}
92${PROG_FULL}: beforelinking
93.endif
94${PROG_FULL}: ${OBJS}
95.if defined(PROG_CXX)
96	${CXX:N${CCACHE_BIN}} ${CXXFLAGS:N-M*} ${LDFLAGS} -o ${.TARGET} \
97	    ${OBJS} ${LDADD}
98.else
99	${CC:N${CCACHE_BIN}} ${CFLAGS:N-M*} ${LDFLAGS} -o ${.TARGET} ${OBJS} \
100	    ${LDADD}
101.endif
102.if ${MK_CTF} != "no"
103	${CTFMERGE} ${CTFFLAGS} -o ${.TARGET} ${OBJS}
104.endif
105
106.else	# !defined(SRCS)
107
108.if !target(${PROG})
109.if defined(PROG_CXX)
110SRCS=	${PROG}.cc
111.else
112SRCS=	${PROG}.c
113.endif
114
115# Always make an intermediate object file because:
116# - it saves time rebuilding when only the library has changed
117# - the name of the object gets put into the executable symbol table instead of
118#   the name of a variable temporary object.
119# - it's useful to keep objects around for crunching.
120OBJS+=	${PROG}.o
121
122.if target(beforelinking)
123beforelinking: ${OBJS}
124${PROG_FULL}: beforelinking
125.endif
126${PROG_FULL}: ${OBJS}
127.if defined(PROG_CXX)
128	${CXX:N${CCACHE_BIN}} ${CXXFLAGS:N-M*} ${LDFLAGS} -o ${.TARGET} \
129	    ${OBJS} ${LDADD}
130.else
131	${CC:N${CCACHE_BIN}} ${CFLAGS:N-M*} ${LDFLAGS} -o ${.TARGET} ${OBJS} \
132	    ${LDADD}
133.endif
134.if ${MK_CTF} != "no"
135	${CTFMERGE} ${CTFFLAGS} -o ${.TARGET} ${OBJS}
136.endif
137.endif # !target(${PROG})
138
139.endif # !defined(SRCS)
140
141.if ${MK_DEBUG_FILES} != "no"
142${PROG}: ${PROG_FULL} ${PROGNAME}.debug
143	${OBJCOPY} --strip-debug --add-gnu-debuglink=${PROGNAME}.debug \
144	    ${PROG_FULL} ${.TARGET}
145
146${PROGNAME}.debug: ${PROG_FULL}
147	${OBJCOPY} --only-keep-debug ${PROG_FULL} ${.TARGET}
148.endif
149
150.if	${MK_MAN} != "no" && !defined(MAN) && \
151	!defined(MAN1) && !defined(MAN2) && !defined(MAN3) && \
152	!defined(MAN4) && !defined(MAN5) && !defined(MAN6) && \
153	!defined(MAN7) && !defined(MAN8) && !defined(MAN9)
154MAN=	${PROG}.1
155MAN1=	${MAN}
156.endif
157.endif # defined(PROG)
158
159.if defined(_SKIP_BUILD)
160all:
161.else
162all: ${PROG} ${SCRIPTS}
163.if ${MK_MAN} != "no"
164all: all-man
165.endif
166.endif
167
168.if defined(PROG)
169CLEANFILES+= ${PROG}
170.if ${MK_DEBUG_FILES} != "no"
171CLEANFILES+=	${PROG_FULL} ${PROGNAME}.debug
172.endif
173.endif
174
175.if defined(OBJS)
176CLEANFILES+= ${OBJS}
177.endif
178
179.include <bsd.libnames.mk>
180
181.if defined(PROG)
182.if !defined(NO_EXTRADEPEND)
183_EXTRADEPEND:
184.if defined(LDFLAGS) && !empty(LDFLAGS:M-nostdlib)
185.if defined(DPADD) && !empty(DPADD)
186	echo ${PROG_FULL}: ${DPADD} >> ${DEPENDFILE}
187.endif
188.else
189	echo ${PROG_FULL}: ${LIBC} ${DPADD} >> ${DEPENDFILE}
190.if defined(PROG_CXX)
191.if ${COMPILER_TYPE} == "clang" && empty(CXXFLAGS:M-stdlib=libstdc++)
192	echo ${PROG_FULL}: ${LIBCPLUSPLUS} >> ${DEPENDFILE}
193.else
194	echo ${PROG_FULL}: ${LIBSTDCPLUSPLUS} >> ${DEPENDFILE}
195.endif
196.endif
197.endif
198.endif	# !defined(NO_EXTRADEPEND)
199.endif
200
201.if !target(install)
202
203.if defined(PRECIOUSPROG)
204.if !defined(NO_FSCHG)
205INSTALLFLAGS+= -fschg
206.endif
207INSTALLFLAGS+= -S
208.endif
209
210_INSTALLFLAGS:=	${INSTALLFLAGS}
211.for ie in ${INSTALLFLAGS_EDIT}
212_INSTALLFLAGS:=	${_INSTALLFLAGS${ie}}
213.endfor
214
215.if !target(realinstall) && !defined(INTERNALPROG)
216realinstall: _proginstall
217.ORDER: beforeinstall _proginstall
218_proginstall:
219.if defined(PROG)
220	${INSTALL} ${TAG_ARGS} ${STRIP} -o ${BINOWN} -g ${BINGRP} -m ${BINMODE} \
221	    ${_INSTALLFLAGS} ${PROG} ${DESTDIR}${BINDIR}/${PROGNAME}
222.if ${MK_DEBUG_FILES} != "no"
223.if defined(DEBUGMKDIR)
224	${INSTALL} ${TAG_ARGS:D${TAG_ARGS},debug} -d ${DESTDIR}${DEBUGFILEDIR}/
225.endif
226	${INSTALL} ${TAG_ARGS:D${TAG_ARGS},debug} -o ${BINOWN} -g ${BINGRP} -m ${DEBUGMODE} \
227	    ${PROGNAME}.debug ${DESTDIR}${DEBUGFILEDIR}/${PROGNAME}.debug
228.endif
229.endif
230.endif	# !target(realinstall)
231
232.if defined(SCRIPTS) && !empty(SCRIPTS)
233realinstall: _scriptsinstall
234.ORDER: beforeinstall _scriptsinstall
235
236SCRIPTSDIR?=	${BINDIR}
237SCRIPTSOWN?=	${BINOWN}
238SCRIPTSGRP?=	${BINGRP}
239SCRIPTSMODE?=	${BINMODE}
240
241STAGE_AS_SETS+= scripts
242stage_as.scripts: ${SCRIPTS}
243FLAGS.stage_as.scripts= -m ${SCRIPTSMODE}
244STAGE_FILES_DIR.scripts= ${STAGE_OBJTOP}
245.for script in ${SCRIPTS}
246.if defined(SCRIPTSNAME)
247SCRIPTSNAME_${script:T}?=	${SCRIPTSNAME}
248.else
249SCRIPTSNAME_${script:T}?=	${script:T:R}
250.endif
251SCRIPTSDIR_${script:T}?=	${SCRIPTSDIR}
252SCRIPTSOWN_${script:T}?=	${SCRIPTSOWN}
253SCRIPTSGRP_${script:T}?=	${SCRIPTSGRP}
254SCRIPTSMODE_${script:T}?=	${SCRIPTSMODE}
255STAGE_AS_${script:T}=		${SCRIPTSDIR_${script:T}}/${SCRIPTSNAME_${script:T}}
256_scriptsinstall: _SCRIPTSINS_${script:T}
257_SCRIPTSINS_${script:T}: ${script}
258	${INSTALL} ${TAG_ARGS} -o ${SCRIPTSOWN_${.ALLSRC:T}} \
259	    -g ${SCRIPTSGRP_${.ALLSRC:T}} -m ${SCRIPTSMODE_${.ALLSRC:T}} \
260	    ${.ALLSRC} \
261	    ${DESTDIR}${SCRIPTSDIR_${.ALLSRC:T}}/${SCRIPTSNAME_${.ALLSRC:T}}
262.endfor
263.endif
264
265NLSNAME?=	${PROG}
266.include <bsd.nls.mk>
267
268.include <bsd.confs.mk>
269.include <bsd.files.mk>
270.include <bsd.incs.mk>
271.include <bsd.links.mk>
272
273.if ${MK_MAN} != "no"
274realinstall: maninstall
275.ORDER: beforeinstall maninstall
276.endif
277
278.endif	# !target(install)
279
280.if !target(lint)
281lint: ${SRCS:M*.c}
282.if defined(PROG)
283	${LINT} ${LINTFLAGS} ${CFLAGS:M-[DIU]*} ${.ALLSRC}
284.endif
285.endif
286
287.if ${MK_MAN} != "no"
288.include <bsd.man.mk>
289.endif
290
291.if defined(PROG)
292OBJS_DEPEND_GUESS+= ${SRCS:M*.h}
293.endif
294
295.include <bsd.dep.mk>
296.include <bsd.clang-analyze.mk>
297.include <bsd.obj.mk>
298.include <bsd.sys.mk>
299