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