[AVR] Fix LLD test (#106739)
[llvm-project.git] / libc / cmake / modules / LLVMLibCCompileOptionRules.cmake
blob7a1c45a814eb682a1ac1a267605b867a0bdcfd29
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})
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})
11   if(LLVM_COMPILER_IS_GCC_COMPATIBLE)
12     if(ADD_FMA_FLAG)
13       if(LIBC_TARGET_ARCHITECTURE_IS_X86)
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)
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     endif()
44   elseif(MSVC)
45     if(ADD_FMA_FLAG)
46       list(APPEND compile_options "/arch:AVX2")
47     endif()
48     if(ADD_EXPLICIT_SIMD_OPT_FLAG)
49       list(APPEND compile_options "/D__LIBC_EXPLICIT_SIMD_OPT")
50     endif()
51   endif()
53   set(${output_var} ${compile_options} PARENT_SCOPE)
54 endfunction(_get_compile_options_from_flags)
56 function(_get_compile_options_from_config output_var)
57   set(config_options "")
59   if(LIBC_CONF_QSORT_IMPL)
60     list(APPEND config_options "-DLIBC_QSORT_IMPL=${LIBC_CONF_QSORT_IMPL}")
61   endif()
63   set(${output_var} ${config_options} PARENT_SCOPE)
64 endfunction(_get_compile_options_from_config)
66 function(_get_common_compile_options output_var flags)
67   _get_compile_options_from_flags(compile_flags ${flags})
68   _get_compile_options_from_config(config_flags)
70   set(compile_options ${LIBC_COMPILE_OPTIONS_DEFAULT} ${compile_flags} ${config_flags})
72   if(LLVM_COMPILER_IS_GCC_COMPATIBLE)
73     list(APPEND compile_options "-fpie")
75     if(LLVM_LIBC_FULL_BUILD)
76       # Only add -ffreestanding flag in non-GPU full build mode.
77       if(NOT LIBC_TARGET_OS_IS_GPU)
78         list(APPEND compile_options "-ffreestanding")
79       endif()
80       list(APPEND compile_options "-DLIBC_FULL_BUILD")
81       # Manually disable standard include paths to prevent system headers from
82       # being included.
83       if(LIBC_CC_SUPPORTS_NOSTDLIBINC)
84         list(APPEND compile_options "-nostdlibinc")
85       elseif(COMPILER_RESOURCE_DIR)
86         # TODO: We should require COMPILER_RESOURCE_DIR to be set.
87         list(APPEND compile_options "-isystem${COMPILER_RESOURCE_DIR}/include")
88         list(APPEND compile_options "-nostdinc")
89       endif()
90       # TODO: We should set this unconditionally on Linux.
91       if(LIBC_TARGET_OS_IS_LINUX AND
92          (LIBC_CC_SUPPORTS_NOSTDLIBINC OR COMPILER_RESOURCE_DIR))
93         # We use -idirafter to avoid preempting libc's own headers in case the
94         # directory (e.g. /usr/include) contains other headers.
95         if(CMAKE_CROSSCOMPILING)
96           list(APPEND compile_options "-idirafter=${LIBC_KERNEL_HEADERS}")
97         else()
98           list(APPEND compile_options "-idirafter${LIBC_KERNEL_HEADERS}")
99         endif()
100       endif()
101     endif()
103     if(LIBC_COMPILER_HAS_FIXED_POINT)
104       list(APPEND compile_options "-ffixed-point")
105     endif()
107     if(NOT LIBC_TARGET_OS_IS_GPU)
108       list(APPEND compile_options "-fno-builtin")
109     endif()
111     list(APPEND compile_options "-fno-exceptions")
112     list(APPEND compile_options "-fno-lax-vector-conversions")
113     list(APPEND compile_options "-fno-unwind-tables")
114     list(APPEND compile_options "-fno-asynchronous-unwind-tables")
115     list(APPEND compile_options "-fno-rtti")
116     if (LIBC_CC_SUPPORTS_PATTERN_INIT)
117       list(APPEND compile_options "-ftrivial-auto-var-init=pattern")
118     endif()
119     if (LIBC_CONF_KEEP_FRAME_POINTER)
120       list(APPEND compile_options "-fno-omit-frame-pointer")
121       if (LIBC_TARGET_ARCHITECTURE_IS_X86)
122         list(APPEND compile_options "-mno-omit-leaf-frame-pointer")
123       endif()
124     endif()
125     if (LIBC_CONF_ENABLE_STACK_PROTECTOR)
126       list(APPEND compile_options "-fstack-protector-strong")
127     endif()
128     list(APPEND compile_options "-Wall")
129     list(APPEND compile_options "-Wextra")
130     # -DLIBC_WNO_ERROR=ON if you can't build cleanly with -Werror.
131     if(NOT LIBC_WNO_ERROR)
132       list(APPEND compile_options "-Werror")
133     endif()
134     list(APPEND compile_options "-Wconversion")
135     list(APPEND compile_options "-Wno-sign-conversion")
136     list(APPEND compile_options "-Wimplicit-fallthrough")
137     list(APPEND compile_options "-Wwrite-strings")
138     list(APPEND compile_options "-Wextra-semi")
139     if(NOT CMAKE_COMPILER_IS_GNUCXX)
140       list(APPEND compile_options "-Wnewline-eof")
141       list(APPEND compile_options "-Wnonportable-system-include-path")
142       list(APPEND compile_options "-Wstrict-prototypes")
143       list(APPEND compile_options "-Wthread-safety")
144       list(APPEND compile_options "-Wglobal-constructors")
145     endif()
146     if(LIBC_CONF_MATH_OPTIMIZATIONS)
147       list(APPEND compile_options "-DLIBC_MATH=${LIBC_CONF_MATH_OPTIMIZATIONS}")
148     endif()
149   elseif(MSVC)
150     list(APPEND compile_options "/EHs-c-")
151     list(APPEND compile_options "/GR-")
152   endif()
153   if (LIBC_TARGET_OS_IS_GPU)
154     list(APPEND compile_options "-nogpulib")
155     list(APPEND compile_options "-fvisibility=hidden")
156     list(APPEND compile_options "-fconvergent-functions")
157     list(APPEND compile_options "-flto")
158     list(APPEND compile_options "-Wno-multi-gpu")
160     if(LIBC_TARGET_ARCHITECTURE_IS_NVPTX)
161       list(APPEND compile_options "-Wno-unknown-cuda-version")
162       list(APPEND compile_options "--cuda-feature=+ptx63")
163       if(LIBC_CUDA_ROOT)
164         list(APPEND compile_options "--cuda-path=${LIBC_CUDA_ROOT}")
165       endif()
166     elseif(LIBC_TARGET_ARCHITECTURE_IS_AMDGPU)
167       list(APPEND compile_options "SHELL:-Xclang -mcode-object-version=none")
168     endif()
169   endif()
170   set(${output_var} ${compile_options} PARENT_SCOPE)
171 endfunction()
173 function(_get_common_test_compile_options output_var c_test flags)
174   _get_compile_options_from_flags(compile_flags ${flags})
176   set(compile_options ${LIBC_COMPILE_OPTIONS_DEFAULT} ${compile_flags})
178   if(LLVM_COMPILER_IS_GCC_COMPATIBLE)
179     list(APPEND compile_options "-fpie")
181     if(LLVM_LIBC_FULL_BUILD)
182       list(APPEND compile_options "-DLIBC_FULL_BUILD")
183       # Only add -ffreestanding flag in full build mode.
184       list(APPEND compile_options "-ffreestanding")
185       list(APPEND compile_options "-fno-exceptions")
186       list(APPEND compile_options "-fno-unwind-tables")
187       list(APPEND compile_options "-fno-asynchronous-unwind-tables")
188       if(NOT ${c_test})
189         list(APPEND compile_options "-fno-rtti")
190       endif()
191     endif()
193     if(LIBC_COMPILER_HAS_FIXED_POINT)
194       list(APPEND compile_options "-ffixed-point")
195     endif()
197     # list(APPEND compile_options "-Wall")
198     # list(APPEND compile_options "-Wextra")
199     # -DLIBC_WNO_ERROR=ON if you can't build cleanly with -Werror.
200     if(NOT LIBC_WNO_ERROR)
201       # list(APPEND compile_options "-Werror")
202     endif()
203     # list(APPEND compile_options "-Wconversion")
204     # list(APPEND compile_options "-Wno-sign-conversion")
205     # list(APPEND compile_options "-Wimplicit-fallthrough")
206     # list(APPEND compile_options "-Wwrite-strings")
207     # list(APPEND compile_options "-Wextra-semi")
208     # if(NOT CMAKE_COMPILER_IS_GNUCXX)
209     #   list(APPEND compile_options "-Wnewline-eof")
210     #   list(APPEND compile_options "-Wnonportable-system-include-path")
211     #   list(APPEND compile_options "-Wstrict-prototypes")
212     #   list(APPEND compile_options "-Wthread-safety")
213     #   list(APPEND compile_options "-Wglobal-constructors")
214     # endif()
215   endif()
216   set(${output_var} ${compile_options} PARENT_SCOPE)
217 endfunction()
219 function(_get_hermetic_test_compile_options output_var flags)
220   _get_compile_options_from_flags(compile_flags ${flags})
221   list(APPEND compile_options ${LIBC_COMPILE_OPTIONS_DEFAULT} ${compile_flags}
222        ${flags} -fpie -ffreestanding -fno-exceptions -fno-rtti)
224   # The GPU build requires overriding the default CMake triple and architecture.
225   if(LIBC_TARGET_ARCHITECTURE_IS_AMDGPU)
226     list(APPEND compile_options
227          -Wno-multi-gpu -nogpulib -mcpu=${LIBC_GPU_TARGET_ARCHITECTURE} -flto
228          -mcode-object-version=${LIBC_GPU_CODE_OBJECT_VERSION})
229   elseif(LIBC_TARGET_ARCHITECTURE_IS_NVPTX)
230     list(APPEND compile_options
231          "SHELL:-mllvm -nvptx-emit-init-fini-kernel=false"
232          -Wno-multi-gpu --cuda-path=${LIBC_CUDA_ROOT}
233          -nogpulib -march=${LIBC_GPU_TARGET_ARCHITECTURE} -fno-use-cxa-atexit)
234   endif()
236   if(LLVM_LIBC_FULL_BUILD)
237     list(APPEND compile_options "-DLIBC_FULL_BUILD")
238   endif()
239   
240   set(${output_var} ${compile_options} PARENT_SCOPE)
241 endfunction()