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