]>
Commit | Line | Data |
---|---|---|
c017a39f | 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-2013 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 | # * 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. | |
25 | # | |
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 | #============================================================================= | |
37 | ||
f22341db | 38 | get_filename_component(_currentDir "${CMAKE_CURRENT_LIST_FILE}" PATH) |
39 | include("${_currentDir}/CheckCCompilerFlag.cmake") | |
40 | include("${_currentDir}/CheckCXXCompilerFlag.cmake") | |
c017a39f | 41 | |
f22341db | 42 | macro(AddCompilerFlag _flag) |
6936ae04 | 43 | string(REGEX REPLACE "[-.+/:= ]" "_" _flag_esc "${_flag}") |
c017a39f | 44 | |
45 | if("${_flag}" STREQUAL "-mfma") | |
46 | # Compiling with FMA3 support may fail only at the assembler level. | |
47 | # In that case we need to have such an instruction in the test code | |
48 | set(_code "#include <immintrin.h> | |
49 | __m128 foo(__m128 x) { return _mm_fmadd_ps(x, x, x); } | |
50 | int main() { return 0; }") | |
51 | check_c_compiler_flag("${_flag}" check_c_compiler_flag_${_flag_esc} "${_code}") | |
52 | check_cxx_compiler_flag("${_flag}" check_cxx_compiler_flag_${_flag_esc} "${_code}") | |
53 | elseif("${_flag}" STREQUAL "-stdlib=libc++") | |
54 | # Compiling with libc++ not only requires a compiler that understands it, but also | |
55 | # the libc++ headers itself | |
56 | set(_code "#include <iostream> | |
57 | int main() { return 0; }") | |
58 | check_c_compiler_flag("${_flag}" check_c_compiler_flag_${_flag_esc} "${_code}") | |
59 | check_cxx_compiler_flag("${_flag}" check_cxx_compiler_flag_${_flag_esc} "${_code}") | |
60 | else() | |
61 | check_c_compiler_flag("${_flag}" check_c_compiler_flag_${_flag_esc}) | |
62 | check_cxx_compiler_flag("${_flag}" check_cxx_compiler_flag_${_flag_esc}) | |
63 | endif() | |
f22341db | 64 | |
65 | set(_c_flags "CMAKE_C_FLAGS") | |
66 | set(_cxx_flags "CMAKE_CXX_FLAGS") | |
67 | if(${ARGC} EQUAL 2) | |
68 | set(${ARGV1} "${check_cxx_compiler_flag_${_flag_esc}}") | |
69 | elseif(${ARGC} GREATER 2) | |
70 | set(state 0) | |
71 | unset(_c_flags) | |
72 | unset(_cxx_flags) | |
73 | foreach(_arg ${ARGN}) | |
74 | if(_arg STREQUAL "C_FLAGS") | |
75 | set(state 1) | |
76 | elseif(_arg STREQUAL "CXX_FLAGS") | |
77 | set(state 2) | |
78 | elseif(_arg STREQUAL "C_RESULT") | |
79 | set(state 3) | |
80 | elseif(_arg STREQUAL "CXX_RESULT") | |
81 | set(state 4) | |
82 | elseif(state EQUAL 1) | |
83 | set(_c_flags "${_arg}") | |
84 | elseif(state EQUAL 2) | |
85 | set(_cxx_flags "${_arg}") | |
86 | elseif(state EQUAL 3) | |
87 | set(${_arg} ${check_c_compiler_flag_${_flag_esc}}) | |
88 | elseif(state EQUAL 4) | |
89 | set(${_arg} ${check_cxx_compiler_flag_${_flag_esc}}) | |
90 | else() | |
91 | message(FATAL_ERROR "Syntax error for AddCompilerFlag") | |
92 | endif() | |
93 | endforeach() | |
94 | endif() | |
95 | ||
96 | if(check_c_compiler_flag_${_flag_esc} AND DEFINED _c_flags) | |
97 | set(${_c_flags} "${${_c_flags}} ${_flag}") | |
98 | endif() | |
99 | if(check_cxx_compiler_flag_${_flag_esc} AND DEFINED _cxx_flags) | |
100 | set(${_cxx_flags} "${${_cxx_flags}} ${_flag}") | |
101 | endif() | |
102 | endmacro(AddCompilerFlag) |