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