xref: /netbsd-src/share/mk/bsd.sys.mk (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1#	$NetBSD: bsd.sys.mk,v 1.240 2014/06/13 01:17:45 mrg 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.if ${MKREPRO:Uno} == "yes"
18CPPFLAGS+=	-Wp,-iremap,${NETBSDSRCDIR}:/usr/src
19CPPFLAGS+=	-Wp,-iremap,${DESTDIR}/:/
20CPPFLAGS+=	-Wp,-iremap,${X11SRCDIR}:/usr/xsrc
21.endif
22
23# NetBSD sources use C99 style, with some GCC extensions.
24CFLAGS+=	${${ACTIVE_CC} == "clang":? -std=gnu99 :}
25CFLAGS+=	${${ACTIVE_CC} == "gcc":? -std=gnu99 :}
26CFLAGS+=	${${ACTIVE_CC} == "pcc":? -std=gnu99 :}
27
28.if defined(WARNS)
29CFLAGS+=	${${ACTIVE_CC} == "clang":? -Wno-sign-compare -Wno-pointer-sign :}
30.if ${WARNS} > 0
31CFLAGS+=	-Wall -Wstrict-prototypes -Wmissing-prototypes -Wpointer-arith
32#CFLAGS+=	-Wmissing-declarations -Wredundant-decls -Wnested-externs
33# Add -Wno-sign-compare.  -Wsign-compare is included in -Wall as of GCC 3.3,
34# but our sources aren't up for it yet. Also, add -Wno-traditional because
35# gcc includes #elif in the warnings, which is 'this code will not compile
36# in a traditional environment' warning, as opposed to 'this code behaves
37# differently in traditional and ansi environments' which is the warning
38# we wanted, and now we don't get anymore.
39CFLAGS+=	-Wno-sign-compare
40CFLAGS+=	${${ACTIVE_CC} == "gcc" :? -Wno-traditional :}
41.if !defined(NOGCCERROR)
42# Set assembler warnings to be fatal
43CFLAGS+=	-Wa,--fatal-warnings
44.endif
45# Set linker warnings to be fatal
46# XXX no proper way to avoid "FOO is a patented algorithm" warnings
47# XXX on linking static libs
48.if (!defined(MKPIC) || ${MKPIC} != "no") && \
49    (!defined(LDSTATIC) || ${LDSTATIC} != "-static")
50# XXX there are some strange problems not yet resolved
51. if !defined(HAVE_GCC) || defined(HAVE_LLVM)
52LDFLAGS+=	-Wl,--fatal-warnings
53. endif
54.endif
55.endif
56.if ${WARNS} > 1
57CFLAGS+=	-Wreturn-type -Wswitch -Wshadow
58.endif
59.if ${WARNS} > 2
60CFLAGS+=	-Wcast-qual -Wwrite-strings
61CFLAGS+=	-Wextra -Wno-unused-parameter
62# Readd -Wno-sign-compare to override -Wextra with clang
63CFLAGS+=	-Wno-sign-compare
64CXXFLAGS+=	-Wabi
65CXXFLAGS+=	-Wold-style-cast
66CXXFLAGS+=	-Wctor-dtor-privacy -Wnon-virtual-dtor -Wreorder \
67		-Wno-deprecated -Woverloaded-virtual -Wsign-promo -Wsynth
68CXXFLAGS+=	${${ACTIVE_CXX} == "gcc":? -Wno-non-template-friend -Wno-pmf-conversions :}
69.endif
70.if ${WARNS} > 3 && (defined(HAVE_GCC) || defined(HAVE_LLVM))
71.if ${WARNS} > 4
72CFLAGS+=	-Wold-style-definition
73.endif
74.if ${WARNS} > 5 && !(defined(HAVE_GCC) && ${HAVE_GCC} <= 45)
75CFLAGS+=	-Wconversion
76.endif
77CFLAGS+=	-Wsign-compare -Wformat=2
78CFLAGS+=	${${ACTIVE_CC} == "gcc":? -Wno-format-zero-length :}
79.endif
80.if ${WARNS} > 3 && defined(HAVE_LLVM)
81CFLAGS+=	${${ACTIVE_CC} == "clang":? -Wpointer-sign -Wmissing-noreturn :}
82.endif
83.if (defined(HAVE_GCC) \
84     && (${MACHINE_ARCH} == "coldfire" || \
85	 ${MACHINE_ARCH} == "sh3eb" || \
86	 ${MACHINE_ARCH} == "sh3el" || \
87	 ${MACHINE_ARCH} == "m68k" || \
88	 ${MACHINE_ARCH} == "m68000"))
89# XXX GCC 4.5 for sh3 and m68k (which we compile with -Os) is extra noisy for
90# cases it should be better with
91CFLAGS+=	-Wno-uninitialized
92.if ${HAVE_GCC} >= 48
93CFLAGS+=	-Wno-maybe-uninitialized
94.endif
95.endif
96.endif
97
98CWARNFLAGS+=	${CWARNFLAGS.${ACTIVE_CC}}
99
100CPPFLAGS+=	${AUDIT:D-D__AUDIT__}
101_NOWERROR=	${defined(NOGCCERROR) || (${ACTIVE_CC} == "clang" && defined(NOCLANGERROR)):?yes:no}
102CFLAGS+=	${${_NOWERROR} == "no" :?-Werror:} ${CWARNFLAGS}
103LINTFLAGS+=	${DESTDIR:D-d ${DESTDIR}/usr/include}
104
105.if (${USE_SSP:Uno} != "no") && (${BINDIR:Ux} != "/usr/mdec")
106.if !defined(KERNSRCDIR) && !defined(KERN) # not for kernels nor kern modules
107CPPFLAGS+=	-D_FORTIFY_SOURCE=2
108.endif
109COPTS+=	-fstack-protector -Wstack-protector
110
111# gcc 4.8 on m68k erroneously does not protect functions with
112# variables needing special alignement, see
113#	http://gcc.gnu.org/bugzilla/show_bug.cgi?id=59674
114# (the underlying issue for sh and vax may be different, needs more
115# investigation, symptoms are similar but for different sources)
116.if "${ACTIVE_CC}" == "gcc" && "${HAVE_GCC}" == "48" && \
117	( ${MACHINE_CPU} == "sh3" || \
118	  ${MACHINE_ARCH} == "vax" || \
119	  ${MACHINE_CPU} == "m68k" )
120COPTS+=	-Wno-error=stack-protector
121.endif
122
123COPTS+=	${${ACTIVE_CC} == "clang":? --param ssp-buffer-size=1 :}
124COPTS+=	${${ACTIVE_CC} == "gcc":? --param ssp-buffer-size=1 :}
125.endif
126
127.if ${MKSOFTFLOAT:Uno} != "no"
128COPTS+=		-msoft-float
129FOPTS+=		-msoft-float
130.elif ${MACHINE_ARCH} == "coldfire"
131COPTS+=		-mhard-float
132FOPTS+=		-mhard-float
133.endif
134
135.if ${MKIEEEFP:Uno} != "no"
136.if ${MACHINE_ARCH} == "alpha"
137CFLAGS+=	-mieee
138FFLAGS+=	-mieee
139.endif
140.endif
141
142.if ${MACHINE} == "sparc64" && ${MACHINE_ARCH} == "sparc"
143CFLAGS+=	-Wa,-Av8plus
144.endif
145
146.if !defined(NOGCCERROR)
147.if (${MACHINE_ARCH} == "mips64el") || (${MACHINE_ARCH} == "mips64eb")
148CPUFLAGS+=	-Wa,--fatal-warnings
149.endif
150.endif
151
152#.if ${MACHINE} == "sbmips"
153#CFLAGS+=	-mips64 -mtune=sb1
154#.endif
155
156#.if (${MACHINE_ARCH} == "mips64el" || ${MACHINE_ARCH} == "mips64eb") && \
157#    (defined(MKPIC) && ${MKPIC} == "no")
158#CPUFLAGS+=	-mno-abicalls -fno-PIC
159#.endif
160CFLAGS+=	${CPUFLAGS}
161AFLAGS+=	${CPUFLAGS}
162
163.if !defined(LDSTATIC) || ${LDSTATIC} != "-static"
164# Position Independent Executable flags
165PIE_CFLAGS?=        -fPIC
166PIE_LDFLAGS?=       -Wl,-pie ${${ACTIVE_CC} == "gcc":? -shared-libgcc :}
167PIE_AFLAGS?=	    -fPIC
168.endif
169
170ELF2ECOFF?=	elf2ecoff
171MKDEP?=		mkdep
172MKDEPCXX?=	mkdep
173OBJCOPY?=	objcopy
174OBJDUMP?=	objdump
175PAXCTL?=	paxctl
176STRIP?=		strip
177
178.SUFFIXES:	.o .ln .lo .c .cc .cpp .cxx .C .m ${YHEADER:D.h}
179
180# C
181.c.o:
182	${_MKTARGET_COMPILE}
183	${COMPILE.c} ${COPTS.${.IMPSRC:T}} ${CPUFLAGS.${.IMPSRC:T}} ${CPPFLAGS.${.IMPSRC:T}} ${.IMPSRC}
184.if defined(CTFCONVERT)
185	${CTFCONVERT} ${CTFFLAGS} ${.TARGET}
186.endif
187
188.c.ln:
189	${_MKTARGET_COMPILE}
190	${LINT} ${LINTFLAGS} ${LINTFLAGS.${.IMPSRC:T}} \
191	    ${CPPFLAGS:C/-([IDU])[  ]*/-\1/Wg:M-[IDU]*} \
192	    ${CPPFLAGS.${.IMPSRC:T}:C/-([IDU])[  ]*/-\1/Wg:M-[IDU]*} \
193	    -i ${.IMPSRC}
194
195# C++
196.cc.o .cpp.o .cxx.o .C.o:
197	${_MKTARGET_COMPILE}
198	${COMPILE.cc} ${COPTS.${.IMPSRC:T}} ${CPUFLAGS.${.IMPSRC:T}} ${CPPFLAGS.${.IMPSRC:T}} ${.IMPSRC}
199
200# Objective C
201# (Defined here rather than in <sys.mk> because `.m' is not just
202#  used for Objective C source)
203.m.o:
204	${_MKTARGET_COMPILE}
205	${COMPILE.m} ${OBJCOPTS} ${OBJCOPTS.${.IMPSRC:T}} ${.IMPSRC}
206.if defined(CTFCONVERT)
207	${CTFCONVERT} ${CTFFLAGS} ${.TARGET}
208.endif
209
210# Host-compiled C objects
211# The intermediate step is necessary for Sun CC, which objects to calling
212# object files anything but *.o
213.c.lo:
214	${_MKTARGET_COMPILE}
215	${HOST_COMPILE.c} -o ${.TARGET}.o ${COPTS.${.IMPSRC:T}} ${CPUFLAGS.${.IMPSRC:T}} ${CPPFLAGS.${.IMPSRC:T}} ${.IMPSRC}
216	mv ${.TARGET}.o ${.TARGET}
217
218# C++
219.cc.lo .cpp.lo .cxx.lo .C.lo:
220	${_MKTARGET_COMPILE}
221	${HOST_COMPILE.cc} -o ${.TARGET}.o ${COPTS.${.IMPSRC:T}} ${CPUFLAGS.${.IMPSRC:T}} ${CPPFLAGS.${.IMPSRC:T}} ${.IMPSRC}
222	mv ${.TARGET}.o ${.TARGET}
223
224# Assembly
225.s.o:
226	${_MKTARGET_COMPILE}
227	${COMPILE.s} ${COPTS.${.IMPSRC:T}} ${CPUFLAGS.${.IMPSRC:T}} ${CPPFLAGS.${.IMPSRC:T}} ${.IMPSRC}
228.if defined(CTFCONVERT)
229	${CTFCONVERT} ${CTFFLAGS} ${.TARGET}
230.endif
231
232.S.o:
233	${_MKTARGET_COMPILE}
234	${COMPILE.S} ${COPTS.${.IMPSRC:T}} ${CPUFLAGS.${.IMPSRC:T}} ${CPPFLAGS.${.IMPSRC:T}} ${.IMPSRC}
235.if defined(CTFCONVERT)
236	${CTFCONVERT} ${CTFFLAGS} ${.TARGET}
237.endif
238
239# Lex
240LFLAGS+=	${LPREFIX.${.IMPSRC:T}:D-P${LPREFIX.${.IMPSRC:T}}}
241LFLAGS+=	${LPREFIX:D-P${LPREFIX}}
242
243.l.c:
244	${_MKTARGET_LEX}
245	${LEX.l} -o${.TARGET} ${.IMPSRC}
246
247# Yacc
248YFLAGS+=	${YPREFIX.${.IMPSRC:T}:D-p${YPREFIX.${.IMPSRC:T}}} ${YHEADER.${.IMPSRC:T}:D-d}
249YFLAGS+=	${YPREFIX:D-p${YPREFIX}} ${YHEADER:D-d}
250
251.y.c:
252	${_MKTARGET_YACC}
253	${YACC.y} -o ${.TARGET} ${.IMPSRC}
254
255.ifdef YHEADER
256.if empty(.MAKEFLAGS:M-n)
257.y.h: ${.TARGET:.h=.c}
258.endif
259.endif
260
261# Objcopy
262# ARM big endian needs to preserve $a/$d/$t symbols for the linker.
263OBJCOPYLIBFLAGS?=${"${.TARGET:M*.po}" != "":?-X:-x} \
264	${"${MACHINE_ARCH:M*arm*eb}" != "":?-K '\$a' -K '\$d' -K '\$t':}
265
266.endif	# !defined(_BSD_SYS_MK_)
267