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