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