xref: /netbsd-src/share/mk/bsd.sys.mk (revision cef8759bd76c1b621f8eab8faa6f208faabc2e15)
1#	$NetBSD: bsd.sys.mk,v 1.302 2020/07/03 16:12:16 maxv 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.if ${MKRELRO:Uno} == "full"
144LDFLAGS+=	-Wl,-z,now
145.endif
146
147.if ${MKSANITIZER:Uno} == "yes"
148SANITIZERFLAGS:=	-fsanitize=${USE_SANITIZER} ${SANITIZERFLAGS}
149.else
150SANITIZERFLAGS=		# empty
151.endif
152
153.if ${MKLIBCSANITIZER:Uno} == "yes"
154LIBCSANITIZERFLAGS:=	-fsanitize=${USE_LIBCSANITIZER} ${LIBCSANITIZERFLAGS}
155LIBCSANITIZERFLAGS+=	-fno-sanitize=vptr	# Unsupported in micro-UBSan
156.else
157LIBCSANITIZERFLAGS=	# empty
158.endif
159
160CWARNFLAGS+=	${CWARNFLAGS.${ACTIVE_CC}}
161
162CPPFLAGS+=	${AUDIT:D-D__AUDIT__}
163_NOWERROR=	${defined(NOGCCERROR) || (${ACTIVE_CC} == "clang" && defined(NOCLANGERROR)):?yes:no}
164CFLAGS+=	${${_NOWERROR} == "no" :?-Werror:} ${CWARNFLAGS}
165LINTFLAGS+=	${DESTDIR:D-d ${DESTDIR}/usr/include}
166
167.if !defined(NOSSP) && (${USE_SSP:Uno} != "no") && (${BINDIR:Ux} != "/usr/mdec")
168.   if !defined(KERNSRCDIR) && !defined(KERN) # not for kernels / kern modules
169CPPFLAGS+=	-D_FORTIFY_SOURCE=2
170.   endif
171.   if !defined(COVERITY_TOP_CONFIG)
172COPTS+=	-fstack-protector -Wstack-protector
173
174# GCC 4.8 on m68k erroneously does not protect functions with
175# variables needing special alignement, see
176#	http://gcc.gnu.org/bugzilla/show_bug.cgi?id=59674
177# (the underlying issue for sh and vax may be different, needs more
178# investigation, symptoms are similar but for different sources)
179# also true for GCC 5, assume GCC 6 too.
180.	if "${ACTIVE_CC}" == "gcc" && \
181     ( ${HAVE_GCC} == "5" || \
182       ${HAVE_GCC} == "6" ) && \
183     ( ${MACHINE_CPU} == "sh3" || \
184       ${MACHINE_ARCH} == "vax" || \
185       ${MACHINE_CPU} == "m68k" || \
186       ${MACHINE_CPU} == "or1k" )
187COPTS+=	-Wno-error=stack-protector
188.	endif
189
190COPTS+=	${${ACTIVE_CC} == "clang":? --param ssp-buffer-size=1 :}
191COPTS+=	${${ACTIVE_CC} == "gcc":? --param ssp-buffer-size=1 :}
192.   endif
193.endif
194
195.if ${MKSOFTFLOAT:Uno} != "no"
196# sh3 defaults to soft-float and specifies hard-float a different way
197.if ${MACHINE_CPU} != "sh3"
198COPTS+=		${${ACTIVE_CC} == "gcc":? -msoft-float :}
199FOPTS+=		-msoft-float
200.endif
201.elif ${MACHINE_ARCH} == "coldfire"
202COPTS+=		-mhard-float
203FOPTS+=		-mhard-float
204.endif
205
206#.if !empty(MACHINE_ARCH:Mearmv7*)
207#COPTS+=		-mthumb
208#FOPTS+=		-mthumb
209#.endif
210
211.if ${MKIEEEFP:Uno} != "no"
212.if ${MACHINE_ARCH} == "alpha"
213CFLAGS+=	-mieee
214FFLAGS+=	-mieee
215.endif
216.endif
217
218.if ${MACHINE} == "sparc64" && ${MACHINE_ARCH} == "sparc"
219CFLAGS+=	-Wa,-Av8plus
220.endif
221
222.if !defined(NOGCCERROR)
223.if (${MACHINE_ARCH} == "mips64el") || (${MACHINE_ARCH} == "mips64eb")
224CPUFLAGS+=	-Wa,--fatal-warnings
225.endif
226.endif
227
228#.if ${MACHINE} == "sbmips"
229#CFLAGS+=	-mips64 -mtune=sb1
230#.endif
231
232#.if (${MACHINE_ARCH} == "mips64el" || ${MACHINE_ARCH} == "mips64eb") && \
233#    (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}
270.if defined(CTFCONVERT)
271	${CTFCONVERT} ${CTFFLAGS} ${.TARGET}
272.endif
273
274.c.ln:
275	${_MKTARGET_COMPILE}
276	${LINT} ${LINTFLAGS} ${LINTFLAGS.${.IMPSRC:T}} \
277	    ${CPPFLAGS:C/-([IDU])[  ]*/-\1/Wg:M-[IDU]*} \
278	    ${CPPFLAGS.${.IMPSRC:T}:C/-([IDU])[  ]*/-\1/Wg:M-[IDU]*} \
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}
292.if defined(CTFCONVERT)
293	${CTFCONVERT} ${CTFFLAGS} ${.TARGET}
294.endif
295
296# Host-compiled C objects
297# The intermediate step is necessary for Sun CC, which objects to calling
298# object files anything but *.o
299.c.lo:
300	${_MKTARGET_COMPILE}
301	${HOST_COMPILE.c} -o ${.TARGET}.o ${COPTS.${.IMPSRC:T}} ${CPUFLAGS.${.IMPSRC:T}} ${CPPFLAGS.${.IMPSRC:T}} ${.IMPSRC}
302	${MV} ${.TARGET}.o ${.TARGET}
303
304# C++
305.cc.lo .cpp.lo .cxx.lo .C.lo:
306	${_MKTARGET_COMPILE}
307	${HOST_COMPILE.cc} -o ${.TARGET}.o ${COPTS.${.IMPSRC:T}} ${CPUFLAGS.${.IMPSRC:T}} ${CPPFLAGS.${.IMPSRC:T}} ${.IMPSRC}
308	${MV} ${.TARGET}.o ${.TARGET}
309
310# Assembly
311.s.o:
312	${_MKTARGET_COMPILE}
313	${COMPILE.s} ${COPTS.${.IMPSRC:T}} ${CPUFLAGS.${.IMPSRC:T}} ${CPPFLAGS.${.IMPSRC:T}} ${.IMPSRC}
314.if defined(CTFCONVERT)
315	${CTFCONVERT} ${CTFFLAGS} ${.TARGET}
316.endif
317
318.S.o:
319	${_MKTARGET_COMPILE}
320	${COMPILE.S} ${COPTS.${.IMPSRC:T}} ${CPUFLAGS.${.IMPSRC:T}} ${CPPFLAGS.${.IMPSRC:T}} ${.IMPSRC}
321.if defined(CTFCONVERT)
322	${CTFCONVERT} ${CTFFLAGS} ${.TARGET}
323.endif
324
325# Lex
326LFLAGS+=	${LPREFIX.${.IMPSRC:T}:D-P${LPREFIX.${.IMPSRC:T}}}
327LFLAGS+=	${LPREFIX:D-P${LPREFIX}} ${LFLAGS.${.IMPSRC:T}}
328
329.l.c:
330	${_MKTARGET_LEX}
331	${LEX.l} -o${.TARGET} ${.IMPSRC}
332
333# Yacc
334YFLAGS+=	${YPREFIX.${.IMPSRC:T}:D-p${YPREFIX.${.IMPSRC:T}}} ${YHEADER.${.IMPSRC:T}:D-d}
335YFLAGS+=	${YPREFIX:D-p${YPREFIX}} ${YHEADER:D-d} ${YFLAGS.${.IMPSRC:T}}
336
337.y.c:
338	${_MKTARGET_YACC}
339	${YACC.y} -o ${.TARGET} ${.IMPSRC}
340
341.ifdef YHEADER
342.if empty(.MAKEFLAGS:M-n)
343.y.h: ${.TARGET:.h=.c}
344.endif
345.endif
346
347# Objcopy
348.if ${MACHINE_ARCH} == aarch64eb
349# AARCH64 big endian needs to preserve $x/$d symbols for the linker.
350OBJCOPYLIBFLAGS_EXTRA=-w -K '[$$][dx]' -K '[$$][dx]\.*'
351.elif ${MACHINE_CPU} == "arm"
352# ARM big endian needs to preserve $a/$d/$t symbols for the linker.
353OBJCOPYLIBFLAGS_EXTRA=-w -K '[$$][adt]' -K '[$$][adt]\.*'
354.endif
355
356.if ${MKSTRIPSYM:Uyes} == "yes"
357OBJCOPYLIBFLAGS?=${"${.TARGET:M*.po}" != "":?-X:-x} ${OBJCOPYLIBFLAGS_EXTRA}
358.else
359OBJCOPYLIBFLAGS?=-X ${OBJCOPYLIBFLAGS_EXTRA}
360.endif
361
362.endif	# !defined(_BSD_SYS_MK_)
363