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