|
4 | 4 | # visible artifacts, but may be undesirable in some uses. In this case, disable the ENABLE_FAST_MATH option |
5 | 5 | # and specify the safe set of flags via the CMAKE_C_FLAGS and CMAKE_CXX_FLAGS variables. |
6 | 6 |
|
7 | | -if(ENABLE_FAST_MATH) |
| 7 | +if (ENABLE_FAST_MATH) |
8 | 8 | include(CheckCCompilerFlag) |
9 | | - if(MSVC) |
| 9 | + if (CMAKE_C_COMPILER_ID STREQUAL "MSVC") |
10 | 10 | check_c_compiler_flag("/fp:fast" FAST_MATH_OPTIMIZATION_SUPPORTED) |
11 | | - if(FAST_MATH_OPTIMIZATION_SUPPORTED) |
| 11 | + if (FAST_MATH_OPTIMIZATION_SUPPORTED) |
12 | 12 | add_compile_options($<$<CONFIG:Release,RelWithDebInfo>:/fp:fast>) |
13 | | - endif() |
14 | | - else() |
| 13 | + endif () |
| 14 | + elseif (CMAKE_C_COMPILER_ID STREQUAL "Clang" OR CMAKE_C_COMPILER_ID STREQUAL "AppleClang") |
| 15 | + check_c_compiler_flag("-O3" ALL_OPTIMIZATIONS_FLAG_SUPPORTED) |
| 16 | + if (ALL_OPTIMIZATIONS_FLAG_SUPPORTED) |
| 17 | + add_compile_options($<$<CONFIG:Release,RelWithDebInfo>:-O3>) |
| 18 | + endif () |
| 19 | + |
| 20 | + # Clang has deprecated -Ofast, use -ffast-math instead. |
| 21 | + check_c_compiler_flag("-ffast-math" FAST_MATH_OPTIMIZATION_SUPPORTED) |
| 22 | + if (FAST_MATH_OPTIMIZATION_SUPPORTED) |
| 23 | + add_compile_options($<$<CONFIG:Release,RelWithDebInfo>:-ffast-math>) |
| 24 | + endif () |
| 25 | + elseif (CMAKE_C_COMPILER_ID STREQUAL "GNU") |
15 | 26 | check_c_compiler_flag("-Ofast" FAST_MATH_OPTIMIZATION_SUPPORTED) |
16 | | - if(FAST_MATH_OPTIMIZATION_SUPPORTED) |
| 27 | + if (FAST_MATH_OPTIMIZATION_SUPPORTED) |
17 | 28 | add_compile_options($<$<CONFIG:Release,RelWithDebInfo>:-Ofast>) |
18 | | - else() |
| 29 | + else () |
19 | 30 | check_c_compiler_flag("-O3" ALL_OPTIMIZATIONS_FLAG_SUPPORTED) |
20 | | - if(ALL_OPTIMIZATIONS_FLAG_SUPPORTED) |
| 31 | + if (ALL_OPTIMIZATIONS_FLAG_SUPPORTED) |
21 | 32 | add_compile_options($<$<CONFIG:Release,RelWithDebInfo>:-O3>) |
22 | | - endif() |
23 | | - endif() |
24 | | - endif() |
25 | | -endif() |
| 33 | + endif () |
| 34 | + endif () |
| 35 | + else () |
| 36 | + message(AUTHOR_WARNING |
| 37 | + "ENABLE_FAST_MATH was requested, but compiler is not supported: ${CMAKE_C_COMPILER_ID}. " |
| 38 | + "Please use CMAKE_C_FLAGS to pass the correct optimization flags manually.") |
| 39 | + endif () |
| 40 | +endif () |
0 commit comments