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