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