xref: /netbsd-src/share/mk/bsd.sys.mk (revision d16b7486a53dcb8072b60ec6fcb4373a2d0c27b7)
1#	$NetBSD: bsd.sys.mk,v 1.314 2023/06/03 21:24:57 lukem 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}
166.if !empty(DESTDIR)
167LINTFLAGS+=	-d ${DESTDIR}
168.endif
169
170.if !defined(NOSSP) && (${USE_SSP:Uno} != "no") && (${BINDIR:Ux} != "/usr/mdec")
171.   if !defined(KERNSRCDIR) && !defined(KERN) # not for kernels / kern modules
172CPPFLAGS+=	-D_FORTIFY_SOURCE=2
173.   endif
174.   if !defined(COVERITY_TOP_CONFIG)
175COPTS+=	-fstack-protector -Wstack-protector
176
177# GCC 4.8 on m68k erroneously does not protect functions with
178# variables needing special alignment, see
179#	http://gcc.gnu.org/bugzilla/show_bug.cgi?id=59674
180# (the underlying issue for sh and vax may be different, needs more
181# investigation, symptoms are similar but for different sources)
182# also true for GCC 5, assume GCC 6 too.
183.	if "${ACTIVE_CC}" == "gcc" && \
184     ( ${HAVE_GCC} == "5" || \
185       ${HAVE_GCC} == "6" ) && \
186     ( ${MACHINE_CPU} == "sh3" || \
187       ${MACHINE_ARCH} == "vax" || \
188       ${MACHINE_CPU} == "m68k" || \
189       ${MACHINE_CPU} == "or1k" )
190COPTS+=	-Wno-error=stack-protector
191.	endif
192
193COPTS+=	${${ACTIVE_CC} == "clang":? --param ssp-buffer-size=1 :}
194COPTS+=	${${ACTIVE_CC} == "gcc":? --param ssp-buffer-size=1 :}
195.   endif
196.endif
197
198.if ${MKSOFTFLOAT:Uno} != "no"
199# sh3 defaults to soft-float and specifies hard-float a different way
200.if ${MACHINE_CPU} != "sh3"
201COPTS+=		${${ACTIVE_CC} == "gcc":? -msoft-float :}
202FOPTS+=		-msoft-float
203.endif
204.elif ${MACHINE_ARCH} == "coldfire"
205COPTS+=		-mhard-float
206FOPTS+=		-mhard-float
207.endif
208
209#.if !empty(MACHINE_ARCH:Mearmv7*)
210#COPTS+=		-mthumb
211#FOPTS+=		-mthumb
212#.endif
213
214.if ${MKIEEEFP:Uno} != "no"
215.if ${MACHINE_ARCH} == "alpha"
216CFLAGS+=	-mieee
217FFLAGS+=	-mieee
218.endif
219.endif
220
221.if ${MACHINE} == "sparc64" && ${MACHINE_ARCH} == "sparc"
222CFLAGS+=	-Wa,-Av8plus
223.endif
224
225.if !defined(NOGCCERROR)
226.if ${MACHINE_MIPS64}
227CPUFLAGS+=	-Wa,--fatal-warnings
228.endif
229.endif
230
231#.if ${MACHINE} == "sbmips"
232#CFLAGS+=	-mips64 -mtune=sb1
233#.endif
234
235#.if ${MACHINE_MIPS64} && defined(MKPIC) && ${MKPIC} == "no"
236#CPUFLAGS+=	-mno-abicalls -fno-PIC
237#.endif
238CFLAGS+=	${CPUFLAGS}
239AFLAGS+=	${CPUFLAGS}
240
241.if ${KCOV:U0} > 0
242KCOVFLAGS=	-fsanitize-coverage=trace-pc,trace-cmp
243.for f in subr_kcov.c subr_asan.c subr_csan.c subr_msan.c ubsan.c
244KCOVFLAGS.${f}=		# empty
245.endfor
246CFLAGS+=	${KCOVFLAGS.${.IMPSRC:T}:U${KCOVFLAGS}}
247.endif
248
249.if !defined(NOPIE) && (!defined(LDSTATIC) || ${LDSTATIC} != "-static")
250# Position Independent Executable flags
251PIE_CFLAGS?=        -fPIE
252PIE_LDFLAGS?=       -pie ${${ACTIVE_CC} == "gcc":? -shared-libgcc :}
253PIE_AFLAGS?=	    -fPIE
254.endif
255
256ARM_ELF2AOUT?=	elf2aout
257M68K_ELF2AOUT?=	elf2aout
258MIPS_ELF2ECOFF?=	elf2ecoff
259MKDEP?=		mkdep
260MKDEPCXX?=	mkdep
261OBJCOPY?=	objcopy
262OBJDUMP?=	objdump
263PAXCTL?=	paxctl
264STRIP?=		strip
265
266.SUFFIXES:	.o .ln .lo .c .cc .cpp .cxx .C .m ${YHEADER:D.h}
267
268# C
269.c.o:
270	${_MKTARGET_COMPILE}
271	${COMPILE.c} ${COPTS.${.IMPSRC:T}} ${CPUFLAGS.${.IMPSRC:T}} ${CPPFLAGS.${.IMPSRC:T}} ${.IMPSRC} ${OBJECT_TARGET}
272	${CTFCONVERT_RUN}
273
274.c.ln:
275	${_MKTARGET_COMPILE}
276	${LINT} ${LINTFLAGS} ${LINTFLAGS.${.IMPSRC:T}} \
277	    ${CPPFLAGS:C/-([IDUW])[  ]*/-\1/Wg:M-[IDUW]*} \
278	    ${CPPFLAGS.${.IMPSRC:T}:C/-([IDUW])[  ]*/-\1/Wg:M-[IDUW]*} \
279	    -i ${.IMPSRC}
280
281# C++
282.cc.o .cpp.o .cxx.o .C.o:
283	${_MKTARGET_COMPILE}
284	${COMPILE.cc} ${COPTS.${.IMPSRC:T}} ${CPUFLAGS.${.IMPSRC:T}} ${CPPFLAGS.${.IMPSRC:T}} ${.IMPSRC}
285
286# Objective C
287# (Defined here rather than in <sys.mk> because `.m' is not just
288#  used for Objective C source)
289.m.o:
290	${_MKTARGET_COMPILE}
291	${COMPILE.m} ${OBJCOPTS} ${OBJCOPTS.${.IMPSRC:T}} ${.IMPSRC} ${OBJECT_TARGET}
292	${CTFCONVERT_RUN}
293
294# Host-compiled C objects
295# The intermediate step is necessary for Sun CC, which objects to calling
296# object files anything but *.o
297.c.lo:
298	${_MKTARGET_COMPILE}
299	${HOST_COMPILE.c} -o ${.TARGET}.o ${COPTS.${.IMPSRC:T}} ${CPUFLAGS.${.IMPSRC:T}} ${CPPFLAGS.${.IMPSRC:T}} ${.IMPSRC}
300	${MV} ${.TARGET}.o ${.TARGET}
301
302# C++
303.cc.lo .cpp.lo .cxx.lo .C.lo:
304	${_MKTARGET_COMPILE}
305	${HOST_COMPILE.cc} -o ${.TARGET}.o ${COPTS.${.IMPSRC:T}} ${CPUFLAGS.${.IMPSRC:T}} ${CPPFLAGS.${.IMPSRC:T}} ${.IMPSRC}
306	${MV} ${.TARGET}.o ${.TARGET}
307
308# Assembly
309.s.o:
310	${_MKTARGET_COMPILE}
311	${COMPILE.s} ${COPTS.${.IMPSRC:T}} ${CPUFLAGS.${.IMPSRC:T}} ${CPPFLAGS.${.IMPSRC:T}} ${.IMPSRC} ${OBJECT_TARGET}
312	${CTFCONVERT_RUN}
313
314.S.o:
315	${_MKTARGET_COMPILE}
316	${COMPILE.S} ${COPTS.${.IMPSRC:T}} ${CPUFLAGS.${.IMPSRC:T}} ${CPPFLAGS.${.IMPSRC:T}} ${.IMPSRC} ${OBJECT_TARGET}
317	${CTFCONVERT_RUN}
318
319# Lex
320LFLAGS+=	${LPREFIX.${.IMPSRC:T}:D-P${LPREFIX.${.IMPSRC:T}}}
321LFLAGS+=	${LPREFIX:D-P${LPREFIX}} ${LFLAGS.${.IMPSRC:T}}
322
323.l.c:
324	${_MKTARGET_LEX}
325	${LEX.l} -o${.TARGET} ${.IMPSRC}
326
327# Yacc
328YFLAGS+=	${YPREFIX.${.IMPSRC:T}:D-p${YPREFIX.${.IMPSRC:T}}} ${YHEADER.${.IMPSRC:T}:D-d}
329YFLAGS+=	${YPREFIX:D-p${YPREFIX}} ${YHEADER:D-d} ${YFLAGS.${.IMPSRC:T}}
330
331.y.c:
332	${_MKTARGET_YACC}
333	${YACC.y} -o ${.TARGET} ${.IMPSRC}
334
335.ifdef YHEADER
336.if empty(.MAKEFLAGS:M-n)
337.y.h: ${.TARGET:.h=.c}
338.endif
339.endif
340
341# Objcopy
342.if ${MACHINE_ARCH} == aarch64eb
343# AARCH64 big endian needs to preserve $x/$d symbols for the linker.
344OBJCOPYLIBFLAGS_EXTRA=-w -K '[$$][dx]' -K '[$$][dx]\.*'
345.elif ${MACHINE_CPU} == "arm"
346# ARM big endian needs to preserve $a/$d/$t symbols for the linker.
347OBJCOPYLIBFLAGS_EXTRA=-w -K '[$$][adt]' -K '[$$][adt]\.*'
348.endif
349
350.if ${MKSTRIPSYM} != "no"
351OBJCOPYLIBFLAGS?=${"${.TARGET:M*.po}" != "":?-X:-x} ${OBJCOPYLIBFLAGS_EXTRA}
352.else
353OBJCOPYLIBFLAGS?=-X ${OBJCOPYLIBFLAGS_EXTRA}
354.endif
355
356.endif	# !defined(_BSD_SYS_MK_)
357