| if(NOT DEFINED LLVM_LIBC_COMPILER_IS_GCC_COMPATIBLE) | |
| if(CMAKE_COMPILER_IS_GNUCXX) | |
| set(LLVM_LIBC_COMPILER_IS_GCC_COMPATIBLE ON) | |
| elseif( MSVC ) | |
| set(LLVM_LIBC_COMPILER_IS_GCC_COMPATIBLE OFF) | |
| elseif( "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang" ) | |
| set(LLVM_LIBC_COMPILER_IS_GCC_COMPATIBLE ON) | |
| elseif( "${CMAKE_CXX_COMPILER_ID}" MATCHES "Intel" ) | |
| set(LLVM_LIBC_COMPILER_IS_GCC_COMPATIBLE ON) | |
| endif() | |
| endif() | |
| function(libc_add_definition output_var def) | |
| if(MSVC) | |
| list(APPEND ${output_var} "/D${def}") | |
| else() | |
| list(APPEND ${output_var} "-D${def}") | |
| endif() | |
| set(${output_var} ${${output_var}} PARENT_SCOPE) | |
| endfunction() | |
| function(libc_set_definition output_var) | |
| set(defs "") | |
| if(MSVC) | |
| foreach(def IN LISTS ARGN) | |
| list(APPEND defs "/D${def}") | |
| endforeach() | |
| else() | |
| foreach(def IN LISTS ARGN) | |
| list(APPEND defs "-D${def}") | |
| endforeach() | |
| endif() | |
| set(${output_var} "${defs}" PARENT_SCOPE) | |
| endfunction() | |
| function(_get_compile_options_from_flags output_var) | |
| if(NOT ARGN) | |
| set(${output_var} "" PARENT_SCOPE) | |
| return() | |
| endif() | |
| set(compile_options "") | |
| if(LIBC_CPU_FEATURES MATCHES "FMA") | |
| check_flag(ADD_FMA_FLAG ${FMA_OPT_FLAG} ${ARGN}) | |
| endif() | |
| check_flag(ADD_ROUND_OPT_FLAG ${ROUND_OPT_FLAG} ${ARGN}) | |
| check_flag(ADD_EXPLICIT_SIMD_OPT_FLAG ${EXPLICIT_SIMD_OPT_FLAG} ${ARGN}) | |
| check_flag(ADD_MISC_MATH_BASIC_OPS_OPT_FLAG ${MISC_MATH_BASIC_OPS_OPT_FLAG} ${ARGN}) | |
| if(LLVM_LIBC_COMPILER_IS_GCC_COMPATIBLE) | |
| if(ADD_FMA_FLAG) | |
| if(LIBC_TARGET_ARCHITECTURE_IS_X86_64) | |
| list(APPEND compile_options "-mavx2") | |
| list(APPEND compile_options "-mfma") | |
| endif() | |
| endif() | |
| if(LIBC_COMPILER_HAS_BUILTIN_ISNAN) | |
| list(APPEND compile_options "-D__LIBC_USE_BUILTIN_ISNAN") | |
| endif() | |
| if(ADD_ROUND_OPT_FLAG) | |
| if(LIBC_TARGET_ARCHITECTURE_IS_X86_64) | |
| # ROUND_OPT_FLAG is only enabled if SSE4.2 is detected, not just SSE4.1, | |
| # because there was code to check for SSE4.2 already, and few CPUs only | |
| # have SSE4.1. | |
| list(APPEND compile_options "-msse4.2") | |
| endif() | |
| if(LIBC_COMPILER_HAS_BUILTIN_CEIL_FLOOR_RINT_TRUNC) | |
| list(APPEND compile_options | |
| "-D__LIBC_USE_BUILTIN_CEIL_FLOOR_RINT_TRUNC") | |
| endif() | |
| if(LIBC_COMPILER_HAS_BUILTIN_ROUND) | |
| list(APPEND compile_options "-D__LIBC_USE_BUILTIN_ROUND") | |
| endif() | |
| if(LIBC_COMPILER_HAS_BUILTIN_ROUNDEVEN) | |
| list(APPEND compile_options "-D__LIBC_USE_BUILTIN_ROUNDEVEN") | |
| endif() | |
| endif() | |
| if(ADD_MISC_MATH_BASIC_OPS_OPT_FLAG) | |
| list(APPEND compile_options "-D__LIBC_MISC_MATH_BASIC_OPS_OPT") | |
| if(LIBC_COMPILER_HAS_BUILTIN_FMAX_FMIN) | |
| list(APPEND compile_options "-D__LIBC_USE_BUILTIN_FMAX_FMIN") | |
| endif() | |
| if(LIBC_COMPILER_HAS_BUILTIN_FMAXF16_FMINF16) | |
| list(APPEND compile_options "-D__LIBC_USE_BUILTIN_FMAXF16_FMINF16") | |
| endif() | |
| if("FullFP16" IN_LIST LIBC_CPU_FEATURES AND | |
| CMAKE_CXX_COMPILER_ID STREQUAL "Clang") | |
| list(APPEND compile_options | |
| "SHELL:-Xclang -target-feature -Xclang +fullfp16") | |
| endif() | |
| endif() | |
| elseif(MSVC) | |
| if(ADD_FMA_FLAG) | |
| list(APPEND compile_options "/arch:AVX2") | |
| endif() | |
| endif() | |
| if(ADD_EXPLICIT_SIMD_OPT_FLAG) | |
| libc_add_definition(compile_options "__LIBC_EXPLICIT_SIMD_OPT") | |
| endif() | |
| set(${output_var} ${compile_options} PARENT_SCOPE) | |
| endfunction() | |
| function(_get_compile_options_from_config output_var) | |
| set(config_options "") | |
| if(LIBC_CONF_STRTOFLOAT_DISABLE_EISEL_LEMIRE) | |
| libc_add_definition(config_options "LIBC_COPT_STRTOFLOAT_DISABLE_EISEL_LEMIRE") | |
| endif() | |
| if(LIBC_CONF_STRTOFLOAT_DISABLE_SIMPLE_DECIMAL_CONVERSION) | |
| libc_add_definition(config_options "LIBC_COPT_STRTOFLOAT_DISABLE_SIMPLE_DECIMAL_CONVERSION") | |
| endif() | |
| if(LIBC_CONF_STRTOFLOAT_DISABLE_CLINGER_FAST_PATH) | |
| libc_add_definition(config_options "LIBC_COPT_STRTOFLOAT_DISABLE_CLINGER_FAST_PATH") | |
| endif() | |
| if(LIBC_CONF_QSORT_IMPL) | |
| libc_add_definition(config_options "LIBC_QSORT_IMPL=${LIBC_CONF_QSORT_IMPL}") | |
| endif() | |
| libc_add_definition(config_options "LIBC_COPT_STRING_LENGTH_IMPL=${LIBC_CONF_STRING_LENGTH_IMPL}") | |
| libc_add_definition(config_options "LIBC_COPT_FIND_FIRST_CHARACTER_IMPL=${LIBC_CONF_FIND_FIRST_CHARACTER_IMPL}") | |
| if(LIBC_CONF_MEMSET_X86_USE_SOFTWARE_PREFETCHING) | |
| libc_add_definition(config_options "LIBC_COPT_MEMSET_X86_USE_SOFTWARE_PREFETCHING") | |
| endif() | |
| if(LIBC_CONF_MEMCPY_X86_USE_NTA_STORES) | |
| libc_add_definition(config_options "LIBC_COPT_MEMCPY_X86_USE_NTA_STORES") | |
| endif() | |
| if(LIBC_CONF_USE_MEM_BUILTINS) | |
| libc_add_definition(config_options "LIBC_COPT_USE_MEM_BUILTINS") | |
| endif() | |
| if(LIBC_ADD_NULL_CHECKS) | |
| libc_add_definition(config_options "LIBC_ADD_NULL_CHECKS") | |
| endif() | |
| if(NOT "${LIBC_CONF_FREXP_INF_NAN_EXPONENT}" STREQUAL "") | |
| libc_add_definition(config_options "LIBC_FREXP_INF_NAN_EXPONENT=${LIBC_CONF_FREXP_INF_NAN_EXPONENT}") | |
| endif() | |
| if(LIBC_CONF_MATH_OPTIMIZATIONS) | |
| libc_add_definition(config_options "LIBC_MATH=(${LIBC_CONF_MATH_OPTIMIZATIONS})") | |
| if(LIBC_CONF_MATH_OPTIMIZATIONS MATCHES "LIBC_MATH_NO_ERRNO") | |
| list(APPEND config_options "-fno-math-errno") | |
| endif() | |
| endif() | |
| if(LIBC_CONF_ERRNO_MODE) | |
| libc_add_definition(config_options "LIBC_ERRNO_MODE=${LIBC_CONF_ERRNO_MODE}") | |
| endif() | |
| if(LIBC_CONF_THREAD_MODE) | |
| libc_add_definition(config_options "LIBC_THREAD_MODE=${LIBC_CONF_THREAD_MODE}") | |
| endif() | |
| if(LIBC_CONF_TIMEOUT_ENSURE_MONOTONICITY) | |
| libc_add_definition(config_options "LIBC_COPT_TIMEOUT_ENSURE_MONOTONICITY") | |
| endif() | |
| if(LIBC_CONF_TRAP_ON_RAISE_FP_EXCEPT) | |
| libc_add_definition(config_options "LIBC_TRAP_ON_RAISE_FP_EXCEPT") | |
| endif() | |
| if(LIBC_CONF_WCTYPE_MODE) | |
| libc_add_definition(config_options "LIBC_CONF_WCTYPE_MODE=${LIBC_CONF_WCTYPE_MODE}") | |
| endif() | |
| if(LIBC_CONF_RAW_MUTEX_DEFAULT_SPIN_COUNT) | |
| libc_add_definition(config_options "LIBC_COPT_RAW_MUTEX_DEFAULT_SPIN_COUNT=${LIBC_CONF_RAW_MUTEX_DEFAULT_SPIN_COUNT}") | |
| endif() | |
| if(LIBC_CONF_MATH_USE_SYSTEM_FENV) | |
| libc_add_definition(config_options "LIBC_MATH_USE_SYSTEM_FENV") | |
| endif() | |
| if(LIBC_CONF_CTYPE_SMALLER_ASCII) | |
| libc_add_definition(config_options "LIBC_COPT_CTYPE_SMALLER_ASCII") | |
| endif() | |
| if(LIBC_CONF_PRINTF_DISABLE_WIDE) | |
| libc_add_definition(config_options "LIBC_COPT_PRINTF_DISABLE_WIDE") | |
| endif() | |
| if(LIBC_COPT_PRINTF_DISABLE_BITINT) | |
| libc_add_definition(config_options "LIBC_COPT_PRINTF_DISABLE_BITINT") | |
| endif() | |
| if(LIBC_COPT_BAREMETAL_HEAP_ENABLE_FREESTORE_ROTATION) | |
| libc_add_definition(config_options "LIBC_COPT_BAREMETAL_HEAP_ENABLE_FREESTORE_ROTATION") | |
| endif() | |
| if(LIBC_COPT_USE_C_ASSERT) | |
| list(APPEND config_options "-DLIBC_COPT_USE_C_ASSERT") | |
| endif() | |
| if(LIBC_COPT_HARDENING_MODE) | |
| libc_add_definition(config_options "LIBC_COPT_HARDENING_MODE=${LIBC_COPT_HARDENING_MODE}") | |
| endif() | |
| if(LIBC_CONF_SCANF_PROVIDE_ISOC99_ALIASES) | |
| list(APPEND config_options "-DLIBC_COPT_SCANF_PROVIDE_ISOC99_ALIASES") | |
| endif() | |
| set(${output_var} ${config_options} PARENT_SCOPE) | |
| endfunction() | |
| _get_compile_options_from_config(LIBC_COMPILE_OPTIONS_FROM_CONFIG) | |
| function(_get_compile_options_from_arch output_var) | |
| # Set options that are not found in src/__support/macros/properties/architectures.h | |
| # and src/__support/macros/properties/os.h | |
| # TODO: we probably want to unify these at some point for consistency | |
| set(config_options "") | |
| if(LIBC_TARGET_OS_IS_BAREMETAL) | |
| libc_add_definition(config_options "LIBC_TARGET_OS_IS_BAREMETAL") | |
| endif() | |
| if(LIBC_TARGET_OS_IS_GPU) | |
| libc_add_definition(config_options "LIBC_TARGET_OS_IS_GPU") | |
| endif() | |
| if(LIBC_TARGET_OS_IS_UEFI) | |
| libc_add_definition(config_options "LIBC_TARGET_OS_IS_UEFI") | |
| endif() | |
| set(${output_var} ${config_options} PARENT_SCOPE) | |
| endfunction() | |
| _get_compile_options_from_arch(LIBC_COMPILE_OPTIONS_FROM_ARCH) | |
| function(_get_common_compile_options_impl output_var) | |
| set(compile_options ${LIBC_COMPILE_OPTIONS_FROM_CONFIG} ${LIBC_COMPILE_OPTIONS_FROM_ARCH}) | |
| if(LLVM_LIBC_COMPILER_IS_GCC_COMPATIBLE) | |
| if(LLVM_LIBC_FULL_BUILD) | |
| # Only add -ffreestanding flag in non-GPU full build mode. | |
| if(NOT LIBC_TARGET_OS_IS_GPU) | |
| list(APPEND compile_options "-ffreestanding") | |
| endif() | |
| list(APPEND compile_options "-DLIBC_FULL_BUILD") | |
| # Manually disable standard include paths to prevent system headers from | |
| # being included. | |
| if(LIBC_CC_SUPPORTS_NOSTDLIBINC) | |
| list(APPEND compile_options "-nostdlibinc") | |
| elseif(COMPILER_RESOURCE_DIR) | |
| # TODO: We should require COMPILER_RESOURCE_DIR to be set. | |
| list(APPEND compile_options "-isystem${COMPILER_RESOURCE_DIR}/include") | |
| list(APPEND compile_options "-nostdinc") | |
| endif() | |
| # TODO: We should set this unconditionally on Linux. | |
| if(LIBC_TARGET_OS_IS_LINUX AND | |
| (LIBC_CC_SUPPORTS_NOSTDLIBINC OR COMPILER_RESOURCE_DIR)) | |
| # We use -idirafter to avoid preempting libc's own headers in case the | |
| # directory (e.g. /usr/include) contains other headers. | |
| foreach(path IN LISTS LIBC_KERNEL_HEADERS) | |
| if(CMAKE_CROSSCOMPILING) | |
| list(APPEND compile_options "-idirafter=${path}") | |
| else() | |
| list(APPEND compile_options "-idirafter${path}") | |
| endif() | |
| endforeach() | |
| endif() | |
| endif() | |
| if(LIBC_COMPILER_HAS_FIXED_POINT) | |
| list(APPEND compile_options "-ffixed-point") | |
| endif() | |
| if(NOT LIBC_TARGET_OS_IS_GPU) | |
| list(APPEND compile_options "-fno-builtin") | |
| endif() | |
| list(APPEND compile_options "-fno-exceptions") | |
| list(APPEND compile_options "-fno-lax-vector-conversions") | |
| list(APPEND compile_options "-fno-unwind-tables") | |
| list(APPEND compile_options "-fno-asynchronous-unwind-tables") | |
| list(APPEND compile_options "-fno-rtti") | |
| if(LIBC_CC_SUPPORTS_PATTERN_INIT) | |
| list(APPEND compile_options "-ftrivial-auto-var-init=pattern") | |
| endif() | |
| if(LIBC_CONF_KEEP_FRAME_POINTER) | |
| list(APPEND compile_options "-fno-omit-frame-pointer") | |
| if(LIBC_TARGET_ARCHITECTURE_IS_X86_64) | |
| list(APPEND compile_options "-mno-omit-leaf-frame-pointer") | |
| endif() | |
| endif() | |
| if(LIBC_CONF_ENABLE_STRONG_STACK_PROTECTOR) | |
| list(APPEND compile_options "-fstack-protector-strong") | |
| endif() | |
| list(APPEND compile_options "-Wall") | |
| list(APPEND compile_options "-Wextra") | |
| # -DLIBC_WNO_ERROR=ON if you can't build cleanly with -Werror. | |
| if(NOT LIBC_WNO_ERROR) | |
| list(APPEND compile_options "-Werror") | |
| endif() | |
| if(NOT (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS "13.0.0")) | |
| list(APPEND compile_options "-Wconversion") | |
| else() | |
| list(APPEND compile_options "-Wno-type-limits") | |
| list(APPEND compile_options "-Wno-attributes") | |
| endif() | |
| list(APPEND compile_options "-Wno-sign-conversion") | |
| list(APPEND compile_options "-Wdeprecated") | |
| if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") | |
| # Silence this warning because _Complex is a part of C99. | |
| list(APPEND compile_options "-fext-numeric-literals") | |
| else() | |
| list(APPEND compile_options "-Wno-c99-extensions") | |
| list(APPEND compile_options "-Wno-gnu-imaginary-constant") | |
| endif() | |
| list(APPEND compile_options "-Wno-pedantic") | |
| list(APPEND compile_options "-Wimplicit-fallthrough") | |
| list(APPEND compile_options "-Wwrite-strings") | |
| if(LIBC_CC_SUPPORTS_EXTRA_SEMI) | |
| list(APPEND compile_options "-Wextra-semi") | |
| endif() | |
| if(NOT CMAKE_COMPILER_IS_GNUCXX) | |
| list(APPEND compile_options "-Wnewline-eof") | |
| list(APPEND compile_options "-Wnonportable-system-include-path") | |
| list(APPEND compile_options "-Wstrict-prototypes") | |
| list(APPEND compile_options "-Wthread-safety") | |
| list(APPEND compile_options "-Wglobal-constructors") | |
| endif() | |
| # Older Clang versions emit false positive shadow warnings for lambda captures | |
| # inside static member functions (fixed in Clang 22, see PR #157667 and #165919). | |
| if(CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL "22.0.0") | |
| list(APPEND compile_options "-Wshadow") | |
| endif() | |
| if(LIBC_COMPILER_HAS_STDC_FENV_ACCESS) | |
| list(APPEND compile_options "-DLIBC_COMPILER_HAS_STDC_FENV_ACCESS") | |
| endif() | |
| elseif(MSVC) | |
| list(APPEND compile_options "/EHs-c-") | |
| list(APPEND compile_options "/GR-") | |
| endif() | |
| if(LIBC_TARGET_OS_IS_GPU) | |
| list(APPEND compile_options "-nogpulib") | |
| list(APPEND compile_options "-fvisibility=hidden") | |
| list(APPEND compile_options "-fconvergent-functions") | |
| list(APPEND compile_options "-flto") | |
| list(APPEND compile_options "-Wno-multi-gpu") | |
| if(LIBC_TARGET_ARCHITECTURE_IS_NVPTX) | |
| list(APPEND compile_options "-Wno-unknown-cuda-version") | |
| list(APPEND compile_options "--cuda-feature=+ptx63") | |
| if(LIBC_CUDA_ROOT) | |
| list(APPEND compile_options "--cuda-path=${LIBC_CUDA_ROOT}") | |
| endif() | |
| elseif(LIBC_TARGET_ARCHITECTURE_IS_AMDGPU) | |
| list(APPEND compile_options "SHELL:-Xclang -mcode-object-version=none") | |
| endif() | |
| endif() | |
| set(${output_var} ${compile_options} PARENT_SCOPE) | |
| endfunction() | |
| _get_common_compile_options_impl(LIBC_COMMON_COMPILE_OPTIONS) | |
| function(_get_common_compile_options output_var flags) | |
| _get_compile_options_from_flags(compile_flags ${flags}) | |
| set(${output_var} ${LIBC_COMPILE_OPTIONS_DEFAULT} ${compile_flags} ${LIBC_COMMON_COMPILE_OPTIONS} PARENT_SCOPE) | |
| endfunction() |