xref: /freebsd/share/mk/bsd.prog.mk (revision 66fd12cf4896eb08ad8e7a2627537f84ead84dd3)
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
76CXXFLAGS+= -ftrivial-auto-var-init=zero
77.if ${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} < 160000
78CFLAGS+= -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang
79CXXFLAGS+= -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang
80.endif
81.else
82.warning InitAll (zeros) requested but not supported by compiler
83.endif
84.elif ${MK_INIT_ALL_PATTERN} == "yes"
85.if ${COMPILER_FEATURES:Minit-all}
86CFLAGS+= -ftrivial-auto-var-init=pattern
87CXXFLAGS+= -ftrivial-auto-var-init=pattern
88.else
89.warning InitAll (pattern) 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