xref: /freebsd/contrib/bmake/mk/warnings.mk (revision 0b3105a37d7adcadcb720112fed4dc4e8040be99)
1# RCSid:
2#	$Id: warnings.mk,v 1.8 2014/04/02 19:20:23 sjg Exp $
3#
4#	@(#) Copyright (c) 2002, Simon J. Gerraty
5#
6#	This file is provided in the hope that it will
7#	be of use.  There is absolutely NO WARRANTY.
8#	Permission to copy, redistribute or otherwise
9#	use this file is hereby granted provided that
10#	the above copyright notice and this notice are
11#	left intact.
12#
13#	Please send copies of changes and bug-fixes to:
14#	sjg@crufty.net
15#
16
17.ifndef _w_cflags
18
19# Any number of warnings sets can be added.
20.-include "warnings-sets.mk"
21
22# Modest defaults - put more elaborate sets in warnings-sets.mk
23# -Wunused  etc are here so you can set
24# W_unused=-Wno-unused etc.
25MIN_WARNINGS?= -Wall \
26	-Wformat \
27	-Wimplicit \
28	-Wunused \
29	-Wuninitialized
30
31LOW_WARNINGS?= ${MIN_WARNINGS} -W -Wstrict-prototypes -Wmissing-prototypes
32
33MEDIUM_WARNINGS?= ${LOW_WARNINGS} -Werror
34
35HIGH_WARNINGS?= ${MEDIUM_WARNINGS} \
36	-Wcast-align \
37	-Wcast-qual \
38	-Wparentheses \
39	-Wpointer-arith \
40	-Wmissing-declarations \
41	-Wreturn-type \
42	-Wswitch \
43	-Wwrite-strings
44
45EXTRA_WARNINGS?= ${HIGH_WARNINGS} -Wextra
46
47# The two step default makes it easier to test build with different defaults.
48DEFAULT_WARNINGS_SET?= MIN
49WARNINGS_SET?= ${DEFAULT_WARNINGS_SET}
50
51# If you add sets, besure to list them (you don't have to touch this list).
52ALL_WARNINGS_SETS+= MIN LOW MEDIUM HIGH EXTRA
53
54.if !empty(WARNINGS_SET)
55.for ws in ${WARNINGS_SET}
56.if empty(${ws}_WARNINGS)
57.if ${MAKE_VERSION:[1]:C/.*-//} >= 20050530
58.BEGIN:	_empty_warnings
59_empty_warnings: .PHONY
60.else
61.BEGIN:
62.endif
63	@echo "ERROR: Invalid: WARNINGS_SET=${ws}"
64	@echo "ERROR: Try one of: ${ALL_WARNINGS_SETS:O:u}"; exit 1
65
66.endif
67.endfor
68.endif
69
70# Without -O or if we've set -O0 somewhere - to make debugging more effective,
71# we need to turn off -Wuninitialized as otherwise we get a warning that
72# -Werror turns into an error.  To be safe, set W_uninitialized blank.
73_w_cflags:= ${CFLAGS} ${CPPFLAGS}
74.if ${_w_cflags:M-O*} == "" || ${_w_cflags:M-O0} != ""
75W_uninitialized=
76.endif
77
78
79# .for loops have the [dis]advantage of being evaluated when read,
80# so adding to WARNINGS_SET[_${MACHINE_ARCH}] after this file is
81# read has no effect.
82# Replacing the above .for loops with the WARNINGS+= below solves that
83# but tiggers a double free bug in bmake-20040118 and earlier.
84# Don't try and read this too fast!
85#
86# The first :@ "loop" handles multiple sets in WARNINGS_SET
87#
88# In the second :@ "loop", the ::?= noise sets W_foo?=-Wfoo etc
89# which makes it easy to turn off override individual flags
90# (see W_uninitialized above).
91#
92# The last bit expands to ${W_foo_${.TARGET:T}:U${W_foo}}
93# which is the bit we ultimately want.  It allows W_* to be set on a
94# per target basis.
95#
96# NOTE: that we force the target extension to be .o
97#
98
99# define this once, we use it a couple of times below (hence the doubled $$).
100M_warnings_list = @s@$${$$s_WARNINGS}@:O:u:@w@$${$${w:C/-(.)/\1_/}::?=$$w} $${$${w:C/-(.)/\1_/}_${MACHINE_ARCH}_${.TARGET:T:R}.o:U$${$${w:C/-(.)/\1_/}_${.TARGET:T:R}.o:U$${$${w:C/-(.)/\1_/}_${MACHINE_ARCH}:U$${$${w:C/-(.)/\1_/}}}}}@
101
102# first a list of warnings from the chosen set
103_warnings = ${WARNINGS_SET_${MACHINE_ARCH}:U${WARNINGS_SET}:${M_warnings_list}}
104# now a list of all -Wno-* overrides not just those defined by WARNINGS_SET
105# since things like -Wall imply lots of others.
106# this should be a super-set of the -Wno-* in _warnings, but
107# just in case...
108_no_warnings = ${_warnings:M-Wno-*} ${ALL_WARNINGS_SETS:${M_warnings_list}:M-Wno-*}
109# -Wno-* must follow any others
110WARNINGS += ${_warnings:N-Wno-*} ${_no_warnings:O:u}
111
112.ifndef NO_CFLAGS_WARNINGS
113# Just ${WARNINGS} should do, but this is more flexible?
114CFLAGS+= ${WARNINGS_${.TARGET:T:R}.o:U${WARNINGS}}
115.endif
116
117# it is rather silly that g++ blows up on some warning flags
118NO_CXX_WARNINGS+= \
119	missing-declarations \
120	missing-prototypes \
121	nested-externs \
122	shadow \
123	strict-prototypes
124
125.for s in ${SRCS:M*.c*:N*.c:N*h}
126.for w in ${NO_CXX_WARNINGS}
127W_$w_${s:T:R}.o=
128.endfor
129.endfor
130
131.endif # _w_cflags
132