xref: /freebsd/share/mk/bsd.prog.mk (revision 1409e715e64d67b3093a889e63b33ef48e280ec8)
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 .bc .c .cc .cpp .cxx .C .m .y .l .ll .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 defined(LLVM_LINK)
151# LLVM bitcode / textual IR representations of the program
152BCOBJS=	${OBJS:.o=.bco}
153LLOBJS=	${OBJS:.o=.llo}
154
155${PROG_FULL}.bc: ${BCOBJS}
156	${LLVM_LINK} -o ${.TARGET} ${BCOBJS}
157
158${PROG_FULL}.ll: ${LLOBJS}
159	${LLVM_LINK} -S -o ${.TARGET} ${LLOBJS}
160
161.endif # defined(LLVM_LINK)
162
163.if	${MK_MAN} != "no" && !defined(MAN) && \
164	!defined(MAN1) && !defined(MAN2) && !defined(MAN3) && \
165	!defined(MAN4) && !defined(MAN5) && !defined(MAN6) && \
166	!defined(MAN7) && !defined(MAN8) && !defined(MAN9)
167MAN=	${PROG}.1
168MAN1=	${MAN}
169.endif
170.endif # defined(PROG)
171
172.if defined(_SKIP_BUILD)
173all:
174.else
175all: ${PROG} ${SCRIPTS}
176.if ${MK_MAN} != "no"
177all: all-man
178.endif
179.endif
180
181.if defined(PROG)
182CLEANFILES+= ${PROG} ${PROG}.bc ${PROG}.ll
183.if ${MK_DEBUG_FILES} != "no"
184CLEANFILES+= ${PROG_FULL} ${PROG_FULL}.bc ${PROGNAME}.debug ${PROG_FULL}.ll
185.endif
186.endif
187
188.if defined(OBJS)
189CLEANFILES+= ${OBJS} ${BCOBJS} ${LLOBJS}
190.endif
191
192.include <bsd.libnames.mk>
193
194.if defined(PROG)
195.if !defined(NO_EXTRADEPEND)
196_EXTRADEPEND:
197.if defined(LDFLAGS) && !empty(LDFLAGS:M-nostdlib)
198.if defined(DPADD) && !empty(DPADD)
199	echo ${PROG_FULL}: ${DPADD} >> ${DEPENDFILE}
200.endif
201.else
202	echo ${PROG_FULL}: ${LIBC} ${DPADD} >> ${DEPENDFILE}
203.if defined(PROG_CXX)
204.if ${COMPILER_TYPE} == "clang" && empty(CXXFLAGS:M-stdlib=libstdc++)
205	echo ${PROG_FULL}: ${LIBCPLUSPLUS} >> ${DEPENDFILE}
206.else
207	echo ${PROG_FULL}: ${LIBSTDCPLUSPLUS} >> ${DEPENDFILE}
208.endif
209.endif
210.endif
211.endif	# !defined(NO_EXTRADEPEND)
212.endif
213
214.if !target(install)
215
216.if defined(PRECIOUSPROG)
217.if !defined(NO_FSCHG)
218INSTALLFLAGS+= -fschg
219.endif
220INSTALLFLAGS+= -S
221.endif
222
223_INSTALLFLAGS:=	${INSTALLFLAGS}
224.for ie in ${INSTALLFLAGS_EDIT}
225_INSTALLFLAGS:=	${_INSTALLFLAGS${ie}}
226.endfor
227
228.if !target(realinstall) && !defined(INTERNALPROG)
229realinstall: _proginstall
230.ORDER: beforeinstall _proginstall
231_proginstall:
232.if defined(PROG)
233	${INSTALL} ${TAG_ARGS} ${STRIP} -o ${BINOWN} -g ${BINGRP} -m ${BINMODE} \
234	    ${_INSTALLFLAGS} ${PROG} ${DESTDIR}${BINDIR}/${PROGNAME}
235.if ${MK_DEBUG_FILES} != "no"
236.if defined(DEBUGMKDIR)
237	${INSTALL} ${TAG_ARGS:D${TAG_ARGS},debug} -d ${DESTDIR}${DEBUGFILEDIR}/
238.endif
239	${INSTALL} ${TAG_ARGS:D${TAG_ARGS},debug} -o ${BINOWN} -g ${BINGRP} -m ${DEBUGMODE} \
240	    ${PROGNAME}.debug ${DESTDIR}${DEBUGFILEDIR}/${PROGNAME}.debug
241.endif
242.endif
243.endif	# !target(realinstall)
244
245.if defined(SCRIPTS) && !empty(SCRIPTS)
246realinstall: _scriptsinstall
247.ORDER: beforeinstall _scriptsinstall
248
249SCRIPTSDIR?=	${BINDIR}
250SCRIPTSOWN?=	${BINOWN}
251SCRIPTSGRP?=	${BINGRP}
252SCRIPTSMODE?=	${BINMODE}
253
254STAGE_AS_SETS+= scripts
255stage_as.scripts: ${SCRIPTS}
256FLAGS.stage_as.scripts= -m ${SCRIPTSMODE}
257STAGE_FILES_DIR.scripts= ${STAGE_OBJTOP}
258.for script in ${SCRIPTS}
259.if defined(SCRIPTSNAME)
260SCRIPTSNAME_${script:T}?=	${SCRIPTSNAME}
261.else
262SCRIPTSNAME_${script:T}?=	${script:T:R}
263.endif
264SCRIPTSDIR_${script:T}?=	${SCRIPTSDIR}
265SCRIPTSOWN_${script:T}?=	${SCRIPTSOWN}
266SCRIPTSGRP_${script:T}?=	${SCRIPTSGRP}
267SCRIPTSMODE_${script:T}?=	${SCRIPTSMODE}
268STAGE_AS_${script:T}=		${SCRIPTSDIR_${script:T}}/${SCRIPTSNAME_${script:T}}
269_scriptsinstall: _SCRIPTSINS_${script:T}
270_SCRIPTSINS_${script:T}: ${script}
271	${INSTALL} ${TAG_ARGS} -o ${SCRIPTSOWN_${.ALLSRC:T}} \
272	    -g ${SCRIPTSGRP_${.ALLSRC:T}} -m ${SCRIPTSMODE_${.ALLSRC:T}} \
273	    ${.ALLSRC} \
274	    ${DESTDIR}${SCRIPTSDIR_${.ALLSRC:T}}/${SCRIPTSNAME_${.ALLSRC:T}}
275.endfor
276.endif
277
278NLSNAME?=	${PROG}
279.include <bsd.nls.mk>
280
281.include <bsd.confs.mk>
282.include <bsd.files.mk>
283.include <bsd.incs.mk>
284.include <bsd.links.mk>
285
286.if ${MK_MAN} != "no"
287realinstall: maninstall
288.ORDER: beforeinstall maninstall
289.endif
290
291.endif	# !target(install)
292
293.if !target(lint)
294lint: ${SRCS:M*.c}
295.if defined(PROG)
296	${LINT} ${LINTFLAGS} ${CFLAGS:M-[DIU]*} ${.ALLSRC}
297.endif
298.endif
299
300.if ${MK_MAN} != "no"
301.include <bsd.man.mk>
302.endif
303
304.if defined(HAS_TESTS)
305MAKE+=			MK_MAKE_CHECK_USE_SANDBOX=yes
306SUBDIR_TARGETS+=	check
307TESTS_LD_LIBRARY_PATH+=	${.OBJDIR}
308TESTS_PATH+=		${.OBJDIR}
309.endif
310
311.if defined(PROG)
312OBJS_DEPEND_GUESS+= ${SRCS:M*.h}
313.endif
314
315.include <bsd.dep.mk>
316.include <bsd.clang-analyze.mk>
317.include <bsd.obj.mk>
318.include <bsd.sys.mk>
319