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