1# - Add flags to compile with extra warnings
2#
3#  enable_extra_compiler_warnings(<targetname>)
4#  globally_enable_extra_compiler_warnings() - to modify CMAKE_CXX_FLAGS, etc
5#    to change for all targets declared after the command, instead of per-command
6#
7#
8# Original Author:
9# 2010 Ryan Pavlik <rpavlik@iastate.edu> <abiryan@ryand.net>
10# http://academic.cleardefinition.com
11# Iowa State University HCI Graduate Program/VRAC
12#
13# Copyright Iowa State University 2009-2010.
14# Distributed under the Boost Software License, Version 1.0.
15# (See accompanying file LICENSE_1_0.txt or copy at
16# http://www.boost.org/LICENSE_1_0.txt)
17
18if(__enable_extra_compiler_warnings)
19	return()
20endif()
21set(__enable_extra_compiler_warnings YES)
22
23macro(_enable_extra_compiler_warnings_flags)
24	set(_flags)
25	if(MSVC)
26		option(COMPILER_WARNINGS_EXTREME
27			"Use compiler warnings that are probably overkill."
28			off)
29		mark_as_advanced(COMPILER_WARNINGS_EXTREME)
30		set(_flags "/W4")
31		if(COMPILER_WARNINGS_EXTREME)
32			set(_flags "${_flags} /Wall /wd4619 /wd4668 /wd4820 /wd4571 /wd4710")
33		endif()
34	else()
35		include(CheckCXXCompilerFlag)
36		set(_flags)
37
38		check_cxx_compiler_flag(-W SUPPORTS_W_FLAG)
39		if(SUPPORTS_W_FLAG)
40			set(_flags "${_flags} -W")
41		endif()
42
43		check_cxx_compiler_flag(-Wall SUPPORTS_WALL_FLAG)
44		if(SUPPORTS_WALL_FLAG)
45			set(_flags "${_flags} -Wall")
46		endif()
47
48		check_cxx_compiler_flag(-Wextra SUPPORTS_WEXTRA_FLAG)
49		if(SUPPORTS_WEXTRA_FLAG)
50			set(_flags "${_flags} -Wextra")
51		endif()
52
53		if(SUPPORTS_WALL_FLAG)
54			# At least GCC includes -Wmaybe-uninitialized in -Wall, which
55			# unneccesarily whines about boost::optional (by it's nature
56			# it's a "maybe" warning - prone to noisy false-positives)
57			check_cxx_compiler_flag(-Wno-maybe-uninitialized SUPPORTS_WNO_MAYBE_UNINITIALIZED_FLAG)
58			if(SUPPORTS_WNO_MAYBE_UNINITIALIZED_FLAG)
59				set(_flags "${_flags} -Wno-maybe-uninitialized")
60			endif()
61		endif()
62	endif()
63endmacro()
64
65function(enable_extra_compiler_warnings _target)
66	_enable_extra_compiler_warnings_flags()
67	get_target_property(_origflags ${_target} COMPILE_FLAGS)
68	if(_origflags)
69		set_property(TARGET
70			${_target}
71			PROPERTY
72			COMPILE_FLAGS
73			"${_flags} ${_origflags}")
74	else()
75		set_property(TARGET
76			${_target}
77			PROPERTY
78			COMPILE_FLAGS
79			"${_flags}")
80	endif()
81
82endfunction()
83
84function(globally_enable_extra_compiler_warnings)
85	_enable_extra_compiler_warnings_flags()
86	set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${_flags}" PARENT_SCOPE)
87	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${_flags}" PARENT_SCOPE)
88endfunction()
89