2 include(CheckCXXCompilerFlag)
4 unset(add_flag_if_supported)
6 # Mangle the name of a compiler flag into a valid CMake identifier.
7 # Ex: --std=c++11 -> STD_EQ_CXX11
8 macro(mangle_name str output)
9 string(STRIP "${str}" strippedStr)
10 string(REGEX REPLACE "^/" "" strippedStr "${strippedStr}")
11 string(REGEX REPLACE "^-+" "" strippedStr "${strippedStr}")
12 string(REGEX REPLACE "-+$" "" strippedStr "${strippedStr}")
13 string(REPLACE "-" "_" strippedStr "${strippedStr}")
14 string(REPLACE "=" "_EQ_" strippedStr "${strippedStr}")
15 string(REPLACE "+" "X" strippedStr "${strippedStr}")
16 string(TOUPPER "${strippedStr}" ${output})
19 # Remove a list of flags from all CMake variables that affect compile flags.
20 # This can be used to remove unwanted flags specified on the command line
21 # or added in other parts of LLVM's cmake configuration.
24 string(REPLACE "${var}" "" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
25 string(REPLACE "${var}" "" CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL}")
26 string(REPLACE "${var}" "" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
27 string(REPLACE "${var}" "" CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
28 string(REPLACE "${var}" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
29 string(REPLACE "${var}" "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
30 string(REPLACE "${var}" "" CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}")
31 string(REPLACE "${var}" "" CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS}")
32 string(REPLACE "${var}" "" CMAKE_SHARED_MODULE_FLAGS "${CMAKE_SHARED_MODULE_FLAGS}")
33 remove_definitions(${var})
35 endmacro(remove_flags)
37 macro(check_flag_supported flag)
38 mangle_name("${flag}" flagname)
39 check_cxx_compiler_flag("${flag}" "CXX_SUPPORTS_${flagname}_FLAG")
42 macro(append_flags DEST)
43 foreach(value ${ARGN})
44 list(APPEND ${DEST} ${value})
45 list(APPEND ${DEST} ${value})
49 # If the specified 'condition' is true then append the specified list of flags to DEST
50 macro(append_flags_if condition DEST)
52 list(APPEND ${DEST} ${ARGN})
56 # Add each flag in the list specified by DEST if that flag is supported by the current compiler.
57 macro(append_flags_if_supported DEST)
59 mangle_name("${flag}" flagname)
60 check_cxx_compiler_flag("${flag}" "CXX_SUPPORTS_${flagname}_FLAG")
61 append_flags_if(CXX_SUPPORTS_${flagname}_FLAG ${DEST} ${flag})
65 # Add a macro definition if condition is true.
66 macro(define_if condition def)
68 add_definitions(${def})
72 # Add a macro definition if condition is not true.
73 macro(define_if_not condition def)
75 add_definitions(${def})
79 # Add a macro definition to the __config_site file if the specified condition
80 # is 'true'. Note that '-D${def}' is not added. Instead it is expected that
81 # the build include the '__config_site' header.
82 macro(config_define_if condition def)
88 macro(config_define_if_not condition def)
94 macro(config_define value def)
98 # Turn a comma separated CMake list into a space separated string.
99 macro(split_list listname)
100 string(REPLACE ";" " " ${listname} "${${listname}}")
103 # For each specified flag, add that compile flag to the provided target.
104 # The flags are added with the given visibility, i.e. PUBLIC|PRIVATE|INTERFACE.
105 function(target_add_compile_flags_if_supported target visibility)
106 foreach(flag ${ARGN})
107 mangle_name("${flag}" flagname)
108 check_cxx_compiler_flag("${flag}" "CXX_SUPPORTS_${flagname}_FLAG")
109 if (CXX_SUPPORTS_${flagname}_FLAG)
110 target_compile_options(${target} ${visibility} ${flag})