1 # - Add a given compiler flag to flags variables.
2 # AddCompilerFlag(<flag> [<var>])
4 # AddCompilerFlag(<flag> [C_FLAGS <var>] [CXX_FLAGS <var>] [C_RESULT <var>]
7 #=============================================================================
8 # Copyright 2010-2013 Matthias Kretz <kretz@kde.org>
10 # Redistribution and use in source and binary forms, with or without
11 # modification, are permitted provided that the following conditions are
14 # * Redistributions of source code must retain the above copyright notice,
15 # this list of conditions and the following disclaimer.
17 # * Redistributions in binary form must reproduce the above copyright notice,
18 # this list of conditions and the following disclaimer in the documentation
19 # and/or other materials provided with the distribution.
21 # * The names of Kitware, Inc., the Insight Consortium, or the names of
22 # any consortium members, or of any contributors, may not be used to
23 # endorse or promote products derived from this software without
24 # specific prior written permission.
26 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS ``AS IS''
27 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
30 # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32 # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
34 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #=============================================================================
38 get_filename_component(_currentDir "${CMAKE_CURRENT_LIST_FILE}" PATH)
39 include("${_currentDir}/CheckCCompilerFlag.cmake")
40 include("${_currentDir}/CheckCXXCompilerFlag.cmake")
42 macro(AddCompilerFlag _flag)
43 string(REGEX REPLACE "[-.+/:= ]" "_" _flag_esc "${_flag}")
45 set(_c_flags "CMAKE_C_FLAGS")
46 set(_cxx_flags "CMAKE_CXX_FLAGS")
50 message(WARNING "Deprecated use of the AddCompilerFlag macro.")
52 set(_cxx_result ${ARGV1})
53 elseif(${ARGC} GREATER 2)
60 if(_arg STREQUAL "C_FLAGS")
62 if(NOT DEFINED _c_result)
65 elseif(_arg STREQUAL "CXX_FLAGS")
67 if(NOT DEFINED _cxx_result)
70 elseif(_arg STREQUAL "C_RESULT")
72 elseif(_arg STREQUAL "CXX_RESULT")
75 set(_c_flags "${_arg}")
77 set(_cxx_flags "${_arg}")
79 set(_c_result "${_arg}")
81 set(_cxx_result "${_arg}")
83 message(FATAL_ERROR "Syntax error for AddCompilerFlag")
88 if("${_flag}" STREQUAL "-mfma")
89 # Compiling with FMA3 support may fail only at the assembler level.
90 # In that case we need to have such an instruction in the test code
91 set(_code "#include <immintrin.h>
92 __m128 foo(__m128 x) { return _mm_fmadd_ps(x, x, x); }
93 int main() { return 0; }")
94 elseif("${_flag}" STREQUAL "-stdlib=libc++")
95 # Compiling with libc++ not only requires a compiler that understands it, but also
96 # the libc++ headers itself
97 set(_code "#include <iostream>
98 int main() { return 0; }")
100 set(_code "int main() { return 0; }")
103 if(DEFINED _c_result)
104 check_c_compiler_flag("${_flag}" check_c_compiler_flag_${_flag_esc} "${_code}")
105 set(${_c_result} ${check_c_compiler_flag_${_flag_esc}})
107 if(DEFINED _cxx_result)
108 check_cxx_compiler_flag("${_flag}" check_cxx_compiler_flag_${_flag_esc} "${_code}")
109 set(${_cxx_result} ${check_cxx_compiler_flag_${_flag_esc}})
112 if(check_c_compiler_flag_${_flag_esc} AND DEFINED _c_flags)
113 set(${_c_flags} "${${_c_flags}} ${_flag}")
115 if(check_cxx_compiler_flag_${_flag_esc} AND DEFINED _cxx_flags)
116 set(${_cxx_flags} "${${_cxx_flags}} ${_flag}")
118 endmacro(AddCompilerFlag)