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