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