650 lines · plain
1include(CMakePushCheckState)2include(CheckSymbolExists)3 4# Because compiler-rt spends a lot of time setting up custom compile flags,5# define a handy helper function for it. The compile flags setting in CMake6# has serious issues that make its syntax challenging at best.7function(set_target_compile_flags target)8 set_property(TARGET ${target} PROPERTY COMPILE_OPTIONS ${ARGN})9endfunction()10 11function(set_target_link_flags target)12 set_property(TARGET ${target} PROPERTY LINK_OPTIONS ${ARGN})13endfunction()14 15# Set the variable var_PYBOOL to True if var holds a true-ish string,16# otherwise set it to False.17macro(pythonize_bool var)18 if (${var})19 set(${var}_PYBOOL True)20 else()21 set(${var}_PYBOOL False)22 endif()23endmacro()24 25# Appends value to all lists in ARGN, if the condition is true.26macro(append_list_if condition value)27 if(${condition})28 foreach(list ${ARGN})29 list(APPEND ${list} ${value})30 endforeach()31 endif()32endmacro()33 34# Appends value to all strings in ARGN, if the condition is true.35macro(append_string_if condition value)36 if(${condition})37 foreach(str ${ARGN})38 set(${str} "${${str}} ${value}")39 endforeach()40 endif()41endmacro()42 43macro(append_rtti_flag polarity list)44 if(${polarity})45 append_list_if(COMPILER_RT_HAS_FRTTI_FLAG -frtti ${list})46 append_list_if(COMPILER_RT_HAS_GR_FLAG /GR ${list})47 else()48 append_list_if(COMPILER_RT_HAS_FNO_RTTI_FLAG -fno-rtti ${list})49 append_list_if(COMPILER_RT_HAS_GR_FLAG /GR- ${list})50 endif()51endmacro()52 53macro(list_intersect output input1 input2)54 set(${output})55 foreach(it ${${input1}})56 list(FIND ${input2} ${it} index)57 if( NOT (index EQUAL -1))58 list(APPEND ${output} ${it})59 endif()60 endforeach()61endmacro()62 63function(list_replace input_list old new)64 set(replaced_list)65 foreach(item ${${input_list}})66 if(${item} STREQUAL ${old})67 list(APPEND replaced_list ${new})68 else()69 list(APPEND replaced_list ${item})70 endif()71 endforeach()72 set(${input_list} "${replaced_list}" PARENT_SCOPE)73endfunction()74 75# Takes ${ARGN} and puts only supported architectures in @out_var list.76function(filter_available_targets out_var)77 set(archs ${${out_var}})78 foreach(arch ${ARGN})79 list(FIND COMPILER_RT_SUPPORTED_ARCH ${arch} ARCH_INDEX)80 if(NOT (ARCH_INDEX EQUAL -1) AND CAN_TARGET_${arch})81 list(APPEND archs ${arch})82 endif()83 endforeach()84 set(${out_var} ${archs} PARENT_SCOPE)85endfunction()86 87# Add $arch as supported with no additional flags.88macro(add_default_target_arch arch)89 set(TARGET_${arch}_CFLAGS "")90 set(CAN_TARGET_${arch} 1)91 list(APPEND COMPILER_RT_SUPPORTED_ARCH ${arch})92endmacro()93 94function(check_compile_definition def argstring out_var)95 if("${def}" STREQUAL "")96 set(${out_var} TRUE PARENT_SCOPE)97 return()98 endif()99 cmake_push_check_state()100 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} ${argstring}")101 check_symbol_exists(${def} "" ${out_var})102 cmake_pop_check_state()103endfunction()104 105# test_target_arch(<arch> <def> <target flags...>)106# Checks if architecture is supported: runs host compiler with provided107# flags to verify that:108# 1) <def> is defined (if non-empty)109# 2) simple file can be successfully built.110# If successful, saves target flags for this architecture.111macro(test_target_arch arch def)112 set(TARGET_${arch}_CFLAGS ${ARGN})113 set(TARGET_${arch}_LINK_FLAGS ${ARGN})114 set(argstring "")115 foreach(arg ${ARGN})116 set(argstring "${argstring} ${arg}")117 endforeach()118 check_compile_definition("${def}" "${argstring}" HAS_${arch}_DEF)119 if(NOT DEFINED CAN_TARGET_${arch})120 if(NOT HAS_${arch}_DEF)121 set(CAN_TARGET_${arch} FALSE)122 elseif(TEST_COMPILE_ONLY)123 try_compile_only(CAN_TARGET_${arch}124 SOURCE "#include <limits.h>\nint foo(int x, int y) { return x + y; }\n"125 FLAGS ${TARGET_${arch}_CFLAGS})126 else()127 set(FLAG_NO_EXCEPTIONS "")128 if(COMPILER_RT_HAS_FNO_EXCEPTIONS_FLAG)129 set(FLAG_NO_EXCEPTIONS " -fno-exceptions ")130 endif()131 set(SAVED_CMAKE_EXE_LINKER_FLAGS ${CMAKE_EXE_LINKER_FLAGS})132 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${argstring}")133 try_compile(CAN_TARGET_${arch} ${CMAKE_BINARY_DIR} ${SIMPLE_SOURCE}134 COMPILE_DEFINITIONS "${TARGET_${arch}_CFLAGS} ${FLAG_NO_EXCEPTIONS}"135 OUTPUT_VARIABLE TARGET_${arch}_OUTPUT)136 set(CMAKE_EXE_LINKER_FLAGS ${SAVED_CMAKE_EXE_LINKER_FLAGS})137 endif()138 endif()139 if(${CAN_TARGET_${arch}})140 list(APPEND COMPILER_RT_SUPPORTED_ARCH ${arch})141 elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" STREQUAL "${arch}" AND142 COMPILER_RT_HAS_EXPLICIT_DEFAULT_TARGET_TRIPLE)143 # Bail out if we cannot target the architecture we plan to test.144 message(FATAL_ERROR "Cannot compile for ${arch}:\n${TARGET_${arch}_OUTPUT}")145 endif()146endmacro()147 148macro(detect_target_arch)149 check_symbol_exists(__AMDGPU__ "" __AMDGPU)150 check_symbol_exists(__arm__ "" __ARM)151 check_symbol_exists(__AVR__ "" __AVR)152 check_symbol_exists(__aarch64__ "" __AARCH64)153 check_symbol_exists(__x86_64__ "" __X86_64)154 check_symbol_exists(__i386__ "" __I386)155 check_symbol_exists(__hexagon__ "" __HEXAGON)156 check_symbol_exists(__loongarch__ "" __LOONGARCH)157 check_symbol_exists(__mips__ "" __MIPS)158 check_symbol_exists(__mips64__ "" __MIPS64)159 check_symbol_exists(__NVPTX__ "" __NVPTX)160 check_symbol_exists(__powerpc__ "" __PPC)161 check_symbol_exists(__powerpc64__ "" __PPC64)162 check_symbol_exists(__powerpc64le__ "" __PPC64LE)163 check_symbol_exists(__riscv "" __RISCV)164 check_symbol_exists(__s390x__ "" __S390X)165 check_symbol_exists(__sparc "" __SPARC)166 check_symbol_exists(__sparcv9 "" __SPARCV9)167 check_symbol_exists(__wasm32__ "" __WEBASSEMBLY32)168 check_symbol_exists(__wasm64__ "" __WEBASSEMBLY64)169 check_symbol_exists(__ve__ "" __VE)170 if(__AMDGPU)171 add_default_target_arch(amdgcn)172 elseif(__ARM)173 add_default_target_arch(arm)174 elseif(__AVR)175 add_default_target_arch(avr)176 elseif(__AARCH64)177 add_default_target_arch(aarch64)178 elseif(__X86_64)179 if(CMAKE_SIZEOF_VOID_P EQUAL "4")180 add_default_target_arch(x32)181 elseif(CMAKE_SIZEOF_VOID_P EQUAL "8")182 add_default_target_arch(x86_64)183 else()184 message(FATAL_ERROR "Unsupported pointer size for X86_64")185 endif()186 elseif(__HEXAGON)187 add_default_target_arch(hexagon)188 elseif(__I386)189 add_default_target_arch(i386)190 elseif(__LOONGARCH)191 if(CMAKE_SIZEOF_VOID_P EQUAL "4")192 add_default_target_arch(loongarch32)193 elseif(CMAKE_SIZEOF_VOID_P EQUAL "8")194 add_default_target_arch(loongarch64)195 else()196 message(FATAL_ERROR "Unsupported pointer size for LoongArch")197 endif()198 elseif(__MIPS64) # must be checked before __MIPS199 add_default_target_arch(mips64)200 elseif(__MIPS)201 add_default_target_arch(mips)202 elseif(__NVPTX)203 add_default_target_arch(nvptx64)204 elseif(__PPC64) # must be checked before __PPC205 add_default_target_arch(powerpc64)206 elseif(__PPC64LE)207 add_default_target_arch(powerpc64le)208 elseif(__PPC)209 add_default_target_arch(powerpc)210 elseif(__RISCV)211 if(CMAKE_SIZEOF_VOID_P EQUAL "4")212 add_default_target_arch(riscv32)213 elseif(CMAKE_SIZEOF_VOID_P EQUAL "8")214 add_default_target_arch(riscv64)215 else()216 message(FATAL_ERROR "Unsupported XLEN for RISC-V")217 endif()218 elseif(__S390X)219 add_default_target_arch(s390x)220 elseif(__SPARCV9)221 add_default_target_arch(sparcv9)222 elseif(__SPARC)223 add_default_target_arch(sparc)224 elseif(__WEBASSEMBLY32)225 add_default_target_arch(wasm32)226 elseif(__WEBASSEMBLY64)227 add_default_target_arch(wasm64)228 elseif(__VE)229 add_default_target_arch(ve)230 endif()231endmacro()232 233function(get_compiler_rt_root_source_dir ROOT_DIR_VAR)234 # Compute the path to the root of the Compiler-RT source tree235 # regardless of how the project was configured.236 #237 # This function is useful because using `${CMAKE_SOURCE_DIR}`238 # is error prone due to the numerous ways Compiler-RT can be239 # configured.240 #241 # `ROOT_DIR_VAR` - the name of the variable to write the result to.242 #243 # TODO(dliew): When CMake min version is 3.17 or newer use244 # `CMAKE_CURRENT_FUNCTION_LIST_DIR` instead.245 if ("${ROOT_DIR_VAR}" STREQUAL "")246 message(FATAL_ERROR "ROOT_DIR_VAR cannot be empty")247 endif()248 249 # Compiler-rt supports different source root paths.250 # Handle each case here.251 set(PATH_TO_COMPILER_RT_SOURCE_ROOT "")252 if (DEFINED CompilerRTBuiltins_SOURCE_DIR)253 # Compiler-RT Builtins standalone build.254 # `llvm-project/compiler-rt/lib/builtins`255 set(PATH_TO_COMPILER_RT_SOURCE_ROOT "${CompilerRTBuiltins_SOURCE_DIR}/../../")256 elseif (DEFINED CompilerRTCRT_SOURCE_DIR)257 # Compiler-RT CRT standalone build.258 # `llvm-project/compiler-rt/lib/crt`259 set(PATH_TO_COMPILER_RT_SOURCE_ROOT "${CompilerRTCRT_SOURCE_DIR}/../../")260 elseif(DEFINED CompilerRT_SOURCE_DIR)261 # Compiler-RT standalone build.262 # `llvm-project/compiler-rt`263 set(PATH_TO_COMPILER_RT_SOURCE_ROOT "${CompilerRT_SOURCE_DIR}")264 elseif (EXISTS "${CMAKE_SOURCE_DIR}/../compiler-rt")265 # In tree build with LLVM as the root project.266 # See `llvm-project/projects/`.267 # Assumes monorepo layout.268 set(PATH_TO_COMPILER_RT_SOURCE_ROOT "${CMAKE_SOURCE_DIR}/../compiler-rt")269 else()270 message(FATAL_ERROR "Unhandled Compiler-RT source root configuration.")271 endif()272 273 get_filename_component(ROOT_DIR "${PATH_TO_COMPILER_RT_SOURCE_ROOT}" ABSOLUTE)274 if (NOT EXISTS "${ROOT_DIR}")275 message(FATAL_ERROR "Path \"${ROOT_DIR}\" doesn't exist")276 endif()277 278 # Sanity check: Make sure we can locate the current source file via the279 # computed path.280 set(PATH_TO_CURRENT_FILE "${ROOT_DIR}/cmake/Modules/CompilerRTUtils.cmake")281 if (NOT EXISTS "${PATH_TO_CURRENT_FILE}")282 message(FATAL_ERROR "Could not find \"${PATH_TO_CURRENT_FILE}\"")283 endif()284 285 set("${ROOT_DIR_VAR}" "${ROOT_DIR}" PARENT_SCOPE)286endfunction()287 288macro(load_llvm_config)289 if (LLVM_CONFIG_PATH AND NOT LLVM_CMAKE_DIR)290 message(WARNING291 "LLVM_CONFIG_PATH is deprecated, please use LLVM_CMAKE_DIR instead")292 # Compute the path to the LLVM install prefix and pass it as LLVM_CMAKE_DIR,293 # CMake will locate the appropriate lib*/cmake subdirectory from there.294 # For example. for -DLLVM_CONFIG_PATH=/usr/lib/llvm/16/bin/llvm-config295 # this will yield LLVM_CMAKE_DIR=/usr/lib/llvm/16.296 get_filename_component(LLVM_CMAKE_DIR "${LLVM_CONFIG_PATH}" DIRECTORY)297 get_filename_component(LLVM_CMAKE_DIR "${LLVM_CMAKE_DIR}" DIRECTORY)298 endif()299 300 # Compute path to LLVM sources assuming the monorepo layout.301 # We don't set `LLVM_MAIN_SRC_DIR` directly to avoid overriding a user provided302 # CMake cache value.303 get_compiler_rt_root_source_dir(COMPILER_RT_ROOT_SRC_PATH)304 get_filename_component(LLVM_MAIN_SRC_DIR_DEFAULT "${COMPILER_RT_ROOT_SRC_PATH}/../llvm" ABSOLUTE)305 if (NOT EXISTS "${LLVM_MAIN_SRC_DIR_DEFAULT}")306 # TODO(dliew): Remove this legacy fallback path.307 message(WARNING308 "LLVM source tree not found at \"${LLVM_MAIN_SRC_DIR_DEFAULT}\". "309 "You are not using the monorepo layout. This configuration is DEPRECATED.")310 endif()311 312 find_package(LLVM HINTS "${LLVM_CMAKE_DIR}")313 if (NOT LLVM_FOUND)314 message(WARNING "UNSUPPORTED COMPILER-RT CONFIGURATION DETECTED: "315 "LLVM cmake package not found.\n"316 "Reconfigure with -DLLVM_CMAKE_DIR=/path/to/llvm.")317 else()318 list(APPEND CMAKE_MODULE_PATH "${LLVM_DIR}")319 # Turn into CACHE PATHs for overwritting320 set(LLVM_BINARY_DIR "${LLVM_BINARY_DIR}" CACHE PATH "Path to LLVM build tree")321 set(LLVM_LIBRARY_DIR "${LLVM_LIBRARY_DIR}" CACHE PATH "Path to llvm/lib")322 set(LLVM_TOOLS_BINARY_DIR "${LLVM_TOOLS_BINARY_DIR}" CACHE PATH "Path to llvm/bin")323 set(LLVM_INCLUDE_DIR ${LLVM_INCLUDE_DIRS} CACHE PATH "Path to llvm/include and any other header dirs needed")324 325 list(FIND LLVM_AVAILABLE_LIBS LLVMXRay XRAY_INDEX)326 set(COMPILER_RT_HAS_LLVMXRAY TRUE)327 if (XRAY_INDEX EQUAL -1)328 message(WARNING "LLVMXRay not found in LLVM_AVAILABLE_LIBS")329 set(COMPILER_RT_HAS_LLVMXRAY FALSE)330 endif()331 332 list(FIND LLVM_AVAILABLE_LIBS LLVMTestingSupport TESTINGSUPPORT_INDEX)333 set(COMPILER_RT_HAS_LLVMTESTINGSUPPORT TRUE)334 if (TESTINGSUPPORT_INDEX EQUAL -1)335 message(WARNING "LLVMTestingSupport not found in LLVM_AVAILABLE_LIBS")336 set(COMPILER_RT_HAS_LLVMTESTINGSUPPORT FALSE)337 endif()338 endif()339 340 set(LLVM_LIBRARY_OUTPUT_INTDIR341 ${LLVM_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib${LLVM_LIBDIR_SUFFIX})342 343 set(LLVM_MAIN_SRC_DIR "${LLVM_MAIN_SRC_DIR_DEFAULT}" CACHE PATH "Path to LLVM source tree")344 message(STATUS "LLVM_MAIN_SRC_DIR: \"${LLVM_MAIN_SRC_DIR}\"")345 if (NOT EXISTS "${LLVM_MAIN_SRC_DIR}")346 # TODO(dliew): Make this a hard error347 message(WARNING "LLVM_MAIN_SRC_DIR (${LLVM_MAIN_SRC_DIR}) does not exist. "348 "You can override the inferred path by adding "349 "`-DLLVM_MAIN_SRC_DIR=<path_to_llvm_src>` to your CMake invocation "350 "where `<path_to_llvm_src>` is the path to the `llvm` directory in "351 "the `llvm-project` repo. "352 "This will be treated as error in the future.")353 endif()354 355 if (NOT LLVM_FOUND)356 # This configuration tries to configure without the prescence of `LLVMConfig.cmake`. It is357 # intended for testing purposes (generating the lit test suites) and will likely not support358 # a build of the runtimes in compiler-rt.359 include(CompilerRTMockLLVMCMakeConfig)360 compiler_rt_mock_llvm_cmake_config()361 endif()362 363endmacro()364 365macro(construct_compiler_rt_default_triple)366 if(COMPILER_RT_DEFAULT_TARGET_ONLY)367 if(DEFINED COMPILER_RT_DEFAULT_TARGET_TRIPLE)368 message(FATAL_ERROR "COMPILER_RT_DEFAULT_TARGET_TRIPLE isn't supported when building for default target only")369 endif()370 if ("${CMAKE_C_COMPILER_TARGET}" STREQUAL "")371 message(FATAL_ERROR "CMAKE_C_COMPILER_TARGET must also be set when COMPILER_RT_DEFAULT_TARGET_ONLY is ON")372 endif()373 message(STATUS "cmake c compiler target: ${CMAKE_C_COMPILER_TARGET}")374 set(COMPILER_RT_DEFAULT_TARGET_TRIPLE ${CMAKE_C_COMPILER_TARGET})375 else()376 set(target_triple ${LLVM_TARGET_TRIPLE})377 # AIX triples can have OS version numbers we don't want for the compiler-rt target.378 if (target_triple MATCHES "aix")379 string(REGEX REPLACE "[0-9.]+$" "" target_triple "${target_triple}")380 endif()381 set(COMPILER_RT_DEFAULT_TARGET_TRIPLE ${target_triple} CACHE STRING382 "Default triple for which compiler-rt runtimes will be built.")383 endif()384 385 if(CMAKE_C_COMPILER_ID MATCHES "Clang")386 set(option_prefix "")387 if (CMAKE_C_SIMULATE_ID MATCHES "MSVC")388 set(option_prefix "/clang:")389 endif()390 set(print_target_triple ${CMAKE_C_COMPILER} ${option_prefix}--target=${COMPILER_RT_DEFAULT_TARGET_TRIPLE} ${option_prefix}-print-target-triple)391 execute_process(COMMAND ${print_target_triple}392 RESULT_VARIABLE result393 OUTPUT_VARIABLE output394 OUTPUT_STRIP_TRAILING_WHITESPACE)395 if(result EQUAL 0)396 set(COMPILER_RT_DEFAULT_TARGET_TRIPLE ${output})397 else()398 string(REPLACE ";" " " print_target_triple "${print_target_triple}")399 # TODO(#97876): Report an error.400 message(WARNING "Failed to execute `${print_target_triple}` to normalize target triple.")401 endif()402 endif()403 404 string(REPLACE "-" ";" LLVM_TARGET_TRIPLE_LIST ${COMPILER_RT_DEFAULT_TARGET_TRIPLE})405 list(GET LLVM_TARGET_TRIPLE_LIST 0 COMPILER_RT_DEFAULT_TARGET_ARCH)406 407 # Map various forms of the architecture names to the canonical forms408 # (as they are used by clang, see getArchNameForCompilerRTLib).409 if("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "^i.86$")410 # Android uses i686, but that's remapped at a later stage.411 set(COMPILER_RT_DEFAULT_TARGET_ARCH "i386")412 endif()413 414 # If we are directly targeting a GPU we need to check that the compiler is415 # compatible and pass some default arguments.416 if(COMPILER_RT_DEFAULT_TARGET_ONLY)417 418 # Pass the necessary flags to make flag detection work.419 if("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "amdgcn")420 set(COMPILER_RT_GPU_BUILD ON)421 elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "nvptx")422 set(COMPILER_RT_GPU_BUILD ON)423 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -flto -c")424 endif()425 endif()426 427 # Determine if test target triple is specified explicitly, and doesn't match the428 # default.429 if(NOT COMPILER_RT_DEFAULT_TARGET_TRIPLE STREQUAL LLVM_TARGET_TRIPLE)430 set(COMPILER_RT_HAS_EXPLICIT_DEFAULT_TARGET_TRIPLE TRUE)431 else()432 set(COMPILER_RT_HAS_EXPLICIT_DEFAULT_TARGET_TRIPLE FALSE)433 endif()434endmacro()435 436# Filter out generic versions of routines that are re-implemented in an437# architecture specific manner. This prevents multiple definitions of the same438# symbols, making the symbol selection non-deterministic.439#440# We follow the convention that a source file that exists in a sub-directory441# (e.g. `ppc/divtc3.c`) is architecture-specific and that if a generic442# implementation exists it will be a top-level source file with the same name443# modulo the file extension (e.g. `divtc3.c`).444function(filter_builtin_sources inout_var name)445 set(intermediate ${${inout_var}})446 foreach(_file ${intermediate})447 get_filename_component(_file_dir ${_file} DIRECTORY)448 if (NOT "${_file_dir}" STREQUAL "")449 # Architecture specific file. If a generic version exists, print a notice450 # and ensure that it is removed from the file list.451 get_filename_component(_name ${_file} NAME)452 string(REGEX REPLACE "\\.S$" ".c" _cname "${_name}")453 if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${_cname}")454 message(STATUS "For ${name} builtins preferring ${_file} to ${_cname}")455 list(REMOVE_ITEM intermediate ${_cname})456 endif()457 endif()458 endforeach()459 set(${inout_var} ${intermediate} PARENT_SCOPE)460endfunction()461 462function(get_compiler_rt_target arch variable)463 string(FIND ${COMPILER_RT_DEFAULT_TARGET_TRIPLE} "-" dash_index)464 string(SUBSTRING ${COMPILER_RT_DEFAULT_TARGET_TRIPLE} ${dash_index} -1 triple_suffix)465 string(SUBSTRING ${COMPILER_RT_DEFAULT_TARGET_TRIPLE} 0 ${dash_index} triple_cpu)466 if(COMPILER_RT_DEFAULT_TARGET_ONLY)467 # Use exact spelling when building only for the target specified to CMake.468 set(target "${COMPILER_RT_DEFAULT_TARGET_TRIPLE}")469 elseif(ANDROID AND ${arch} STREQUAL "i386")470 set(target "i686${triple_suffix}")471 elseif(${arch} STREQUAL "amd64")472 set(target "x86_64${triple_suffix}")473 elseif(${arch} STREQUAL "sparc64")474 set(target "sparcv9${triple_suffix}")475 elseif("${arch}" MATCHES "mips64|mips64el")476 string(REGEX REPLACE "-gnu.*" "-gnuabi64" triple_suffix_gnu "${triple_suffix}")477 string(REGEX REPLACE "mipsisa32" "mipsisa64" triple_cpu_mips "${triple_cpu}")478 string(REGEX REPLACE "^mips$" "mips64" triple_cpu_mips "${triple_cpu_mips}")479 string(REGEX REPLACE "^mipsel$" "mips64el" triple_cpu_mips "${triple_cpu_mips}")480 set(target "${triple_cpu_mips}${triple_suffix_gnu}")481 elseif("${arch}" MATCHES "mips|mipsel")482 string(REGEX REPLACE "-gnuabi.*" "-gnu" triple_suffix_gnu "${triple_suffix}")483 string(REGEX REPLACE "mipsisa64" "mipsisa32" triple_cpu_mips "${triple_cpu}")484 string(REGEX REPLACE "mips64" "mips" triple_cpu_mips "${triple_cpu_mips}")485 set(target "${triple_cpu_mips}${triple_suffix_gnu}")486 elseif("${arch}" MATCHES "^arm")487 # Arch is arm, armhf, armv6m (anything else would come from using488 # COMPILER_RT_DEFAULT_TARGET_ONLY, which is checked above).489 if (${arch} STREQUAL "armhf")490 # If we are building for hard float but our ABI is soft float.491 if ("${triple_suffix}" MATCHES ".*eabi$")492 # Change "eabi" -> "eabihf"493 set(triple_suffix "${triple_suffix}hf")494 endif()495 # ABI is already set in the triple, don't repeat it in the architecture.496 set(arch "arm")497 else ()498 # If we are building for soft float, but the triple's ABI is hard float.499 if ("${triple_suffix}" MATCHES ".*eabihf$")500 # Change "eabihf" -> "eabi"501 string(REGEX REPLACE "hf$" "" triple_suffix "${triple_suffix}")502 endif()503 endif()504 set(target "${arch}${triple_suffix}")505 elseif("${arch}" MATCHES "^amdgcn")506 set(target "amdgcn-amd-amdhsa")507 elseif("${arch}" MATCHES "^nvptx")508 set(target "nvptx64-nvidia-cuda")509 else()510 set(target "${arch}${triple_suffix}")511 endif()512 set(${variable} ${target} PARENT_SCOPE)513endfunction()514 515function(get_compiler_rt_install_dir arch install_dir)516 if(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR AND NOT APPLE)517 get_compiler_rt_target(${arch} target)518 set(${install_dir} ${COMPILER_RT_INSTALL_LIBRARY_DIR}/${target} PARENT_SCOPE)519 else()520 set(${install_dir} ${COMPILER_RT_INSTALL_LIBRARY_DIR} PARENT_SCOPE)521 endif()522endfunction()523 524function(get_compiler_rt_output_dir arch output_dir)525 if(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR AND NOT APPLE)526 get_compiler_rt_target(${arch} target)527 set(${output_dir} ${COMPILER_RT_OUTPUT_LIBRARY_DIR}/${target} PARENT_SCOPE)528 else()529 set(${output_dir} ${COMPILER_RT_OUTPUT_LIBRARY_DIR} PARENT_SCOPE)530 endif()531endfunction()532 533# compiler_rt_process_sources(534# <OUTPUT_VAR>535# <SOURCE_FILE> ...536# [ADDITIONAL_HEADERS <header> ...]537# )538#539# Process the provided sources and write the list of new sources540# into `<OUTPUT_VAR>`.541#542# ADDITIONAL_HEADERS - Adds the supplied header to list of sources for IDEs.543#544# This function is very similar to `llvm_process_sources()` but exists here545# because we need to support standalone builds of compiler-rt.546function(compiler_rt_process_sources OUTPUT_VAR)547 cmake_parse_arguments(548 ARG549 ""550 ""551 "ADDITIONAL_HEADERS"552 ${ARGN}553 )554 set(sources ${ARG_UNPARSED_ARGUMENTS})555 set(headers "")556 if (XCODE OR MSVC_IDE OR CMAKE_EXTRA_GENERATOR)557 # For IDEs we need to tell CMake about header files.558 # Otherwise they won't show up in UI.559 set(headers ${ARG_ADDITIONAL_HEADERS})560 list(LENGTH headers headers_length)561 if (${headers_length} GREATER 0)562 set_source_files_properties(${headers}563 PROPERTIES HEADER_FILE_ONLY ON)564 endif()565 endif()566 set("${OUTPUT_VAR}" ${sources} ${headers} PARENT_SCOPE)567endfunction()568 569# Create install targets for a library and its parent component (if specified).570function(add_compiler_rt_install_targets name)571 cmake_parse_arguments(ARG "" "PARENT_TARGET" "" ${ARGN})572 573 if(ARG_PARENT_TARGET AND NOT TARGET install-${ARG_PARENT_TARGET})574 # The parent install target specifies the parent component to scrape up575 # anything not installed by the individual install targets, and to handle576 # installation when running the multi-configuration generators.577 add_custom_target(install-${ARG_PARENT_TARGET}578 DEPENDS ${ARG_PARENT_TARGET}579 COMMAND "${CMAKE_COMMAND}"580 -DCMAKE_INSTALL_COMPONENT=${ARG_PARENT_TARGET}581 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")582 add_custom_target(install-${ARG_PARENT_TARGET}-stripped583 DEPENDS ${ARG_PARENT_TARGET}584 COMMAND "${CMAKE_COMMAND}"585 -DCMAKE_INSTALL_COMPONENT=${ARG_PARENT_TARGET}586 -DCMAKE_INSTALL_DO_STRIP=1587 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")588 set_target_properties(install-${ARG_PARENT_TARGET} PROPERTIES589 FOLDER "Compiler-RT/Installation")590 set_target_properties(install-${ARG_PARENT_TARGET}-stripped PROPERTIES591 FOLDER "Compiler-RT/Installation")592 add_dependencies(install-compiler-rt install-${ARG_PARENT_TARGET})593 add_dependencies(install-compiler-rt-stripped install-${ARG_PARENT_TARGET}-stripped)594 endif()595 596 # We only want to generate per-library install targets if you aren't using597 # an IDE because the extra targets get cluttered in IDEs.598 if(NOT CMAKE_CONFIGURATION_TYPES)599 add_custom_target(install-${name}600 DEPENDS ${name}601 COMMAND "${CMAKE_COMMAND}"602 -DCMAKE_INSTALL_COMPONENT=${name}603 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")604 add_custom_target(install-${name}-stripped605 DEPENDS ${name}606 COMMAND "${CMAKE_COMMAND}"607 -DCMAKE_INSTALL_COMPONENT=${name}608 -DCMAKE_INSTALL_DO_STRIP=1609 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")610 # If you have a parent target specified, we bind the new install target611 # to the parent install target.612 if(LIB_PARENT_TARGET)613 add_dependencies(install-${LIB_PARENT_TARGET} install-${name})614 add_dependencies(install-${LIB_PARENT_TARGET}-stripped install-${name}-stripped)615 endif()616 endif()617endfunction()618 619# Add warnings to catch potential errors that can lead to security620# vulnerabilities.621function(add_security_warnings out_flags macosx_sdk_version)622 set(flags "${${out_flags}}")623 624 append_list_if(COMPILER_RT_HAS_ARRAY_BOUNDS_FLAG -Werror=array-bounds flags)625 append_list_if(COMPILER_RT_HAS_UNINITIALIZED_FLAG -Werror=uninitialized flags)626 append_list_if(COMPILER_RT_HAS_SHADOW_FLAG -Werror=shadow flags)627 append_list_if(COMPILER_RT_HAS_EMPTY_BODY_FLAG -Werror=empty-body flags)628 append_list_if(COMPILER_RT_HAS_SIZEOF_POINTER_MEMACCESS_FLAG -Werror=sizeof-pointer-memaccess flags)629 append_list_if(COMPILER_RT_HAS_SIZEOF_ARRAY_ARGUMENT_FLAG -Werror=sizeof-array-argument flags)630 append_list_if(COMPILER_RT_HAS_SUSPICIOUS_MEMACCESS_FLAG -Werror=suspicious-memaccess flags)631 append_list_if(COMPILER_RT_HAS_BUILTIN_MEMCPY_CHK_SIZE_FLAG -Werror=builtin-memcpy-chk-size flags)632 append_list_if(COMPILER_RT_HAS_ARRAY_BOUNDS_POINTER_ARITHMETIC_FLAG -Werror=array-bounds-pointer-arithmetic flags)633 append_list_if(COMPILER_RT_HAS_RETURN_STACK_ADDRESS_FLAG -Werror=return-stack-address flags)634 append_list_if(COMPILER_RT_HAS_SIZEOF_ARRAY_DECAY_FLAG -Werror=sizeof-array-decay flags)635 append_list_if(COMPILER_RT_HAS_FORMAT_INSUFFICIENT_ARGS_FLAG -Werror=format-insufficient-args flags)636 # GCC complains if we pass -Werror=format-security without -Wformat637 append_list_if(COMPILER_RT_HAS_BUILTIN_FORMAL_SECURITY_FLAG -Wformat -Werror=format-security flags)638 append_list_if(COMPILER_RT_HAS_SIZEOF_ARRAY_DIV_FLAG -Werror=sizeof-array-div)639 append_list_if(COMPILER_RT_HAS_SIZEOF_POINTER_DIV_FLAG -Werror=sizeof-pointer-div)640 641 # Add -Wformat-nonliteral only if we can avoid adding the definition of642 # eprintf. On Apple platforms, eprintf is needed only on macosx and only if643 # its version is older than 10.7.644 if ("${macosx_sdk_version}" VERSION_GREATER_EQUAL 10.7)645 list(APPEND flags -Werror=format-nonliteral -DDONT_DEFINE_EPRINTF)646 endif()647 648 set(${out_flags} "${flags}" PARENT_SCOPE)649endfunction()650