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