xref: /freebsd/share/mk/bsd.prog.mk (revision 895f86f15fbf6540071feb9328c3c50ed1f027b8)
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: beforebuild .WAIT ${PROG} ${SCRIPTS}
152beforebuild: objwarn
153.if ${MK_MAN} != "no"
154all: _manpages
155.endif
156.endif
157
158.if defined(PROG)
159CLEANFILES+= ${PROG}
160.if ${MK_DEBUG_FILES} != "no"
161CLEANFILES+=	${PROG_FULL} ${PROGNAME}.debug
162.endif
163.endif
164
165.if defined(OBJS)
166CLEANFILES+= ${OBJS}
167.endif
168
169.include <bsd.libnames.mk>
170
171.if defined(PROG)
172_EXTRADEPEND:
173.if defined(LDFLAGS) && !empty(LDFLAGS:M-nostdlib)
174.if defined(DPADD) && !empty(DPADD)
175	echo ${PROG_FULL}: ${DPADD} >> ${DEPENDFILE}
176.endif
177.else
178	echo ${PROG_FULL}: ${LIBC} ${DPADD} >> ${DEPENDFILE}
179.if defined(PROG_CXX)
180.if ${COMPILER_TYPE} == "clang" && empty(CXXFLAGS:M-stdlib=libstdc++)
181	echo ${PROG_FULL}: ${LIBCPLUSPLUS} >> ${DEPENDFILE}
182.else
183	echo ${PROG_FULL}: ${LIBSTDCPLUSPLUS} >> ${DEPENDFILE}
184.endif
185.endif
186.endif
187.endif
188
189.if !target(install)
190
191.if defined(PRECIOUSPROG)
192.if !defined(NO_FSCHG)
193INSTALLFLAGS+= -fschg
194.endif
195INSTALLFLAGS+= -S
196.endif
197
198_INSTALLFLAGS:=	${INSTALLFLAGS}
199.for ie in ${INSTALLFLAGS_EDIT}
200_INSTALLFLAGS:=	${_INSTALLFLAGS${ie}}
201.endfor
202
203.if !target(realinstall) && !defined(INTERNALPROG)
204realinstall: _proginstall
205.ORDER: beforeinstall _proginstall
206_proginstall:
207.if defined(PROG)
208	${INSTALL} ${STRIP} -o ${BINOWN} -g ${BINGRP} -m ${BINMODE} \
209	    ${_INSTALLFLAGS} ${PROG} ${DESTDIR}${BINDIR}/${PROGNAME}
210.if ${MK_DEBUG_FILES} != "no"
211.if defined(DEBUGMKDIR)
212	${INSTALL} -T debug -d ${DESTDIR}${DEBUGFILEDIR}/
213.endif
214	${INSTALL} -T debug -o ${BINOWN} -g ${BINGRP} -m ${DEBUGMODE} \
215	    ${PROGNAME}.debug ${DESTDIR}${DEBUGFILEDIR}/${PROGNAME}.debug
216.endif
217.endif
218.endif	# !target(realinstall)
219
220.if defined(SCRIPTS) && !empty(SCRIPTS)
221realinstall: _scriptsinstall
222.ORDER: beforeinstall _scriptsinstall
223
224SCRIPTSDIR?=	${BINDIR}
225SCRIPTSOWN?=	${BINOWN}
226SCRIPTSGRP?=	${BINGRP}
227SCRIPTSMODE?=	${BINMODE}
228
229STAGE_AS_SETS+= scripts
230stage_as.scripts: ${SCRIPTS}
231FLAGS.stage_as.scripts= -m ${SCRIPTSMODE}
232STAGE_FILES_DIR.scripts= ${STAGE_OBJTOP}
233.for script in ${SCRIPTS}
234.if defined(SCRIPTSNAME)
235SCRIPTSNAME_${script:T}?=	${SCRIPTSNAME}
236.else
237SCRIPTSNAME_${script:T}?=	${script:T:R}
238.endif
239SCRIPTSDIR_${script:T}?=	${SCRIPTSDIR}
240SCRIPTSOWN_${script:T}?=	${SCRIPTSOWN}
241SCRIPTSGRP_${script:T}?=	${SCRIPTSGRP}
242SCRIPTSMODE_${script:T}?=	${SCRIPTSMODE}
243STAGE_AS_${script:T}=		${SCRIPTSDIR_${script:T}}/${SCRIPTSNAME_${script:T}}
244_scriptsinstall: _SCRIPTSINS_${script:T}
245_SCRIPTSINS_${script:T}: ${script}
246	${INSTALL} -o ${SCRIPTSOWN_${.ALLSRC:T}} \
247	    -g ${SCRIPTSGRP_${.ALLSRC:T}} -m ${SCRIPTSMODE_${.ALLSRC:T}} \
248	    ${.ALLSRC} \
249	    ${DESTDIR}${SCRIPTSDIR_${.ALLSRC:T}}/${SCRIPTSNAME_${.ALLSRC:T}}
250.endfor
251.endif
252
253NLSNAME?=	${PROG}
254.include <bsd.nls.mk>
255
256.include <bsd.confs.mk>
257.include <bsd.files.mk>
258.include <bsd.incs.mk>
259.include <bsd.links.mk>
260
261.if ${MK_MAN} != "no"
262realinstall: _maninstall
263.ORDER: beforeinstall _maninstall
264.endif
265
266.endif	# !target(install)
267
268.if !target(lint)
269lint: ${SRCS:M*.c}
270.if defined(PROG)
271	${LINT} ${LINTFLAGS} ${CFLAGS:M-[DIU]*} ${.ALLSRC}
272.endif
273.endif
274
275.if ${MK_MAN} != "no"
276.include <bsd.man.mk>
277.endif
278
279.include <bsd.dep.mk>
280
281.if defined(PROG) && !exists(${.OBJDIR}/${DEPENDFILE})
282${OBJS}: ${SRCS:M*.h}
283.endif
284
285.include <bsd.obj.mk>
286
287.include <bsd.sys.mk>
288