1 # ------------------------------------------------------------------------------
2 # Compiler features definition and flags
3 # ------------------------------------------------------------------------------
7 "builtin_ceil_floor_rint_trunc"
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.
22 # <list of cpu features>
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)
29 unset(${output_var} PARENT_SCOPE)
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}")
44 set(${output_var} ${tmp} PARENT_SCOPE)
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})
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
65 set(CMAKE_TRY_COMPILE_TARGET_TYPE EXECUTABLE)
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}
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)
93 message(STATUS "Compiler features available: ${AVAILABLE_COMPILER_FEATURES}")
95 ### Compiler Feature Detection ###
98 check_cxx_compiler_flag("-ftrivial-auto-var-init=pattern" LIBC_CC_SUPPORTS_PATTERN_INIT)
101 check_cxx_compiler_flag("-nostdlib++" LIBC_CC_SUPPORTS_NOSTDLIBPP)
104 check_cxx_compiler_flag("-nostdlibinc" LIBC_CC_SUPPORTS_NOSTDLIBINC)