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