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