-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathadd_flags.cmake
128 lines (113 loc) · 4.22 KB
/
add_flags.cmake
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# from libcxx HandleLibcxxFlags.cmake
# Mangle the name of a compiler flag into a valid CMake identifier.
# Ex: --std=c++11 -> STD_EQ_CXX11
# macros, global scope
macro(mangle_name str output)
string(STRIP "${str}" strippedStr)
string(REGEX REPLACE "^/" "" strippedStr "${strippedStr}")
string(REGEX REPLACE "^-+" "" strippedStr "${strippedStr}")
string(REGEX REPLACE "-+$" "" strippedStr "${strippedStr}")
string(REPLACE "-" "_" strippedStr "${strippedStr}")
string(REPLACE ":" "_COLON_" strippedStr "${strippedStr}")
string(REPLACE "=" "_EQ_" strippedStr "${strippedStr}")
string(REPLACE "+" "X" strippedStr "${strippedStr}")
string(TOUPPER "${strippedStr}" ${output})
endmacro()
if(MSVC)
set(WERROR "-W4 -WX") # FIXME: why -WX does not work? -W4 is not supported in link
else()
set(WERROR "-Wall -Wextra -Wconversion -pedantic -Wfatal-errors -Werror")
endif()
function(add_compile_options_if_supported)
foreach(flag ${ARGN})
mangle_name("${flag}" flagname)
check_cxx_compiler_flag("${WERROR} ${flag}" "C_CXX_SUPPORTS_${flagname}_FLAG") # c,c++
if(${C_CXX_SUPPORTS_${flagname}_FLAG})
add_compile_options($<$<COMPILE_LANGUAGE:C,CXX>:${flag}>)
endif()
endforeach()
endfunction()
# Add a list of flags to 'CMAKE_C_FLAGS'.
macro(add_c_flags)
foreach(f ${ARGN})
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${f}")
endforeach()
endmacro()
# If 'condition' is true then add the specified list of flags to
# 'CMAKE_C_FLAGS'
macro(add_c_flags_if condition)
if (${condition})
add_c_flags(${ARGN})
endif()
endmacro()
# For each specified flag, add that flag to 'CMAKE_C_FLAGS' if the
# flag is supported by the C++ compiler.
macro(add_c_flags_if_supported)
foreach(flag ${ARGN})
mangle_name("${flag}" flagname)
check_c_compiler_flag("${WERROR} ${flag}" "C_SUPPORTS_${flagname}_FLAG")
add_c_flags_if(C_SUPPORTS_${flagname}_FLAG ${flag})
endforeach()
endmacro()
# Add a list of flags to 'CMAKE_CXX_FLAGS'.
macro(add_cxx_flags)
foreach(f ${ARGN})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${f}")
endforeach()
endmacro()
# If 'condition' is true then add the specified list of flags to
# 'CMAKE_CXX_FLAGS'
macro(add_cxx_flags_if condition)
if (${condition})
add_cxx_flags(${ARGN})
endif()
endmacro()
# For each specified flag, add that flag to 'CMAKE_CXX_FLAGS' if the
# flag is supported by the C++ compiler.
macro(add_cxx_flags_if_supported)
foreach(flag ${ARGN})
mangle_name("${flag}" flagname)
check_cxx_compiler_flag("${WERROR} ${flag}" "CXX_SUPPORTS_${flagname}_FLAG")
add_cxx_flags_if(CXX_SUPPORTS_${flagname}_FLAG ${flag})
endforeach()
endmacro()
# Add a list of flags to 'CMAKE_SHARED_LINKER_FLAGS' and 'CMAKE_EXE_LINKER_FLAGS'.
macro(add_link_flags)
foreach(f ${ARGN})
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${f}")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${f}")
set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} ${f}")
endforeach()
endmacro()
# cmake 3.13: add_link_options(), target_link_options
if(CMAKE_VERSION VERSION_LESS 3.13)
macro(add_link_options)
add_link_flags(${ARGN})
endmacro()
endif()
# If 'condition' is true then add the specified list of flags to
# add_lflag_if_not
macro(add_link_flags_if condition)
if (${condition})
add_link_options(${ARGN})
endif()
endmacro()
function(is_link_flag_supported flag out)
# unsupported flags can be a warning (clang, vc)
set(CMAKE_REQUIRED_LIBRARIES_OLD ${CMAKE_REQUIRED_LIBRARIES})
set(CMAKE_REQUIRED_LIBRARIES ${flag})
list(APPEND CMAKE_REQUIRED_LIBRARIES ${WERROR})
# can not check "-Werror ${flags}" because it will be used by not only linker but also -c and warns linker flags are unused
check_cxx_compiler_flag("" "${out}") # out is cached
#set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES}) # add for macro
endfunction()
# For each specified flag, add that flag to 'LIBCXX_LINK_FLAGS' if the
# flag is supported by the C++ compiler.
macro(add_link_flags_if_supported)
foreach(flag ${ARGN})
mangle_name("${flag}" flagname)
is_link_flag_supported("${flag}" "L_SUPPORTS_${flagname}_FLAG")
add_link_flags_if(L_SUPPORTS_${flagname}_FLAG ${flag})
set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
endforeach()
endmacro()