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