]> git.uio.no Git - u/mrichter/AliRoot.git/blame - Vc/cmake/AddCompilerFlag.cmake
comments on some sed magic
[u/mrichter/AliRoot.git] / Vc / cmake / AddCompilerFlag.cmake
CommitLineData
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 38get_filename_component(_currentDir "${CMAKE_CURRENT_LIST_FILE}" PATH)
39include("${_currentDir}/CheckCCompilerFlag.cmake")
40include("${_currentDir}/CheckCXXCompilerFlag.cmake")
c017a39f 41
f22341db 42macro(AddCompilerFlag _flag)
6936ae04 43 string(REGEX REPLACE "[-.+/:= ]" "_" _flag_esc "${_flag}")
c017a39f 44
f22341db 45 set(_c_flags "CMAKE_C_FLAGS")
46 set(_cxx_flags "CMAKE_CXX_FLAGS")
79c86c14 47 set(_c_result tmp)
48 set(_cxx_result tmp)
f22341db 49 if(${ARGC} EQUAL 2)
79c86c14 50 message(WARNING "Deprecated use of the AddCompilerFlag macro.")
51 unset(_c_result)
52 set(_cxx_result ${ARGV1})
f22341db 53 elseif(${ARGC} GREATER 2)
54 set(state 0)
55 unset(_c_flags)
56 unset(_cxx_flags)
79c86c14 57 unset(_c_result)
58 unset(_cxx_result)
f22341db 59 foreach(_arg ${ARGN})
60 if(_arg STREQUAL "C_FLAGS")
61 set(state 1)
79c86c14 62 if(NOT DEFINED _c_result)
63 set(_c_result tmp)
64 endif()
f22341db 65 elseif(_arg STREQUAL "CXX_FLAGS")
66 set(state 2)
79c86c14 67 if(NOT DEFINED _cxx_result)
68 set(_cxx_result tmp)
69 endif()
f22341db 70 elseif(_arg STREQUAL "C_RESULT")
71 set(state 3)
72 elseif(_arg STREQUAL "CXX_RESULT")
73 set(state 4)
74 elseif(state EQUAL 1)
75 set(_c_flags "${_arg}")
76 elseif(state EQUAL 2)
77 set(_cxx_flags "${_arg}")
78 elseif(state EQUAL 3)
79c86c14 79 set(_c_result "${_arg}")
f22341db 80 elseif(state EQUAL 4)
79c86c14 81 set(_cxx_result "${_arg}")
f22341db 82 else()
83 message(FATAL_ERROR "Syntax error for AddCompilerFlag")
84 endif()
85 endforeach()
86 endif()
87
79c86c14 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; }")
99 else()
100 set(_code "int main() { return 0; }")
101 endif()
102
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}})
106 endif()
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}})
110 endif()
111
f22341db 112 if(check_c_compiler_flag_${_flag_esc} AND DEFINED _c_flags)
113 set(${_c_flags} "${${_c_flags}} ${_flag}")
114 endif()
115 if(check_cxx_compiler_flag_${_flag_esc} AND DEFINED _cxx_flags)
116 set(${_cxx_flags} "${${_cxx_flags}} ${_flag}")
117 endif()
118endmacro(AddCompilerFlag)