xref: /llvm-project/libc/cmake/modules/CheckCompilerFeatures.cmake (revision 029bfd6329aee0b60761998986dadba38e1de6f3)
1# ------------------------------------------------------------------------------
2# Compiler features definition and flags
3# ------------------------------------------------------------------------------
4
5# Initialize ALL_COMPILER_FEATURES as empty list.
6set(ALL_COMPILER_FEATURES "float128")
7
8# Making sure ALL_COMPILER_FEATURES is sorted.
9list(SORT ALL_COMPILER_FEATURES)
10
11# Function to check whether the compiler supports the provided set of features.
12# Usage:
13# compiler_supports(
14#   <output variable>
15#   <list of cpu features>
16# )
17function(compiler_supports output_var features)
18  _intersection(var "${LIBC_CPU_FEATURES}" "${features}")
19  if("${var}" STREQUAL "${features}")
20    set(${output_var} TRUE PARENT_SCOPE)
21  else()
22    unset(${output_var} PARENT_SCOPE)
23  endif()
24endfunction()
25
26# ------------------------------------------------------------------------------
27# Internal helpers and utilities.
28# ------------------------------------------------------------------------------
29
30# Computes the intersection between two lists.
31function(_intersection output_var list1 list2)
32  foreach(element IN LISTS list1)
33    if("${list2}" MATCHES "(^|;)${element}(;|$)")
34      list(APPEND tmp "${element}")
35    endif()
36  endforeach()
37  set(${output_var} ${tmp} PARENT_SCOPE)
38endfunction()
39
40set(AVAILABLE_COMPILER_FEATURES "")
41
42# Try compile a C file to check if flag is supported.
43set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
44foreach(feature IN LISTS ALL_COMPILER_FEATURES)
45  try_compile(
46    has_feature
47    ${CMAKE_CURRENT_BINARY_DIR}/compiler_features
48    SOURCES ${LIBC_SOURCE_DIR}/cmake/modules/compiler_features/check_${feature}.cpp
49    COMPILE_DEFINITIONS -I${LIBC_SOURCE_DIR} ${LIBC_COMPILE_OPTIONS_NATIVE}
50  )
51  if(has_feature)
52    list(APPEND AVAILABLE_COMPILER_FEATURES ${feature})
53    if(${feature} STREQUAL "float128")
54      set(LIBC_COMPILER_HAS_FLOAT128 TRUE)
55    endif()
56  endif()
57endforeach()
58
59message(STATUS "Compiler features available: ${AVAILABLE_COMPILER_FEATURES}")
60
61### Compiler Feature Detection ###
62
63# clang-8+, gcc-12+
64check_cxx_compiler_flag("-ftrivial-auto-var-init=pattern" LIBC_CC_SUPPORTS_PATTERN_INIT)
65
66# clang-6+, gcc-13+
67check_cxx_compiler_flag("-nostdlib++" LIBC_CC_SUPPORTS_NOSTDLIBPP)
68