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