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.include <bsd.linker.mk>
7
8.SUFFIXES: .out .o .bc .c .cc .cpp .cxx .C .m .y .l .ll .ln .s .S .asm
9
10# XXX The use of COPTS in modern makefiles is discouraged.
11.if defined(COPTS)
12.warning ${.CURDIR}: COPTS should be CFLAGS.
13CFLAGS+=${COPTS}
14.endif
15
16.if ${MK_ASSERT_DEBUG} == "no"
17CFLAGS+= -DNDEBUG
18# XXX: shouldn't we ensure that !asserts marks potentially unused variables as
19# __unused instead of disabling -Werror globally?
20MK_WERROR=	no
21.endif
22
23.if defined(DEBUG_FLAGS)
24CFLAGS+=${DEBUG_FLAGS}
25CXXFLAGS+=${DEBUG_FLAGS}
26
27.if ${MK_CTF} != "no" && ${DEBUG_FLAGS:M-g} != ""
28CTFFLAGS+= -g
29.endif
30.endif
31
32.if defined(PROG_CXX)
33PROG=	${PROG_CXX}
34.endif
35
36.if !empty(LDFLAGS:M-Wl,*--oformat,*) || !empty(LDFLAGS:M-static)
37MK_DEBUG_FILES=	no
38.endif
39
40# ELF hardening knobs
41.if ${MK_BIND_NOW} != "no"
42LDFLAGS+= -Wl,-znow
43.endif
44.if ${MK_PIE} != "no"
45# Static PIE is not yet supported/tested.
46.if !defined(NO_SHARED) || ${NO_SHARED:tl} == "no"
47CFLAGS+= -fPIE
48CXXFLAGS+= -fPIE
49LDFLAGS+= -pie
50.endif
51.endif
52.if ${MK_RETPOLINE} != "no"
53.if ${COMPILER_FEATURES:Mretpoline} && ${LINKER_FEATURES:Mretpoline}
54CFLAGS+= -mretpoline
55CXXFLAGS+= -mretpoline
56# retpolineplt is broken with static linking (PR 233336)
57.if !defined(NO_SHARED) || ${NO_SHARED:tl} == "no"
58LDFLAGS+= -Wl,-zretpolineplt
59.endif
60.else
61.warning Retpoline requested but not supported by compiler or linker
62.endif
63.endif
64
65# Initialize stack variables on function entry
66.if ${MK_INIT_ALL_ZERO} == "yes"
67.if ${COMPILER_FEATURES:Minit-all}
68CFLAGS+= -ftrivial-auto-var-init=zero \
69    -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang
70CXXFLAGS+= -ftrivial-auto-var-init=zero \
71    -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang
72.else
73.warning InitAll (zeros) requested but not support by compiler
74.endif
75.elif ${MK_INIT_ALL_PATTERN} == "yes"
76.if ${COMPILER_FEATURES:Minit-all}
77CFLAGS+= -ftrivial-auto-var-init=pattern
78CXXFLAGS+= -ftrivial-auto-var-init=pattern
79.else
80.warning InitAll (pattern) requested but not support by compiler
81.endif
82.endif
83
84.if ${MACHINE_CPUARCH} == "riscv" && ${LINKER_FEATURES:Mriscv-relaxations} == ""
85CFLAGS += -mno-relax
86.endif
87
88.if defined(CRUNCH_CFLAGS)
89CFLAGS+=${CRUNCH_CFLAGS}
90.else
91.if ${MK_DEBUG_FILES} != "no" && empty(DEBUG_FLAGS:M-g) && \
92    empty(DEBUG_FLAGS:M-gdwarf-*)
93CFLAGS+= ${DEBUG_FILES_CFLAGS}
94CTFFLAGS+= -g
95.endif
96.endif
97
98.if !defined(DEBUG_FLAGS)
99STRIP?=	-s
100.endif
101
102.if defined(NO_ROOT)
103.if !defined(TAGS) || ! ${TAGS:Mpackage=*}
104TAGS+=		package=${PACKAGE:Uutilities}
105.endif
106TAG_ARGS=	-T ${TAGS:[*]:S/ /,/g}
107.endif
108
109.if defined(NO_SHARED) && ${NO_SHARED:tl} != "no"
110LDFLAGS+= -static
111.endif
112
113# clang currently defaults to dynamic TLS for mips64 binaries
114.if ${MACHINE_ARCH:Mmips64*} && ${COMPILER_TYPE} == "clang"
115CFLAGS+= -ftls-model=initial-exec
116.endif
117
118.if ${MK_DEBUG_FILES} != "no"
119PROG_FULL=${PROG}.full
120# Use ${DEBUGDIR} for base system debug files, else .debug subdirectory
121.if defined(BINDIR) && (\
122    ${BINDIR} == "/bin" ||\
123    ${BINDIR:C%/libexec(/.*)?%/libexec%} == "/libexec" ||\
124    ${BINDIR} == "/sbin" ||\
125    ${BINDIR:C%/usr/(bin|bsdinstall|libexec|lpr|sendmail|sm.bin|sbin|tests)(/.*)?%/usr/bin%} == "/usr/bin" ||\
126    ${BINDIR} == "/usr/lib" \
127     )
128DEBUGFILEDIR=	${DEBUGDIR}${BINDIR}
129.else
130DEBUGFILEDIR?=	${BINDIR}/.debug
131.endif
132.if !exists(${DESTDIR}${DEBUGFILEDIR})
133DEBUGMKDIR=
134.endif
135.else
136PROG_FULL=	${PROG}
137.endif
138
139.if defined(PROG)
140PROGNAME?=	${PROG}
141
142.if defined(SRCS)
143
144OBJS+=  ${SRCS:N*.h:${OBJS_SRCS_FILTER:ts:}:S/$/.o/g}
145
146# LLVM bitcode / textual IR representations of the program
147BCOBJS+=${SRCS:N*.[hsS]:N*.asm:${OBJS_SRCS_FILTER:ts:}:S/$/.bco/g}
148LLOBJS+=${SRCS:N*.[hsS]:N*.asm:${OBJS_SRCS_FILTER:ts:}:S/$/.llo/g}
149
150.if target(beforelinking)
151beforelinking: ${OBJS}
152${PROG_FULL}: beforelinking
153.endif
154${PROG_FULL}: ${OBJS}
155.if defined(PROG_CXX)
156	${CXX:N${CCACHE_BIN}} ${CXXFLAGS:N-M*} ${LDFLAGS} -o ${.TARGET} \
157	    ${OBJS} ${LDADD}
158.else
159	${CC:N${CCACHE_BIN}} ${CFLAGS:N-M*} ${LDFLAGS} -o ${.TARGET} ${OBJS} \
160	    ${LDADD}
161.endif
162.if ${MK_CTF} != "no"
163	${CTFMERGE} ${CTFFLAGS} -o ${.TARGET} ${OBJS}
164.endif
165
166.else	# !defined(SRCS)
167
168.if !target(${PROG})
169.if defined(PROG_CXX)
170SRCS=	${PROG}.cc
171.else
172SRCS=	${PROG}.c
173.endif
174
175# Always make an intermediate object file because:
176# - it saves time rebuilding when only the library has changed
177# - the name of the object gets put into the executable symbol table instead of
178#   the name of a variable temporary object.
179# - it's useful to keep objects around for crunching.
180OBJS+=		${PROG}.o
181BCOBJS+=	${PROG}.bc
182LLOBJS+=	${PROG}.ll
183CLEANFILES+=	${PROG}.o ${PROG}.bc ${PROG}.ll
184
185.if target(beforelinking)
186beforelinking: ${OBJS}
187${PROG_FULL}: beforelinking
188.endif
189${PROG_FULL}: ${OBJS}
190.if defined(PROG_CXX)
191	${CXX:N${CCACHE_BIN}} ${CXXFLAGS:N-M*} ${LDFLAGS} -o ${.TARGET} \
192	    ${OBJS} ${LDADD}
193.else
194	${CC:N${CCACHE_BIN}} ${CFLAGS:N-M*} ${LDFLAGS} -o ${.TARGET} ${OBJS} \
195	    ${LDADD}
196.endif
197.if ${MK_CTF} != "no"
198	${CTFMERGE} ${CTFFLAGS} -o ${.TARGET} ${OBJS}
199.endif
200.endif # !target(${PROG})
201
202.endif # !defined(SRCS)
203
204.if ${MK_DEBUG_FILES} != "no"
205${PROG}: ${PROG_FULL} ${PROGNAME}.debug
206	${OBJCOPY} --strip-debug --add-gnu-debuglink=${PROGNAME}.debug \
207	    ${PROG_FULL} ${.TARGET}
208
209${PROGNAME}.debug: ${PROG_FULL}
210	${OBJCOPY} --only-keep-debug ${PROG_FULL} ${.TARGET}
211.endif
212
213.if defined(LLVM_LINK)
214${PROG_FULL}.bc: ${BCOBJS}
215	${LLVM_LINK} -o ${.TARGET} ${BCOBJS}
216
217${PROG_FULL}.ll: ${LLOBJS}
218	${LLVM_LINK} -S -o ${.TARGET} ${LLOBJS}
219
220CLEANFILES+=	${PROG_FULL}.bc ${PROG_FULL}.ll
221.endif # defined(LLVM_LINK)
222
223.if	${MK_MAN} != "no" && !defined(MAN) && \
224	!defined(MAN1) && !defined(MAN2) && !defined(MAN3) && \
225	!defined(MAN4) && !defined(MAN5) && !defined(MAN6) && \
226	!defined(MAN7) && !defined(MAN8) && !defined(MAN9)
227MAN=	${PROG}.1
228MAN1=	${MAN}
229.endif
230.endif # defined(PROG)
231
232.if defined(_SKIP_BUILD)
233all:
234.else
235all: ${PROG} ${SCRIPTS}
236.if ${MK_MAN} != "no"
237all: all-man
238.endif
239.endif
240
241.if defined(PROG)
242CLEANFILES+= ${PROG} ${PROG}.bc ${PROG}.ll
243.if ${MK_DEBUG_FILES} != "no"
244CLEANFILES+= ${PROG_FULL} ${PROGNAME}.debug
245.endif
246.endif
247
248.if defined(OBJS)
249CLEANFILES+= ${OBJS} ${BCOBJS} ${LLOBJS}
250.endif
251
252.include <bsd.libnames.mk>
253
254.if defined(PROG)
255.if !defined(NO_EXTRADEPEND)
256_EXTRADEPEND:
257.if defined(LDFLAGS) && !empty(LDFLAGS:M-nostdlib)
258.if defined(DPADD) && !empty(DPADD)
259	echo ${PROG_FULL}: ${DPADD} >> ${DEPENDFILE}
260.endif
261.else
262	echo ${PROG_FULL}: ${LIBC} ${DPADD} >> ${DEPENDFILE}
263.if defined(PROG_CXX)
264.if ${COMPILER_TYPE} == "clang" && empty(CXXFLAGS:M-stdlib=libstdc++)
265	echo ${PROG_FULL}: ${LIBCPLUSPLUS} >> ${DEPENDFILE}
266.else
267	echo ${PROG_FULL}: ${LIBSTDCPLUSPLUS} >> ${DEPENDFILE}
268.endif
269.endif
270.endif
271.endif	# !defined(NO_EXTRADEPEND)
272.endif
273
274.if !target(install)
275
276.if defined(PRECIOUSPROG)
277.if !defined(NO_FSCHG)
278INSTALLFLAGS+= -fschg
279.endif
280INSTALLFLAGS+= -S
281.endif
282
283_INSTALLFLAGS:=	${INSTALLFLAGS}
284.for ie in ${INSTALLFLAGS_EDIT}
285_INSTALLFLAGS:=	${_INSTALLFLAGS${ie}}
286.endfor
287
288.if !target(realinstall) && !defined(INTERNALPROG)
289realinstall: _proginstall
290.ORDER: beforeinstall _proginstall
291_proginstall:
292.if defined(PROG)
293	${INSTALL} ${TAG_ARGS} ${STRIP} -o ${BINOWN} -g ${BINGRP} -m ${BINMODE} \
294	    ${_INSTALLFLAGS} ${PROG} ${DESTDIR}${BINDIR}/${PROGNAME}
295.if ${MK_DEBUG_FILES} != "no"
296.if defined(DEBUGMKDIR)
297	${INSTALL} ${TAG_ARGS:D${TAG_ARGS},dbg} -d ${DESTDIR}${DEBUGFILEDIR}/
298.endif
299	${INSTALL} ${TAG_ARGS:D${TAG_ARGS},dbg} -o ${BINOWN} -g ${BINGRP} -m ${DEBUGMODE} \
300	    ${PROGNAME}.debug ${DESTDIR}${DEBUGFILEDIR}/${PROGNAME}.debug
301.endif
302.endif
303.endif	# !target(realinstall)
304
305.if defined(SCRIPTS) && !empty(SCRIPTS)
306realinstall: _scriptsinstall
307.ORDER: beforeinstall _scriptsinstall
308
309SCRIPTSDIR?=	${BINDIR}
310SCRIPTSOWN?=	${BINOWN}
311SCRIPTSGRP?=	${BINGRP}
312SCRIPTSMODE?=	${BINMODE}
313
314STAGE_AS_SETS+= scripts
315stage_as.scripts: ${SCRIPTS}
316FLAGS.stage_as.scripts= -m ${SCRIPTSMODE}
317STAGE_FILES_DIR.scripts= ${STAGE_OBJTOP}
318.for script in ${SCRIPTS}
319.if defined(SCRIPTSNAME)
320SCRIPTSNAME_${script:T}?=	${SCRIPTSNAME}
321.else
322SCRIPTSNAME_${script:T}?=	${script:T:R}
323.endif
324SCRIPTSDIR_${script:T}?=	${SCRIPTSDIR}
325SCRIPTSOWN_${script:T}?=	${SCRIPTSOWN}
326SCRIPTSGRP_${script:T}?=	${SCRIPTSGRP}
327SCRIPTSMODE_${script:T}?=	${SCRIPTSMODE}
328STAGE_AS_${script:T}=		${SCRIPTSDIR_${script:T}}/${SCRIPTSNAME_${script:T}}
329_scriptsinstall: _SCRIPTSINS_${script:T}
330_SCRIPTSINS_${script:T}: ${script}
331	${INSTALL} ${TAG_ARGS} -o ${SCRIPTSOWN_${.ALLSRC:T}} \
332	    -g ${SCRIPTSGRP_${.ALLSRC:T}} -m ${SCRIPTSMODE_${.ALLSRC:T}} \
333	    ${.ALLSRC} \
334	    ${DESTDIR}${SCRIPTSDIR_${.ALLSRC:T}}/${SCRIPTSNAME_${.ALLSRC:T}}
335.endfor
336.endif
337
338NLSNAME?=	${PROG}
339.include <bsd.nls.mk>
340
341.include <bsd.confs.mk>
342.include <bsd.files.mk>
343.include <bsd.incs.mk>
344
345LINKOWN?=	${BINOWN}
346LINKGRP?=	${BINGRP}
347LINKMODE?=	${BINMODE}
348.include <bsd.links.mk>
349
350.if ${MK_MAN} != "no"
351realinstall: maninstall
352.ORDER: beforeinstall maninstall
353.endif
354
355.endif	# !target(install)
356
357.if ${MK_MAN} != "no"
358.include <bsd.man.mk>
359.endif
360
361.if defined(HAS_TESTS)
362MAKE+=			MK_MAKE_CHECK_USE_SANDBOX=yes
363SUBDIR_TARGETS+=	check
364TESTS_LD_LIBRARY_PATH+=	${.OBJDIR}
365TESTS_PATH+=		${.OBJDIR}
366.endif
367
368.if defined(PROG)
369OBJS_DEPEND_GUESS+= ${SRCS:M*.h}
370.endif
371
372.include <bsd.dep.mk>
373.include <bsd.clang-analyze.mk>
374.include <bsd.obj.mk>
375.include <bsd.sys.mk>
376