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