| 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 |
# SPDX-FileCopyrightText: 2010-2015 Matthias Kretz <[email protected]> |
| 9 |
# SPDX-FileCopyrightText: 2021 L. E. Segovia <[email protected] |
| 10 |
# SPDX-License-Identifier: BSD-3-Clause |
| 11 |
#============================================================================= |
| 12 |
|
| 13 |
include(CheckCCompilerFlag) |
| 14 |
include(CheckCXXCompilerFlag) |
| 15 |
|
| 16 |
macro(AddCompilerFlag _flag) |
| 17 |
string(REGEX REPLACE "[-.+/:= ]" "_" _flag_esc "${_flag}") |
| 18 |
|
| 19 |
set(_c_flags "CMAKE_C_FLAGS") |
| 20 |
set(_cxx_flags "CMAKE_CXX_FLAGS") |
| 21 |
set(_c_result tmp) |
| 22 |
set(_cxx_result tmp) |
| 23 |
if(${ARGC} EQUAL 2) |
| 24 |
message(WARNING "Deprecated use of the AddCompilerFlag macro.") |
| 25 |
unset(_c_result) |
| 26 |
set(_cxx_result ${ARGV1}) |
| 27 |
elseif(${ARGC} GREATER 2) |
| 28 |
set(state 0) |
| 29 |
unset(_c_flags) |
| 30 |
unset(_cxx_flags) |
| 31 |
unset(_c_result) |
| 32 |
unset(_cxx_result) |
| 33 |
foreach(_arg ${ARGN}) |
| 34 |
if("x${_arg}" STREQUAL "xC_FLAGS") |
| 35 |
set(state 1) |
| 36 |
if(NOT DEFINED _c_result) |
| 37 |
set(_c_result tmp0) |
| 38 |
endif() |
| 39 |
elseif("x${_arg}" STREQUAL "xCXX_FLAGS") |
| 40 |
set(state 2) |
| 41 |
if(NOT DEFINED _cxx_result) |
| 42 |
set(_cxx_result tmp1) |
| 43 |
endif() |
| 44 |
elseif("x${_arg}" STREQUAL "xC_RESULT") |
| 45 |
set(state 3) |
| 46 |
elseif("x${_arg}" STREQUAL "xCXX_RESULT") |
| 47 |
set(state 4) |
| 48 |
elseif(state EQUAL 1) |
| 49 |
set(_c_flags "${_arg}") |
| 50 |
elseif(state EQUAL 2) |
| 51 |
set(_cxx_flags "${_arg}") |
| 52 |
elseif(state EQUAL 3) |
| 53 |
set(_c_result "${_arg}") |
| 54 |
elseif(state EQUAL 4) |
| 55 |
set(_cxx_result "${_arg}") |
| 56 |
else() |
| 57 |
message(FATAL_ERROR "Syntax error for AddCompilerFlag") |
| 58 |
endif() |
| 59 |
endforeach() |
| 60 |
endif() |
| 61 |
|
| 62 |
set(_c_code "int main() { return 0; }") |
| 63 |
set(_cxx_code "int main() { return 0; }") |
| 64 |
if("${_flag}" STREQUAL "-mfma") |
| 65 |
# Compiling with FMA3 support may fail only at the assembler level. |
| 66 |
# In that case we need to have such an instruction in the test code |
| 67 |
set(_c_code "#include <immintrin.h> |
| 68 |
__m128 foo(__m128 x) { return _mm_fmadd_ps(x, x, x); } |
| 69 |
int main() { return 0; }") |
| 70 |
set(_cxx_code "${_c_code}") |
| 71 |
elseif("${_flag}" STREQUAL "-stdlib=libc++") |
| 72 |
# Compiling with libc++ not only requires a compiler that understands it, but also |
| 73 |
# the libc++ headers itself |
| 74 |
set(_cxx_code "#include <iostream> |
| 75 |
#include <cstdio> |
| 76 |
int main() { return 0; }") |
| 77 |
else() |
| 78 |
set(_cxx_code "#include <cstdio> |
| 79 |
int main() { return 0; }") |
| 80 |
endif() |
| 81 |
|
| 82 |
if(DEFINED _c_result) |
| 83 |
check_c_compiler_flag("${_flag}" check_c_compiler_flag_${_flag_esc} "${_c_code}") |
| 84 |
set(${_c_result} ${check_c_compiler_flag_${_flag_esc}}) |
| 85 |
endif() |
| 86 |
if(DEFINED _cxx_result) |
| 87 |
check_cxx_compiler_flag("${_flag}" check_cxx_compiler_flag_${_flag_esc} "${_cxx_code}") |
| 88 |
set(${_cxx_result} ${check_cxx_compiler_flag_${_flag_esc}}) |
| 89 |
endif() |
| 90 |
|
| 91 |
macro(my_append _list _flag _special) |
| 92 |
if("x${_list}" STREQUAL "x${_special}") |
| 93 |
set(${_list} "${${_list}} ${_flag}") |
| 94 |
else() |
| 95 |
list(APPEND ${_list} "${_flag}") |
| 96 |
endif() |
| 97 |
endmacro() |
| 98 |
|
| 99 |
if(check_c_compiler_flag_${_flag_esc} AND DEFINED _c_flags) |
| 100 |
my_append(${_c_flags} "${_flag}" CMAKE_C_FLAGS) |
| 101 |
endif() |
| 102 |
if(check_cxx_compiler_flag_${_flag_esc} AND DEFINED _cxx_flags) |
| 103 |
my_append(${_cxx_flags} "${_flag}" CMAKE_CXX_FLAGS) |
| 104 |
endif() |
| 105 |
endmacro(AddCompilerFlag) |