1#         $NetBSD: bsd.sys.mk,v 1.317 2025/01/13 15:40:18 riastradh Exp $
2#
3# Build definitions used for NetBSD source tree builds.
4
5.if !defined(_BSD_SYS_MK_)
6_BSD_SYS_MK_=1
7
8.if !empty(.INCLUDEDFROMFILE:MMakefile*)
9error1:
10          @(echo "bsd.sys.mk should not be included from Makefiles" >& 2; exit 1)
11.endif
12.if !defined(_BSD_OWN_MK_)
13error2:
14          @(echo "bsd.own.mk must be included before bsd.sys.mk" >& 2; exit 1)
15.endif
16
17# XXX: LLVM does not support -iremap and -fdebug-*
18.if ${MKREPRO:Uno} == "yes" && ${MKLLVM:Uno} != "yes"
19.export NETBSDSRCDIR DESTDIR X11SRCDIR
20
21.if !empty(DESTDIR)
22CPPFLAGS+=          -Wp,-iremap,${DESTDIR}:
23REPROFLAGS+=        -fdebug-prefix-map=\$$DESTDIR=
24REPROFLAGS+=        -fmacro-prefix-map=\$$DESTDIR=
25.endif
26
27CPPFLAGS+=          -Wp,-fno-canonical-system-headers
28CPPFLAGS+=          -Wp,-iremap,${NETBSDSRCDIR}:/usr/src
29CPPFLAGS+=          -Wp,-iremap,${X11SRCDIR}:/usr/xsrc
30
31REPROFLAGS+=        -fdebug-prefix-map=\$$NETBSDSRCDIR=/usr/src
32REPROFLAGS+=        -fmacro-prefix-map=\$$NETBSDSRCDIR=/usr/src
33REPROFLAGS+=        -fdebug-prefix-map=\$$X11SRCDIR=/usr/xsrc
34REPROFLAGS+=        -fmacro-prefix-map=\$$X11SRCDIR=/usr/xsrc
35.if defined(MAKEOBJDIRPREFIX)
36NETBSDOBJDIR=       ${MAKEOBJDIRPREFIX}${NETBSDSRCDIR}
37.endif
38
39.if defined(NETBSDOBJDIR)
40.export NETBSDOBJDIR
41REPROFLAGS+=        -fdebug-prefix-map=\$$NETBSDOBJDIR=/usr/obj
42REPROFLAGS+=        -fmacro-prefix-map=\$$NETBSDOBJDIR=/usr/obj
43.endif
44
45LINTFLAGS+=         -R${NETBSDSRCDIR}=/usr/src -R${X11SRCDIR}=/usr/xsrc
46LINTFLAGS+=         -R${DESTDIR}=
47
48# XXX: Cannot handle MAKEOBJDIR, yet.
49REPROFLAGS+=        -fdebug-regex-map='/usr/src/(.*)/obj$$=/usr/obj/\1'
50REPROFLAGS+=        -fdebug-regex-map='/usr/src/(.*)/obj/(.*)=/usr/obj/\1/\2'
51REPROFLAGS+=        -fdebug-regex-map='/usr/src/(.*)/obj\..*=/usr/obj/\1'
52REPROFLAGS+=        -fdebug-regex-map='/usr/src/(.*)/obj\..*/(.*)=/usr/obj/\1/\2'
53
54CFLAGS+=  ${REPROFLAGS}
55CXXFLAGS+=          ${REPROFLAGS}
56.endif
57
58# NetBSD sources use C99 style, with some GCC extensions.
59# Coverity does not like -std=gnu99 (XXX untested but likely not gnu11 either)
60#
61# XXX Ideally we would not rely on GNU extensions, but currently some
62# code uses alloca(3) which -std=c11 compiles broken-at-runtime.  Until
63# we fix that (PR toolchain/58969: use of alloca is warning, not error,
64# with -std=c11), we have to continue using -std=gnu11 and not
65# -std=c11.
66.if !defined(COVERITY_TOP_CONFIG) && empty(CFLAGS:M*-std=*)
67CFLAGS+=  ${${ACTIVE_CC} == "clang":? -std=gnu11 :}
68CFLAGS+=  ${${ACTIVE_CC} == "gcc":? -std=gnu11 :}
69CFLAGS+=  ${${ACTIVE_CC} == "pcc":? -std=gnu11 :}
70.endif
71
72.if defined(WARNS)
73CFLAGS+=  ${${ACTIVE_CC} == "clang":? -Wno-sign-compare -Wno-pointer-sign :}
74.if ${WARNS} > 0
75CFLAGS+=  -Wall -Wstrict-prototypes -Wmissing-prototypes -Wpointer-arith
76#CFLAGS+= -Wmissing-declarations -Wredundant-decls -Wnested-externs
77# Add -Wno-sign-compare.  -Wsign-compare is included in -Wall as of GCC 3.3,
78# but our sources aren't up for it yet. Also, add -Wno-traditional because
79# gcc includes #elif in the warnings, which is 'this code will not compile
80# in a traditional environment' warning, as opposed to 'this code behaves
81# differently in traditional and ansi environments' which is the warning
82# we wanted, and now we don't get anymore.
83CFLAGS+=  -Wno-sign-compare
84# Don't suppress warnings coming from constructs in system headers.
85# Our system headers should be clean and we want to warn about things like:
86# isdigit((char)1)
87CFLAGS+=  ${${ACTIVE_CC} == "gcc" :? -Wsystem-headers :}
88CFLAGS+=  ${${ACTIVE_CC} == "gcc" :? -Wno-traditional :}
89.if !defined(NOGCCERROR)
90# Set assembler warnings to be fatal
91CFLAGS+=  ${${ACTIVE_CC} == "gcc" :? -Wa,--fatal-warnings :}
92.endif
93
94# Set linker warnings to be fatal
95# XXX no proper way to avoid "FOO is a patented algorithm" warnings
96# XXX on linking static libs
97.if (!defined(MKPIC) || ${MKPIC} != "no") && \
98    (!defined(LDSTATIC) || ${LDSTATIC} != "-static")
99# XXX there are some strange problems not yet resolved
100. if !defined(HAVE_GCC) || defined(HAVE_LLVM)
101LDFLAGS+= -Wl,--fatal-warnings
102. endif
103.endif
104.endif
105
106LDFLAGS+= -Wl,--warn-shared-textrel
107
108.if ${WARNS} > 1
109CFLAGS+=  -Wreturn-type -Wswitch -Wshadow
110.endif
111.if ${WARNS} > 2
112CFLAGS+=  -Wcast-qual -Wwrite-strings
113CFLAGS+=  -Wextra -Wno-unused-parameter
114# Readd -Wno-sign-compare to override -Wextra with clang
115CFLAGS+=  -Wno-sign-compare
116.if "${ACTIVE_CC}" == "gcc" && ${HAVE_GCC} < 8
117#  XXX: Won't warn about anything.  -Wabi warns about differences from
118#  the most up-to-date ABI, which in g++ 8 is used by default.
119CXXFLAGS+=          -Wabi
120.endif
121CXXFLAGS+=          -Wold-style-cast
122CXXFLAGS+=          -Wctor-dtor-privacy -Wnon-virtual-dtor -Wreorder \
123                    -Wno-deprecated -Woverloaded-virtual -Wsign-promo -Wsynth
124CXXFLAGS+=          ${${ACTIVE_CXX} == "gcc":? -Wno-non-template-friend -Wno-pmf-conversions :}
125.endif
126.if ${WARNS} > 3 && (defined(HAVE_GCC) || defined(HAVE_LLVM))
127.if ${WARNS} > 4
128CFLAGS+=  -Wold-style-definition
129.endif
130.if ${WARNS} > 5
131CFLAGS+=  -Wconversion
132.endif
133CFLAGS+=  -Wsign-compare -Wformat=2
134CFLAGS+=  ${${ACTIVE_CC} == "gcc":? -Wno-format-zero-length :}
135.endif
136.if ${WARNS} > 3 && defined(HAVE_LLVM)
137CFLAGS+=  ${${ACTIVE_CC} == "clang":? -Wpointer-sign -Wmissing-noreturn :}
138.endif
139.if (defined(HAVE_GCC) \
140     && (${MACHINE_ARCH} == "coldfire" || \
141           ${MACHINE_CPU} == "sh3" || \
142           ${MACHINE_CPU} == "m68k"))
143# XXX GCC 4.5 for sh3 and m68k (which we compile with -Os) is extra noisy for
144# cases it should be better with
145CFLAGS+=  -Wno-uninitialized
146CFLAGS+=  -Wno-maybe-uninitialized
147.endif
148.endif
149
150.if ${MKRELRO:Uno} != "no"
151LDFLAGS+= -Wl,-z,relro
152.endif
153
154.if ${MKRELRO:Uno} == "full" && ${NOFULLRELRO:Uno} == "no"
155LDFLAGS+= -Wl,-z,now
156.endif
157
158.if ${MKSANITIZER:Uno} == "yes"
159SANITIZERFLAGS:=    -fsanitize=${USE_SANITIZER} ${SANITIZERFLAGS}
160.else
161SANITIZERFLAGS=               # empty
162.endif
163
164.if ${MKLIBCSANITIZER:Uno} == "yes"
165LIBCSANITIZERFLAGS:=          -fsanitize=${USE_LIBCSANITIZER} ${LIBCSANITIZERFLAGS}
166LIBCSANITIZERFLAGS+=          -fno-sanitize=vptr  # Unsupported in micro-UBSan
167.else
168LIBCSANITIZERFLAGS= # empty
169.endif
170
171CWARNFLAGS+=        ${CWARNFLAGS.${ACTIVE_CC}}
172
173CPPFLAGS+=          ${AUDIT:D-D__AUDIT__}
174_NOWERROR=          ${defined(NOGCCERROR) || (${ACTIVE_CC} == "clang" && defined(NOCLANGERROR)):?yes:no}
175CFLAGS+=  ${${_NOWERROR} == "no" :?-Werror:} ${CWARNFLAGS}
176.if !empty(DESTDIR)
177LINTFLAGS+=         -d ${DESTDIR}
178.endif
179
180.if !defined(NOSSP) && (${USE_SSP:Uno} != "no") && (${BINDIR:Ux} != "/usr/mdec")
181.   if !defined(KERNSRCDIR) && !defined(KERN) # not for kernels / kern modules
182CPPFLAGS+=          -D_FORTIFY_SOURCE=2
183.   endif
184.   if !defined(COVERITY_TOP_CONFIG)
185COPTS+=   -fstack-protector -Wstack-protector
186
187# GCC 4.8 on m68k erroneously does not protect functions with
188# variables needing special alignment, see
189#         http://gcc.gnu.org/bugzilla/show_bug.cgi?id=59674
190# (the underlying issue for sh and vax may be different, needs more
191# investigation, symptoms are similar but for different sources)
192# also true for GCC 5, assume GCC 6 too.
193.         if "${ACTIVE_CC}" == "gcc" && \
194     ( ${HAVE_GCC} == "5" || \
195       ${HAVE_GCC} == "6" ) && \
196     ( ${MACHINE_CPU} == "sh3" || \
197       ${MACHINE_ARCH} == "vax" || \
198       ${MACHINE_CPU} == "m68k" || \
199       ${MACHINE_CPU} == "or1k" )
200COPTS+=   -Wno-error=stack-protector
201.         endif
202
203COPTS+=   ${${ACTIVE_CC} == "clang":? --param ssp-buffer-size=1 :}
204COPTS+=   ${${ACTIVE_CC} == "gcc":? --param ssp-buffer-size=1 :}
205.   endif
206.endif
207
208.if ${MKSOFTFLOAT:Uno} != "no"
209# sh3 defaults to soft-float and specifies hard-float a different way
210.if ${MACHINE_CPU} != "sh3"
211COPTS+=             ${${ACTIVE_CC} == "gcc":? -msoft-float :}
212FOPTS+=             -msoft-float
213.endif
214.elif ${MACHINE_ARCH} == "coldfire"
215COPTS+=             -mhard-float
216FOPTS+=             -mhard-float
217.endif
218
219#.if !empty(MACHINE_ARCH:Mearmv7*)
220#COPTS+=            -mthumb
221#FOPTS+=            -mthumb
222#.endif
223
224.if ${MKIEEEFP:Uno} != "no"
225.if ${MACHINE_ARCH} == "alpha"
226CFLAGS+=  -mieee
227FFLAGS+=  -mieee
228.endif
229.endif
230
231.if ${MACHINE} == "sparc64" && ${MACHINE_ARCH} == "sparc"
232CFLAGS+=  -Wa,-Av8plus
233.endif
234
235.if !defined(NOGCCERROR)
236.if ${MACHINE_MIPS64}
237CPUFLAGS+=          -Wa,--fatal-warnings
238.endif
239.endif
240
241#.if ${MACHINE} == "sbmips"
242#CFLAGS+= -mips64 -mtune=sb1
243#.endif
244
245#.if ${MACHINE_MIPS64} && defined(MKPIC) && ${MKPIC} == "no"
246#CPUFLAGS+=         -mno-abicalls -fno-PIC
247#.endif
248CFLAGS+=  ${CPUFLAGS}
249AFLAGS+=  ${CPUFLAGS}
250
251.if ${KCOV:U0} > 0
252KCOVFLAGS=          -fsanitize-coverage=trace-pc,trace-cmp
253.for f in subr_kcov.c subr_asan.c subr_csan.c subr_msan.c ubsan.c
254KCOVFLAGS.${f}=               # empty
255.endfor
256CFLAGS+=  ${KCOVFLAGS.${.IMPSRC:T}:U${KCOVFLAGS}}
257.endif
258
259.if !defined(NOPIE) && (!defined(LDSTATIC) || ${LDSTATIC} != "-static")
260# Position Independent Executable flags
261PIE_CFLAGS?=        -fPIE
262PIE_LDFLAGS?=       -pie ${${ACTIVE_CC} == "gcc":? -shared-libgcc :}
263PIE_AFLAGS?=            -fPIE
264.endif
265
266ARM_ELF2AOUT?=      elf2aout
267M68K_ELF2AOUT?=     elf2aout
268MIPS_ELF2ECOFF?=    elf2ecoff
269MKDEP?=             mkdep
270MKDEPCXX?=          mkdep
271OBJCOPY?= objcopy
272OBJDUMP?= objdump
273PAXCTL?=  paxctl
274STRIP?=             strip
275
276.SUFFIXES:          .o .ln .lo .c .cc .cpp .cxx .C .m ${YHEADER:D.h}
277
278# C
279.c.o:
280          ${_MKTARGET_COMPILE}
281          ${COMPILE.c} ${COPTS.${.IMPSRC:T}} ${CPUFLAGS.${.IMPSRC:T}} ${CPPFLAGS.${.IMPSRC:T}} ${.IMPSRC} ${OBJECT_TARGET}
282          ${CTFCONVERT_RUN}
283
284.c.ln:
285          ${_MKTARGET_COMPILE}
286          ${LINT} ${LINTFLAGS} ${LINTFLAGS.${.IMPSRC:T}} \
287              ${CPPFLAGS:C/-([IDUW])[  ]*/-\1/Wg:M-[IDUW]*} \
288              ${CPPFLAGS.${.IMPSRC:T}:C/-([IDUW])[  ]*/-\1/Wg:M-[IDUW]*} \
289              -i ${.IMPSRC}
290
291# C++
292.cc.o .cpp.o .cxx.o .C.o:
293          ${_MKTARGET_COMPILE}
294          ${COMPILE.cc} ${COPTS.${.IMPSRC:T}} ${CPUFLAGS.${.IMPSRC:T}} ${CPPFLAGS.${.IMPSRC:T}} ${.IMPSRC}
295
296# Objective C
297# (Defined here rather than in <sys.mk> because `.m' is not just
298#  used for Objective C source)
299.m.o:
300          ${_MKTARGET_COMPILE}
301          ${COMPILE.m} ${OBJCOPTS} ${OBJCOPTS.${.IMPSRC:T}} ${.IMPSRC} ${OBJECT_TARGET}
302          ${CTFCONVERT_RUN}
303
304# Host-compiled C objects
305# The intermediate step is necessary for Sun CC, which objects to calling
306# object files anything but *.o
307.c.lo:
308          ${_MKTARGET_COMPILE}
309          ${HOST_COMPILE.c} -o ${.TARGET}.o ${COPTS.${.IMPSRC:T}} ${CPUFLAGS.${.IMPSRC:T}} ${CPPFLAGS.${.IMPSRC:T}} ${.IMPSRC}
310          ${MV} ${.TARGET}.o ${.TARGET}
311
312# C++
313.cc.lo .cpp.lo .cxx.lo .C.lo:
314          ${_MKTARGET_COMPILE}
315          ${HOST_COMPILE.cc} -o ${.TARGET}.o ${COPTS.${.IMPSRC:T}} ${CPUFLAGS.${.IMPSRC:T}} ${CPPFLAGS.${.IMPSRC:T}} ${.IMPSRC}
316          ${MV} ${.TARGET}.o ${.TARGET}
317
318# Assembly
319.s.o:
320          ${_MKTARGET_COMPILE}
321          ${COMPILE.s} ${COPTS.${.IMPSRC:T}} ${CPUFLAGS.${.IMPSRC:T}} ${CPPFLAGS.${.IMPSRC:T}} ${.IMPSRC} ${OBJECT_TARGET}
322          ${CTFCONVERT_RUN}
323
324.S.o:
325          ${_MKTARGET_COMPILE}
326          ${COMPILE.S} ${COPTS.${.IMPSRC:T}} ${CPUFLAGS.${.IMPSRC:T}} ${CPPFLAGS.${.IMPSRC:T}} ${.IMPSRC} ${OBJECT_TARGET}
327          ${CTFCONVERT_RUN}
328
329# Lex
330LFLAGS+=  ${LPREFIX.${.IMPSRC:T}:D-P${LPREFIX.${.IMPSRC:T}}}
331LFLAGS+=  ${LPREFIX:D-P${LPREFIX}} ${LFLAGS.${.IMPSRC:T}}
332
333.l.c:
334          ${_MKTARGET_LEX}
335          ${LEX.l} -o${.TARGET} ${.IMPSRC}
336
337# Yacc
338YFLAGS+=  ${YPREFIX.${.IMPSRC:T}:D-p${YPREFIX.${.IMPSRC:T}}} ${YHEADER.${.IMPSRC:T}:D-d}
339YFLAGS+=  ${YPREFIX:D-p${YPREFIX}} ${YHEADER:D-d} ${YFLAGS.${.IMPSRC:T}}
340
341.y.c:
342          ${_MKTARGET_YACC}
343          ${YACC.y} -o ${.TARGET} ${.IMPSRC}
344
345.ifdef YHEADER
346.if empty(.MAKEFLAGS:M-n)
347.y.h: ${.TARGET:.h=.c}
348.endif
349.endif
350
351# Objcopy
352.if ${MACHINE_ARCH} == aarch64eb
353# AARCH64 big endian needs to preserve $x/$d symbols for the linker.
354OBJCOPYLIBFLAGS_EXTRA=-w -K '[$$][dx]' -K '[$$][dx]\.*'
355.elif ${MACHINE_CPU} == "arm"
356# ARM big endian needs to preserve $a/$d/$t symbols for the linker.
357OBJCOPYLIBFLAGS_EXTRA=-w -K '[$$][adt]' -K '[$$][adt]\.*'
358.endif
359
360.if ${MKSTRIPSYM} != "no"
361OBJCOPYLIBFLAGS?=${"${.TARGET:M*.po}" != "":?-X:-x} ${OBJCOPYLIBFLAGS_EXTRA}
362.else
363OBJCOPYLIBFLAGS?=-X ${OBJCOPYLIBFLAGS_EXTRA}
364.endif
365
366.endif    # !defined(_BSD_SYS_MK_)
367