llvm.org GIT mirror llvm / b1d6b8e
CMake: fix compiler feature detection add_flag_if_supported() and add_flag_or_print_warning() were effectively no-ops, just returning the value of the first result (usually '-fno-omit-frame-pointer') for all subsequent checks for different flags. Due to the way CMake caches feature detection results, we need to provide symbolic variable names which will persist the cached results. This commit fixes feature detection using these two macros. The feature checks now run and get stored correctly, and the correct output can be observed in configure logs: -- Performing Test C_SUPPORTS_FPIC -- Performing Test C_SUPPORTS_FPIC - Success -- Performing Test CXX_SUPPORTS_FPIC -- Performing Test CXX_SUPPORTS_FPIC - Success git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@212586 91177308-0d34-0410-b5e6-96231b3b80d8 Alp Toker 5 years ago
1 changed file(s) with 36 addition(s) and 45 deletion(s). Raw diff Collapse all Expand all
106106 set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -Wl,-flat_namespace -Wl,-undefined -Wl,suppress")
107107 endif()
108108
109 function(add_flag_or_print_warning flag)
110 check_c_compiler_flag(${flag} C_SUPPORTS_FLAG)
111 check_cxx_compiler_flag(${flag} CXX_SUPPORTS_FLAG)
112 if (C_SUPPORTS_FLAG AND CXX_SUPPORTS_FLAG)
109 function(append value)
110 foreach(variable ${ARGN})
111 set(${variable} "${${variable}} ${value}" PARENT_SCOPE)
112 endforeach(variable)
113 endfunction()
114
115 function(append_if condition value)
116 if (${condition})
117 foreach(variable ${ARGN})
118 set(${variable} "${${variable}} ${value}" PARENT_SCOPE)
119 endforeach(variable)
120 endif()
121 endfunction()
122
123 macro(add_flag_if_supported flag name)
124 check_c_compiler_flag("-Werror ${flag}" "C_SUPPORTS_${name}")
125 append_if("C_SUPPORTS_${name}" "${flag}" CMAKE_C_FLAGS)
126 check_cxx_compiler_flag("-Werror ${flag}" "CXX_SUPPORTS_${name}")
127 append_if("CXX_SUPPORTS_${name}" "${flag}" CMAKE_CXX_FLAGS)
128 endmacro()
129
130 function(add_flag_or_print_warning flag name)
131 check_c_compiler_flag("-Werror ${flag}" "C_SUPPORTS_${name}")
132 check_cxx_compiler_flag("-Werror ${flag}" "CXX_SUPPORTS_${name}")
133 if ("C_SUPPORTS_${name}" AND "CXX_SUPPORTS_${name}")
113134 message(STATUS "Building with ${flag}")
114135 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}" PARENT_SCOPE)
115136 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${flag}" PARENT_SCOPE)
117138 message(WARNING "${flag} is not supported.")
118139 endif()
119140 endfunction()
120
121 function(append value)
122 foreach(variable ${ARGN})
123 set(${variable} "${${variable}} ${value}" PARENT_SCOPE)
124 endforeach(variable)
125 endfunction()
126
127 function(append_if condition value)
128 if (${condition})
129 foreach(variable ${ARGN})
130 set(${variable} "${${variable}} ${value}" PARENT_SCOPE)
131 endforeach(variable)
132 endif()
133 endfunction()
134
135 macro(add_flag_if_supported flag)
136 check_c_compiler_flag(${flag} C_SUPPORTS_FLAG)
137 append_if(C_SUPPORTS_FLAG "${flag}" CMAKE_C_FLAGS)
138 check_cxx_compiler_flag(${flag} CXX_SUPPORTS_FLAG)
139 append_if(CXX_SUPPORTS_FLAG "${flag}" CMAKE_CXX_FLAGS)
140 endmacro()
141141
142142 if( LLVM_ENABLE_PIC )
143143 if( XCODE )
147147 elseif( WIN32 OR CYGWIN)
148148 # On Windows all code is PIC. MinGW warns if -fPIC is used.
149149 else()
150 add_flag_or_print_warning("-fPIC")
150 add_flag_or_print_warning("-fPIC" FPIC)
151151
152152 if( WIN32 OR CYGWIN)
153153 # MinGW warns if -fvisibility-inlines-hidden is used.
283283 endif()
284284
285285 append_if(LLVM_ENABLE_PEDANTIC "-pedantic -Wno-long-long" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
286 check_cxx_compiler_flag("-Werror -Wcovered-switch-default" CXX_SUPPORTS_COVERED_SWITCH_DEFAULT_FLAG)
287 append_if(CXX_SUPPORTS_COVERED_SWITCH_DEFAULT_FLAG "-Wcovered-switch-default" CMAKE_CXX_FLAGS)
288 check_c_compiler_flag("-Werror -Wcovered-switch-default" C_SUPPORTS_COVERED_SWITCH_DEFAULT_FLAG)
289 append_if(C_SUPPORTS_COVERED_SWITCH_DEFAULT_FLAG "-Wcovered-switch-default" CMAKE_C_FLAGS)
286 add_flag_if_supported("-Wcovered-switch-default" COVERED_SWITCH_DEFAULT_FLAG)
290287 append_if(USE_NO_UNINITIALIZED "-Wno-uninitialized" CMAKE_CXX_FLAGS)
291288 append_if(USE_NO_MAYBE_UNINITIALIZED "-Wno-maybe-uninitialized" CMAKE_CXX_FLAGS)
292289 check_cxx_compiler_flag("-Werror -Wnon-virtual-dtor" CXX_SUPPORTS_NON_VIRTUAL_DTOR_FLAG)
326323 macro(append_common_sanitizer_flags)
327324 # Append -fno-omit-frame-pointer and turn on debug info to get better
328325 # stack traces.
329 add_flag_if_supported("-fno-omit-frame-pointer")
326 add_flag_if_supported("-fno-omit-frame-pointer" FNO_OMIT_FRAME_POINTER)
330327 if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG" AND
331328 NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "RELWITHDEBINFO")
332 add_flag_if_supported("-gline-tables-only")
329 add_flag_if_supported("-gline-tables-only" GLINE_TABLES_ONLY)
333330 endif()
334331 # Use -O1 even in debug mode, otherwise sanitizers slowdown is too large.
335332 if (uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG")
336 add_flag_if_supported("-O1")
333 add_flag_if_supported("-O1" O1)
337334 endif()
338335 endmacro()
339336
342339 if (LLVM_ON_UNIX)
343340 if (LLVM_USE_SANITIZER STREQUAL "Address")
344341 append_common_sanitizer_flags()
345 add_flag_or_print_warning("-fsanitize=address")
342 add_flag_or_print_warning("-fsanitize=address" FSANITIZE_ADDRESS)
346343 elseif (LLVM_USE_SANITIZER MATCHES "Memory(WithOrigins)?")
347344 append_common_sanitizer_flags()
348 add_flag_or_print_warning("-fsanitize=memory")
345 add_flag_or_print_warning("-fsanitize=memory" FSANITIZE_MEMORY)
349346 if(LLVM_USE_SANITIZER STREQUAL "MemoryWithOrigins")
350 add_flag_or_print_warning("-fsanitize-memory-track-origins")
347 add_flag_or_print_warning("-fsanitize-memory-track-origins" FSANITIZE_MEMORY_TRACK_ORIGINS)
351348 endif()
352349 else()
353350 message(WARNING "Unsupported value of LLVM_USE_SANITIZER: ${LLVM_USE_SANITIZER}")
383380 if (C_SUPPORTS_FNO_FUNCTION_SECTIONS)
384381 # Don't add -ffunction-section if it can be disabled with -fno-function-sections.
385382 # Doing so will break sanitizers.
386 check_c_compiler_flag("-Werror -ffunction-sections" C_SUPPORTS_FFUNCTION_SECTIONS)
387 check_cxx_compiler_flag("-Werror -ffunction-sections" CXX_SUPPORTS_FFUNCTION_SECTIONS)
388 append_if(C_SUPPORTS_FFUNCTION_SECTIONS "-ffunction-sections" CMAKE_C_FLAGS)
389 append_if(CXX_SUPPORTS_FFUNCTION_SECTIONS "-ffunction-sections" CMAKE_CXX_FLAGS)
390 endif()
391 check_c_compiler_flag("-Werror -fdata-sections" C_SUPPORTS_FDATA_SECTIONS)
392 check_cxx_compiler_flag("-Werror -fdata-sections" CXX_SUPPORTS_FDATA_SECTIONS)
393 append_if(C_SUPPORTS_FDATA_SECTIONS "-fdata-sections" CMAKE_C_FLAGS)
394 append_if(CXX_SUPPORTS_FDATA_SECTIONS "-fdata-sections" CMAKE_CXX_FLAGS)
383 add_flag_if_supported("-ffunction-sections" FFUNCTION_SECTIONS)
384 endif()
385 add_flag_if_supported("-fdata-sections" FDATA_SECTIONS)
395386 endif()
396387 endif()
397388