|
| 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() |
0 commit comments