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