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