xref: /freebsd/share/mk/bsd.prog.mk (revision 6966ac055c3b7a39266fb982493330df7a097997)
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
18NO_WERROR=
19.endif
20
21.if defined(DEBUG_FLAGS)
22CFLAGS+=${DEBUG_FLAGS}
23CXXFLAGS+=${DEBUG_FLAGS}
24
25.if ${MK_CTF} != "no" && ${DEBUG_FLAGS:M-g} != ""
26CTFFLAGS+= -g
27.endif
28.endif
29
30.if defined(PROG_CXX)
31PROG=	${PROG_CXX}
32.endif
33
34.if !empty(LDFLAGS:M-Wl,*--oformat,*) || !empty(LDFLAGS:M-static)
35MK_DEBUG_FILES=	no
36.endif
37
38# ELF hardening knobs
39.if ${MK_BIND_NOW} != "no"
40LDFLAGS+= -Wl,-znow
41.endif
42.if ${MK_PIE} != "no" && (!defined(NO_SHARED) || ${NO_SHARED:tl} == "no")
43CFLAGS+= -fPIE
44CXXFLAGS+= -fPIE
45LDFLAGS+= -pie
46.endif
47.if ${MK_RETPOLINE} != "no"
48.if ${COMPILER_FEATURES:Mretpoline} && ${LINKER_FEATURES:Mretpoline}
49CFLAGS+= -mretpoline
50CXXFLAGS+= -mretpoline
51# retpolineplt is broken with static linking (PR 233336)
52.if !defined(NO_SHARED) || ${NO_SHARED:tl} == "no"
53LDFLAGS+= -Wl,-zretpolineplt
54.endif
55.else
56.warning Retpoline requested but not supported by compiler or linker
57.endif
58.endif
59
60.if ${MACHINE_CPUARCH} == "riscv" && ${LINKER_FEATURES:Mriscv-relaxations} == ""
61CFLAGS += -mno-relax
62.endif
63
64.if defined(CRUNCH_CFLAGS)
65CFLAGS+=${CRUNCH_CFLAGS}
66.else
67.if ${MK_DEBUG_FILES} != "no" && empty(DEBUG_FLAGS:M-g) && \
68    empty(DEBUG_FLAGS:M-gdwarf-*)
69CFLAGS+= ${DEBUG_FILES_CFLAGS}
70CTFFLAGS+= -g
71.endif
72.endif
73
74.if !defined(DEBUG_FLAGS)
75STRIP?=	-s
76.endif
77
78.if defined(NO_ROOT)
79.if !defined(TAGS) || ! ${TAGS:Mpackage=*}
80TAGS+=		package=${PACKAGE:Uutilities}
81.endif
82TAG_ARGS=	-T ${TAGS:[*]:S/ /,/g}
83.endif
84
85.if defined(NO_SHARED) && ${NO_SHARED:tl} != "no"
86LDFLAGS+= -static
87.endif
88
89# clang currently defaults to dynamic TLS for mips64 binaries
90.if ${MACHINE_ARCH:Mmips64*} && ${COMPILER_TYPE} == "clang"
91CFLAGS+= -ftls-model=initial-exec
92.endif
93
94.if ${MK_DEBUG_FILES} != "no"
95PROG_FULL=${PROG}.full
96# Use ${DEBUGDIR} for base system debug files, else .debug subdirectory
97.if defined(BINDIR) && (\
98    ${BINDIR} == "/bin" ||\
99    ${BINDIR:C%/libexec(/.*)?%/libexec%} == "/libexec" ||\
100    ${BINDIR} == "/sbin" ||\
101    ${BINDIR:C%/usr/(bin|bsdinstall|libexec|lpr|sendmail|sm.bin|sbin|tests)(/.*)?%/usr/bin%} == "/usr/bin" ||\
102    ${BINDIR} == "/usr/lib" \
103     )
104DEBUGFILEDIR=	${DEBUGDIR}${BINDIR}
105.else
106DEBUGFILEDIR?=	${BINDIR}/.debug
107.endif
108.if !exists(${DESTDIR}${DEBUGFILEDIR})
109DEBUGMKDIR=
110.endif
111.else
112PROG_FULL=	${PROG}
113.endif
114
115.if defined(PROG)
116PROGNAME?=	${PROG}
117
118.if defined(SRCS)
119
120OBJS+=  ${SRCS:N*.h:${OBJS_SRCS_FILTER:ts:}:S/$/.o/g}
121
122# LLVM bitcode / textual IR representations of the program
123BCOBJS+=${SRCS:N*.[hsS]:N*.asm:${OBJS_SRCS_FILTER:ts:}:S/$/.bco/g}
124LLOBJS+=${SRCS:N*.[hsS]:N*.asm:${OBJS_SRCS_FILTER:ts:}:S/$/.llo/g}
125
126.if target(beforelinking)
127beforelinking: ${OBJS}
128${PROG_FULL}: beforelinking
129.endif
130${PROG_FULL}: ${OBJS}
131.if defined(PROG_CXX)
132	${CXX:N${CCACHE_BIN}} ${CXXFLAGS:N-M*} ${LDFLAGS} -o ${.TARGET} \
133	    ${OBJS} ${LDADD}
134.else
135	${CC:N${CCACHE_BIN}} ${CFLAGS:N-M*} ${LDFLAGS} -o ${.TARGET} ${OBJS} \
136	    ${LDADD}
137.endif
138.if ${MK_CTF} != "no"
139	${CTFMERGE} ${CTFFLAGS} -o ${.TARGET} ${OBJS}
140.endif
141
142.else	# !defined(SRCS)
143
144.if !target(${PROG})
145.if defined(PROG_CXX)
146SRCS=	${PROG}.cc
147.else
148SRCS=	${PROG}.c
149.endif
150
151# Always make an intermediate object file because:
152# - it saves time rebuilding when only the library has changed
153# - the name of the object gets put into the executable symbol table instead of
154#   the name of a variable temporary object.
155# - it's useful to keep objects around for crunching.
156OBJS+=		${PROG}.o
157BCOBJS+=	${PROG}.bc
158LLOBJS+=	${PROG}.ll
159CLEANFILES+=	${PROG}.o ${PROG}.bc ${PROG}.ll
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.endif # !target(${PROG})
177
178.endif # !defined(SRCS)
179
180.if ${MK_DEBUG_FILES} != "no"
181${PROG}: ${PROG_FULL} ${PROGNAME}.debug
182	${OBJCOPY} --strip-debug --add-gnu-debuglink=${PROGNAME}.debug \
183	    ${PROG_FULL} ${.TARGET}
184
185${PROGNAME}.debug: ${PROG_FULL}
186	${OBJCOPY} --only-keep-debug ${PROG_FULL} ${.TARGET}
187.endif
188
189.if defined(LLVM_LINK)
190${PROG_FULL}.bc: ${BCOBJS}
191	${LLVM_LINK} -o ${.TARGET} ${BCOBJS}
192
193${PROG_FULL}.ll: ${LLOBJS}
194	${LLVM_LINK} -S -o ${.TARGET} ${LLOBJS}
195
196CLEANFILES+=	${PROG_FULL}.bc ${PROG_FULL}.ll
197.endif # defined(LLVM_LINK)
198
199.if	${MK_MAN} != "no" && !defined(MAN) && \
200	!defined(MAN1) && !defined(MAN2) && !defined(MAN3) && \
201	!defined(MAN4) && !defined(MAN5) && !defined(MAN6) && \
202	!defined(MAN7) && !defined(MAN8) && !defined(MAN9)
203MAN=	${PROG}.1
204MAN1=	${MAN}
205.endif
206.endif # defined(PROG)
207
208.if defined(_SKIP_BUILD)
209all:
210.else
211all: ${PROG} ${SCRIPTS}
212.if ${MK_MAN} != "no"
213all: all-man
214.endif
215.endif
216
217.if defined(PROG)
218CLEANFILES+= ${PROG} ${PROG}.bc ${PROG}.ll
219.if ${MK_DEBUG_FILES} != "no"
220CLEANFILES+= ${PROG_FULL} ${PROGNAME}.debug
221.endif
222.endif
223
224.if defined(OBJS)
225CLEANFILES+= ${OBJS} ${BCOBJS} ${LLOBJS}
226.endif
227
228.include <bsd.libnames.mk>
229
230.if defined(PROG)
231.if !defined(NO_EXTRADEPEND)
232_EXTRADEPEND:
233.if defined(LDFLAGS) && !empty(LDFLAGS:M-nostdlib)
234.if defined(DPADD) && !empty(DPADD)
235	echo ${PROG_FULL}: ${DPADD} >> ${DEPENDFILE}
236.endif
237.else
238	echo ${PROG_FULL}: ${LIBC} ${DPADD} >> ${DEPENDFILE}
239.if defined(PROG_CXX)
240.if ${COMPILER_TYPE} == "clang" && empty(CXXFLAGS:M-stdlib=libstdc++)
241	echo ${PROG_FULL}: ${LIBCPLUSPLUS} >> ${DEPENDFILE}
242.else
243	echo ${PROG_FULL}: ${LIBSTDCPLUSPLUS} >> ${DEPENDFILE}
244.endif
245.endif
246.endif
247.endif	# !defined(NO_EXTRADEPEND)
248.endif
249
250.if !target(install)
251
252.if defined(PRECIOUSPROG)
253.if !defined(NO_FSCHG)
254INSTALLFLAGS+= -fschg
255.endif
256INSTALLFLAGS+= -S
257.endif
258
259_INSTALLFLAGS:=	${INSTALLFLAGS}
260.for ie in ${INSTALLFLAGS_EDIT}
261_INSTALLFLAGS:=	${_INSTALLFLAGS${ie}}
262.endfor
263
264.if !target(realinstall) && !defined(INTERNALPROG)
265realinstall: _proginstall
266.ORDER: beforeinstall _proginstall
267_proginstall:
268.if defined(PROG)
269	${INSTALL} ${TAG_ARGS} ${STRIP} -o ${BINOWN} -g ${BINGRP} -m ${BINMODE} \
270	    ${_INSTALLFLAGS} ${PROG} ${DESTDIR}${BINDIR}/${PROGNAME}
271.if ${MK_DEBUG_FILES} != "no"
272.if defined(DEBUGMKDIR)
273	${INSTALL} ${TAG_ARGS:D${TAG_ARGS},debug} -d ${DESTDIR}${DEBUGFILEDIR}/
274.endif
275	${INSTALL} ${TAG_ARGS:D${TAG_ARGS},debug} -o ${BINOWN} -g ${BINGRP} -m ${DEBUGMODE} \
276	    ${PROGNAME}.debug ${DESTDIR}${DEBUGFILEDIR}/${PROGNAME}.debug
277.endif
278.endif
279.endif	# !target(realinstall)
280
281.if defined(SCRIPTS) && !empty(SCRIPTS)
282realinstall: _scriptsinstall
283.ORDER: beforeinstall _scriptsinstall
284
285SCRIPTSDIR?=	${BINDIR}
286SCRIPTSOWN?=	${BINOWN}
287SCRIPTSGRP?=	${BINGRP}
288SCRIPTSMODE?=	${BINMODE}
289
290STAGE_AS_SETS+= scripts
291stage_as.scripts: ${SCRIPTS}
292FLAGS.stage_as.scripts= -m ${SCRIPTSMODE}
293STAGE_FILES_DIR.scripts= ${STAGE_OBJTOP}
294.for script in ${SCRIPTS}
295.if defined(SCRIPTSNAME)
296SCRIPTSNAME_${script:T}?=	${SCRIPTSNAME}
297.else
298SCRIPTSNAME_${script:T}?=	${script:T:R}
299.endif
300SCRIPTSDIR_${script:T}?=	${SCRIPTSDIR}
301SCRIPTSOWN_${script:T}?=	${SCRIPTSOWN}
302SCRIPTSGRP_${script:T}?=	${SCRIPTSGRP}
303SCRIPTSMODE_${script:T}?=	${SCRIPTSMODE}
304STAGE_AS_${script:T}=		${SCRIPTSDIR_${script:T}}/${SCRIPTSNAME_${script:T}}
305_scriptsinstall: _SCRIPTSINS_${script:T}
306_SCRIPTSINS_${script:T}: ${script}
307	${INSTALL} ${TAG_ARGS} -o ${SCRIPTSOWN_${.ALLSRC:T}} \
308	    -g ${SCRIPTSGRP_${.ALLSRC:T}} -m ${SCRIPTSMODE_${.ALLSRC:T}} \
309	    ${.ALLSRC} \
310	    ${DESTDIR}${SCRIPTSDIR_${.ALLSRC:T}}/${SCRIPTSNAME_${.ALLSRC:T}}
311.endfor
312.endif
313
314NLSNAME?=	${PROG}
315.include <bsd.nls.mk>
316
317.include <bsd.confs.mk>
318.include <bsd.files.mk>
319.include <bsd.incs.mk>
320
321LINKOWN?=	${BINOWN}
322LINKGRP?=	${BINGRP}
323LINKMODE?=	${BINMODE}
324.include <bsd.links.mk>
325
326.if ${MK_MAN} != "no"
327realinstall: maninstall
328.ORDER: beforeinstall maninstall
329.endif
330
331.endif	# !target(install)
332
333.if ${MK_MAN} != "no"
334.include <bsd.man.mk>
335.endif
336
337.if defined(HAS_TESTS)
338MAKE+=			MK_MAKE_CHECK_USE_SANDBOX=yes
339SUBDIR_TARGETS+=	check
340TESTS_LD_LIBRARY_PATH+=	${.OBJDIR}
341TESTS_PATH+=		${.OBJDIR}
342.endif
343
344.if defined(PROG)
345OBJS_DEPEND_GUESS+= ${SRCS:M*.h}
346.endif
347
348.include <bsd.dep.mk>
349.include <bsd.clang-analyze.mk>
350.include <bsd.obj.mk>
351.include <bsd.sys.mk>
352