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