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