xref: /llvm-project/compiler-rt/cmake/Modules/CompilerRTUtils.cmake (revision 71c5453fa9a1fb5bb927589c6d38058552d263b6)
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 CMake
6# 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 provided
107# 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}" AND
142         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 __MIPS
199    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 __PPC
205    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 "Unsupport 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 tree
235  # 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 be
239  # 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 use
244  # `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 the
279  # 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(WARNING
291      "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-config
295    # 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 provided
302  # 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(WARNING
308      "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 overwritting
320    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_INTDIR
341    ${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 error
347    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 is
357    # intended for testing purposes (generating the lit test suites) and will likely not support
358    # 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(COMPILER_RT_DEFAULT_TARGET_TRIPLE ${LLVM_TARGET_TRIPLE} CACHE STRING
377          "Default triple for which compiler-rt runtimes will be built.")
378  endif()
379
380  if(CMAKE_C_COMPILER_ID MATCHES "Clang")
381    set(option_prefix "")
382    if (CMAKE_C_SIMULATE_ID MATCHES "MSVC")
383      set(option_prefix "/clang:")
384    endif()
385    set(print_target_triple ${CMAKE_C_COMPILER} ${option_prefix}--target=${COMPILER_RT_DEFAULT_TARGET_TRIPLE} ${option_prefix}-print-target-triple)
386    execute_process(COMMAND ${print_target_triple}
387      RESULT_VARIABLE result
388      OUTPUT_VARIABLE output
389      OUTPUT_STRIP_TRAILING_WHITESPACE)
390    if(result EQUAL 0)
391      set(COMPILER_RT_DEFAULT_TARGET_TRIPLE ${output})
392    else()
393      string(REPLACE ";" " " print_target_triple "${print_target_triple}")
394      # TODO(#97876): Report an error.
395      message(WARNING "Failed to execute `${print_target_triple}` to normalize target triple.")
396    endif()
397  endif()
398
399  string(REPLACE "-" ";" LLVM_TARGET_TRIPLE_LIST ${COMPILER_RT_DEFAULT_TARGET_TRIPLE})
400  list(GET LLVM_TARGET_TRIPLE_LIST 0 COMPILER_RT_DEFAULT_TARGET_ARCH)
401
402  # Map various forms of the architecture names to the canonical forms
403  # (as they are used by clang, see getArchNameForCompilerRTLib).
404  if("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "^i.86$")
405    # Android uses i686, but that's remapped at a later stage.
406    set(COMPILER_RT_DEFAULT_TARGET_ARCH "i386")
407  endif()
408
409  # If we are directly targeting a GPU we need to check that the compiler is
410  # compatible and pass some default arguments.
411  if(COMPILER_RT_DEFAULT_TARGET_ONLY)
412
413    # Pass the necessary flags to make flag detection work.
414    if("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "amdgcn")
415      set(COMPILER_RT_GPU_BUILD ON)
416      set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nogpulib")
417    elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "nvptx")
418      set(COMPILER_RT_GPU_BUILD ON)
419      set(CMAKE_REQUIRED_FLAGS
420          "${CMAKE_REQUIRED_FLAGS} -flto -c -Wno-unused-command-line-argument")
421    endif()
422  endif()
423
424  # Determine if test target triple is specified explicitly, and doesn't match the
425  # default.
426  if(NOT COMPILER_RT_DEFAULT_TARGET_TRIPLE STREQUAL LLVM_TARGET_TRIPLE)
427    set(COMPILER_RT_HAS_EXPLICIT_DEFAULT_TARGET_TRIPLE TRUE)
428  else()
429    set(COMPILER_RT_HAS_EXPLICIT_DEFAULT_TARGET_TRIPLE FALSE)
430  endif()
431endmacro()
432
433# Filter out generic versions of routines that are re-implemented in an
434# architecture specific manner. This prevents multiple definitions of the same
435# symbols, making the symbol selection non-deterministic.
436#
437# We follow the convention that a source file that exists in a sub-directory
438# (e.g. `ppc/divtc3.c`) is architecture-specific and that if a generic
439# implementation exists it will be a top-level source file with the same name
440# modulo the file extension (e.g. `divtc3.c`).
441function(filter_builtin_sources inout_var name)
442  set(intermediate ${${inout_var}})
443  foreach(_file ${intermediate})
444    get_filename_component(_file_dir ${_file} DIRECTORY)
445    if (NOT "${_file_dir}" STREQUAL "")
446      # Architecture specific file. If a generic version exists, print a notice
447      # and ensure that it is removed from the file list.
448      get_filename_component(_name ${_file} NAME)
449      string(REGEX REPLACE "\\.S$" ".c" _cname "${_name}")
450      if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${_cname}")
451        message(STATUS "For ${name} builtins preferring ${_file} to ${_cname}")
452        list(REMOVE_ITEM intermediate ${_cname})
453      endif()
454    endif()
455  endforeach()
456  set(${inout_var} ${intermediate} PARENT_SCOPE)
457endfunction()
458
459function(get_compiler_rt_target arch variable)
460  string(FIND ${COMPILER_RT_DEFAULT_TARGET_TRIPLE} "-" dash_index)
461  string(SUBSTRING ${COMPILER_RT_DEFAULT_TARGET_TRIPLE} ${dash_index} -1 triple_suffix)
462  string(SUBSTRING ${COMPILER_RT_DEFAULT_TARGET_TRIPLE} 0 ${dash_index} triple_cpu)
463  if(COMPILER_RT_DEFAULT_TARGET_ONLY)
464    # Use exact spelling when building only for the target specified to CMake.
465    set(target "${COMPILER_RT_DEFAULT_TARGET_TRIPLE}")
466  elseif(ANDROID AND ${arch} STREQUAL "i386")
467    set(target "i686${triple_suffix}")
468  elseif(${arch} STREQUAL "amd64")
469    set(target "x86_64${triple_suffix}")
470  elseif(${arch} STREQUAL "sparc64")
471    set(target "sparcv9${triple_suffix}")
472  elseif("${arch}" MATCHES "mips64|mips64el")
473    string(REGEX REPLACE "-gnu.*" "-gnuabi64" triple_suffix_gnu "${triple_suffix}")
474    string(REGEX REPLACE "mipsisa32" "mipsisa64" triple_cpu_mips "${triple_cpu}")
475    string(REGEX REPLACE "^mips$" "mips64" triple_cpu_mips "${triple_cpu_mips}")
476    string(REGEX REPLACE "^mipsel$" "mips64el" triple_cpu_mips "${triple_cpu_mips}")
477    set(target "${triple_cpu_mips}${triple_suffix_gnu}")
478  elseif("${arch}" MATCHES "mips|mipsel")
479    string(REGEX REPLACE "-gnuabi.*" "-gnu" triple_suffix_gnu "${triple_suffix}")
480    string(REGEX REPLACE "mipsisa64" "mipsisa32" triple_cpu_mips "${triple_cpu}")
481    string(REGEX REPLACE "mips64" "mips" triple_cpu_mips "${triple_cpu_mips}")
482    set(target "${triple_cpu_mips}${triple_suffix_gnu}")
483  elseif("${arch}" MATCHES "^arm")
484    # Arch is arm, armhf, armv6m (anything else would come from using
485    # COMPILER_RT_DEFAULT_TARGET_ONLY, which is checked above).
486    if (${arch} STREQUAL "armhf")
487      # If we are building for hard float but our ABI is soft float.
488      if ("${triple_suffix}" MATCHES ".*eabi$")
489        # Change "eabi" -> "eabihf"
490        set(triple_suffix "${triple_suffix}hf")
491      endif()
492      # ABI is already set in the triple, don't repeat it in the architecture.
493      set(arch "arm")
494    else ()
495      # If we are building for soft float, but the triple's ABI is hard float.
496      if ("${triple_suffix}" MATCHES ".*eabihf$")
497        # Change "eabihf" -> "eabi"
498        string(REGEX REPLACE "hf$" "" triple_suffix "${triple_suffix}")
499      endif()
500    endif()
501    set(target "${arch}${triple_suffix}")
502  elseif("${arch}" MATCHES "^amdgcn")
503    set(target "amdgcn-amd-amdhsa")
504  elseif("${arch}" MATCHES "^nvptx")
505    set(target "nvptx64-nvidia-cuda")
506  else()
507    set(target "${arch}${triple_suffix}")
508  endif()
509  set(${variable} ${target} PARENT_SCOPE)
510endfunction()
511
512function(get_compiler_rt_install_dir arch install_dir)
513  if(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR AND NOT APPLE)
514    get_compiler_rt_target(${arch} target)
515    set(${install_dir} ${COMPILER_RT_INSTALL_LIBRARY_DIR}/${target} PARENT_SCOPE)
516  else()
517    set(${install_dir} ${COMPILER_RT_INSTALL_LIBRARY_DIR} PARENT_SCOPE)
518  endif()
519endfunction()
520
521function(get_compiler_rt_output_dir arch output_dir)
522  if(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR AND NOT APPLE)
523    get_compiler_rt_target(${arch} target)
524    set(${output_dir} ${COMPILER_RT_OUTPUT_LIBRARY_DIR}/${target} PARENT_SCOPE)
525  else()
526    set(${output_dir} ${COMPILER_RT_OUTPUT_LIBRARY_DIR} PARENT_SCOPE)
527  endif()
528endfunction()
529
530# compiler_rt_process_sources(
531#   <OUTPUT_VAR>
532#   <SOURCE_FILE> ...
533#  [ADDITIONAL_HEADERS <header> ...]
534# )
535#
536# Process the provided sources and write the list of new sources
537# into `<OUTPUT_VAR>`.
538#
539# ADDITIONAL_HEADERS     - Adds the supplied header to list of sources for IDEs.
540#
541# This function is very similar to `llvm_process_sources()` but exists here
542# because we need to support standalone builds of compiler-rt.
543function(compiler_rt_process_sources OUTPUT_VAR)
544  cmake_parse_arguments(
545    ARG
546    ""
547    ""
548    "ADDITIONAL_HEADERS"
549    ${ARGN}
550  )
551  set(sources ${ARG_UNPARSED_ARGUMENTS})
552  set(headers "")
553  if (XCODE OR MSVC_IDE OR CMAKE_EXTRA_GENERATOR)
554    # For IDEs we need to tell CMake about header files.
555    # Otherwise they won't show up in UI.
556    set(headers ${ARG_ADDITIONAL_HEADERS})
557    list(LENGTH headers headers_length)
558    if (${headers_length} GREATER 0)
559      set_source_files_properties(${headers}
560        PROPERTIES HEADER_FILE_ONLY ON)
561    endif()
562  endif()
563  set("${OUTPUT_VAR}" ${sources} ${headers} PARENT_SCOPE)
564endfunction()
565
566# Create install targets for a library and its parent component (if specified).
567function(add_compiler_rt_install_targets name)
568  cmake_parse_arguments(ARG "" "PARENT_TARGET" "" ${ARGN})
569
570  if(ARG_PARENT_TARGET AND NOT TARGET install-${ARG_PARENT_TARGET})
571    # The parent install target specifies the parent component to scrape up
572    # anything not installed by the individual install targets, and to handle
573    # installation when running the multi-configuration generators.
574    add_custom_target(install-${ARG_PARENT_TARGET}
575                      DEPENDS ${ARG_PARENT_TARGET}
576                      COMMAND "${CMAKE_COMMAND}"
577                              -DCMAKE_INSTALL_COMPONENT=${ARG_PARENT_TARGET}
578                              -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
579    add_custom_target(install-${ARG_PARENT_TARGET}-stripped
580                      DEPENDS ${ARG_PARENT_TARGET}
581                      COMMAND "${CMAKE_COMMAND}"
582                              -DCMAKE_INSTALL_COMPONENT=${ARG_PARENT_TARGET}
583                              -DCMAKE_INSTALL_DO_STRIP=1
584                              -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
585    set_target_properties(install-${ARG_PARENT_TARGET} PROPERTIES
586                          FOLDER "Compiler-RT/Installation")
587    set_target_properties(install-${ARG_PARENT_TARGET}-stripped PROPERTIES
588                          FOLDER "Compiler-RT/Installation")
589    add_dependencies(install-compiler-rt install-${ARG_PARENT_TARGET})
590    add_dependencies(install-compiler-rt-stripped install-${ARG_PARENT_TARGET}-stripped)
591  endif()
592
593  # We only want to generate per-library install targets if you aren't using
594  # an IDE because the extra targets get cluttered in IDEs.
595  if(NOT CMAKE_CONFIGURATION_TYPES)
596    add_custom_target(install-${name}
597                      DEPENDS ${name}
598                      COMMAND "${CMAKE_COMMAND}"
599                              -DCMAKE_INSTALL_COMPONENT=${name}
600                              -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
601    add_custom_target(install-${name}-stripped
602                      DEPENDS ${name}
603                      COMMAND "${CMAKE_COMMAND}"
604                              -DCMAKE_INSTALL_COMPONENT=${name}
605                              -DCMAKE_INSTALL_DO_STRIP=1
606                              -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
607    # If you have a parent target specified, we bind the new install target
608    # to the parent install target.
609    if(LIB_PARENT_TARGET)
610      add_dependencies(install-${LIB_PARENT_TARGET} install-${name})
611      add_dependencies(install-${LIB_PARENT_TARGET}-stripped install-${name}-stripped)
612    endif()
613  endif()
614endfunction()
615
616# Add warnings to catch potential errors that can lead to security
617# vulnerabilities.
618function(add_security_warnings out_flags macosx_sdk_version)
619  set(flags "${${out_flags}}")
620
621  append_list_if(COMPILER_RT_HAS_ARRAY_BOUNDS_FLAG -Werror=array-bounds flags)
622  append_list_if(COMPILER_RT_HAS_UNINITIALIZED_FLAG -Werror=uninitialized flags)
623  append_list_if(COMPILER_RT_HAS_SHADOW_FLAG -Werror=shadow flags)
624  append_list_if(COMPILER_RT_HAS_EMPTY_BODY_FLAG -Werror=empty-body flags)
625  append_list_if(COMPILER_RT_HAS_SIZEOF_POINTER_MEMACCESS_FLAG -Werror=sizeof-pointer-memaccess flags)
626  append_list_if(COMPILER_RT_HAS_SIZEOF_ARRAY_ARGUMENT_FLAG -Werror=sizeof-array-argument flags)
627  append_list_if(COMPILER_RT_HAS_SUSPICIOUS_MEMACCESS_FLAG -Werror=suspicious-memaccess flags)
628  append_list_if(COMPILER_RT_HAS_BUILTIN_MEMCPY_CHK_SIZE_FLAG -Werror=builtin-memcpy-chk-size flags)
629  append_list_if(COMPILER_RT_HAS_ARRAY_BOUNDS_POINTER_ARITHMETIC_FLAG -Werror=array-bounds-pointer-arithmetic flags)
630  append_list_if(COMPILER_RT_HAS_RETURN_STACK_ADDRESS_FLAG -Werror=return-stack-address flags)
631  append_list_if(COMPILER_RT_HAS_SIZEOF_ARRAY_DECAY_FLAG -Werror=sizeof-array-decay flags)
632  append_list_if(COMPILER_RT_HAS_FORMAT_INSUFFICIENT_ARGS_FLAG -Werror=format-insufficient-args flags)
633  # GCC complains if we pass -Werror=format-security without -Wformat
634  append_list_if(COMPILER_RT_HAS_BUILTIN_FORMAL_SECURITY_FLAG -Wformat -Werror=format-security flags)
635  append_list_if(COMPILER_RT_HAS_SIZEOF_ARRAY_DIV_FLAG -Werror=sizeof-array-div)
636  append_list_if(COMPILER_RT_HAS_SIZEOF_POINTER_DIV_FLAG -Werror=sizeof-pointer-div)
637
638  # Add -Wformat-nonliteral only if we can avoid adding the definition of
639  # eprintf. On Apple platforms, eprintf is needed only on macosx and only if
640  # its version is older than 10.7.
641  if ("${macosx_sdk_version}" VERSION_GREATER_EQUAL 10.7)
642    list(APPEND flags -Werror=format-nonliteral -DDONT_DEFINE_EPRINTF)
643  endif()
644
645  set(${out_flags} "${flags}" PARENT_SCOPE)
646endfunction()
647