254 lines · plain
1if(NOT DEFINED LLVM_LIBC_COMPILER_IS_GCC_COMPATIBLE)2 if(CMAKE_COMPILER_IS_GNUCXX)3 set(LLVM_LIBC_COMPILER_IS_GCC_COMPATIBLE ON)4 elseif( MSVC )5 set(LLVM_LIBC_COMPILER_IS_GCC_COMPATIBLE OFF)6 elseif( "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang" )7 set(LLVM_LIBC_COMPILER_IS_GCC_COMPATIBLE ON)8 elseif( "${CMAKE_CXX_COMPILER_ID}" MATCHES "Intel" )9 set(LLVM_LIBC_COMPILER_IS_GCC_COMPATIBLE ON)10 endif()11endif()12 13function(_get_compile_options_from_flags output_var)14 set(compile_options "")15 16 if(LIBC_CPU_FEATURES MATCHES "FMA")17 check_flag(ADD_FMA_FLAG ${FMA_OPT_FLAG} ${ARGN})18 endif()19 check_flag(ADD_ROUND_OPT_FLAG ${ROUND_OPT_FLAG} ${ARGN})20 check_flag(ADD_EXPLICIT_SIMD_OPT_FLAG ${EXPLICIT_SIMD_OPT_FLAG} ${ARGN})21 check_flag(ADD_MISC_MATH_BASIC_OPS_OPT_FLAG ${MISC_MATH_BASIC_OPS_OPT_FLAG} ${ARGN})22 23 if(LLVM_LIBC_COMPILER_IS_GCC_COMPATIBLE)24 if(ADD_FMA_FLAG)25 if(LIBC_TARGET_ARCHITECTURE_IS_X86_64)26 list(APPEND compile_options "-mavx2")27 list(APPEND compile_options "-mfma")28 endif()29 endif()30 if(ADD_ROUND_OPT_FLAG)31 if(LIBC_TARGET_ARCHITECTURE_IS_X86_64)32 # ROUND_OPT_FLAG is only enabled if SSE4.2 is detected, not just SSE4.1,33 # because there was code to check for SSE4.2 already, and few CPUs only34 # have SSE4.1.35 list(APPEND compile_options "-msse4.2")36 endif()37 if(LIBC_COMPILER_HAS_BUILTIN_CEIL_FLOOR_RINT_TRUNC)38 list(APPEND compile_options39 "-D__LIBC_USE_BUILTIN_CEIL_FLOOR_RINT_TRUNC")40 endif()41 if(LIBC_COMPILER_HAS_BUILTIN_ROUND)42 list(APPEND compile_options "-D__LIBC_USE_BUILTIN_ROUND")43 endif()44 if(LIBC_COMPILER_HAS_BUILTIN_ROUNDEVEN)45 list(APPEND compile_options "-D__LIBC_USE_BUILTIN_ROUNDEVEN")46 endif()47 endif()48 if(ADD_EXPLICIT_SIMD_OPT_FLAG)49 list(APPEND compile_options "-D__LIBC_EXPLICIT_SIMD_OPT")50 endif()51 if(ADD_MISC_MATH_BASIC_OPS_OPT_FLAG)52 list(APPEND compile_options "-D__LIBC_MISC_MATH_BASIC_OPS_OPT")53 if(LIBC_COMPILER_HAS_BUILTIN_FMAX_FMIN)54 list(APPEND compile_options "-D__LIBC_USE_BUILTIN_FMAX_FMIN")55 endif()56 if(LIBC_COMPILER_HAS_BUILTIN_FMAXF16_FMINF16)57 list(APPEND compile_options "-D__LIBC_USE_BUILTIN_FMAXF16_FMINF16")58 endif()59 if("FullFP16" IN_LIST LIBC_CPU_FEATURES AND60 CMAKE_CXX_COMPILER_ID STREQUAL "Clang")61 list(APPEND compile_options62 "SHELL:-Xclang -target-feature -Xclang +fullfp16")63 endif()64 endif()65 elseif(MSVC)66 if(ADD_FMA_FLAG)67 list(APPEND compile_options "/arch:AVX2")68 endif()69 if(ADD_EXPLICIT_SIMD_OPT_FLAG)70 list(APPEND compile_options "/D__LIBC_EXPLICIT_SIMD_OPT")71 endif()72 endif()73 74 set(${output_var} ${compile_options} PARENT_SCOPE)75endfunction(_get_compile_options_from_flags)76 77function(_get_compile_options_from_config output_var)78 set(config_options "")79 80 if(LIBC_CONF_QSORT_IMPL)81 list(APPEND config_options "-DLIBC_QSORT_IMPL=${LIBC_CONF_QSORT_IMPL}")82 endif()83 84 if(LIBC_CONF_STRING_UNSAFE_WIDE_READ)85 list(APPEND config_options "-DLIBC_COPT_STRING_UNSAFE_WIDE_READ")86 endif()87 88 if(LIBC_CONF_MEMSET_X86_USE_SOFTWARE_PREFETCHING)89 list(APPEND config_options "-DLIBC_COPT_MEMSET_X86_USE_SOFTWARE_PREFETCHING")90 endif()91 92 if(LIBC_TYPES_TIME_T_IS_32_BIT AND LLVM_LIBC_FULL_BUILD)93 list(APPEND config_options "-DLIBC_TYPES_TIME_T_IS_32_BIT")94 endif()95 96 if(LIBC_ADD_NULL_CHECKS)97 list(APPEND config_options "-DLIBC_ADD_NULL_CHECKS")98 endif()99 100 if(NOT "${LIBC_CONF_FREXP_INF_NAN_EXPONENT}" STREQUAL "")101 list(APPEND config_options "-DLIBC_FREXP_INF_NAN_EXPONENT=${LIBC_CONF_FREXP_INF_NAN_EXPONENT}")102 endif()103 104 if(LIBC_CONF_MATH_OPTIMIZATIONS)105 list(APPEND config_options "-DLIBC_MATH=${LIBC_CONF_MATH_OPTIMIZATIONS}")106 if(LIBC_CONF_MATH_OPTIMIZATIONS MATCHES "LIBC_MATH_NO_ERRNO")107 list(APPEND config_options "-fno-math-errno")108 endif()109 endif()110 111 if(LIBC_CONF_ERRNO_MODE)112 list(APPEND config_options "-DLIBC_ERRNO_MODE=${LIBC_CONF_ERRNO_MODE}")113 endif()114 115 if(LIBC_CONF_THREAD_MODE)116 list(APPEND config_options "-DLIBC_THREAD_MODE=${LIBC_CONF_THREAD_MODE}")117 endif()118 119 set(${output_var} ${config_options} PARENT_SCOPE)120endfunction(_get_compile_options_from_config)121 122function(_get_compile_options_from_arch output_var)123 # Set options that are not found in src/__support/macros/properties/architectures.h124 # and src/__support/macros/properties/os.h125 # TODO: we probably want to unify these at some point for consistency126 set(config_options "")127 128 if (LIBC_TARGET_OS_IS_BAREMETAL)129 list(APPEND config_options "-DLIBC_TARGET_OS_IS_BAREMETAL") 130 endif()131 if (LIBC_TARGET_OS_IS_GPU)132 list(APPEND config_options "-DLIBC_TARGET_OS_IS_GPU") 133 endif()134 if (LIBC_TARGET_OS_IS_UEFI)135 list(APPEND config_options "-DLIBC_TARGET_OS_IS_UEFI") 136 endif()137 138 set(${output_var} ${config_options} PARENT_SCOPE)139endfunction(_get_compile_options_from_arch)140 141function(_get_common_compile_options output_var flags)142 _get_compile_options_from_flags(compile_flags ${flags})143 _get_compile_options_from_config(config_flags)144 _get_compile_options_from_arch(arch_flags)145 146 set(compile_options ${LIBC_COMPILE_OPTIONS_DEFAULT} ${compile_flags} ${config_flags} ${arch_flags})147 148 if(LLVM_LIBC_COMPILER_IS_GCC_COMPATIBLE)149 list(APPEND compile_options "-fpie")150 151 if(LLVM_LIBC_FULL_BUILD)152 # Only add -ffreestanding flag in non-GPU full build mode.153 if(NOT LIBC_TARGET_OS_IS_GPU)154 list(APPEND compile_options "-ffreestanding")155 endif()156 list(APPEND compile_options "-DLIBC_FULL_BUILD")157 # Manually disable standard include paths to prevent system headers from158 # being included.159 if(LIBC_CC_SUPPORTS_NOSTDLIBINC)160 list(APPEND compile_options "-nostdlibinc")161 elseif(COMPILER_RESOURCE_DIR)162 # TODO: We should require COMPILER_RESOURCE_DIR to be set.163 list(APPEND compile_options "-isystem${COMPILER_RESOURCE_DIR}/include")164 list(APPEND compile_options "-nostdinc")165 endif()166 # TODO: We should set this unconditionally on Linux.167 if(LIBC_TARGET_OS_IS_LINUX AND168 (LIBC_CC_SUPPORTS_NOSTDLIBINC OR COMPILER_RESOURCE_DIR))169 # We use -idirafter to avoid preempting libc's own headers in case the170 # directory (e.g. /usr/include) contains other headers.171 if(CMAKE_CROSSCOMPILING)172 list(APPEND compile_options "-idirafter=${LIBC_KERNEL_HEADERS}")173 else()174 list(APPEND compile_options "-idirafter${LIBC_KERNEL_HEADERS}")175 endif()176 endif()177 endif()178 179 if(LIBC_COMPILER_HAS_FIXED_POINT)180 list(APPEND compile_options "-ffixed-point")181 endif()182 183 if(NOT LIBC_TARGET_OS_IS_GPU)184 list(APPEND compile_options "-fno-builtin")185 endif()186 187 list(APPEND compile_options "-fno-exceptions")188 list(APPEND compile_options "-fno-lax-vector-conversions")189 list(APPEND compile_options "-fno-unwind-tables")190 list(APPEND compile_options "-fno-asynchronous-unwind-tables")191 list(APPEND compile_options "-fno-rtti")192 if (LIBC_CC_SUPPORTS_PATTERN_INIT)193 list(APPEND compile_options "-ftrivial-auto-var-init=pattern")194 endif()195 if (LIBC_CONF_KEEP_FRAME_POINTER)196 list(APPEND compile_options "-fno-omit-frame-pointer")197 if (LIBC_TARGET_ARCHITECTURE_IS_X86_64)198 list(APPEND compile_options "-mno-omit-leaf-frame-pointer")199 endif()200 endif()201 if (LIBC_CONF_ENABLE_STACK_PROTECTOR)202 list(APPEND compile_options "-fstack-protector-strong")203 endif()204 list(APPEND compile_options "-Wall")205 list(APPEND compile_options "-Wextra")206 # -DLIBC_WNO_ERROR=ON if you can't build cleanly with -Werror.207 if(NOT LIBC_WNO_ERROR)208 list(APPEND compile_options "-Werror")209 endif()210 list(APPEND compile_options "-Wconversion")211 list(APPEND compile_options "-Wno-sign-conversion")212 list(APPEND compile_options "-Wdeprecated")213 if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")214 # Silence this warning because _Complex is a part of C99.215 list(APPEND compile_options "-fext-numeric-literals")216 else()217 list(APPEND compile_options "-Wno-c99-extensions")218 list(APPEND compile_options "-Wno-gnu-imaginary-constant")219 endif()220 list(APPEND compile_options "-Wno-pedantic")221 list(APPEND compile_options "-Wimplicit-fallthrough")222 list(APPEND compile_options "-Wwrite-strings")223 list(APPEND compile_options "-Wextra-semi")224 if(NOT CMAKE_COMPILER_IS_GNUCXX)225 list(APPEND compile_options "-Wnewline-eof")226 list(APPEND compile_options "-Wnonportable-system-include-path")227 list(APPEND compile_options "-Wstrict-prototypes")228 list(APPEND compile_options "-Wthread-safety")229 list(APPEND compile_options "-Wglobal-constructors")230 endif()231 elseif(MSVC)232 list(APPEND compile_options "/EHs-c-")233 list(APPEND compile_options "/GR-")234 endif()235 if (LIBC_TARGET_OS_IS_GPU)236 list(APPEND compile_options "-nogpulib")237 list(APPEND compile_options "-fvisibility=hidden")238 list(APPEND compile_options "-fconvergent-functions")239 list(APPEND compile_options "-flto")240 list(APPEND compile_options "-Wno-multi-gpu")241 242 if(LIBC_TARGET_ARCHITECTURE_IS_NVPTX)243 list(APPEND compile_options "-Wno-unknown-cuda-version")244 list(APPEND compile_options "--cuda-feature=+ptx63")245 if(LIBC_CUDA_ROOT)246 list(APPEND compile_options "--cuda-path=${LIBC_CUDA_ROOT}")247 endif()248 elseif(LIBC_TARGET_ARCHITECTURE_IS_AMDGPU)249 list(APPEND compile_options "SHELL:-Xclang -mcode-object-version=none")250 endif()251 endif()252 set(${output_var} ${compile_options} PARENT_SCOPE)253endfunction()254