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