Skip to content

Commit d0c2c0a

Browse files
[libc] Move compile options to new cmake file (#81917)
The cmake test generator needed to be updated to support the same flags as the source. To simplify the code, I moved it to a new file.
1 parent 3e77871 commit d0c2c0a

File tree

5 files changed

+229
-181
lines changed

5 files changed

+229
-181
lines changed
Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
function(_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} ${flags})
6+
endif()
7+
check_flag(ADD_SSE4_2_FLAG ${ROUND_OPT_FLAG} ${flags})
8+
check_flag(ADD_EXPLICIT_SIMD_OPT_FLAG ${EXPLICIT_SIMD_OPT_FLAG} ${flags})
9+
10+
if(LLVM_COMPILER_IS_GCC_COMPATIBLE)
11+
if(ADD_FMA_FLAG)
12+
if(LIBC_TARGET_ARCHITECTURE_IS_X86)
13+
list(APPEND compile_options "-mavx2")
14+
list(APPEND compile_options "-mfma")
15+
elseif(LIBC_TARGET_ARCHITECTURE_IS_RISCV64)
16+
list(APPEND compile_options "-D__LIBC_RISCV_USE_FMA")
17+
endif()
18+
endif()
19+
if(ADD_SSE4_2_FLAG)
20+
list(APPEND compile_options "-msse4.2")
21+
endif()
22+
if(ADD_EXPLICIT_SIMD_OPT_FLAG)
23+
list(APPEND compile_options "-D__LIBC_EXPLICIT_SIMD_OPT")
24+
endif()
25+
elseif(MSVC)
26+
if(ADD_FMA_FLAG)
27+
list(APPEND compile_options "/arch:AVX2")
28+
endif()
29+
if(ADD_EXPLICIT_SIMD_OPT_FLAG)
30+
list(APPEND compile_options "/D__LIBC_EXPLICIT_SIMD_OPT")
31+
endif()
32+
endif()
33+
34+
set(${output_var} ${compile_options} PARENT_SCOPE)
35+
endfunction(_get_compile_options_from_flags)
36+
37+
function(_get_common_compile_options output_var flags)
38+
_get_compile_options_from_flags(compile_flags ${flags})
39+
40+
set(compile_options ${LIBC_COMPILE_OPTIONS_DEFAULT} ${compile_flags})
41+
42+
if(LLVM_COMPILER_IS_GCC_COMPATIBLE)
43+
list(APPEND compile_options "-fpie")
44+
45+
if(LLVM_LIBC_FULL_BUILD)
46+
# Only add -ffreestanding flag in full build mode.
47+
list(APPEND compile_options "-ffreestanding")
48+
endif()
49+
50+
if(LIBC_COMPILER_HAS_FIXED_POINT)
51+
list(APPEND compile_options "-ffixed-point")
52+
endif()
53+
54+
list(APPEND compile_options "-fno-builtin")
55+
list(APPEND compile_options "-fno-exceptions")
56+
list(APPEND compile_options "-fno-lax-vector-conversions")
57+
list(APPEND compile_options "-fno-unwind-tables")
58+
list(APPEND compile_options "-fno-asynchronous-unwind-tables")
59+
list(APPEND compile_options "-fno-rtti")
60+
if (LIBC_CC_SUPPORTS_PATTERN_INIT)
61+
list(APPEND compile_options "-ftrivial-auto-var-init=pattern")
62+
endif()
63+
list(APPEND compile_options "-Wall")
64+
list(APPEND compile_options "-Wextra")
65+
# -DLIBC_WNO_ERROR=ON if you can't build cleanly with -Werror.
66+
if(NOT LIBC_WNO_ERROR)
67+
list(APPEND compile_options "-Werror")
68+
endif()
69+
list(APPEND compile_options "-Wconversion")
70+
list(APPEND compile_options "-Wno-sign-conversion")
71+
list(APPEND compile_options "-Wimplicit-fallthrough")
72+
list(APPEND compile_options "-Wwrite-strings")
73+
list(APPEND compile_options "-Wextra-semi")
74+
if(NOT CMAKE_COMPILER_IS_GNUCXX)
75+
list(APPEND compile_options "-Wnewline-eof")
76+
list(APPEND compile_options "-Wnonportable-system-include-path")
77+
list(APPEND compile_options "-Wstrict-prototypes")
78+
list(APPEND compile_options "-Wthread-safety")
79+
list(APPEND compile_options "-Wglobal-constructors")
80+
endif()
81+
elseif(MSVC)
82+
list(APPEND compile_options "/EHs-c-")
83+
list(APPEND compile_options "/GR-")
84+
endif()
85+
if (LIBC_TARGET_ARCHITECTURE_IS_GPU)
86+
list(APPEND compile_options "-nogpulib")
87+
list(APPEND compile_options "-fvisibility=hidden")
88+
list(APPEND compile_options "-fconvergent-functions")
89+
90+
# Manually disable all standard include paths and include the resource
91+
# directory to prevent system headers from being included.
92+
list(APPEND compile_options "-isystem${COMPILER_RESOURCE_DIR}/include")
93+
list(APPEND compile_options "-nostdinc")
94+
endif()
95+
set(${output_var} ${compile_options} PARENT_SCOPE)
96+
endfunction()
97+
98+
function(_get_common_test_compile_options output_var flags)
99+
_get_compile_options_from_flags(compile_flags ${flags})
100+
101+
set(compile_options ${LIBC_COMPILE_OPTIONS_DEFAULT} ${compile_flags})
102+
103+
if(LLVM_COMPILER_IS_GCC_COMPATIBLE)
104+
list(APPEND compile_options "-fpie")
105+
106+
if(LLVM_LIBC_FULL_BUILD)
107+
# Only add -ffreestanding flag in full build mode.
108+
list(APPEND compile_options "-ffreestanding")
109+
list(APPEND compile_options "-fno-exceptions")
110+
list(APPEND compile_options "-fno-unwind-tables")
111+
list(APPEND compile_options "-fno-asynchronous-unwind-tables")
112+
list(APPEND compile_options "-fno-rtti")
113+
endif()
114+
115+
if(LIBC_COMPILER_HAS_FIXED_POINT)
116+
list(APPEND compile_options "-ffixed-point")
117+
endif()
118+
119+
# list(APPEND compile_options "-Wall")
120+
# list(APPEND compile_options "-Wextra")
121+
# -DLIBC_WNO_ERROR=ON if you can't build cleanly with -Werror.
122+
if(NOT LIBC_WNO_ERROR)
123+
# list(APPEND compile_options "-Werror")
124+
endif()
125+
# list(APPEND compile_options "-Wconversion")
126+
# list(APPEND compile_options "-Wno-sign-conversion")
127+
# list(APPEND compile_options "-Wimplicit-fallthrough")
128+
# list(APPEND compile_options "-Wwrite-strings")
129+
# list(APPEND compile_options "-Wextra-semi")
130+
# if(NOT CMAKE_COMPILER_IS_GNUCXX)
131+
# list(APPEND compile_options "-Wnewline-eof")
132+
# list(APPEND compile_options "-Wnonportable-system-include-path")
133+
# list(APPEND compile_options "-Wstrict-prototypes")
134+
# list(APPEND compile_options "-Wthread-safety")
135+
# list(APPEND compile_options "-Wglobal-constructors")
136+
# endif()
137+
endif()
138+
if (LIBC_TARGET_ARCHITECTURE_IS_GPU)
139+
# TODO: Set these flags
140+
# list(APPEND compile_options "-nogpulib")
141+
# list(APPEND compile_options "-fvisibility=hidden")
142+
# list(APPEND compile_options "-fconvergent-functions")
143+
144+
# # Manually disable all standard include paths and include the resource
145+
# # directory to prevent system headers from being included.
146+
# list(APPEND compile_options "-isystem${COMPILER_RESOURCE_DIR}/include")
147+
# list(APPEND compile_options "-nostdinc")
148+
endif()
149+
set(${output_var} ${compile_options} PARENT_SCOPE)
150+
endfunction()
151+
152+
153+
# Obtains NVPTX specific arguments for compilation.
154+
# The PTX feature is primarily based on the CUDA toolchain version. We want to
155+
# be able to target NVPTX without an existing CUDA installation, so we need to
156+
# set this manually. This simply sets the PTX feature to the minimum required
157+
# for the features we wish to use on that target. The minimum PTX features used
158+
# here roughly corresponds to the CUDA 9.0 release.
159+
# Adjust as needed for desired PTX features.
160+
function(get_nvptx_compile_options output_var gpu_arch)
161+
set(nvptx_options "")
162+
list(APPEND nvptx_options "-march=${gpu_arch}")
163+
list(APPEND nvptx_options "-Wno-unknown-cuda-version")
164+
list(APPEND nvptx_options "SHELL:-mllvm -nvptx-emit-init-fini-kernel=false")
165+
if(${gpu_arch} STREQUAL "sm_35")
166+
list(APPEND nvptx_options "--cuda-feature=+ptx63")
167+
elseif(${gpu_arch} STREQUAL "sm_37")
168+
list(APPEND nvptx_options "--cuda-feature=+ptx63")
169+
elseif(${gpu_arch} STREQUAL "sm_50")
170+
list(APPEND nvptx_options "--cuda-feature=+ptx63")
171+
elseif(${gpu_arch} STREQUAL "sm_52")
172+
list(APPEND nvptx_options "--cuda-feature=+ptx63")
173+
elseif(${gpu_arch} STREQUAL "sm_53")
174+
list(APPEND nvptx_options "--cuda-feature=+ptx63")
175+
elseif(${gpu_arch} STREQUAL "sm_60")
176+
list(APPEND nvptx_options "--cuda-feature=+ptx63")
177+
elseif(${gpu_arch} STREQUAL "sm_61")
178+
list(APPEND nvptx_options "--cuda-feature=+ptx63")
179+
elseif(${gpu_arch} STREQUAL "sm_62")
180+
list(APPEND nvptx_options "--cuda-feature=+ptx63")
181+
elseif(${gpu_arch} STREQUAL "sm_70")
182+
list(APPEND nvptx_options "--cuda-feature=+ptx63")
183+
elseif(${gpu_arch} STREQUAL "sm_72")
184+
list(APPEND nvptx_options "--cuda-feature=+ptx63")
185+
elseif(${gpu_arch} STREQUAL "sm_75")
186+
list(APPEND nvptx_options "--cuda-feature=+ptx63")
187+
elseif(${gpu_arch} STREQUAL "sm_80")
188+
list(APPEND nvptx_options "--cuda-feature=+ptx72")
189+
elseif(${gpu_arch} STREQUAL "sm_86")
190+
list(APPEND nvptx_options "--cuda-feature=+ptx72")
191+
elseif(${gpu_arch} STREQUAL "sm_89")
192+
list(APPEND nvptx_options "--cuda-feature=+ptx72")
193+
elseif(${gpu_arch} STREQUAL "sm_90")
194+
list(APPEND nvptx_options "--cuda-feature=+ptx72")
195+
else()
196+
message(FATAL_ERROR "Unknown Nvidia GPU architecture '${gpu_arch}'")
197+
endif()
198+
199+
if(LIBC_CUDA_ROOT)
200+
list(APPEND nvptx_options "--cuda-path=${LIBC_CUDA_ROOT}")
201+
endif()
202+
set(${output_var} ${nvptx_options} PARENT_SCOPE)
203+
endfunction()
204+
205+
#TODO: Fold this into a function to get test framework compile options (which
206+
# need to be separate from the main test compile options because otherwise they
207+
# error)
208+
set(LIBC_HERMETIC_TEST_COMPILE_OPTIONS ${LIBC_COMPILE_OPTIONS_DEFAULT}
209+
-fpie -ffreestanding -fno-exceptions -fno-rtti)
210+
# The GPU build requires overriding the default CMake triple and architecture.
211+
if(LIBC_GPU_TARGET_ARCHITECTURE_IS_AMDGPU)
212+
list(APPEND LIBC_HERMETIC_TEST_COMPILE_OPTIONS
213+
-nogpulib -mcpu=${LIBC_GPU_TARGET_ARCHITECTURE} -flto
214+
--target=${LIBC_GPU_TARGET_TRIPLE}
215+
-mcode-object-version=${LIBC_GPU_CODE_OBJECT_VERSION})
216+
elseif(LIBC_GPU_TARGET_ARCHITECTURE_IS_NVPTX)
217+
get_nvptx_compile_options(nvptx_options ${LIBC_GPU_TARGET_ARCHITECTURE})
218+
list(APPEND LIBC_HERMETIC_TEST_COMPILE_OPTIONS
219+
-nogpulib ${nvptx_options} -fno-use-cxa-atexit --target=${LIBC_GPU_TARGET_TRIPLE})
220+
endif()

libc/cmake/modules/LLVMLibCObjectRules.cmake

Lines changed: 0 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -1,154 +1,5 @@
11
set(OBJECT_LIBRARY_TARGET_TYPE "OBJECT_LIBRARY")
22

3-
function(_get_compile_options_from_flags output_var)
4-
set(compile_options "")
5-
6-
if(LIBC_TARGET_ARCHITECTURE_IS_RISCV64 OR(LIBC_CPU_FEATURES MATCHES "FMA"))
7-
check_flag(ADD_FMA_FLAG ${FMA_OPT_FLAG} ${flags})
8-
endif()
9-
check_flag(ADD_SSE4_2_FLAG ${ROUND_OPT_FLAG} ${flags})
10-
check_flag(ADD_EXPLICIT_SIMD_OPT_FLAG ${EXPLICIT_SIMD_OPT_FLAG} ${flags})
11-
12-
if(LLVM_COMPILER_IS_GCC_COMPATIBLE)
13-
if(ADD_FMA_FLAG)
14-
if(LIBC_TARGET_ARCHITECTURE_IS_X86)
15-
list(APPEND compile_options "-mavx2")
16-
list(APPEND compile_options "-mfma")
17-
elseif(LIBC_TARGET_ARCHITECTURE_IS_RISCV64)
18-
list(APPEND compile_options "-D__LIBC_RISCV_USE_FMA")
19-
endif()
20-
endif()
21-
if(ADD_SSE4_2_FLAG)
22-
list(APPEND compile_options "-msse4.2")
23-
endif()
24-
if(ADD_EXPLICIT_SIMD_OPT_FLAG)
25-
list(APPEND compile_options "-D__LIBC_EXPLICIT_SIMD_OPT")
26-
endif()
27-
elseif(MSVC)
28-
if(ADD_FMA_FLAG)
29-
list(APPEND compile_options "/arch:AVX2")
30-
endif()
31-
if(ADD_EXPLICIT_SIMD_OPT_FLAG)
32-
list(APPEND compile_options "/D__LIBC_EXPLICIT_SIMD_OPT")
33-
endif()
34-
endif()
35-
36-
set(${output_var} ${compile_options} PARENT_SCOPE)
37-
endfunction(_get_compile_options_from_flags)
38-
39-
function(_get_common_compile_options output_var flags)
40-
_get_compile_options_from_flags(compile_flags ${flags})
41-
42-
set(compile_options ${LIBC_COMPILE_OPTIONS_DEFAULT} ${compile_flags})
43-
44-
if(LLVM_COMPILER_IS_GCC_COMPATIBLE)
45-
list(APPEND compile_options "-fpie")
46-
47-
if(LLVM_LIBC_FULL_BUILD)
48-
# Only add -ffreestanding flag in full build mode.
49-
list(APPEND compile_options "-ffreestanding")
50-
endif()
51-
52-
if(LIBC_COMPILER_HAS_FIXED_POINT)
53-
list(APPEND compile_options "-ffixed-point")
54-
endif()
55-
56-
list(APPEND compile_options "-fno-builtin")
57-
list(APPEND compile_options "-fno-exceptions")
58-
list(APPEND compile_options "-fno-lax-vector-conversions")
59-
list(APPEND compile_options "-fno-unwind-tables")
60-
list(APPEND compile_options "-fno-asynchronous-unwind-tables")
61-
list(APPEND compile_options "-fno-rtti")
62-
if (LIBC_CC_SUPPORTS_PATTERN_INIT)
63-
list(APPEND compile_options "-ftrivial-auto-var-init=pattern")
64-
endif()
65-
list(APPEND compile_options "-Wall")
66-
list(APPEND compile_options "-Wextra")
67-
# -DLIBC_WNO_ERROR=ON if you can't build cleanly with -Werror.
68-
if(NOT LIBC_WNO_ERROR)
69-
list(APPEND compile_options "-Werror")
70-
endif()
71-
list(APPEND compile_options "-Wconversion")
72-
list(APPEND compile_options "-Wno-sign-conversion")
73-
list(APPEND compile_options "-Wimplicit-fallthrough")
74-
list(APPEND compile_options "-Wwrite-strings")
75-
list(APPEND compile_options "-Wextra-semi")
76-
if(NOT CMAKE_COMPILER_IS_GNUCXX)
77-
list(APPEND compile_options "-Wnewline-eof")
78-
list(APPEND compile_options "-Wnonportable-system-include-path")
79-
list(APPEND compile_options "-Wstrict-prototypes")
80-
list(APPEND compile_options "-Wthread-safety")
81-
list(APPEND compile_options "-Wglobal-constructors")
82-
endif()
83-
elseif(MSVC)
84-
list(APPEND compile_options "/EHs-c-")
85-
list(APPEND compile_options "/GR-")
86-
endif()
87-
if (LIBC_TARGET_ARCHITECTURE_IS_GPU)
88-
list(APPEND compile_options "-nogpulib")
89-
list(APPEND compile_options "-fvisibility=hidden")
90-
list(APPEND compile_options "-fconvergent-functions")
91-
92-
# Manually disable all standard include paths and include the resource
93-
# directory to prevent system headers from being included.
94-
list(APPEND compile_options "-isystem${COMPILER_RESOURCE_DIR}/include")
95-
list(APPEND compile_options "-nostdinc")
96-
endif()
97-
set(${output_var} ${compile_options} PARENT_SCOPE)
98-
endfunction()
99-
100-
# Obtains NVPTX specific arguments for compilation.
101-
# The PTX feature is primarily based on the CUDA toolchain version. We want to
102-
# be able to target NVPTX without an existing CUDA installation, so we need to
103-
# set this manually. This simply sets the PTX feature to the minimum required
104-
# for the features we wish to use on that target. The minimum PTX features used
105-
# here roughly corresponds to the CUDA 9.0 release.
106-
# Adjust as needed for desired PTX features.
107-
function(get_nvptx_compile_options output_var gpu_arch)
108-
set(nvptx_options "")
109-
list(APPEND nvptx_options "-march=${gpu_arch}")
110-
list(APPEND nvptx_options "-Wno-unknown-cuda-version")
111-
list(APPEND nvptx_options "SHELL:-mllvm -nvptx-emit-init-fini-kernel=false")
112-
if(${gpu_arch} STREQUAL "sm_35")
113-
list(APPEND nvptx_options "--cuda-feature=+ptx63")
114-
elseif(${gpu_arch} STREQUAL "sm_37")
115-
list(APPEND nvptx_options "--cuda-feature=+ptx63")
116-
elseif(${gpu_arch} STREQUAL "sm_50")
117-
list(APPEND nvptx_options "--cuda-feature=+ptx63")
118-
elseif(${gpu_arch} STREQUAL "sm_52")
119-
list(APPEND nvptx_options "--cuda-feature=+ptx63")
120-
elseif(${gpu_arch} STREQUAL "sm_53")
121-
list(APPEND nvptx_options "--cuda-feature=+ptx63")
122-
elseif(${gpu_arch} STREQUAL "sm_60")
123-
list(APPEND nvptx_options "--cuda-feature=+ptx63")
124-
elseif(${gpu_arch} STREQUAL "sm_61")
125-
list(APPEND nvptx_options "--cuda-feature=+ptx63")
126-
elseif(${gpu_arch} STREQUAL "sm_62")
127-
list(APPEND nvptx_options "--cuda-feature=+ptx63")
128-
elseif(${gpu_arch} STREQUAL "sm_70")
129-
list(APPEND nvptx_options "--cuda-feature=+ptx63")
130-
elseif(${gpu_arch} STREQUAL "sm_72")
131-
list(APPEND nvptx_options "--cuda-feature=+ptx63")
132-
elseif(${gpu_arch} STREQUAL "sm_75")
133-
list(APPEND nvptx_options "--cuda-feature=+ptx63")
134-
elseif(${gpu_arch} STREQUAL "sm_80")
135-
list(APPEND nvptx_options "--cuda-feature=+ptx72")
136-
elseif(${gpu_arch} STREQUAL "sm_86")
137-
list(APPEND nvptx_options "--cuda-feature=+ptx72")
138-
elseif(${gpu_arch} STREQUAL "sm_89")
139-
list(APPEND nvptx_options "--cuda-feature=+ptx72")
140-
elseif(${gpu_arch} STREQUAL "sm_90")
141-
list(APPEND nvptx_options "--cuda-feature=+ptx72")
142-
else()
143-
message(FATAL_ERROR "Unknown Nvidia GPU architecture '${gpu_arch}'")
144-
endif()
145-
146-
if(LIBC_CUDA_ROOT)
147-
list(APPEND nvptx_options "--cuda-path=${LIBC_CUDA_ROOT}")
148-
endif()
149-
set(${output_var} ${nvptx_options} PARENT_SCOPE)
150-
endfunction()
151-
1523
# Build the object target for a single GPU arch.
1534
# Usage:
1545
# _build_gpu_object_for_single_arch(

libc/cmake/modules/LLVMLibCRules.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
include(LLVMLibCCompileOptionRules)
12
include(LLVMLibCTargetNameUtils)
23
include(LLVMLibCFlagRules)
34
include(LLVMLibCObjectRules)

0 commit comments

Comments
 (0)