11541Srgrimes# RCSid:
21541Srgrimes#	$Id: warnings.mk,v 1.14 2016/04/05 15:58:37 sjg Exp $
31541Srgrimes#
41541Srgrimes#	@(#) Copyright (c) 2002, Simon J. Gerraty
5165896Srwatson#
6165896Srwatson#	This file is provided in the hope that it will
7165896Srwatson#	be of use.  There is absolutely NO WARRANTY.
81541Srgrimes#	Permission to copy, redistribute or otherwise
91541Srgrimes#	use this file is hereby granted provided that 
101541Srgrimes#	the above copyright notice and this notice are
111541Srgrimes#	left intact. 
121541Srgrimes#      
13165896Srwatson#	Please send copies of changes and bug-fixes to:
14165896Srwatson#	sjg@crufty.net
15165896Srwatson#
16165896Srwatson
17165896Srwatson.ifndef _w_cflags
18165896Srwatson# make sure we get the behavior we expect
19165896Srwatson.MAKE.SAVE_DOLLARS = no
20165896Srwatson
21165896Srwatson# Any number of warnings sets can be added.
22165896Srwatson.-include <warnings-sets.mk>
23165896Srwatson
24165896Srwatson# Modest defaults - put more elaborate sets in warnings-sets.mk
25165896Srwatson# -Wunused  etc are here so you can set
26165896Srwatson# W_unused=-Wno-unused etc.
27165896SrwatsonMIN_WARNINGS?= -Wall \
28165896Srwatson	-Wformat \
29165896Srwatson	-Wimplicit \
30165896Srwatson	-Wunused \
31165896Srwatson	-Wuninitialized 
32165896Srwatson
33165896SrwatsonLOW_WARNINGS?= ${MIN_WARNINGS} -W -Wstrict-prototypes -Wmissing-prototypes
34165896Srwatson 
35165896SrwatsonMEDIUM_WARNINGS?= ${LOW_WARNINGS} -Werror
36165896Srwatson
37152328SrwatsonHIGH_WARNINGS?= ${MEDIUM_WARNINGS} \
38152328Srwatson	-Wcast-align \
391541Srgrimes	-Wcast-qual \
401541Srgrimes	-Wparentheses \
411541Srgrimes	-Wpointer-arith \
421541Srgrimes	-Wmissing-declarations \
431541Srgrimes	-Wreturn-type \
441541Srgrimes	-Wswitch \
451541Srgrimes	-Wwrite-strings
461541Srgrimes
471541SrgrimesEXTRA_WARNINGS?= ${HIGH_WARNINGS} -Wextra
481541Srgrimes
491541Srgrimes# The two step default makes it easier to test build with different defaults.
501541SrgrimesDEFAULT_WARNINGS_SET?= MIN
511541SrgrimesWARNINGS_SET?= ${DEFAULT_WARNINGS_SET}
521541Srgrimes
531541Srgrimes# There is always someone who wants more...
541541Srgrimes.if !empty(WARNINGS_XTRAS)
551541Srgrimes${WARNINGS_SET}_WARNINGS += ${WARNINGS_XTRAS}
561541Srgrimes.endif
571541Srgrimes
581541Srgrimes# If you add sets, besure to list them (you don't have to touch this list).
591541SrgrimesALL_WARNINGS_SETS+= MIN LOW MEDIUM HIGH EXTRA
601541Srgrimes
611541Srgrimes.if !empty(WARNINGS_SET)
621541Srgrimes.for ws in ${WARNINGS_SET}
631541Srgrimes.if empty(${ws}_WARNINGS)
641541Srgrimes.if ${MAKE_VERSION:[1]:C/.*-//} >= 20050530
651541Srgrimes.BEGIN:	_empty_warnings
661541Srgrimes_empty_warnings: .PHONY
673124Sdg.else
681541Srgrimes.BEGIN:
691541Srgrimes.endif
70116182Sobrien	@echo "ERROR: Invalid: WARNINGS_SET=${ws}"
71116182Sobrien	@echo "ERROR: Try one of: ${ALL_WARNINGS_SETS:O:u}"; exit 1
72116182Sobrien
731541Srgrimes.endif
742807Sbde.endfor
75155262Sjhb.endif
76155262Sjhb
77155262Sjhb# Without -O or if we've set -O0 somewhere - to make debugging more effective,
78155431Sjhb# we need to turn off -Wuninitialized as otherwise we get a warning that
79172023Sdds# -Werror turns into an error.  To be safe, set W_uninitialized blank.
8076166Smarkm_w_cflags= ${CFLAGS} ${CFLAGS_LAST} ${CPPFLAGS}
81155262Sjhb.if ${_w_cflags:M-O*} == "" || ${_w_cflags:M-O0} != ""
8276166SmarkmW_uninitialized=
83155262Sjhb.endif
84164033Srwatson
851541Srgrimes
86155262Sjhb# .for loops have the [dis]advantage of being evaluated when read,
87155431Sjhb# so adding to WARNINGS_SET[_${MACHINE_ARCH}] after this file is 
88152328Srwatson# read has no effect.
89155262Sjhb# Replacing the above .for loops with the WARNINGS+= below solves that
9022521Sdyson# but tiggers a double free bug in bmake-20040118 and earlier.
91155262Sjhb# Don't try and read this too fast!
92155262Sjhb#
933124Sdg# The first :@ "loop" handles multiple sets in WARNINGS_SET
94155262Sjhb#
951541Srgrimes# In the second :@ "loop", the ::?= noise sets W_foo?=-Wfoo etc
96163606Srwatson# which makes it easy to turn off override individual flags
97163606Srwatson# (see W_uninitialized above).
983124Sdg# 
993124Sdg# The last bit expands to ${W_foo_${.TARGET:T}:U${W_foo}}
1003124Sdg# which is the bit we ultimately want.  It allows W_* to be set on a
1013124Sdg# per target basis.
1023124Sdg# 
103169857Sdds# NOTE: that we force the target extension to be .o
104169857Sdds#
105169857Sdds
1063124Sdg# define this once, we use it a couple of times below (hence the doubled $$).
1073124SdgM_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_/}}}}}@
1083124Sdg
1093124Sdg# first a list of warnings from the chosen set
1103124Sdg_warnings = ${WARNINGS_SET_${MACHINE_ARCH}:U${WARNINGS_SET}:${M_warnings_list}}
1113124Sdg# now a list of all -Wno-* overrides not just those defined by WARNINGS_SET
112169857Sdds# since things like -Wall imply lots of others.
113169857Sdds# this should be a super-set of the -Wno-* in _warnings, but 
114169857Sdds# just in case...
115169857Sdds_no_warnings = ${_warnings:M-Wno-*} ${ALL_WARNINGS_SETS:${M_warnings_list}:M-Wno-*}
1163124Sdg# -Wno-* must follow any others
1173124SdgWARNINGS += ${_warnings:N-Wno-*} ${_no_warnings:O:u}
1183124Sdg
1193124Sdg.ifndef NO_CFLAGS_WARNINGS
1203124Sdg# Just ${WARNINGS} should do, but this is more flexible?
121169857SddsCFLAGS+= ${WARNINGS_${.TARGET:T:R}.o:U${WARNINGS}}
122169857Sdds.endif
123155431Sjhb
124155431Sjhb# it is rather silly that g++ blows up on some warning flags
125234927SjhbNO_CXX_WARNINGS+= \
1263124Sdg	implicit \
1273124Sdg	missing-declarations \
128100444Sjohan	missing-prototypes \
129152328Srwatson	nested-externs \
130152328Srwatson	shadow \
1313124Sdg	strict-prototypes
132162370Srwatson
133152328Srwatson.for s in ${SRCS:M*.c*:N*.c:N*h}
134152328Srwatson.for w in ${NO_CXX_WARNINGS}
135152328SrwatsonW_$w_${s:T:R}.o=
136252422Smjg.endfor
137152328Srwatson.endfor
138152328Srwatson
1393124Sdg.endif # _w_cflags
140152328Srwatson