xref: /freebsd/share/mk/bsd.prog.mk (revision 752d135e0dacd9a463d24ffb89779b67ce0a7ea0)
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# ELF hardening knobs
38.if ${MK_BIND_NOW} != "no"
39LDFLAGS+= -Wl,-znow
40.endif
41.if ${MK_RETPOLINE} != "no"
42CFLAGS+= -mretpoline
43CXXFLAGS+= -mretpoline
44LDFLAGS+= -Wl,-zretpolineplt
45.endif
46
47.if defined(CRUNCH_CFLAGS)
48CFLAGS+=${CRUNCH_CFLAGS}
49.else
50.if ${MK_DEBUG_FILES} != "no" && empty(DEBUG_FLAGS:M-g) && \
51    empty(DEBUG_FLAGS:M-gdwarf-*)
52CFLAGS+= ${DEBUG_FILES_CFLAGS}
53CTFFLAGS+= -g
54.endif
55.endif
56
57.if !defined(DEBUG_FLAGS)
58STRIP?=	-s
59.endif
60
61.if defined(NO_ROOT)
62.if !defined(TAGS) || ! ${TAGS:Mpackage=*}
63TAGS+=		package=${PACKAGE:Uruntime}
64.endif
65TAG_ARGS=	-T ${TAGS:[*]:S/ /,/g}
66.endif
67
68.if defined(NO_SHARED) && (${NO_SHARED} != "no" && ${NO_SHARED} != "NO")
69LDFLAGS+= -static
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:C%/libexec(/.*)?%/libexec%} == "/libexec" ||\
78    ${BINDIR} == "/sbin" ||\
79    ${BINDIR:C%/usr/(bin|bsdinstall|libexec|lpr|sendmail|sm.bin|sbin|tests)(/.*)?%/usr/bin%} == "/usr/bin" ||\
80    ${BINDIR} == "/usr/lib" \
81     )
82DEBUGFILEDIR=	${DEBUGDIR}${BINDIR}
83.else
84DEBUGFILEDIR?=	${BINDIR}/.debug
85.endif
86.if !exists(${DESTDIR}${DEBUGFILEDIR})
87DEBUGMKDIR=
88.endif
89.else
90PROG_FULL=	${PROG}
91.endif
92
93.if defined(PROG)
94PROGNAME?=	${PROG}
95
96.if defined(SRCS)
97
98OBJS+=  ${SRCS:N*.h:${OBJS_SRCS_FILTER:ts:}:S/$/.o/g}
99
100# LLVM bitcode / textual IR representations of the program
101BCOBJS+=${SRCS:N*.[hsS]:N*.asm:${OBJS_SRCS_FILTER:ts:}:S/$/.bco/g}
102LLOBJS+=${SRCS:N*.[hsS]:N*.asm:${OBJS_SRCS_FILTER:ts:}:S/$/.llo/g}
103
104.if target(beforelinking)
105beforelinking: ${OBJS}
106${PROG_FULL}: beforelinking
107.endif
108${PROG_FULL}: ${OBJS}
109.if defined(PROG_CXX)
110	${CXX:N${CCACHE_BIN}} ${CXXFLAGS:N-M*} ${LDFLAGS} -o ${.TARGET} \
111	    ${OBJS} ${LDADD}
112.else
113	${CC:N${CCACHE_BIN}} ${CFLAGS:N-M*} ${LDFLAGS} -o ${.TARGET} ${OBJS} \
114	    ${LDADD}
115.endif
116.if ${MK_CTF} != "no"
117	${CTFMERGE} ${CTFFLAGS} -o ${.TARGET} ${OBJS}
118.endif
119
120.else	# !defined(SRCS)
121
122.if !target(${PROG})
123.if defined(PROG_CXX)
124SRCS=	${PROG}.cc
125.else
126SRCS=	${PROG}.c
127.endif
128
129# Always make an intermediate object file because:
130# - it saves time rebuilding when only the library has changed
131# - the name of the object gets put into the executable symbol table instead of
132#   the name of a variable temporary object.
133# - it's useful to keep objects around for crunching.
134OBJS+=		${PROG}.o
135BCOBJS+=	${PROG}.bc
136LLOBJS+=	${PROG}.ll
137CLEANFILES+=	${PROG}.o ${PROG}.bc ${PROG}.ll
138
139.if target(beforelinking)
140beforelinking: ${OBJS}
141${PROG_FULL}: beforelinking
142.endif
143${PROG_FULL}: ${OBJS}
144.if defined(PROG_CXX)
145	${CXX:N${CCACHE_BIN}} ${CXXFLAGS:N-M*} ${LDFLAGS} -o ${.TARGET} \
146	    ${OBJS} ${LDADD}
147.else
148	${CC:N${CCACHE_BIN}} ${CFLAGS:N-M*} ${LDFLAGS} -o ${.TARGET} ${OBJS} \
149	    ${LDADD}
150.endif
151.if ${MK_CTF} != "no"
152	${CTFMERGE} ${CTFFLAGS} -o ${.TARGET} ${OBJS}
153.endif
154.endif # !target(${PROG})
155
156.endif # !defined(SRCS)
157
158.if ${MK_DEBUG_FILES} != "no"
159${PROG}: ${PROG_FULL} ${PROGNAME}.debug
160	${OBJCOPY} --strip-debug --add-gnu-debuglink=${PROGNAME}.debug \
161	    ${PROG_FULL} ${.TARGET}
162
163${PROGNAME}.debug: ${PROG_FULL}
164	${OBJCOPY} --only-keep-debug ${PROG_FULL} ${.TARGET}
165.endif
166
167.if defined(LLVM_LINK)
168${PROG_FULL}.bc: ${BCOBJS}
169	${LLVM_LINK} -o ${.TARGET} ${BCOBJS}
170
171${PROG_FULL}.ll: ${LLOBJS}
172	${LLVM_LINK} -S -o ${.TARGET} ${LLOBJS}
173
174CLEANFILES+=	${PROG_FULL}.bc ${PROG_FULL}.ll
175.endif # defined(LLVM_LINK)
176
177.if	${MK_MAN} != "no" && !defined(MAN) && \
178	!defined(MAN1) && !defined(MAN2) && !defined(MAN3) && \
179	!defined(MAN4) && !defined(MAN5) && !defined(MAN6) && \
180	!defined(MAN7) && !defined(MAN8) && !defined(MAN9)
181MAN=	${PROG}.1
182MAN1=	${MAN}
183.endif
184.endif # defined(PROG)
185
186.if defined(_SKIP_BUILD)
187all:
188.else
189all: ${PROG} ${SCRIPTS}
190.if ${MK_MAN} != "no"
191all: all-man
192.endif
193.endif
194
195.if defined(PROG)
196CLEANFILES+= ${PROG} ${PROG}.bc ${PROG}.ll
197.if ${MK_DEBUG_FILES} != "no"
198CLEANFILES+= ${PROG_FULL} ${PROGNAME}.debug
199.endif
200.endif
201
202.if defined(OBJS)
203CLEANFILES+= ${OBJS} ${BCOBJS} ${LLOBJS}
204.endif
205
206.include <bsd.libnames.mk>
207
208.if defined(PROG)
209.if !defined(NO_EXTRADEPEND)
210_EXTRADEPEND:
211.if defined(LDFLAGS) && !empty(LDFLAGS:M-nostdlib)
212.if defined(DPADD) && !empty(DPADD)
213	echo ${PROG_FULL}: ${DPADD} >> ${DEPENDFILE}
214.endif
215.else
216	echo ${PROG_FULL}: ${LIBC} ${DPADD} >> ${DEPENDFILE}
217.if defined(PROG_CXX)
218.if ${COMPILER_TYPE} == "clang" && empty(CXXFLAGS:M-stdlib=libstdc++)
219	echo ${PROG_FULL}: ${LIBCPLUSPLUS} >> ${DEPENDFILE}
220.else
221	echo ${PROG_FULL}: ${LIBSTDCPLUSPLUS} >> ${DEPENDFILE}
222.endif
223.endif
224.endif
225.endif	# !defined(NO_EXTRADEPEND)
226.endif
227
228.if !target(install)
229
230.if defined(PRECIOUSPROG)
231.if !defined(NO_FSCHG)
232INSTALLFLAGS+= -fschg
233.endif
234INSTALLFLAGS+= -S
235.endif
236
237_INSTALLFLAGS:=	${INSTALLFLAGS}
238.for ie in ${INSTALLFLAGS_EDIT}
239_INSTALLFLAGS:=	${_INSTALLFLAGS${ie}}
240.endfor
241
242.if !target(realinstall) && !defined(INTERNALPROG)
243realinstall: _proginstall
244.ORDER: beforeinstall _proginstall
245_proginstall:
246.if defined(PROG)
247	${INSTALL} ${TAG_ARGS} ${STRIP} -o ${BINOWN} -g ${BINGRP} -m ${BINMODE} \
248	    ${_INSTALLFLAGS} ${PROG} ${DESTDIR}${BINDIR}/${PROGNAME}
249.if ${MK_DEBUG_FILES} != "no"
250.if defined(DEBUGMKDIR)
251	${INSTALL} ${TAG_ARGS:D${TAG_ARGS},debug} -d ${DESTDIR}${DEBUGFILEDIR}/
252.endif
253	${INSTALL} ${TAG_ARGS:D${TAG_ARGS},debug} -o ${BINOWN} -g ${BINGRP} -m ${DEBUGMODE} \
254	    ${PROGNAME}.debug ${DESTDIR}${DEBUGFILEDIR}/${PROGNAME}.debug
255.endif
256.endif
257.endif	# !target(realinstall)
258
259.if defined(SCRIPTS) && !empty(SCRIPTS)
260realinstall: _scriptsinstall
261.ORDER: beforeinstall _scriptsinstall
262
263SCRIPTSDIR?=	${BINDIR}
264SCRIPTSOWN?=	${BINOWN}
265SCRIPTSGRP?=	${BINGRP}
266SCRIPTSMODE?=	${BINMODE}
267
268STAGE_AS_SETS+= scripts
269stage_as.scripts: ${SCRIPTS}
270FLAGS.stage_as.scripts= -m ${SCRIPTSMODE}
271STAGE_FILES_DIR.scripts= ${STAGE_OBJTOP}
272.for script in ${SCRIPTS}
273.if defined(SCRIPTSNAME)
274SCRIPTSNAME_${script:T}?=	${SCRIPTSNAME}
275.else
276SCRIPTSNAME_${script:T}?=	${script:T:R}
277.endif
278SCRIPTSDIR_${script:T}?=	${SCRIPTSDIR}
279SCRIPTSOWN_${script:T}?=	${SCRIPTSOWN}
280SCRIPTSGRP_${script:T}?=	${SCRIPTSGRP}
281SCRIPTSMODE_${script:T}?=	${SCRIPTSMODE}
282STAGE_AS_${script:T}=		${SCRIPTSDIR_${script:T}}/${SCRIPTSNAME_${script:T}}
283_scriptsinstall: _SCRIPTSINS_${script:T}
284_SCRIPTSINS_${script:T}: ${script}
285	${INSTALL} ${TAG_ARGS} -o ${SCRIPTSOWN_${.ALLSRC:T}} \
286	    -g ${SCRIPTSGRP_${.ALLSRC:T}} -m ${SCRIPTSMODE_${.ALLSRC:T}} \
287	    ${.ALLSRC} \
288	    ${DESTDIR}${SCRIPTSDIR_${.ALLSRC:T}}/${SCRIPTSNAME_${.ALLSRC:T}}
289.endfor
290.endif
291
292NLSNAME?=	${PROG}
293.include <bsd.nls.mk>
294
295.include <bsd.confs.mk>
296.include <bsd.files.mk>
297.include <bsd.incs.mk>
298
299LINKOWN?=	${BINOWN}
300LINKGRP?=	${BINGRP}
301LINKMODE?=	${BINMODE}
302.include <bsd.links.mk>
303
304.if ${MK_MAN} != "no"
305realinstall: maninstall
306.ORDER: beforeinstall maninstall
307.endif
308
309.endif	# !target(install)
310
311.if ${MK_MAN} != "no"
312.include <bsd.man.mk>
313.endif
314
315.if defined(HAS_TESTS)
316MAKE+=			MK_MAKE_CHECK_USE_SANDBOX=yes
317SUBDIR_TARGETS+=	check
318TESTS_LD_LIBRARY_PATH+=	${.OBJDIR}
319TESTS_PATH+=		${.OBJDIR}
320.endif
321
322.if defined(PROG)
323OBJS_DEPEND_GUESS+= ${SRCS:M*.h}
324.endif
325
326.include <bsd.dep.mk>
327.include <bsd.clang-analyze.mk>
328.include <bsd.obj.mk>
329.include <bsd.sys.mk>
330