xref: /netbsd-src/share/mk/bsd.sys.mk (revision b8ecfcfef0e343ad71faea7a54fb5fcb42ad4e27)
1#	$NetBSD: bsd.sys.mk,v 1.245 2014/09/03 19:22:53 matt 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" || \
120	  ${MACHINE_CPU} == "or1k" )
121COPTS+=	-Wno-error=stack-protector
122.endif
123
124COPTS+=	${${ACTIVE_CC} == "clang":? --param ssp-buffer-size=1 :}
125COPTS+=	${${ACTIVE_CC} == "gcc":? --param ssp-buffer-size=1 :}
126.endif
127
128.if ${MKSOFTFLOAT:Uno} != "no"
129COPTS+=		${${ACTIVE_CC} == "gcc":? -msoft-float :}
130FOPTS+=		-msoft-float
131.elif ${MACHINE_ARCH} == "coldfire"
132COPTS+=		-mhard-float
133FOPTS+=		-mhard-float
134.endif
135
136.if ${MKIEEEFP:Uno} != "no"
137.if ${MACHINE_ARCH} == "alpha"
138CFLAGS+=	-mieee
139FFLAGS+=	-mieee
140.endif
141.endif
142
143.if ${MACHINE} == "sparc64" && ${MACHINE_ARCH} == "sparc"
144CFLAGS+=	-Wa,-Av8plus
145.endif
146
147.if !defined(NOGCCERROR)
148.if (${MACHINE_ARCH} == "mips64el") || (${MACHINE_ARCH} == "mips64eb")
149CPUFLAGS+=	-Wa,--fatal-warnings
150.endif
151.endif
152
153#.if ${MACHINE} == "sbmips"
154#CFLAGS+=	-mips64 -mtune=sb1
155#.endif
156
157#.if (${MACHINE_ARCH} == "mips64el" || ${MACHINE_ARCH} == "mips64eb") && \
158#    (defined(MKPIC) && ${MKPIC} == "no")
159#CPUFLAGS+=	-mno-abicalls -fno-PIC
160#.endif
161CFLAGS+=	${CPUFLAGS}
162AFLAGS+=	${CPUFLAGS}
163
164.if !defined(LDSTATIC) || ${LDSTATIC} != "-static"
165# Position Independent Executable flags
166PIE_CFLAGS?=        -fPIC
167PIE_LDFLAGS?=       -Wl,-pie ${${ACTIVE_CC} == "gcc":? -shared-libgcc :}
168PIE_AFLAGS?=	    -fPIC
169.endif
170
171ELF2ECOFF?=	elf2ecoff
172MKDEP?=		mkdep
173MKDEPCXX?=	mkdep
174OBJCOPY?=	objcopy
175OBJDUMP?=	objdump
176PAXCTL?=	paxctl
177STRIP?=		strip
178
179.SUFFIXES:	.o .ln .lo .c .cc .cpp .cxx .C .m ${YHEADER:D.h}
180
181# C
182.c.o:
183	${_MKTARGET_COMPILE}
184	${COMPILE.c} ${COPTS.${.IMPSRC:T}} ${CPUFLAGS.${.IMPSRC:T}} ${CPPFLAGS.${.IMPSRC:T}} ${.IMPSRC}
185.if defined(CTFCONVERT)
186	${CTFCONVERT} ${CTFFLAGS} ${.TARGET}
187.endif
188
189.c.ln:
190	${_MKTARGET_COMPILE}
191	${LINT} ${LINTFLAGS} ${LINTFLAGS.${.IMPSRC:T}} \
192	    ${CPPFLAGS:C/-([IDU])[  ]*/-\1/Wg:M-[IDU]*} \
193	    ${CPPFLAGS.${.IMPSRC:T}:C/-([IDU])[  ]*/-\1/Wg:M-[IDU]*} \
194	    -i ${.IMPSRC}
195
196# C++
197.cc.o .cpp.o .cxx.o .C.o:
198	${_MKTARGET_COMPILE}
199	${COMPILE.cc} ${COPTS.${.IMPSRC:T}} ${CPUFLAGS.${.IMPSRC:T}} ${CPPFLAGS.${.IMPSRC:T}} ${.IMPSRC}
200
201# Objective C
202# (Defined here rather than in <sys.mk> because `.m' is not just
203#  used for Objective C source)
204.m.o:
205	${_MKTARGET_COMPILE}
206	${COMPILE.m} ${OBJCOPTS} ${OBJCOPTS.${.IMPSRC:T}} ${.IMPSRC}
207.if defined(CTFCONVERT)
208	${CTFCONVERT} ${CTFFLAGS} ${.TARGET}
209.endif
210
211# Host-compiled C objects
212# The intermediate step is necessary for Sun CC, which objects to calling
213# object files anything but *.o
214.c.lo:
215	${_MKTARGET_COMPILE}
216	${HOST_COMPILE.c} -o ${.TARGET}.o ${COPTS.${.IMPSRC:T}} ${CPUFLAGS.${.IMPSRC:T}} ${CPPFLAGS.${.IMPSRC:T}} ${.IMPSRC}
217	mv ${.TARGET}.o ${.TARGET}
218
219# C++
220.cc.lo .cpp.lo .cxx.lo .C.lo:
221	${_MKTARGET_COMPILE}
222	${HOST_COMPILE.cc} -o ${.TARGET}.o ${COPTS.${.IMPSRC:T}} ${CPUFLAGS.${.IMPSRC:T}} ${CPPFLAGS.${.IMPSRC:T}} ${.IMPSRC}
223	mv ${.TARGET}.o ${.TARGET}
224
225# Assembly
226.s.o:
227	${_MKTARGET_COMPILE}
228	${COMPILE.s} ${COPTS.${.IMPSRC:T}} ${CPUFLAGS.${.IMPSRC:T}} ${CPPFLAGS.${.IMPSRC:T}} ${.IMPSRC}
229.if defined(CTFCONVERT)
230	${CTFCONVERT} ${CTFFLAGS} ${.TARGET}
231.endif
232
233.S.o:
234	${_MKTARGET_COMPILE}
235	${COMPILE.S} ${COPTS.${.IMPSRC:T}} ${CPUFLAGS.${.IMPSRC:T}} ${CPPFLAGS.${.IMPSRC:T}} ${.IMPSRC}
236.if defined(CTFCONVERT)
237	${CTFCONVERT} ${CTFFLAGS} ${.TARGET}
238.endif
239
240# Lex
241LFLAGS+=	${LPREFIX.${.IMPSRC:T}:D-P${LPREFIX.${.IMPSRC:T}}}
242LFLAGS+=	${LPREFIX:D-P${LPREFIX}}
243
244.l.c:
245	${_MKTARGET_LEX}
246	${LEX.l} -o${.TARGET} ${.IMPSRC}
247
248# Yacc
249YFLAGS+=	${YPREFIX.${.IMPSRC:T}:D-p${YPREFIX.${.IMPSRC:T}}} ${YHEADER.${.IMPSRC:T}:D-d}
250YFLAGS+=	${YPREFIX:D-p${YPREFIX}} ${YHEADER:D-d}
251
252.y.c:
253	${_MKTARGET_YACC}
254	${YACC.y} -o ${.TARGET} ${.IMPSRC}
255
256.ifdef YHEADER
257.if empty(.MAKEFLAGS:M-n)
258.y.h: ${.TARGET:.h=.c}
259.endif
260.endif
261
262# Objcopy
263.if ${MACHINE_ARCH} == aarch64eb
264# AARCH64 big endian needs to preserve $x/$d symbols for the linker.
265OBJCOPYLIBFLAGS_EXTRA=-w -K '[$$][dx]' -K '[$$][dx]\.*'
266.elif !empty(MACHINE_ARCH:M*arm*eb)
267# ARM big endian needs to preserve $a/$d/$t symbols for the linker.
268OBJCOPYLIBFLAGS_EXTRA=-w -K '[$$][adt]' -K '[$$][adt]\.*'
269.endif
270OBJCOPYLIBFLAGS?=${"${.TARGET:M*.po}" != "":?-X:-x} ${OBJCOPYLIBFLAGS_EXTRA}
271
272.endif	# !defined(_BSD_SYS_MK_)
273