1 function(_get_compile_options_from_flags output_var)
2 set(compile_options "")
4 if(LIBC_TARGET_ARCHITECTURE_IS_RISCV64 OR(LIBC_CPU_FEATURES MATCHES "FMA"))
5 check_flag(ADD_FMA_FLAG ${FMA_OPT_FLAG} ${ARGN})
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})
11 if(LLVM_COMPILER_IS_GCC_COMPATIBLE)
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")
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
25 list(APPEND compile_options "-msse4.2")
27 if(LIBC_COMPILER_HAS_BUILTIN_CEIL_FLOOR_RINT_TRUNC)
28 list(APPEND compile_options
29 "-D__LIBC_USE_BUILTIN_CEIL_FLOOR_RINT_TRUNC")
31 if(LIBC_COMPILER_HAS_BUILTIN_ROUND)
32 list(APPEND compile_options "-D__LIBC_USE_BUILTIN_ROUND")
34 if(LIBC_COMPILER_HAS_BUILTIN_ROUNDEVEN)
35 list(APPEND compile_options "-D__LIBC_USE_BUILTIN_ROUNDEVEN")
38 if(ADD_EXPLICIT_SIMD_OPT_FLAG)
39 list(APPEND compile_options "-D__LIBC_EXPLICIT_SIMD_OPT")
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")
46 if(LIBC_COMPILER_HAS_BUILTIN_FMAXF16_FMINF16)
47 list(APPEND compile_options "-D__LIBC_USE_BUILTIN_FMAXF16_FMINF16")
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")
57 list(APPEND compile_options "/arch:AVX2")
59 if(ADD_EXPLICIT_SIMD_OPT_FLAG)
60 list(APPEND compile_options "/D__LIBC_EXPLICIT_SIMD_OPT")
64 set(${output_var} ${compile_options} PARENT_SCOPE)
65 endfunction(_get_compile_options_from_flags)
67 function(_get_compile_options_from_config output_var)
68 set(config_options "")
70 if(LIBC_CONF_QSORT_IMPL)
71 list(APPEND config_options "-DLIBC_QSORT_IMPL=${LIBC_CONF_QSORT_IMPL}")
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")
78 if(LIBC_ADD_NULL_CHECKS)
79 list(APPEND config_options "-DLIBC_ADD_NULL_CHECKS")
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}")
86 if(LIBC_CONF_MATH_OPTIMIZATIONS)
87 list(APPEND config_options "-DLIBC_MATH=${LIBC_CONF_MATH_OPTIMIZATIONS}")
90 set(${output_var} ${config_options} PARENT_SCOPE)
91 endfunction(_get_compile_options_from_config)
93 function(_get_common_compile_options output_var flags)
94 _get_compile_options_from_flags(compile_flags ${flags})
95 _get_compile_options_from_config(config_flags)
97 set(compile_options ${LIBC_COMPILE_OPTIONS_DEFAULT} ${compile_flags} ${config_flags})
99 if(LLVM_COMPILER_IS_GCC_COMPATIBLE)
100 list(APPEND compile_options "-fpie")
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")
107 list(APPEND compile_options "-DLIBC_FULL_BUILD")
108 # Manually disable standard include paths to prevent system headers from
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")
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}")
125 list(APPEND compile_options "-idirafter${LIBC_KERNEL_HEADERS}")
130 if(LIBC_COMPILER_HAS_FIXED_POINT)
131 list(APPEND compile_options "-ffixed-point")
134 if(NOT LIBC_TARGET_OS_IS_GPU)
135 list(APPEND compile_options "-fno-builtin")
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")
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")
152 if (LIBC_CONF_ENABLE_STACK_PROTECTOR)
153 list(APPEND compile_options "-fstack-protector-strong")
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")
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")
167 list(APPEND compile_options "-Wno-c99-extensions")
168 list(APPEND compile_options "-Wno-gnu-imaginary-constant")
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")
182 list(APPEND compile_options "/EHs-c-")
183 list(APPEND compile_options "/GR-")
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")
192 if(LIBC_TARGET_ARCHITECTURE_IS_NVPTX)
193 list(APPEND compile_options "-Wno-unknown-cuda-version")
194 list(APPEND compile_options "--cuda-feature=+ptx63")
196 list(APPEND compile_options "--cuda-path=${LIBC_CUDA_ROOT}")
198 elseif(LIBC_TARGET_ARCHITECTURE_IS_AMDGPU)
199 list(APPEND compile_options "SHELL:-Xclang -mcode-object-version=none")
202 set(${output_var} ${compile_options} PARENT_SCOPE)
205 function(_get_common_test_compile_options output_var c_test flags)
206 _get_compile_options_from_flags(compile_flags ${flags})
209 ${LIBC_COMPILE_OPTIONS_DEFAULT}
210 ${LIBC_TEST_COMPILE_OPTIONS_DEFAULT}
213 if(LLVM_COMPILER_IS_GCC_COMPATIBLE)
214 list(APPEND compile_options "-fpie")
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")
224 list(APPEND compile_options "-fno-rtti")
228 if(LIBC_COMPILER_HAS_FIXED_POINT)
229 list(APPEND compile_options "-ffixed-point")
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")
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")
246 list(APPEND compile_options "-fext-numeric-literals")
249 list(APPEND compile_options "-Wno-c99-extensions")
250 list(APPEND compile_options "-Wno-gnu-imaginary-constant")
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")
261 set(${output_var} ${compile_options} PARENT_SCOPE)
264 function(_get_hermetic_test_compile_options output_var flags)
265 _get_common_test_compile_options(compile_options "" "${flags}")
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")
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)
284 set(${output_var} ${compile_options} PARENT_SCOPE)