Bump version to 19.1.0 (final)
[llvm-project.git] / libc / cmake / modules / CheckCompilerFeatures.cmake
bloba6d793d495c45e8b905dfdc45cf5a87278c1e972
1 # ------------------------------------------------------------------------------
2 # Compiler features definition and flags
3 # ------------------------------------------------------------------------------
5 set(
6   ALL_COMPILER_FEATURES
7     "builtin_ceil_floor_rint_trunc"
8     "builtin_round"
9     "builtin_roundeven"
10     "float16"
11     "float128"
12     "fixed_point"
15 # Making sure ALL_COMPILER_FEATURES is sorted.
16 list(SORT ALL_COMPILER_FEATURES)
18 # Function to check whether the compiler supports the provided set of features.
19 # Usage:
20 # compiler_supports(
21 #   <output variable>
22 #   <list of cpu features>
23 # )
24 function(compiler_supports output_var features)
25   _intersection(var "${LIBC_CPU_FEATURES}" "${features}")
26   if("${var}" STREQUAL "${features}")
27     set(${output_var} TRUE PARENT_SCOPE)
28   else()
29     unset(${output_var} PARENT_SCOPE)
30   endif()
31 endfunction()
33 # ------------------------------------------------------------------------------
34 # Internal helpers and utilities.
35 # ------------------------------------------------------------------------------
37 # Computes the intersection between two lists.
38 function(_intersection output_var list1 list2)
39   foreach(element IN LISTS list1)
40     if("${list2}" MATCHES "(^|;)${element}(;|$)")
41       list(APPEND tmp "${element}")
42     endif()
43   endforeach()
44   set(${output_var} ${tmp} PARENT_SCOPE)
45 endfunction()
47 set(AVAILABLE_COMPILER_FEATURES "")
49 # Try compile a C file to check if flag is supported.
50 foreach(feature IN LISTS ALL_COMPILER_FEATURES)
51   set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
52   set(compile_options ${LIBC_COMPILE_OPTIONS_NATIVE})
53   set(link_options "")
54   if(${feature} STREQUAL "fixed_point")
55     list(APPEND compile_options "-ffixed-point")
56   elseif(${feature} MATCHES "^builtin_")
57     set(compile_options ${LIBC_COMPILE_OPTIONS_DEFAULT})
58     set(link_options -nostdlib)
59     # The compiler might handle calls to rounding builtins by generating calls
60     # to the respective libc math functions, in which case we cannot use these
61     # builtins in our implementations of these functions. We check that this is
62     # not the case by trying to link an executable, since linking would fail due
63     # to unresolved references with -nostdlib if calls to libc functions were
64     # generated.
65     set(CMAKE_TRY_COMPILE_TARGET_TYPE EXECUTABLE)
66   endif()
68   try_compile(
69     has_feature
70     ${CMAKE_CURRENT_BINARY_DIR}/compiler_features
71     SOURCES ${LIBC_SOURCE_DIR}/cmake/modules/compiler_features/check_${feature}.cpp
72     COMPILE_DEFINITIONS -I${LIBC_SOURCE_DIR} ${compile_options}
73     LINK_OPTIONS ${link_options}
74   )
75   if(has_feature)
76     list(APPEND AVAILABLE_COMPILER_FEATURES ${feature})
77     if(${feature} STREQUAL "float16")
78       set(LIBC_TYPES_HAS_FLOAT16 TRUE)
79     elseif(${feature} STREQUAL "float128")
80       set(LIBC_TYPES_HAS_FLOAT128 TRUE)
81     elseif(${feature} STREQUAL "fixed_point")
82       set(LIBC_COMPILER_HAS_FIXED_POINT TRUE)
83     elseif(${feature} STREQUAL "builtin_ceil_floor_rint_trunc")
84       set(LIBC_COMPILER_HAS_BUILTIN_CEIL_FLOOR_RINT_TRUNC TRUE)
85     elseif(${feature} STREQUAL "builtin_round")
86       set(LIBC_COMPILER_HAS_BUILTIN_ROUND TRUE)
87     elseif(${feature} STREQUAL "builtin_roundeven")
88       set(LIBC_COMPILER_HAS_BUILTIN_ROUNDEVEN TRUE)
89     endif()
90   endif()
91 endforeach()
93 message(STATUS "Compiler features available: ${AVAILABLE_COMPILER_FEATURES}")
95 ### Compiler Feature Detection ###
97 # clang-8+, gcc-12+
98 check_cxx_compiler_flag("-ftrivial-auto-var-init=pattern" LIBC_CC_SUPPORTS_PATTERN_INIT)
100 # clang-6+, gcc-13+
101 check_cxx_compiler_flag("-nostdlib++" LIBC_CC_SUPPORTS_NOSTDLIBPP)
103 # clang-3.0+
104 check_cxx_compiler_flag("-nostdlibinc" LIBC_CC_SUPPORTS_NOSTDLIBINC)