xref: /minix3/share/mk/bsd.sys.mk (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
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 defined(__MINIX)
18#LSC: Be a bit smarter about the default compiler
19.if exists(/usr/pkg/bin/clang) || exists(/usr/bin/clang)
20CC?=	clang
21.endif
22
23.if exists(/usr/pkg/bin/gcc) || exists(/usr/bin/gcc)
24CC?=	gcc
25.endif
26
27MKDEP?= CC=${CC:Q} mkdep
28MKDEPCXX?= CC=${CXX:Q} mkdep
29.endif # defined(__MINIX)
30
31.if ${MKREPRO:Uno} == "yes"
32CPPFLAGS+=	-Wp,-iremap,${NETBSDSRCDIR}:/usr/src
33CPPFLAGS+=	-Wp,-iremap,${DESTDIR}/:/
34CPPFLAGS+=	-Wp,-iremap,${X11SRCDIR}:/usr/xsrc
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) && !defined(USE_BITCODE)
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_LLVM) && !defined(USE_BITCODE)
66LDFLAGS+=	-Wl,--fatal-warnings
67. endif
68.endif
69.endif
70.if ${WARNS} > 1
71CFLAGS+=	-Wreturn-type -Wswitch -Wshadow
72.endif
73.if ${WARNS} > 2
74CFLAGS+=	-Wcast-qual -Wwrite-strings
75CFLAGS+=	-Wextra -Wno-unused-parameter
76# Readd -Wno-sign-compare to override -Wextra with clang
77CFLAGS+=	-Wno-sign-compare
78CXXFLAGS+=	-Wabi
79CXXFLAGS+=	-Wold-style-cast
80CXXFLAGS+=	-Wctor-dtor-privacy -Wnon-virtual-dtor -Wreorder \
81		-Wno-deprecated -Woverloaded-virtual -Wsign-promo -Wsynth
82CXXFLAGS+=	${${ACTIVE_CXX} == "gcc":? -Wno-non-template-friend -Wno-pmf-conversions :}
83.endif
84.if ${WARNS} > 3 && (defined(HAVE_GCC) || defined(HAVE_LLVM))
85.if ${WARNS} > 4
86CFLAGS+=	-Wold-style-definition
87.endif
88.if ${WARNS} > 5 && !(defined(HAVE_GCC) && ${HAVE_GCC} <= 45)
89CFLAGS+=	-Wconversion
90.endif
91CFLAGS+=	-Wsign-compare -Wformat=2
92CFLAGS+=	${${ACTIVE_CC} == "gcc":? -Wno-format-zero-length :}
93.endif
94.if ${WARNS} > 3 && defined(HAVE_LLVM)
95CFLAGS+=	${${ACTIVE_CC} == "clang":? -Wpointer-sign -Wmissing-noreturn :}
96.endif
97.if (defined(HAVE_GCC) \
98     && (${MACHINE_ARCH} == "coldfire" || \
99	 ${MACHINE_ARCH} == "sh3eb" || \
100	 ${MACHINE_ARCH} == "sh3el" || \
101	 ${MACHINE_ARCH} == "m68k" || \
102	 ${MACHINE_ARCH} == "m68000"))
103# XXX GCC 4.5 for sh3 and m68k (which we compile with -Os) is extra noisy for
104# cases it should be better with
105CFLAGS+=	-Wno-uninitialized
106.if ${HAVE_GCC} >= 48
107CFLAGS+=	-Wno-maybe-uninitialized
108.endif
109.endif
110.endif
111
112CWARNFLAGS+=	${CWARNFLAGS.${ACTIVE_CC}}
113
114CPPFLAGS+=	${AUDIT:D-D__AUDIT__}
115_NOWERROR=	${defined(NOGCCERROR) || (${ACTIVE_CC} == "clang" && defined(NOCLANGERROR)):?yes:no}
116.if defined(__MINIX) && ${USE_BITCODE:Uno} == "yes"
117_NOWERROR=	yes
118.endif # defined(__MINIX) && ${USE_BITCODE} == "yes"
119CFLAGS+=	${${_NOWERROR} == "no" :?-Werror:} ${CWARNFLAGS}
120LINTFLAGS+=	${DESTDIR:D-d ${DESTDIR}/usr/include}
121
122.if (${USE_SSP:Uno} != "no") && (${BINDIR:Ux} != "/usr/mdec")
123.if !defined(KERNSRCDIR) && !defined(KERN) # not for kernels nor kern modules
124CPPFLAGS+=	-D_FORTIFY_SOURCE=2
125.endif
126COPTS+=	-fstack-protector -Wstack-protector
127
128# gcc 4.8 on m68k erroneously does not protect functions with
129# variables needing special alignement, see
130#	http://gcc.gnu.org/bugzilla/show_bug.cgi?id=59674
131# (the underlying issue for sh and vax may be different, needs more
132# investigation, symptoms are similar but for different sources)
133.if "${ACTIVE_CC}" == "gcc" && "${HAVE_GCC}" == "48" && \
134	( ${MACHINE_CPU} == "sh3" || \
135	  ${MACHINE_ARCH} == "vax" || \
136	  ${MACHINE_CPU} == "m68k" || \
137	  ${MACHINE_CPU} == "or1k" )
138COPTS+=	-Wno-error=stack-protector
139.endif
140
141COPTS+=	${${ACTIVE_CC} == "clang":? --param ssp-buffer-size=1 :}
142COPTS+=	${${ACTIVE_CC} == "gcc":? --param ssp-buffer-size=1 :}
143.endif
144
145.if ${MKSOFTFLOAT:Uno} != "no"
146COPTS+=		${${ACTIVE_CC} == "gcc":? -msoft-float :}
147FOPTS+=		-msoft-float
148.elif ${MACHINE_ARCH} == "coldfire"
149COPTS+=		-mhard-float
150FOPTS+=		-mhard-float
151.endif
152
153.if ${MKIEEEFP:Uno} != "no"
154.if ${MACHINE_ARCH} == "alpha"
155CFLAGS+=	-mieee
156FFLAGS+=	-mieee
157.endif
158.endif
159
160.if ${MACHINE} == "sparc64" && ${MACHINE_ARCH} == "sparc"
161CFLAGS+=	-Wa,-Av8plus
162.endif
163
164.if !defined(NOGCCERROR)
165.if (${MACHINE_ARCH} == "mips64el") || (${MACHINE_ARCH} == "mips64eb")
166CPUFLAGS+=	-Wa,--fatal-warnings
167.endif
168.endif
169
170#.if ${MACHINE} == "sbmips"
171#CFLAGS+=	-mips64 -mtune=sb1
172#.endif
173
174#.if (${MACHINE_ARCH} == "mips64el" || ${MACHINE_ARCH} == "mips64eb") && \
175#    (defined(MKPIC) && ${MKPIC} == "no")
176#CPUFLAGS+=	-mno-abicalls -fno-PIC
177#.endif
178CFLAGS+=	${CPUFLAGS}
179AFLAGS+=	${CPUFLAGS}
180
181.if !defined(LDSTATIC) || ${LDSTATIC} != "-static"
182# Position Independent Executable flags
183PIE_CFLAGS?=        -fPIC
184PIE_LDFLAGS?=       -Wl,-pie ${${ACTIVE_CC} == "gcc":? -shared-libgcc :}
185PIE_AFLAGS?=	    -fPIC
186.endif
187
188ELF2ECOFF?=	elf2ecoff
189MKDEP?=		mkdep
190MKDEPCXX?=	mkdep
191OBJCOPY?=	objcopy
192OBJDUMP?=	objdump
193PAXCTL?=	paxctl
194STRIP?=		strip
195
196.SUFFIXES:	.o .ln .lo .c .cc .cpp .cxx .C .m ${YHEADER:D.h}
197
198# C
199.c.o:
200	${_MKTARGET_COMPILE}
201	${COMPILE.c} ${COPTS.${.IMPSRC:T}} ${CPUFLAGS.${.IMPSRC:T}} ${CPPFLAGS.${.IMPSRC:T}} ${.IMPSRC}
202.if defined(CTFCONVERT)
203	${CTFCONVERT} ${CTFFLAGS} ${.TARGET}
204.endif
205
206.c.ln:
207	${_MKTARGET_COMPILE}
208	${LINT} ${LINTFLAGS} ${LINTFLAGS.${.IMPSRC:T}} \
209	    ${CPPFLAGS:C/-([IDU])[  ]*/-\1/Wg:M-[IDU]*} \
210	    ${CPPFLAGS.${.IMPSRC:T}:C/-([IDU])[  ]*/-\1/Wg:M-[IDU]*} \
211	    -i ${.IMPSRC}
212
213# C++
214.cc.o .cpp.o .cxx.o .C.o:
215	${_MKTARGET_COMPILE}
216	${COMPILE.cc} ${COPTS.${.IMPSRC:T}} ${CPUFLAGS.${.IMPSRC:T}} ${CPPFLAGS.${.IMPSRC:T}} ${.IMPSRC}
217
218# Objective C
219# (Defined here rather than in <sys.mk> because `.m' is not just
220#  used for Objective C source)
221.m.o:
222	${_MKTARGET_COMPILE}
223	${COMPILE.m} ${OBJCOPTS} ${OBJCOPTS.${.IMPSRC:T}} ${.IMPSRC}
224.if defined(CTFCONVERT)
225	${CTFCONVERT} ${CTFFLAGS} ${.TARGET}
226.endif
227
228# Host-compiled C objects
229# The intermediate step is necessary for Sun CC, which objects to calling
230# object files anything but *.o
231.c.lo:
232	${_MKTARGET_COMPILE}
233	${HOST_COMPILE.c} -o ${.TARGET}.o ${COPTS.${.IMPSRC:T}} ${CPUFLAGS.${.IMPSRC:T}} ${CPPFLAGS.${.IMPSRC:T}} ${.IMPSRC}
234	mv ${.TARGET}.o ${.TARGET}
235
236# C++
237.cc.lo .cpp.lo .cxx.lo .C.lo:
238	${_MKTARGET_COMPILE}
239	${HOST_COMPILE.cc} -o ${.TARGET}.o ${COPTS.${.IMPSRC:T}} ${CPUFLAGS.${.IMPSRC:T}} ${CPPFLAGS.${.IMPSRC:T}} ${.IMPSRC}
240	mv ${.TARGET}.o ${.TARGET}
241
242# Assembly
243.s.o:
244	${_MKTARGET_COMPILE}
245	${COMPILE.s} ${COPTS.${.IMPSRC:T}} ${CPUFLAGS.${.IMPSRC:T}} ${CPPFLAGS.${.IMPSRC:T}} ${.IMPSRC}
246.if defined(CTFCONVERT)
247	${CTFCONVERT} ${CTFFLAGS} ${.TARGET}
248.endif
249
250.S.o:
251	${_MKTARGET_COMPILE}
252	${COMPILE.S} ${COPTS.${.IMPSRC:T}} ${CPUFLAGS.${.IMPSRC:T}} ${CPPFLAGS.${.IMPSRC:T}} ${.IMPSRC}
253.if defined(CTFCONVERT)
254	${CTFCONVERT} ${CTFFLAGS} ${.TARGET}
255.endif
256
257# Lex
258LFLAGS+=	${LPREFIX.${.IMPSRC:T}:D-P${LPREFIX.${.IMPSRC:T}}}
259LFLAGS+=	${LPREFIX:D-P${LPREFIX}}
260
261.l.c:
262	${_MKTARGET_LEX}
263	${LEX.l} -o${.TARGET} ${.IMPSRC}
264
265# Yacc
266YFLAGS+=	${YPREFIX.${.IMPSRC:T}:D-p${YPREFIX.${.IMPSRC:T}}} ${YHEADER.${.IMPSRC:T}:D-d}
267YFLAGS+=	${YPREFIX:D-p${YPREFIX}} ${YHEADER:D-d}
268
269.y.c:
270	${_MKTARGET_YACC}
271	${YACC.y} -o ${.TARGET} ${.IMPSRC}
272
273.ifdef YHEADER
274.if empty(.MAKEFLAGS:M-n)
275.y.h: ${.TARGET:.h=.c}
276.endif
277.endif
278
279# Objcopy
280.if ${MACHINE_ARCH} == aarch64eb
281# AARCH64 big endian needs to preserve $x/$d symbols for the linker.
282OBJCOPYLIBFLAGS_EXTRA=-w -K '[$$][dx]' -K '[$$][dx]\.*'
283.elif !empty(MACHINE_ARCH:M*arm*eb)
284# ARM big endian needs to preserve $a/$d/$t symbols for the linker.
285OBJCOPYLIBFLAGS_EXTRA=-w -K '[$$][adt]' -K '[$$][adt]\.*'
286.endif
287OBJCOPYLIBFLAGS?=${"${.TARGET:M*.po}" != "":?-X:-x} ${OBJCOPYLIBFLAGS_EXTRA}
288
289.endif	# !defined(_BSD_SYS_MK_)
290