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