summaryrefslogtreecommitdiff
path: root/cmake/AddCFlagIfSupported.cmake
blob: b7aaa7910a969afe106300e301ebb905f0b382b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# - Append compiler flag to CMAKE_C_FLAGS if compiler supports it
# ADD_C_FLAG_IF_SUPPORTED(<flag>)
#  <flag> - the compiler flag to test
# This internally calls the CHECK_C_COMPILER_FLAG macro.

INCLUDE(CheckCCompilerFlag)

MACRO(ADD_C_FLAG _FLAG)
	STRING(TOUPPER ${_FLAG} UPCASE)
	STRING(REGEX REPLACE "[-=]" "_" UPCASE_PRETTY ${UPCASE})
	STRING(REGEX REPLACE "^_+" "" UPCASE_PRETTY ${UPCASE_PRETTY})
	CHECK_C_COMPILER_FLAG(${_FLAG} IS_${UPCASE_PRETTY}_SUPPORTED)

	IF(IS_${UPCASE_PRETTY}_SUPPORTED)
		SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${_FLAG}")
	ELSE()
		MESSAGE(FATAL_ERROR "Required flag ${_FLAG} is not supported")
	ENDIF()
ENDMACRO()

MACRO(ADD_C_FLAG_IF_SUPPORTED _FLAG)
	STRING(TOUPPER ${_FLAG} UPCASE)
	STRING(REGEX REPLACE "[-=]" "_" UPCASE_PRETTY ${UPCASE})
	STRING(REGEX REPLACE "^_+" "" UPCASE_PRETTY ${UPCASE_PRETTY})
	CHECK_C_COMPILER_FLAG(${_FLAG} IS_${UPCASE_PRETTY}_SUPPORTED)

	IF(IS_${UPCASE_PRETTY}_SUPPORTED)
		SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${_FLAG}")
	ENDIF()
ENDMACRO()