11cb0ef41Sopenharmony_ci# Copyright (c) Monetra Technologies LLC
21cb0ef41Sopenharmony_ci# SPDX-License-Identifier: MIT
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ci# EnableWarnings.cmake
51cb0ef41Sopenharmony_ci#
61cb0ef41Sopenharmony_ci# Checks for and turns on a large number of warning C flags.
71cb0ef41Sopenharmony_ci#
81cb0ef41Sopenharmony_ci# Adds the following helper functions:
91cb0ef41Sopenharmony_ci#
101cb0ef41Sopenharmony_ci#	remove_warnings(... list of warnings ...)
111cb0ef41Sopenharmony_ci#		Turn off given list of individual warnings for all targets and subdirectories added after this.
121cb0ef41Sopenharmony_ci#
131cb0ef41Sopenharmony_ci#   remove_all_warnings()
141cb0ef41Sopenharmony_ci#		Remove all warning flags, add -w to suppress built-in warnings.
151cb0ef41Sopenharmony_ci#
161cb0ef41Sopenharmony_ci#   remove_all_warnings_from_targets(... list of targets ...)
171cb0ef41Sopenharmony_ci#       Suppress warnings for the given targets only.
181cb0ef41Sopenharmony_ci#
191cb0ef41Sopenharmony_ci#   push_warnings()
201cb0ef41Sopenharmony_ci#		Save current warning flags by pushing them onto an internal stack. Note that modifications to the internal
211cb0ef41Sopenharmony_ci#		stack are only visible in the current CMakeLists.txt file and its children.
221cb0ef41Sopenharmony_ci#
231cb0ef41Sopenharmony_ci#       Note: changing warning flags multiple times in the same directory only affects add_subdirectory() calls.
241cb0ef41Sopenharmony_ci#             Targets in the directory will always use the warning flags in effect at the end of the CMakeLists.txt
251cb0ef41Sopenharmony_ci#             file - this is due to really weird and annoying legacy behavior of CMAKE_C_FLAGS.
261cb0ef41Sopenharmony_ci#
271cb0ef41Sopenharmony_ci#   pop_warnings()
281cb0ef41Sopenharmony_ci#       Restore the last set of flags that were saved with push_warnings(). Note that modifications to the internal
291cb0ef41Sopenharmony_ci#		stack are only visible in the current CMakeLists.txt file and its children.
301cb0ef41Sopenharmony_ci#
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ciif (_internal_enable_warnings_already_run)
331cb0ef41Sopenharmony_ci	return()
341cb0ef41Sopenharmony_ciendif ()
351cb0ef41Sopenharmony_ciset(_internal_enable_warnings_already_run TRUE)
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ciinclude(CheckCCompilerFlag)
381cb0ef41Sopenharmony_ciinclude(CheckCXXCompilerFlag)
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ciget_property(languages GLOBAL PROPERTY ENABLED_LANGUAGES)
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci# internal helper: _int_enable_warnings_set_flags_ex(langs_var configs_var [warnings flags])
431cb0ef41Sopenharmony_cifunction(_int_enable_warnings_set_flags_ex langs_var configs_var)
441cb0ef41Sopenharmony_ci	if (NOT ARGN)
451cb0ef41Sopenharmony_ci		return()
461cb0ef41Sopenharmony_ci	endif ()
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci	if (NOT ${configs_var})
491cb0ef41Sopenharmony_ci		set(${configs_var} "NONE")
501cb0ef41Sopenharmony_ci	endif ()
511cb0ef41Sopenharmony_ci	string(TOUPPER "${${configs_var}}" ${configs_var})
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci	foreach(_flag ${ARGN})
541cb0ef41Sopenharmony_ci		string(MAKE_C_IDENTIFIER "HAVE_${_flag}" varname)
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ci		if ("C" IN_LIST ${langs_var})
571cb0ef41Sopenharmony_ci			check_c_compiler_flag(${_flag} ${varname})
581cb0ef41Sopenharmony_ci			if (${varname})
591cb0ef41Sopenharmony_ci				foreach (config IN LISTS ${configs_var})
601cb0ef41Sopenharmony_ci					if (config STREQUAL "NONE")
611cb0ef41Sopenharmony_ci						set(config)
621cb0ef41Sopenharmony_ci					else ()
631cb0ef41Sopenharmony_ci						set(config "_${config}")
641cb0ef41Sopenharmony_ci					endif ()
651cb0ef41Sopenharmony_ci					string(APPEND CMAKE_C_FLAGS${config} " ${_flag}")
661cb0ef41Sopenharmony_ci				endforeach ()
671cb0ef41Sopenharmony_ci			endif ()
681cb0ef41Sopenharmony_ci		endif ()
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_ci		if ("CXX" IN_LIST ${langs_var})
711cb0ef41Sopenharmony_ci			string(APPEND varname "_CXX")
721cb0ef41Sopenharmony_ci			check_cxx_compiler_flag(${_flag} ${varname})
731cb0ef41Sopenharmony_ci			if (${varname})
741cb0ef41Sopenharmony_ci				foreach (config IN LISTS ${configs_var})
751cb0ef41Sopenharmony_ci					if (config STREQUAL "NONE")
761cb0ef41Sopenharmony_ci						set(config)
771cb0ef41Sopenharmony_ci					else ()
781cb0ef41Sopenharmony_ci						set(config "_${config}")
791cb0ef41Sopenharmony_ci					endif ()
801cb0ef41Sopenharmony_ci					string(APPEND CMAKE_CXX_FLAGS${config} " ${_flag}")
811cb0ef41Sopenharmony_ci				endforeach ()
821cb0ef41Sopenharmony_ci			endif ()
831cb0ef41Sopenharmony_ci		endif ()
841cb0ef41Sopenharmony_ci	endforeach()
851cb0ef41Sopenharmony_ci
861cb0ef41Sopenharmony_ci	foreach(lang C CXX)
871cb0ef41Sopenharmony_ci		foreach (config IN LISTS ${configs_var})
881cb0ef41Sopenharmony_ci			string(TOUPPER "${config}" config)
891cb0ef41Sopenharmony_ci			if (config STREQUAL "NONE")
901cb0ef41Sopenharmony_ci				set(config)
911cb0ef41Sopenharmony_ci			else ()
921cb0ef41Sopenharmony_ci				set(config "_${config}")
931cb0ef41Sopenharmony_ci			endif ()
941cb0ef41Sopenharmony_ci			string(STRIP "${CMAKE_${lang}_FLAGS${config}}" CMAKE_${lang}_FLAGS${config})
951cb0ef41Sopenharmony_ci			set(CMAKE_${lang}_FLAGS${config} "${CMAKE_${lang}_FLAGS${config}}" PARENT_SCOPE)
961cb0ef41Sopenharmony_ci		endforeach ()
971cb0ef41Sopenharmony_ci	endforeach()
981cb0ef41Sopenharmony_ciendfunction()
991cb0ef41Sopenharmony_ci
1001cb0ef41Sopenharmony_ci# internal helper: _int_enable_warnings_set_flags(langs_var [warnings flags])
1011cb0ef41Sopenharmony_cimacro(_int_enable_warnings_set_flags langs_var)
1021cb0ef41Sopenharmony_ci	set(configs "NONE")
1031cb0ef41Sopenharmony_ci	_int_enable_warnings_set_flags_ex(${langs_var} configs ${ARGN})
1041cb0ef41Sopenharmony_ciendmacro()
1051cb0ef41Sopenharmony_ci
1061cb0ef41Sopenharmony_ciset(_flags_C)
1071cb0ef41Sopenharmony_ciset(_flags_CXX)
1081cb0ef41Sopenharmony_ciset(_debug_flags_C)
1091cb0ef41Sopenharmony_ciset(_debug_flags_CXX)
1101cb0ef41Sopenharmony_ci
1111cb0ef41Sopenharmony_ciif (MSVC)
1121cb0ef41Sopenharmony_ci	# Visual Studio uses a completely different nomenclature for warnings than gcc/mingw/clang, so none of the
1131cb0ef41Sopenharmony_ci	# "-W[name]" warnings will work.
1141cb0ef41Sopenharmony_ci
1151cb0ef41Sopenharmony_ci	# W4 would be better but it produces unnecessary warnings like:
1161cb0ef41Sopenharmony_ci	# *  warning C4706: assignment within conditional expression
1171cb0ef41Sopenharmony_ci	#     Triggered when doing "while(1)"
1181cb0ef41Sopenharmony_ci	# * warning C4115: 'timeval' : named type definition in parentheses
1191cb0ef41Sopenharmony_ci	# * warning C4201: nonstandard extension used : nameless struct/union
1201cb0ef41Sopenharmony_ci	#     Triggered by system includes (commctrl.h, shtypes.h, Shlobj.h)
1211cb0ef41Sopenharmony_ci	set(_flags
1221cb0ef41Sopenharmony_ci		/W3
1231cb0ef41Sopenharmony_ci		/we4013 # Treat "function undefined, assuming extern returning int" warning as an error. https://docs.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-3-c4013
1241cb0ef41Sopenharmony_ci	)
1251cb0ef41Sopenharmony_ci
1261cb0ef41Sopenharmony_ci	list(APPEND _flags_C   ${_flags})
1271cb0ef41Sopenharmony_ci	list(APPEND _flags_CXX ${_flags})
1281cb0ef41Sopenharmony_ci
1291cb0ef41Sopenharmony_cielseif (CMAKE_C_COMPILER_ID MATCHES "Intel")
1301cb0ef41Sopenharmony_ci	# Intel's compiler warning flags are more like Visual Studio than GCC, though the numbers aren't the same.
1311cb0ef41Sopenharmony_ci	set(_flags
1321cb0ef41Sopenharmony_ci		# Use warning level 3, quite wordy.
1331cb0ef41Sopenharmony_ci		-w3
1341cb0ef41Sopenharmony_ci		# Disable warnings we don't care about (add more as they are encountered).
1351cb0ef41Sopenharmony_ci		-wd383    # Spammy warning about initializing from a temporary object in C++ (which is done all the time ...).
1361cb0ef41Sopenharmony_ci		-wd11074  # Diagnostic related to inlining.
1371cb0ef41Sopenharmony_ci		-wd11076  # Diagnostic related to inlining.
1381cb0ef41Sopenharmony_ci	)
1391cb0ef41Sopenharmony_ci
1401cb0ef41Sopenharmony_ci	list(APPEND _flags_C   ${_flags})
1411cb0ef41Sopenharmony_ci	list(APPEND _flags_CXX ${_flags})
1421cb0ef41Sopenharmony_ci
1431cb0ef41Sopenharmony_cielseif (CMAKE_C_COMPILER_ID MATCHES "XL")
1441cb0ef41Sopenharmony_ci	set (_flags
1451cb0ef41Sopenharmony_ci		-qwarn64
1461cb0ef41Sopenharmony_ci		-qformat=all
1471cb0ef41Sopenharmony_ci		-qflag=i:i
1481cb0ef41Sopenharmony_ci	)
1491cb0ef41Sopenharmony_ci	list(APPEND _flags_C   ${_flags})
1501cb0ef41Sopenharmony_ci	list(APPEND _flags_CXX ${_flags})
1511cb0ef41Sopenharmony_ci
1521cb0ef41Sopenharmony_cielse ()
1531cb0ef41Sopenharmony_ci	# If we're compiling with GCC / Clang / MinGW (or anything else besides Visual Studio or Intel):
1541cb0ef41Sopenharmony_ci	# C Flags:
1551cb0ef41Sopenharmony_ci	list(APPEND _flags_C
1561cb0ef41Sopenharmony_ci		-Wall
1571cb0ef41Sopenharmony_ci		-Wextra
1581cb0ef41Sopenharmony_ci
1591cb0ef41Sopenharmony_ci		# Enable additional warnings not covered by Wall and Wextra.
1601cb0ef41Sopenharmony_ci		-Wcast-align
1611cb0ef41Sopenharmony_ci		-Wconversion
1621cb0ef41Sopenharmony_ci		-Wdeclaration-after-statement
1631cb0ef41Sopenharmony_ci		-Wdouble-promotion
1641cb0ef41Sopenharmony_ci		-Wfloat-equal
1651cb0ef41Sopenharmony_ci		-Wformat-security
1661cb0ef41Sopenharmony_ci		-Winit-self
1671cb0ef41Sopenharmony_ci		-Wjump-misses-init
1681cb0ef41Sopenharmony_ci		-Wlogical-op
1691cb0ef41Sopenharmony_ci		-Wmissing-braces
1701cb0ef41Sopenharmony_ci		-Wmissing-declarations
1711cb0ef41Sopenharmony_ci		-Wmissing-format-attribute
1721cb0ef41Sopenharmony_ci		-Wmissing-include-dirs
1731cb0ef41Sopenharmony_ci		-Wmissing-prototypes
1741cb0ef41Sopenharmony_ci		-Wnested-externs
1751cb0ef41Sopenharmony_ci		-Wno-coverage-mismatch
1761cb0ef41Sopenharmony_ci		-Wold-style-definition
1771cb0ef41Sopenharmony_ci		-Wpacked
1781cb0ef41Sopenharmony_ci		-Wpointer-arith
1791cb0ef41Sopenharmony_ci		-Wredundant-decls
1801cb0ef41Sopenharmony_ci		-Wshadow
1811cb0ef41Sopenharmony_ci		-Wsign-conversion
1821cb0ef41Sopenharmony_ci		-Wstrict-overflow
1831cb0ef41Sopenharmony_ci		-Wstrict-prototypes
1841cb0ef41Sopenharmony_ci		-Wtrampolines
1851cb0ef41Sopenharmony_ci		-Wundef
1861cb0ef41Sopenharmony_ci		-Wunused
1871cb0ef41Sopenharmony_ci		-Wvariadic-macros
1881cb0ef41Sopenharmony_ci		-Wvla
1891cb0ef41Sopenharmony_ci		-Wwrite-strings
1901cb0ef41Sopenharmony_ci
1911cb0ef41Sopenharmony_ci		# On Windows MinGW I think implicit fallthrough enabled by -Wextra must not default to 3
1921cb0ef41Sopenharmony_ci		-Wimplicit-fallthrough=3
1931cb0ef41Sopenharmony_ci
1941cb0ef41Sopenharmony_ci		# Treat implicit variable typing and implicit function declarations as errors.
1951cb0ef41Sopenharmony_ci		-Werror=implicit-int
1961cb0ef41Sopenharmony_ci		-Werror=implicit-function-declaration
1971cb0ef41Sopenharmony_ci
1981cb0ef41Sopenharmony_ci		# Make MacOSX honor -mmacosx-version-min
1991cb0ef41Sopenharmony_ci		-Werror=partial-availability
2001cb0ef41Sopenharmony_ci
2011cb0ef41Sopenharmony_ci		# Some clang versions might warn if an argument like "-I/path/to/headers" is unused,
2021cb0ef41Sopenharmony_ci		# silence these.
2031cb0ef41Sopenharmony_ci		-Qunused-arguments
2041cb0ef41Sopenharmony_ci	)
2051cb0ef41Sopenharmony_ci
2061cb0ef41Sopenharmony_ci	# C++ flags:
2071cb0ef41Sopenharmony_ci	list(APPEND _flags_CXX
2081cb0ef41Sopenharmony_ci		-Wall
2091cb0ef41Sopenharmony_ci		-Wextra
2101cb0ef41Sopenharmony_ci
2111cb0ef41Sopenharmony_ci		# Enable additional warnings not covered by Wall and Wextra.
2121cb0ef41Sopenharmony_ci		-Wcast-align
2131cb0ef41Sopenharmony_ci		-Wformat-security
2141cb0ef41Sopenharmony_ci		-Wmissing-declarations
2151cb0ef41Sopenharmony_ci		-Wmissing-format-attribute
2161cb0ef41Sopenharmony_ci		-Wpacked-bitfield-compat
2171cb0ef41Sopenharmony_ci		-Wredundant-decls
2181cb0ef41Sopenharmony_ci		-Wvla
2191cb0ef41Sopenharmony_ci
2201cb0ef41Sopenharmony_ci		# Turn off unused parameter warnings with C++ (they happen often in C++ and Qt).
2211cb0ef41Sopenharmony_ci		-Wno-unused-parameter
2221cb0ef41Sopenharmony_ci
2231cb0ef41Sopenharmony_ci		# Some clang versions might warn if an argument like "-I/path/to/headers" is unused,
2241cb0ef41Sopenharmony_ci		# silence these.
2251cb0ef41Sopenharmony_ci		-Qunused-arguments
2261cb0ef41Sopenharmony_ci	)
2271cb0ef41Sopenharmony_ci
2281cb0ef41Sopenharmony_ci	# Note: when cross-compiling to Windows from Cygwin, the Qt Mingw packages have a bunch of
2291cb0ef41Sopenharmony_ci	#       noisy type-conversion warnings in headers. So, only enable those warnings if we're
2301cb0ef41Sopenharmony_ci	#       not building that configuration.
2311cb0ef41Sopenharmony_ci	if (NOT (WIN32 AND (CMAKE_HOST_SYSTEM_NAME MATCHES "CYGWIN")))
2321cb0ef41Sopenharmony_ci		list(APPEND _flags_CXX
2331cb0ef41Sopenharmony_ci			-Wconversion
2341cb0ef41Sopenharmony_ci			-Wfloat-equal
2351cb0ef41Sopenharmony_ci			-Wsign-conversion
2361cb0ef41Sopenharmony_ci		)
2371cb0ef41Sopenharmony_ci	endif ()
2381cb0ef41Sopenharmony_ci
2391cb0ef41Sopenharmony_ci	# Add flags to force colored output even when output is redirected via pipe.
2401cb0ef41Sopenharmony_ci	if (CMAKE_GENERATOR MATCHES "Ninja")
2411cb0ef41Sopenharmony_ci		set(color_default TRUE)
2421cb0ef41Sopenharmony_ci	else ()
2431cb0ef41Sopenharmony_ci		set(color_default FALSE)
2441cb0ef41Sopenharmony_ci	endif ()
2451cb0ef41Sopenharmony_ci	option(FORCE_COLOR "Force compiler to always colorize, even when output is redirected." ${color_default})
2461cb0ef41Sopenharmony_ci	mark_as_advanced(FORCE FORCE_COLOR)
2471cb0ef41Sopenharmony_ci	if (FORCE_COLOR)
2481cb0ef41Sopenharmony_ci		set(_flags
2491cb0ef41Sopenharmony_ci			-fdiagnostics-color=always # GCC
2501cb0ef41Sopenharmony_ci			-fcolor-diagnostics        # Clang
2511cb0ef41Sopenharmony_ci		)
2521cb0ef41Sopenharmony_ci		list(APPEND _flags_C   ${_flags})
2531cb0ef41Sopenharmony_ci		list(APPEND _flags_CXX ${_flags})
2541cb0ef41Sopenharmony_ci	endif ()
2551cb0ef41Sopenharmony_ci
2561cb0ef41Sopenharmony_ci	# Add -fno-omit-frame-pointer (and optionally -fno-inline) to make debugging and stack dumps nicer.
2571cb0ef41Sopenharmony_ci	set(_flags
2581cb0ef41Sopenharmony_ci		-fno-omit-frame-pointer
2591cb0ef41Sopenharmony_ci	)
2601cb0ef41Sopenharmony_ci	option(M_NO_INLINE "Disable function inlining for RelWithDebInfo and Debug configurations?" FALSE)
2611cb0ef41Sopenharmony_ci	if (M_NO_INLINE)
2621cb0ef41Sopenharmony_ci		list(APPEND _flags
2631cb0ef41Sopenharmony_ci			-fno-inline
2641cb0ef41Sopenharmony_ci		)
2651cb0ef41Sopenharmony_ci	endif ()
2661cb0ef41Sopenharmony_ci	list(APPEND _debug_flags_C   ${_flags})
2671cb0ef41Sopenharmony_ci	list(APPEND _debug_flags_CXX ${_flags})
2681cb0ef41Sopenharmony_ciendif ()
2691cb0ef41Sopenharmony_ci
2701cb0ef41Sopenharmony_ci# Check and set compiler flags.
2711cb0ef41Sopenharmony_ciset(_debug_configs
2721cb0ef41Sopenharmony_ci	RelWithDebInfo
2731cb0ef41Sopenharmony_ci	Debug
2741cb0ef41Sopenharmony_ci)
2751cb0ef41Sopenharmony_ciforeach(_lang ${languages})
2761cb0ef41Sopenharmony_ci	_int_enable_warnings_set_flags(_lang ${_flags_${_lang}})
2771cb0ef41Sopenharmony_ci	_int_enable_warnings_set_flags_ex(_lang _debug_configs ${_debug_flags_${_lang}})
2781cb0ef41Sopenharmony_ci
2791cb0ef41Sopenharmony_ci	# Ensure pure Debug builds are NOT optimized (not possible on Visual Studio).
2801cb0ef41Sopenharmony_ci	# Any optimization of a Debug build will prevent debuggers like lldb from
2811cb0ef41Sopenharmony_ci	# fully displaying backtraces and stepping.
2821cb0ef41Sopenharmony_ci	if (NOT MSVC)
2831cb0ef41Sopenharmony_ci		set(_config Debug)
2841cb0ef41Sopenharmony_ci		_int_enable_warnings_set_flags_ex(_lang _config -O0)
2851cb0ef41Sopenharmony_ci	endif ()
2861cb0ef41Sopenharmony_ciendforeach()
2871cb0ef41Sopenharmony_ci
2881cb0ef41Sopenharmony_ci
2891cb0ef41Sopenharmony_ci
2901cb0ef41Sopenharmony_ci# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
2911cb0ef41Sopenharmony_ci# Helper functions
2921cb0ef41Sopenharmony_ci
2931cb0ef41Sopenharmony_ci
2941cb0ef41Sopenharmony_ci# This function can be called in subdirectories, to prune out warnings that they don't want.
2951cb0ef41Sopenharmony_ci#  vararg: warning flags to remove from list of enabled warnings. All "no" flags after EXPLICIT_DISABLE
2961cb0ef41Sopenharmony_ci#          will be added to C flags.
2971cb0ef41Sopenharmony_ci#
2981cb0ef41Sopenharmony_ci# Ex.: remove_warnings(-Wall -Wdouble-promotion -Wcomment) prunes those warnings flags from the compile command.
2991cb0ef41Sopenharmony_cifunction(remove_warnings)
3001cb0ef41Sopenharmony_ci	get_property(languages GLOBAL PROPERTY ENABLED_LANGUAGES)
3011cb0ef41Sopenharmony_ci	set(langs C)
3021cb0ef41Sopenharmony_ci	if ("CXX" IN_LIST languages)
3031cb0ef41Sopenharmony_ci		list(APPEND langs CXX)
3041cb0ef41Sopenharmony_ci	endif ()
3051cb0ef41Sopenharmony_ci
3061cb0ef41Sopenharmony_ci	foreach(lang ${langs})
3071cb0ef41Sopenharmony_ci		set(toadd)
3081cb0ef41Sopenharmony_ci		set(in_explicit_disable FALSE)
3091cb0ef41Sopenharmony_ci		foreach (flag ${ARGN})
3101cb0ef41Sopenharmony_ci			if (flag STREQUAL "EXPLICIT_DISABLE")
3111cb0ef41Sopenharmony_ci				set(in_explicit_disable TRUE)
3121cb0ef41Sopenharmony_ci			elseif (in_explicit_disable)
3131cb0ef41Sopenharmony_ci				list(APPEND toadd "${flag}")
3141cb0ef41Sopenharmony_ci			else ()
3151cb0ef41Sopenharmony_ci				string(REGEX REPLACE "${flag}([ \t]+|$)" "" CMAKE_${lang}_FLAGS "${CMAKE_${lang}_FLAGS}")
3161cb0ef41Sopenharmony_ci			endif ()
3171cb0ef41Sopenharmony_ci		endforeach ()
3181cb0ef41Sopenharmony_ci		_int_enable_warnings_set_flags(lang ${toadd})
3191cb0ef41Sopenharmony_ci		string(STRIP "${CMAKE_${lang}_FLAGS}" CMAKE_${lang}_FLAGS)
3201cb0ef41Sopenharmony_ci		set(CMAKE_${lang}_FLAGS "${CMAKE_${lang}_FLAGS}" PARENT_SCOPE)
3211cb0ef41Sopenharmony_ci	endforeach()
3221cb0ef41Sopenharmony_ciendfunction()
3231cb0ef41Sopenharmony_ci
3241cb0ef41Sopenharmony_ci
3251cb0ef41Sopenharmony_ci# Explicitly suppress all warnings. As long as this flag is the last warning flag, warnings will be
3261cb0ef41Sopenharmony_ci# suppressed even if earlier flags enabled warnings.
3271cb0ef41Sopenharmony_cifunction(remove_all_warnings)
3281cb0ef41Sopenharmony_ci	get_property(languages GLOBAL PROPERTY ENABLED_LANGUAGES)
3291cb0ef41Sopenharmony_ci	set(langs C)
3301cb0ef41Sopenharmony_ci	if ("CXX" IN_LIST languages)
3311cb0ef41Sopenharmony_ci		list(APPEND langs CXX)
3321cb0ef41Sopenharmony_ci	endif ()
3331cb0ef41Sopenharmony_ci
3341cb0ef41Sopenharmony_ci	foreach(lang ${langs})
3351cb0ef41Sopenharmony_ci		string(REGEX REPLACE "[-/][Ww][^ \t]*([ \t]+|$)" "" CMAKE_${lang}_FLAGS "${CMAKE_${lang}_FLAGS}")
3361cb0ef41Sopenharmony_ci		if (MSVC)
3371cb0ef41Sopenharmony_ci			string(APPEND CMAKE_${lang}_FLAGS " /w")
3381cb0ef41Sopenharmony_ci		else ()
3391cb0ef41Sopenharmony_ci			string(APPEND CMAKE_${lang}_FLAGS " -w")
3401cb0ef41Sopenharmony_ci		endif ()
3411cb0ef41Sopenharmony_ci		string(STRIP "${CMAKE_${lang}_FLAGS}" CMAKE_${lang}_FLAGS)
3421cb0ef41Sopenharmony_ci		set(CMAKE_${lang}_FLAGS "${CMAKE_${lang}_FLAGS}" PARENT_SCOPE)
3431cb0ef41Sopenharmony_ci	endforeach()
3441cb0ef41Sopenharmony_ciendfunction()
3451cb0ef41Sopenharmony_ci
3461cb0ef41Sopenharmony_ci
3471cb0ef41Sopenharmony_cifunction(remove_all_warnings_from_targets)
3481cb0ef41Sopenharmony_ci	foreach (target ${ARGN})
3491cb0ef41Sopenharmony_ci		if (MSVC)
3501cb0ef41Sopenharmony_ci			target_compile_options(${target} PRIVATE "/w")
3511cb0ef41Sopenharmony_ci		else ()
3521cb0ef41Sopenharmony_ci			target_compile_options(${target} PRIVATE "-w")
3531cb0ef41Sopenharmony_ci		endif ()
3541cb0ef41Sopenharmony_ci	endforeach()
3551cb0ef41Sopenharmony_ciendfunction()
3561cb0ef41Sopenharmony_ci
3571cb0ef41Sopenharmony_ci
3581cb0ef41Sopenharmony_ci# Save the current warning settings to an internal variable.
3591cb0ef41Sopenharmony_cifunction(push_warnings)
3601cb0ef41Sopenharmony_ci	get_property(languages GLOBAL PROPERTY ENABLED_LANGUAGES)
3611cb0ef41Sopenharmony_ci	set(langs C)
3621cb0ef41Sopenharmony_ci	if ("CXX" IN_LIST languages)
3631cb0ef41Sopenharmony_ci		list(APPEND langs CXX)
3641cb0ef41Sopenharmony_ci	endif ()
3651cb0ef41Sopenharmony_ci
3661cb0ef41Sopenharmony_ci	foreach(lang ${langs})
3671cb0ef41Sopenharmony_ci		if (CMAKE_${lang}_FLAGS MATCHES ";")
3681cb0ef41Sopenharmony_ci			message(AUTHOR_WARNING "Cannot push warnings for ${lang}, CMAKE_${lang}_FLAGS contains semicolons")
3691cb0ef41Sopenharmony_ci			continue()
3701cb0ef41Sopenharmony_ci		endif ()
3711cb0ef41Sopenharmony_ci		# Add current flags to end of internal list.
3721cb0ef41Sopenharmony_ci		list(APPEND _enable_warnings_internal_${lang}_flags_stack "${CMAKE_${lang}_FLAGS}")
3731cb0ef41Sopenharmony_ci		# Propagate results up to caller's scope.
3741cb0ef41Sopenharmony_ci		set(_enable_warnings_internal_${lang}_flags_stack "${_enable_warnings_internal_${lang}_flags_stack}" PARENT_SCOPE)
3751cb0ef41Sopenharmony_ci	endforeach()
3761cb0ef41Sopenharmony_ciendfunction()
3771cb0ef41Sopenharmony_ci
3781cb0ef41Sopenharmony_ci
3791cb0ef41Sopenharmony_ci# Restore the current warning settings from an internal variable.
3801cb0ef41Sopenharmony_cifunction(pop_warnings)
3811cb0ef41Sopenharmony_ci	get_property(languages GLOBAL PROPERTY ENABLED_LANGUAGES)
3821cb0ef41Sopenharmony_ci	set(langs C)
3831cb0ef41Sopenharmony_ci	if ("CXX" IN_LIST languages)
3841cb0ef41Sopenharmony_ci		list(APPEND langs CXX)
3851cb0ef41Sopenharmony_ci	endif ()
3861cb0ef41Sopenharmony_ci
3871cb0ef41Sopenharmony_ci	foreach(lang ${langs})
3881cb0ef41Sopenharmony_ci		if (NOT _enable_warnings_internal_${lang}_flags_stack)
3891cb0ef41Sopenharmony_ci			continue()
3901cb0ef41Sopenharmony_ci		endif ()
3911cb0ef41Sopenharmony_ci		# Pop flags off of end of list, overwrite current flags with whatever we popped off.
3921cb0ef41Sopenharmony_ci		list(GET _enable_warnings_internal_${lang}_flags_stack -1 CMAKE_${lang}_FLAGS)
3931cb0ef41Sopenharmony_ci		list(REMOVE_AT _enable_warnings_internal_${lang}_flags_stack -1)
3941cb0ef41Sopenharmony_ci		# Propagate results up to caller's scope.
3951cb0ef41Sopenharmony_ci		set(_enable_warnings_internal_${lang}_flags_stack "${_enable_warnings_internal_${lang}_flags_stack}" PARENT_SCOPE)
3961cb0ef41Sopenharmony_ci		string(STRIP "${CMAKE_${lang}_FLAGS}" CMAKE_${lang}_FLAGS)
3971cb0ef41Sopenharmony_ci		set(CMAKE_${lang}_FLAGS "${CMAKE_${lang}_FLAGS}" PARENT_SCOPE)
3981cb0ef41Sopenharmony_ci	endforeach()
3991cb0ef41Sopenharmony_ciendfunction()
400