xref: /llvm-project/libc/cmake/modules/CheckCompilerFeatures.cmake (revision abd91023447d146f36357326fc97c458b49e40af)
13f906f51Slntue# ------------------------------------------------------------------------------
23f906f51Slntue# Compiler features definition and flags
33f906f51Slntue# ------------------------------------------------------------------------------
43f906f51Slntue
54531f82cSOverMightyset(
64531f82cSOverMighty  ALL_COMPILER_FEATURES
74531f82cSOverMighty    "builtin_ceil_floor_rint_trunc"
8e7f8d4beSOverMighty    "builtin_fmax_fmin"
9e7f8d4beSOverMighty    "builtin_fmaxf16_fminf16"
104531f82cSOverMighty    "builtin_round"
114531f82cSOverMighty    "builtin_roundeven"
124531f82cSOverMighty    "float16"
13127349fcSOverMighty    "float16_conversion"
144531f82cSOverMighty    "float128"
154531f82cSOverMighty    "fixed_point"
16*abd91023SShourya Goel    "cfloat16"
17*abd91023SShourya Goel    "cfloat128"
184531f82cSOverMighty)
193f906f51Slntue
203f906f51Slntue# Making sure ALL_COMPILER_FEATURES is sorted.
213f906f51Slntuelist(SORT ALL_COMPILER_FEATURES)
223f906f51Slntue
2381ce7960SOverMighty# Compiler features that are unavailable on GPU targets with the in-tree Clang.
2481ce7960SOverMightyset(
2581ce7960SOverMighty  CPU_ONLY_COMPILER_FEATURES
2681ce7960SOverMighty    "float128"
2781ce7960SOverMighty)
2881ce7960SOverMighty
293f906f51Slntue# Function to check whether the compiler supports the provided set of features.
303f906f51Slntue# Usage:
313f906f51Slntue# compiler_supports(
323f906f51Slntue#   <output variable>
333f906f51Slntue#   <list of cpu features>
343f906f51Slntue# )
353f906f51Slntuefunction(compiler_supports output_var features)
363f906f51Slntue  _intersection(var "${LIBC_CPU_FEATURES}" "${features}")
373f906f51Slntue  if("${var}" STREQUAL "${features}")
383f906f51Slntue    set(${output_var} TRUE PARENT_SCOPE)
393f906f51Slntue  else()
403f906f51Slntue    unset(${output_var} PARENT_SCOPE)
413f906f51Slntue  endif()
423f906f51Slntueendfunction()
433f906f51Slntue
443f906f51Slntue# ------------------------------------------------------------------------------
453f906f51Slntue# Internal helpers and utilities.
463f906f51Slntue# ------------------------------------------------------------------------------
473f906f51Slntue
483f906f51Slntue# Computes the intersection between two lists.
493f906f51Slntuefunction(_intersection output_var list1 list2)
503f906f51Slntue  foreach(element IN LISTS list1)
513f906f51Slntue    if("${list2}" MATCHES "(^|;)${element}(;|$)")
523f906f51Slntue      list(APPEND tmp "${element}")
533f906f51Slntue    endif()
543f906f51Slntue  endforeach()
553f906f51Slntue  set(${output_var} ${tmp} PARENT_SCOPE)
563f906f51Slntueendfunction()
573f906f51Slntue
583f906f51Slntueset(AVAILABLE_COMPILER_FEATURES "")
593f906f51Slntue
603f906f51Slntue# Try compile a C file to check if flag is supported.
613f906f51Slntueforeach(feature IN LISTS ALL_COMPILER_FEATURES)
624531f82cSOverMighty  set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
6384277fe9Slntue  set(compile_options ${LIBC_COMPILE_OPTIONS_NATIVE})
644531f82cSOverMighty  set(link_options "")
6584277fe9Slntue  if(${feature} STREQUAL "fixed_point")
6684277fe9Slntue    list(APPEND compile_options "-ffixed-point")
67127349fcSOverMighty  elseif(${feature} MATCHES "^builtin_" OR
68127349fcSOverMighty         ${feature} STREQUAL "float16_conversion")
694531f82cSOverMighty    set(compile_options ${LIBC_COMPILE_OPTIONS_DEFAULT})
704531f82cSOverMighty    set(link_options -nostdlib)
71127349fcSOverMighty    # The compiler might handle calls to math builtins by generating calls to
72127349fcSOverMighty    # the respective libc math functions, in which case we cannot use these
734531f82cSOverMighty    # builtins in our implementations of these functions. We check that this is
744531f82cSOverMighty    # not the case by trying to link an executable, since linking would fail due
754531f82cSOverMighty    # to unresolved references with -nostdlib if calls to libc functions were
764531f82cSOverMighty    # generated.
77127349fcSOverMighty    #
78127349fcSOverMighty    # We also had issues with soft-float float16 conversion functions using both
79127349fcSOverMighty    # compiler-rt and libgcc, so we also check whether we can convert from and
80127349fcSOverMighty    # to float16 without calls to compiler runtime functions by trying to link
81127349fcSOverMighty    # an executable with -nostdlib.
824531f82cSOverMighty    set(CMAKE_TRY_COMPILE_TARGET_TYPE EXECUTABLE)
8384277fe9Slntue  endif()
8484277fe9Slntue
8581ce7960SOverMighty  if(LIBC_TARGET_OS_IS_GPU)
8681ce7960SOverMighty    # CUDA shouldn't be required to build the libc, only to test it, so we can't
8781ce7960SOverMighty    # try to build CUDA binaries here. Since GPU builds are always compiled with
8881ce7960SOverMighty    # the in-tree Clang, we just hardcode which compiler features are available
8981ce7960SOverMighty    # when targeting GPUs.
9081ce7960SOverMighty    if(feature IN_LIST CPU_ONLY_COMPILER_FEATURES)
9181ce7960SOverMighty      set(has_feature FALSE)
9281ce7960SOverMighty    else()
9381ce7960SOverMighty      set(has_feature TRUE)
9481ce7960SOverMighty    endif()
9581ce7960SOverMighty  else()
963f906f51Slntue    try_compile(
973f906f51Slntue      has_feature
983f906f51Slntue      ${CMAKE_CURRENT_BINARY_DIR}/compiler_features
993f906f51Slntue      SOURCES ${LIBC_SOURCE_DIR}/cmake/modules/compiler_features/check_${feature}.cpp
10084277fe9Slntue      COMPILE_DEFINITIONS -I${LIBC_SOURCE_DIR} ${compile_options}
1014531f82cSOverMighty      LINK_OPTIONS ${link_options}
1023f906f51Slntue    )
10381ce7960SOverMighty  endif()
10481ce7960SOverMighty
1053f906f51Slntue  if(has_feature)
1063f906f51Slntue    list(APPEND AVAILABLE_COMPILER_FEATURES ${feature})
1070eb9e021SOverMighty    if(${feature} STREQUAL "float16")
1080eb9e021SOverMighty      set(LIBC_TYPES_HAS_FLOAT16 TRUE)
109127349fcSOverMighty    elseif(${feature} STREQUAL "float16_conversion")
110127349fcSOverMighty      add_compile_definitions(__LIBC_USE_FLOAT16_CONVERSION)
1110eb9e021SOverMighty    elseif(${feature} STREQUAL "float128")
11275fb825bSGuillaume Chatelet      set(LIBC_TYPES_HAS_FLOAT128 TRUE)
11384277fe9Slntue    elseif(${feature} STREQUAL "fixed_point")
11484277fe9Slntue      set(LIBC_COMPILER_HAS_FIXED_POINT TRUE)
115*abd91023SShourya Goel    elseif(${feature} STREQUAL "cfloat16")
116*abd91023SShourya Goel      set(LIBC_TYPES_HAS_CFLOAT16 TRUE)
117*abd91023SShourya Goel    elseif(${feature} STREQUAL "cfloat128")
118*abd91023SShourya Goel      set(LIBC_TYPES_HAS_CFLOAT128 TRUE)
1194531f82cSOverMighty    elseif(${feature} STREQUAL "builtin_ceil_floor_rint_trunc")
1204531f82cSOverMighty      set(LIBC_COMPILER_HAS_BUILTIN_CEIL_FLOOR_RINT_TRUNC TRUE)
121e7f8d4beSOverMighty    elseif(${feature} STREQUAL "builtin_fmax_fmin")
122e7f8d4beSOverMighty      set(LIBC_COMPILER_HAS_BUILTIN_FMAX_FMIN TRUE)
123e7f8d4beSOverMighty    elseif(${feature} STREQUAL "builtin_fmaxf16_fminf16")
124e7f8d4beSOverMighty      set(LIBC_COMPILER_HAS_BUILTIN_FMAXF16_FMINF16 TRUE)
1254531f82cSOverMighty    elseif(${feature} STREQUAL "builtin_round")
1264531f82cSOverMighty      set(LIBC_COMPILER_HAS_BUILTIN_ROUND TRUE)
1274531f82cSOverMighty    elseif(${feature} STREQUAL "builtin_roundeven")
1284531f82cSOverMighty      set(LIBC_COMPILER_HAS_BUILTIN_ROUNDEVEN TRUE)
1293f906f51Slntue    endif()
1303f906f51Slntue  endif()
1313f906f51Slntueendforeach()
1323f906f51Slntue
133127349fcSOverMightyset(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
134127349fcSOverMightyset(compile_options ${LIBC_COMPILE_OPTIONS_DEFAULT})
135127349fcSOverMightyset(link_options "")
136127349fcSOverMighty
1373f906f51Slntuemessage(STATUS "Compiler features available: ${AVAILABLE_COMPILER_FEATURES}")
1381d5c16d7SNick Desaulniers
1391d5c16d7SNick Desaulniers### Compiler Feature Detection ###
1401d5c16d7SNick Desaulniers
1411d5c16d7SNick Desaulniers# clang-8+, gcc-12+
1421d5c16d7SNick Desaulnierscheck_cxx_compiler_flag("-ftrivial-auto-var-init=pattern" LIBC_CC_SUPPORTS_PATTERN_INIT)
143029bfd63Slntue
144029bfd63Slntue# clang-6+, gcc-13+
145029bfd63Slntuecheck_cxx_compiler_flag("-nostdlib++" LIBC_CC_SUPPORTS_NOSTDLIBPP)
146b8bbc57bSPetr Hosek
147b8bbc57bSPetr Hosek# clang-3.0+
148b8bbc57bSPetr Hosekcheck_cxx_compiler_flag("-nostdlibinc" LIBC_CC_SUPPORTS_NOSTDLIBINC)
149