|
| 1 | +# - Add a given compiler flag to flags variables. |
| 2 | +# AddCompilerFlag(<flag> [<var>]) |
| 3 | +# or |
| 4 | +# AddCompilerFlag(<flag> [C_FLAGS <var>] [CXX_FLAGS <var>] [C_RESULT <var>] |
| 5 | +# [CXX_RESULT <var>]) |
| 6 | + |
| 7 | +#============================================================================= |
| 8 | +# Copyright 2010-2015 Matthias Kretz <kretz@kde.org> |
| 9 | +# |
| 10 | +# Redistribution and use in source and binary forms, with or without |
| 11 | +# modification, are permitted provided that the following conditions are |
| 12 | +# met: |
| 13 | +# |
| 14 | +# * Redistributions of source code must retain the above copyright notice, |
| 15 | +# this list of conditions and the following disclaimer. |
| 16 | +# |
| 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. |
| 20 | +# |
| 21 | +# * Neither the names of contributing organizations nor the |
| 22 | +# names of its contributors may be used to endorse or promote products |
| 23 | +# derived from this software without specific prior written permission. |
| 24 | +# |
| 25 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS ``AS IS'' |
| 26 | +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 27 | +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 28 | +# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR |
| 29 | +# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 30 | +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 31 | +# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 32 | +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 33 | +# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 34 | +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 35 | +#============================================================================= |
| 36 | + |
| 37 | +get_filename_component(_currentDir "${CMAKE_CURRENT_LIST_FILE}" PATH) |
| 38 | +include("${_currentDir}/CheckCCompilerFlag.cmake") |
| 39 | +include("${_currentDir}/CheckCXXCompilerFlag.cmake") |
| 40 | + |
| 41 | +macro(AddCompilerFlag _flag) |
| 42 | + string(REGEX REPLACE "[-.+/:= ]" "_" _flag_esc "${_flag}") |
| 43 | + |
| 44 | + set(_c_flags "CMAKE_C_FLAGS") |
| 45 | + set(_cxx_flags "CMAKE_CXX_FLAGS") |
| 46 | + set(_c_result tmp) |
| 47 | + set(_cxx_result tmp) |
| 48 | + if(${ARGC} EQUAL 2) |
| 49 | + message(WARNING "Deprecated use of the AddCompilerFlag macro.") |
| 50 | + unset(_c_result) |
| 51 | + set(_cxx_result ${ARGV1}) |
| 52 | + elseif(${ARGC} GREATER 2) |
| 53 | + set(state 0) |
| 54 | + unset(_c_flags) |
| 55 | + unset(_cxx_flags) |
| 56 | + unset(_c_result) |
| 57 | + unset(_cxx_result) |
| 58 | + foreach(_arg ${ARGN}) |
| 59 | + if("x${_arg}" STREQUAL "xC_FLAGS") |
| 60 | + set(state 1) |
| 61 | + if(NOT DEFINED _c_result) |
| 62 | + set(_c_result tmp0) |
| 63 | + endif() |
| 64 | + elseif("x${_arg}" STREQUAL "xCXX_FLAGS") |
| 65 | + set(state 2) |
| 66 | + if(NOT DEFINED _cxx_result) |
| 67 | + set(_cxx_result tmp1) |
| 68 | + endif() |
| 69 | + elseif("x${_arg}" STREQUAL "xC_RESULT") |
| 70 | + set(state 3) |
| 71 | + elseif("x${_arg}" STREQUAL "xCXX_RESULT") |
| 72 | + set(state 4) |
| 73 | + elseif(state EQUAL 1) |
| 74 | + set(_c_flags "${_arg}") |
| 75 | + elseif(state EQUAL 2) |
| 76 | + set(_cxx_flags "${_arg}") |
| 77 | + elseif(state EQUAL 3) |
| 78 | + set(_c_result "${_arg}") |
| 79 | + elseif(state EQUAL 4) |
| 80 | + set(_cxx_result "${_arg}") |
| 81 | + else() |
| 82 | + message(FATAL_ERROR "Syntax error for AddCompilerFlag") |
| 83 | + endif() |
| 84 | + endforeach() |
| 85 | + endif() |
| 86 | + |
| 87 | + set(_c_code "int main() { return 0; }") |
| 88 | + set(_cxx_code "int main() { return 0; }") |
| 89 | + if("${_flag}" STREQUAL "-mfma") |
| 90 | + # Compiling with FMA3 support may fail only at the assembler level. |
| 91 | + # In that case we need to have such an instruction in the test code |
| 92 | + set(_c_code "#include <immintrin.h> |
| 93 | + __m128 foo(__m128 x) { return _mm_fmadd_ps(x, x, x); } |
| 94 | + int main() { return 0; }") |
| 95 | + set(_cxx_code "${_c_code}") |
| 96 | + elseif("${_flag}" STREQUAL "-stdlib=libc++") |
| 97 | + # Compiling with libc++ not only requires a compiler that understands it, but also |
| 98 | + # the libc++ headers itself |
| 99 | + set(_cxx_code "#include <iostream> |
| 100 | + #include <cstdio> |
| 101 | + int main() { return 0; }") |
| 102 | + else() |
| 103 | + set(_cxx_code "#include <cstdio> |
| 104 | + int main() { return 0; }") |
| 105 | + endif() |
| 106 | + |
| 107 | + if(DEFINED _c_result) |
| 108 | + check_c_compiler_flag("${_flag}" check_c_compiler_flag_${_flag_esc} "${_c_code}") |
| 109 | + set(${_c_result} ${check_c_compiler_flag_${_flag_esc}}) |
| 110 | + endif() |
| 111 | + if(DEFINED _cxx_result) |
| 112 | + check_cxx_compiler_flag("${_flag}" check_cxx_compiler_flag_${_flag_esc} "${_cxx_code}") |
| 113 | + set(${_cxx_result} ${check_cxx_compiler_flag_${_flag_esc}}) |
| 114 | + endif() |
| 115 | + |
| 116 | + macro(my_append _list _flag _special) |
| 117 | + if("x${_list}" STREQUAL "x${_special}") |
| 118 | + set(${_list} "${${_list}} ${_flag}") |
| 119 | + else() |
| 120 | + list(APPEND ${_list} "${_flag}") |
| 121 | + endif() |
| 122 | + endmacro() |
| 123 | + |
| 124 | + if(check_c_compiler_flag_${_flag_esc} AND DEFINED _c_flags) |
| 125 | + my_append(${_c_flags} "${_flag}" CMAKE_C_FLAGS) |
| 126 | + endif() |
| 127 | + if(check_cxx_compiler_flag_${_flag_esc} AND DEFINED _cxx_flags) |
| 128 | + my_append(${_cxx_flags} "${_flag}" CMAKE_CXX_FLAGS) |
| 129 | + endif() |
| 130 | +endmacro(AddCompilerFlag) |
0 commit comments