xref: /netbsd-src/share/mk/bsd.sys.mk (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1#	$NetBSD: bsd.sys.mk,v 1.284 2018/06/24 19:35:12 kamil 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
106CXXFLAGS+=	-Wabi
107CXXFLAGS+=	-Wold-style-cast
108CXXFLAGS+=	-Wctor-dtor-privacy -Wnon-virtual-dtor -Wreorder \
109		-Wno-deprecated -Woverloaded-virtual -Wsign-promo -Wsynth
110CXXFLAGS+=	${${ACTIVE_CXX} == "gcc":? -Wno-non-template-friend -Wno-pmf-conversions :}
111.endif
112.if ${WARNS} > 3 && (defined(HAVE_GCC) || defined(HAVE_LLVM))
113.if ${WARNS} > 4
114CFLAGS+=	-Wold-style-definition
115.endif
116.if ${WARNS} > 5
117CFLAGS+=	-Wconversion
118.endif
119CFLAGS+=	-Wsign-compare -Wformat=2
120CFLAGS+=	${${ACTIVE_CC} == "gcc":? -Wno-format-zero-length :}
121.endif
122.if ${WARNS} > 3 && defined(HAVE_LLVM)
123CFLAGS+=	${${ACTIVE_CC} == "clang":? -Wpointer-sign -Wmissing-noreturn :}
124.endif
125.if (defined(HAVE_GCC) \
126     && (${MACHINE_ARCH} == "coldfire" || \
127	 ${MACHINE_CPU} == "sh3" || \
128	 ${MACHINE_CPU} == "m68k"))
129# XXX GCC 4.5 for sh3 and m68k (which we compile with -Os) is extra noisy for
130# cases it should be better with
131CFLAGS+=	-Wno-uninitialized
132CFLAGS+=	-Wno-maybe-uninitialized
133.endif
134.endif
135
136.if ${MKRELRO:Uno} != "no"
137LDFLAGS+=	-Wl,-z,relro
138.endif
139.if ${MKRELRO:Uno} == "full"
140LDFLAGS+=	-Wl,-z,now
141.endif
142
143.if ${MKSANITIZER:Uno} == "yes"
144SANITIZERFLAGS+=	-fsanitize=${USE_SANITIZER}
145.else
146SANITIZERFLAGS=		# empty
147.endif
148
149
150CWARNFLAGS+=	${CWARNFLAGS.${ACTIVE_CC}}
151
152CPPFLAGS+=	${AUDIT:D-D__AUDIT__}
153_NOWERROR=	${defined(NOGCCERROR) || (${ACTIVE_CC} == "clang" && defined(NOCLANGERROR)):?yes:no}
154CFLAGS+=	${${_NOWERROR} == "no" :?-Werror:} ${CWARNFLAGS}
155LINTFLAGS+=	${DESTDIR:D-d ${DESTDIR}/usr/include}
156
157.if !defined(NOSSP) && (${USE_SSP:Uno} != "no") && (${BINDIR:Ux} != "/usr/mdec")
158.   if !defined(KERNSRCDIR) && !defined(KERN) # not for kernels / kern modules
159CPPFLAGS+=	-D_FORTIFY_SOURCE=2
160.   endif
161.   if !defined(COVERITY_TOP_CONFIG)
162COPTS+=	-fstack-protector -Wstack-protector
163
164# GCC 4.8 on m68k erroneously does not protect functions with
165# variables needing special alignement, see
166#	http://gcc.gnu.org/bugzilla/show_bug.cgi?id=59674
167# (the underlying issue for sh and vax may be different, needs more
168# investigation, symptoms are similar but for different sources)
169# also true for GCC 5, assume GCC 6 too.
170.	if "${ACTIVE_CC}" == "gcc" && \
171     ( ${HAVE_GCC} == "5" || \
172       ${HAVE_GCC} == "6" ) && \
173     ( ${MACHINE_CPU} == "sh3" || \
174       ${MACHINE_ARCH} == "vax" || \
175       ${MACHINE_CPU} == "m68k" || \
176       ${MACHINE_CPU} == "or1k" )
177COPTS+=	-Wno-error=stack-protector
178.	endif
179
180COPTS+=	${${ACTIVE_CC} == "clang":? --param ssp-buffer-size=1 :}
181COPTS+=	${${ACTIVE_CC} == "gcc":? --param ssp-buffer-size=1 :}
182.   endif
183.endif
184
185.if ${MKSOFTFLOAT:Uno} != "no"
186# sh3 defaults to soft-float and specifies hard-float a different way
187.if ${MACHINE_CPU} != "sh3"
188COPTS+=		${${ACTIVE_CC} == "gcc":? -msoft-float :}
189FOPTS+=		-msoft-float
190.endif
191.elif ${MACHINE_ARCH} == "coldfire"
192COPTS+=		-mhard-float
193FOPTS+=		-mhard-float
194.endif
195
196#.if !empty(MACHINE_ARCH:Mearmv7*)
197#COPTS+=		-mthumb
198#FOPTS+=		-mthumb
199#.endif
200
201.if ${MKIEEEFP:Uno} != "no"
202.if ${MACHINE_ARCH} == "alpha"
203CFLAGS+=	-mieee
204FFLAGS+=	-mieee
205.endif
206.endif
207
208.if ${MACHINE} == "sparc64" && ${MACHINE_ARCH} == "sparc"
209CFLAGS+=	-Wa,-Av8plus
210.endif
211
212.if !defined(NOGCCERROR)
213.if (${MACHINE_ARCH} == "mips64el") || (${MACHINE_ARCH} == "mips64eb")
214CPUFLAGS+=	-Wa,--fatal-warnings
215.endif
216.endif
217
218#.if ${MACHINE} == "sbmips"
219#CFLAGS+=	-mips64 -mtune=sb1
220#.endif
221
222#.if (${MACHINE_ARCH} == "mips64el" || ${MACHINE_ARCH} == "mips64eb") && \
223#    (defined(MKPIC) && ${MKPIC} == "no")
224#CPUFLAGS+=	-mno-abicalls -fno-PIC
225#.endif
226CFLAGS+=	${CPUFLAGS}
227AFLAGS+=	${CPUFLAGS}
228
229.if !defined(NOPIE) && (!defined(LDSTATIC) || ${LDSTATIC} != "-static")
230# Position Independent Executable flags
231PIE_CFLAGS?=        -fPIE
232PIE_LDFLAGS?=       -pie ${${ACTIVE_CC} == "gcc":? -shared-libgcc :}
233PIE_AFLAGS?=	    -fPIE
234.endif
235
236ELF2ECOFF?=	elf2ecoff
237MKDEP?=		mkdep
238MKDEPCXX?=	mkdep
239OBJCOPY?=	objcopy
240OBJDUMP?=	objdump
241PAXCTL?=	paxctl
242STRIP?=		strip
243
244.SUFFIXES:	.o .ln .lo .c .cc .cpp .cxx .C .m ${YHEADER:D.h}
245
246# C
247.c.o:
248	${_MKTARGET_COMPILE}
249	${COMPILE.c} ${COPTS.${.IMPSRC:T}} ${CPUFLAGS.${.IMPSRC:T}} ${CPPFLAGS.${.IMPSRC:T}} ${.IMPSRC}
250.if defined(CTFCONVERT)
251	${CTFCONVERT} ${CTFFLAGS} ${.TARGET}
252.endif
253
254.c.ln:
255	${_MKTARGET_COMPILE}
256	${LINT} ${LINTFLAGS} ${LINTFLAGS.${.IMPSRC:T}} \
257	    ${CPPFLAGS:C/-([IDU])[  ]*/-\1/Wg:M-[IDU]*} \
258	    ${CPPFLAGS.${.IMPSRC:T}:C/-([IDU])[  ]*/-\1/Wg:M-[IDU]*} \
259	    -i ${.IMPSRC}
260
261# C++
262.cc.o .cpp.o .cxx.o .C.o:
263	${_MKTARGET_COMPILE}
264	${COMPILE.cc} ${COPTS.${.IMPSRC:T}} ${CPUFLAGS.${.IMPSRC:T}} ${CPPFLAGS.${.IMPSRC:T}} ${.IMPSRC}
265
266# Objective C
267# (Defined here rather than in <sys.mk> because `.m' is not just
268#  used for Objective C source)
269.m.o:
270	${_MKTARGET_COMPILE}
271	${COMPILE.m} ${OBJCOPTS} ${OBJCOPTS.${.IMPSRC:T}} ${.IMPSRC}
272.if defined(CTFCONVERT)
273	${CTFCONVERT} ${CTFFLAGS} ${.TARGET}
274.endif
275
276# Host-compiled C objects
277# The intermediate step is necessary for Sun CC, which objects to calling
278# object files anything but *.o
279.c.lo:
280	${_MKTARGET_COMPILE}
281	${HOST_COMPILE.c} -o ${.TARGET}.o ${COPTS.${.IMPSRC:T}} ${CPUFLAGS.${.IMPSRC:T}} ${CPPFLAGS.${.IMPSRC:T}} ${.IMPSRC}
282	mv ${.TARGET}.o ${.TARGET}
283
284# C++
285.cc.lo .cpp.lo .cxx.lo .C.lo:
286	${_MKTARGET_COMPILE}
287	${HOST_COMPILE.cc} -o ${.TARGET}.o ${COPTS.${.IMPSRC:T}} ${CPUFLAGS.${.IMPSRC:T}} ${CPPFLAGS.${.IMPSRC:T}} ${.IMPSRC}
288	mv ${.TARGET}.o ${.TARGET}
289
290# Assembly
291.s.o:
292	${_MKTARGET_COMPILE}
293	${COMPILE.s} ${COPTS.${.IMPSRC:T}} ${CPUFLAGS.${.IMPSRC:T}} ${CPPFLAGS.${.IMPSRC:T}} ${.IMPSRC}
294.if defined(CTFCONVERT)
295	${CTFCONVERT} ${CTFFLAGS} ${.TARGET}
296.endif
297
298.S.o:
299	${_MKTARGET_COMPILE}
300	${COMPILE.S} ${COPTS.${.IMPSRC:T}} ${CPUFLAGS.${.IMPSRC:T}} ${CPPFLAGS.${.IMPSRC:T}} ${.IMPSRC}
301.if defined(CTFCONVERT)
302	${CTFCONVERT} ${CTFFLAGS} ${.TARGET}
303.endif
304
305# Lex
306LFLAGS+=	${LPREFIX.${.IMPSRC:T}:D-P${LPREFIX.${.IMPSRC:T}}}
307LFLAGS+=	${LPREFIX:D-P${LPREFIX}}
308
309.l.c:
310	${_MKTARGET_LEX}
311	${LEX.l} -o${.TARGET} ${.IMPSRC}
312
313# Yacc
314YFLAGS+=	${YPREFIX.${.IMPSRC:T}:D-p${YPREFIX.${.IMPSRC:T}}} ${YHEADER.${.IMPSRC:T}:D-d}
315YFLAGS+=	${YPREFIX:D-p${YPREFIX}} ${YHEADER:D-d}
316
317.y.c:
318	${_MKTARGET_YACC}
319	${YACC.y} -o ${.TARGET} ${.IMPSRC}
320
321.ifdef YHEADER
322.if empty(.MAKEFLAGS:M-n)
323.y.h: ${.TARGET:.h=.c}
324.endif
325.endif
326
327# Objcopy
328.if ${MACHINE_ARCH} == aarch64eb
329# AARCH64 big endian needs to preserve $x/$d symbols for the linker.
330OBJCOPYLIBFLAGS_EXTRA=-w -K '[$$][dx]' -K '[$$][dx]\.*'
331.elif ${MACHINE_CPU} == "arm"
332# ARM big endian needs to preserve $a/$d/$t symbols for the linker.
333OBJCOPYLIBFLAGS_EXTRA=-w -K '[$$][adt]' -K '[$$][adt]\.*'
334.endif
335
336.if ${MKSTRIPSYM:Uyes} == "yes"
337OBJCOPYLIBFLAGS?=${"${.TARGET:M*.po}" != "":?-X:-x} ${OBJCOPYLIBFLAGS_EXTRA}
338.else
339OBJCOPYLIBFLAGS?=-X ${OBJCOPYLIBFLAGS_EXTRA}
340.endif
341
342.endif	# !defined(_BSD_SYS_MK_)
343