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