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(argstring "")
9  foreach(arg ${ARGN})
10    set(argstring "${argstring} ${arg}")
11  endforeach()
12  set_property(TARGET ${target} PROPERTY COMPILE_FLAGS "${argstring}")
13endfunction()
14
15function(set_target_link_flags target)
16  set(argstring "")
17  foreach(arg ${ARGN})
18    set(argstring "${argstring} ${arg}")
19  endforeach()
20  set_property(TARGET ${target} PROPERTY LINK_FLAGS "${argstring}")
21endfunction()
22
23# Set the variable var_PYBOOL to True if var holds a true-ish string,
24# otherwise set it to False.
25macro(pythonize_bool var)
26  if (${var})
27    set(${var}_PYBOOL True)
28  else()
29    set(${var}_PYBOOL False)
30  endif()
31endmacro()
32
33# Appends value to all lists in ARGN, if the condition is true.
34macro(append_list_if condition value)
35  if(${condition})
36    foreach(list ${ARGN})
37      list(APPEND ${list} ${value})
38    endforeach()
39  endif()
40endmacro()
41
42# Appends value to all strings in ARGN, if the condition is true.
43macro(append_string_if condition value)
44  if(${condition})
45    foreach(str ${ARGN})
46      set(${str} "${${str}} ${value}")
47    endforeach()
48  endif()
49endmacro()
50
51macro(append_rtti_flag polarity list)
52  if(${polarity})
53    append_list_if(COMPILER_RT_HAS_FRTTI_FLAG -frtti ${list})
54    append_list_if(COMPILER_RT_HAS_GR_FLAG /GR ${list})
55  else()
56    append_list_if(COMPILER_RT_HAS_FNO_RTTI_FLAG -fno-rtti ${list})
57    append_list_if(COMPILER_RT_HAS_GR_FLAG /GR- ${list})
58  endif()
59endmacro()
60
61macro(list_intersect output input1 input2)
62  set(${output})
63  foreach(it ${${input1}})
64    list(FIND ${input2} ${it} index)
65    if( NOT (index EQUAL -1))
66      list(APPEND ${output} ${it})
67    endif()
68  endforeach()
69endmacro()
70
71function(list_replace input_list old new)
72  set(replaced_list)
73  foreach(item ${${input_list}})
74    if(${item} STREQUAL ${old})
75      list(APPEND replaced_list ${new})
76    else()
77      list(APPEND replaced_list ${item})
78    endif()
79  endforeach()
80  set(${input_list} "${replaced_list}" PARENT_SCOPE)
81endfunction()
82
83# Takes ${ARGN} and puts only supported architectures in @out_var list.
84function(filter_available_targets out_var)
85  set(archs ${${out_var}})
86  foreach(arch ${ARGN})
87    list(FIND COMPILER_RT_SUPPORTED_ARCH ${arch} ARCH_INDEX)
88    if(NOT (ARCH_INDEX EQUAL -1) AND CAN_TARGET_${arch})
89      list(APPEND archs ${arch})
90    endif()
91  endforeach()
92  set(${out_var} ${archs} PARENT_SCOPE)
93endfunction()
94
95# Add $arch as supported with no additional flags.
96macro(add_default_target_arch arch)
97  set(TARGET_${arch}_CFLAGS "")
98  set(CAN_TARGET_${arch} 1)
99  list(APPEND COMPILER_RT_SUPPORTED_ARCH ${arch})
100endmacro()
101
102function(check_compile_definition def argstring out_var)
103  if("${def}" STREQUAL "")
104    set(${out_var} TRUE PARENT_SCOPE)
105    return()
106  endif()
107  cmake_push_check_state()
108  set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} ${argstring}")
109  check_symbol_exists(${def} "" ${out_var})
110  cmake_pop_check_state()
111endfunction()
112
113# test_target_arch(<arch> <def> <target flags...>)
114# Checks if architecture is supported: runs host compiler with provided
115# flags to verify that:
116#   1) <def> is defined (if non-empty)
117#   2) simple file can be successfully built.
118# If successful, saves target flags for this architecture.
119macro(test_target_arch arch def)
120  set(TARGET_${arch}_CFLAGS ${ARGN})
121  set(TARGET_${arch}_LINK_FLAGS ${ARGN})
122  set(argstring "")
123  foreach(arg ${ARGN})
124    set(argstring "${argstring} ${arg}")
125  endforeach()
126  check_compile_definition("${def}" "${argstring}" HAS_${arch}_DEF)
127  if(NOT DEFINED CAN_TARGET_${arch})
128    if(NOT HAS_${arch}_DEF)
129      set(CAN_TARGET_${arch} FALSE)
130    elseif(TEST_COMPILE_ONLY)
131      try_compile_only(CAN_TARGET_${arch} FLAGS ${TARGET_${arch}_CFLAGS})
132    else()
133      set(FLAG_NO_EXCEPTIONS "")
134      if(COMPILER_RT_HAS_FNO_EXCEPTIONS_FLAG)
135        set(FLAG_NO_EXCEPTIONS " -fno-exceptions ")
136      endif()
137      set(SAVED_CMAKE_EXE_LINKER_FLAGS ${CMAKE_EXE_LINKER_FLAGS})
138      set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${argstring}")
139      try_compile(CAN_TARGET_${arch} ${CMAKE_BINARY_DIR} ${SIMPLE_SOURCE}
140                  COMPILE_DEFINITIONS "${TARGET_${arch}_CFLAGS} ${FLAG_NO_EXCEPTIONS}"
141                  OUTPUT_VARIABLE TARGET_${arch}_OUTPUT)
142      set(CMAKE_EXE_LINKER_FLAGS ${SAVED_CMAKE_EXE_LINKER_FLAGS})
143    endif()
144  endif()
145  if(${CAN_TARGET_${arch}})
146    list(APPEND COMPILER_RT_SUPPORTED_ARCH ${arch})
147  elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" STREQUAL "${arch}" AND
148         COMPILER_RT_HAS_EXPLICIT_DEFAULT_TARGET_TRIPLE)
149    # Bail out if we cannot target the architecture we plan to test.
150    message(FATAL_ERROR "Cannot compile for ${arch}:\n${TARGET_${arch}_OUTPUT}")
151  endif()
152endmacro()
153
154macro(detect_target_arch)
155  check_symbol_exists(__arm__ "" __ARM)
156  check_symbol_exists(__aarch64__ "" __AARCH64)
157  check_symbol_exists(__x86_64__ "" __X86_64)
158  check_symbol_exists(__i386__ "" __I386)
159  check_symbol_exists(__mips__ "" __MIPS)
160  check_symbol_exists(__mips64__ "" __MIPS64)
161  check_symbol_exists(__powerpc__ "" __PPC)
162  check_symbol_exists(__powerpc64__ "" __PPC64)
163  check_symbol_exists(__powerpc64le__ "" __PPC64LE)
164  check_symbol_exists(__riscv "" __RISCV)
165  check_symbol_exists(__s390x__ "" __S390X)
166  check_symbol_exists(__sparc "" __SPARC)
167  check_symbol_exists(__sparcv9 "" __SPARCV9)
168  check_symbol_exists(__wasm32__ "" __WEBASSEMBLY32)
169  check_symbol_exists(__wasm64__ "" __WEBASSEMBLY64)
170  check_symbol_exists(__ve__ "" __VE)
171  if(__ARM)
172    add_default_target_arch(arm)
173  elseif(__AARCH64)
174    add_default_target_arch(aarch64)
175  elseif(__X86_64)
176    if(CMAKE_SIZEOF_VOID_P EQUAL "4")
177      add_default_target_arch(x32)
178    elseif(CMAKE_SIZEOF_VOID_P EQUAL "8")
179      add_default_target_arch(x86_64)
180    else()
181      message(FATAL_ERROR "Unsupported pointer size for X86_64")
182    endif()
183  elseif(__I386)
184    add_default_target_arch(i386)
185  elseif(__MIPS64) # must be checked before __MIPS
186    add_default_target_arch(mips64)
187  elseif(__MIPS)
188    add_default_target_arch(mips)
189  elseif(__PPC64) # must be checked before __PPC
190    add_default_target_arch(powerpc64)
191  elseif(__PPC64LE)
192    add_default_target_arch(powerpc64le)
193  elseif(__PPC)
194    add_default_target_arch(powerpc)
195  elseif(__RISCV)
196    if(CMAKE_SIZEOF_VOID_P EQUAL "4")
197      add_default_target_arch(riscv32)
198    elseif(CMAKE_SIZEOF_VOID_P EQUAL "8")
199      add_default_target_arch(riscv64)
200    else()
201      message(FATAL_ERROR "Unsupport XLEN for RISC-V")
202    endif()
203  elseif(__S390X)
204    add_default_target_arch(s390x)
205  elseif(__SPARCV9)
206    add_default_target_arch(sparcv9)
207  elseif(__SPARC)
208    add_default_target_arch(sparc)
209  elseif(__WEBASSEMBLY32)
210    add_default_target_arch(wasm32)
211  elseif(__WEBASSEMBLY64)
212    add_default_target_arch(wasm64)
213  elseif(__VE)
214    add_default_target_arch(ve)
215  endif()
216endmacro()
217
218function(get_compiler_rt_root_source_dir ROOT_DIR_VAR)
219  # Compute the path to the root of the Compiler-RT source tree
220  # regardless of how the project was configured.
221  #
222  # This function is useful because using `${CMAKE_SOURCE_DIR}`
223  # is error prone due to the numerous ways Compiler-RT can be
224  # configured.
225  #
226  # `ROOT_DIR_VAR` - the name of the variable to write the result to.
227  #
228  # TODO(dliew): When CMake min version is 3.17 or newer use
229  # `CMAKE_CURRENT_FUNCTION_LIST_DIR` instead.
230  if ("${ROOT_DIR_VAR}" STREQUAL "")
231    message(FATAL_ERROR "ROOT_DIR_VAR cannot be empty")
232  endif()
233
234  # Compiler-rt supports different source root paths.
235  # Handle each case here.
236  set(PATH_TO_COMPILER_RT_SOURCE_ROOT "")
237  if (DEFINED CompilerRTBuiltins_SOURCE_DIR)
238    # Compiler-RT Builtins standalone build.
239    # `llvm-project/compiler-rt/lib/builtins`
240    set(PATH_TO_COMPILER_RT_SOURCE_ROOT "${CompilerRTBuiltins_SOURCE_DIR}/../../")
241  elseif(DEFINED CompilerRT_SOURCE_DIR)
242    # Compiler-RT standalone build.
243    # `llvm-project/compiler-rt`
244    set(PATH_TO_COMPILER_RT_SOURCE_ROOT "${CompilerRT_SOURCE_DIR}")
245  elseif (EXISTS "${CMAKE_SOURCE_DIR}/../compiler-rt")
246    # In tree build with LLVM as the root project.
247    # See `llvm-project/projects/`.
248    # Assumes monorepo layout.
249    set(PATH_TO_COMPILER_RT_SOURCE_ROOT "${CMAKE_SOURCE_DIR}/../compiler-rt")
250  else()
251    message(FATAL_ERROR "Unhandled Compiler-RT source root configuration.")
252  endif()
253
254  get_filename_component(ROOT_DIR "${PATH_TO_COMPILER_RT_SOURCE_ROOT}" ABSOLUTE)
255  if (NOT EXISTS "${ROOT_DIR}")
256    message(FATAL_ERROR "Path \"${ROOT_DIR}\" doesn't exist")
257  endif()
258
259  # Sanity check: Make sure we can locate the current source file via the
260  # computed path.
261  set(PATH_TO_CURRENT_FILE "${ROOT_DIR}/cmake/Modules/CompilerRTUtils.cmake")
262  if (NOT EXISTS "${PATH_TO_CURRENT_FILE}")
263    message(FATAL_ERROR "Could not find \"${PATH_TO_CURRENT_FILE}\"")
264  endif()
265
266  set("${ROOT_DIR_VAR}" "${ROOT_DIR}" PARENT_SCOPE)
267endfunction()
268
269macro(load_llvm_config)
270  if (NOT LLVM_CONFIG_PATH)
271    find_program(LLVM_CONFIG_PATH "llvm-config"
272                 DOC "Path to llvm-config binary")
273    if (NOT LLVM_CONFIG_PATH)
274      message(WARNING "UNSUPPORTED COMPILER-RT CONFIGURATION DETECTED: "
275                      "llvm-config not found.\n"
276                      "Reconfigure with -DLLVM_CONFIG_PATH=path/to/llvm-config.")
277    endif()
278  endif()
279
280  # Compute path to LLVM sources assuming the monorepo layout.
281  # We don't set `LLVM_MAIN_SRC_DIR` directly to avoid overriding a user provided
282  # CMake cache value.
283  get_compiler_rt_root_source_dir(COMPILER_RT_ROOT_SRC_PATH)
284  get_filename_component(LLVM_MAIN_SRC_DIR_DEFAULT "${COMPILER_RT_ROOT_SRC_PATH}/../llvm" ABSOLUTE)
285  if (NOT EXISTS "${LLVM_MAIN_SRC_DIR_DEFAULT}")
286    # TODO(dliew): Remove this legacy fallback path.
287    message(WARNING
288      "LLVM source tree not found at \"${LLVM_MAIN_SRC_DIR_DEFAULT}\". "
289      "You are not using the monorepo layout. This configuration is DEPRECATED.")
290  endif()
291
292  set(FOUND_LLVM_CMAKE_PATH FALSE)
293  if (LLVM_CONFIG_PATH)
294    execute_process(
295      COMMAND ${LLVM_CONFIG_PATH} "--obj-root" "--bindir" "--libdir" "--src-root" "--includedir"
296      RESULT_VARIABLE HAD_ERROR
297      OUTPUT_VARIABLE CONFIG_OUTPUT)
298    if (HAD_ERROR)
299      message(FATAL_ERROR "llvm-config failed with status ${HAD_ERROR}")
300    endif()
301    string(REGEX REPLACE "[ \t]*[\r\n]+[ \t]*" ";" CONFIG_OUTPUT ${CONFIG_OUTPUT})
302    list(GET CONFIG_OUTPUT 0 BINARY_DIR)
303    list(GET CONFIG_OUTPUT 1 TOOLS_BINARY_DIR)
304    list(GET CONFIG_OUTPUT 2 LIBRARY_DIR)
305    list(GET CONFIG_OUTPUT 3 MAIN_SRC_DIR)
306    list(GET CONFIG_OUTPUT 4 INCLUDE_DIR)
307
308    set(LLVM_BINARY_DIR ${BINARY_DIR} CACHE PATH "Path to LLVM build tree")
309    set(LLVM_LIBRARY_DIR ${LIBRARY_DIR} CACHE PATH "Path to llvm/lib")
310    set(LLVM_TOOLS_BINARY_DIR ${TOOLS_BINARY_DIR} CACHE PATH "Path to llvm/bin")
311    set(LLVM_INCLUDE_DIR ${INCLUDE_DIR} CACHE PATH "Paths to LLVM headers")
312
313    if (NOT EXISTS "${LLVM_MAIN_SRC_DIR_DEFAULT}")
314      # TODO(dliew): Remove this legacy fallback path.
315      message(WARNING
316        "Consulting llvm-config for the LLVM source path "
317        "as a fallback. This behavior will be removed in the future.")
318      # We don't set `LLVM_MAIN_SRC_DIR` directly to avoid overriding a user
319      # provided CMake cache value.
320      set(LLVM_MAIN_SRC_DIR_DEFAULT "${MAIN_SRC_DIR}")
321      message(STATUS "Using LLVM source path (${LLVM_MAIN_SRC_DIR_DEFAULT}) from llvm-config")
322    endif()
323
324    # Detect if we have the LLVMXRay and TestingSupport library installed and
325    # available from llvm-config.
326    execute_process(
327      COMMAND ${LLVM_CONFIG_PATH} "--ldflags" "--libs" "xray"
328      RESULT_VARIABLE HAD_ERROR
329      OUTPUT_VARIABLE CONFIG_OUTPUT
330      ERROR_QUIET)
331    if (HAD_ERROR)
332      message(WARNING "llvm-config finding xray failed with status ${HAD_ERROR}")
333      set(COMPILER_RT_HAS_LLVMXRAY FALSE)
334    else()
335      string(REGEX REPLACE "[ \t]*[\r\n]+[ \t]*" ";" CONFIG_OUTPUT ${CONFIG_OUTPUT})
336      list(GET CONFIG_OUTPUT 0 LDFLAGS)
337      list(GET CONFIG_OUTPUT 1 LIBLIST)
338      file(TO_CMAKE_PATH "${LDFLAGS}" LDFLAGS)
339      file(TO_CMAKE_PATH "${LIBLIST}" LIBLIST)
340      set(LLVM_XRAY_LDFLAGS ${LDFLAGS} CACHE STRING "Linker flags for LLVMXRay library")
341      set(LLVM_XRAY_LIBLIST ${LIBLIST} CACHE STRING "Library list for LLVMXRay")
342      set(COMPILER_RT_HAS_LLVMXRAY TRUE)
343    endif()
344
345    set(COMPILER_RT_HAS_LLVMTESTINGSUPPORT FALSE)
346    execute_process(
347      COMMAND ${LLVM_CONFIG_PATH} "--ldflags" "--libs" "testingsupport"
348      RESULT_VARIABLE HAD_ERROR
349      OUTPUT_VARIABLE CONFIG_OUTPUT
350      ERROR_QUIET)
351    if (HAD_ERROR)
352      message(WARNING "llvm-config finding testingsupport failed with status ${HAD_ERROR}")
353    elseif(COMPILER_RT_INCLUDE_TESTS)
354      string(REGEX REPLACE "[ \t]*[\r\n]+[ \t]*" ";" CONFIG_OUTPUT ${CONFIG_OUTPUT})
355      list(GET CONFIG_OUTPUT 0 LDFLAGS)
356      list(GET CONFIG_OUTPUT 1 LIBLIST)
357      if (LIBLIST STREQUAL "")
358        message(WARNING "testingsupport library not installed, some tests will be skipped")
359      else()
360        file(TO_CMAKE_PATH "${LDFLAGS}" LDFLAGS)
361        file(TO_CMAKE_PATH "${LIBLIST}" LIBLIST)
362        set(LLVM_TESTINGSUPPORT_LDFLAGS ${LDFLAGS} CACHE STRING "Linker flags for LLVMTestingSupport library")
363        set(LLVM_TESTINGSUPPORT_LIBLIST ${LIBLIST} CACHE STRING "Library list for LLVMTestingSupport")
364        set(COMPILER_RT_HAS_LLVMTESTINGSUPPORT TRUE)
365      endif()
366    endif()
367
368    # Make use of LLVM CMake modules.
369    # --cmakedir is supported since llvm r291218 (4.0 release)
370    execute_process(
371      COMMAND ${LLVM_CONFIG_PATH} --cmakedir
372      RESULT_VARIABLE HAD_ERROR
373      OUTPUT_VARIABLE CONFIG_OUTPUT)
374    if(NOT HAD_ERROR)
375      string(STRIP "${CONFIG_OUTPUT}" LLVM_CMAKE_PATH_FROM_LLVM_CONFIG)
376      file(TO_CMAKE_PATH ${LLVM_CMAKE_PATH_FROM_LLVM_CONFIG} LLVM_CMAKE_PATH)
377    else()
378      file(TO_CMAKE_PATH ${LLVM_BINARY_DIR} LLVM_BINARY_DIR_CMAKE_STYLE)
379      set(LLVM_CMAKE_PATH "${LLVM_BINARY_DIR_CMAKE_STYLE}/lib${LLVM_LIBDIR_SUFFIX}/cmake/llvm")
380    endif()
381
382    set(LLVM_CMAKE_INCLUDE_FILE "${LLVM_CMAKE_PATH}/LLVMConfig.cmake")
383    if (EXISTS "${LLVM_CMAKE_INCLUDE_FILE}")
384      list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_PATH}")
385      # Get some LLVM variables from LLVMConfig.
386      include("${LLVM_CMAKE_INCLUDE_FILE}")
387      set(FOUND_LLVM_CMAKE_PATH TRUE)
388    else()
389      set(FOUND_LLVM_CMAKE_PATH FALSE)
390      message(WARNING "LLVM CMake path (${LLVM_CMAKE_INCLUDE_FILE}) reported by llvm-config does not exist")
391    endif()
392    unset(LLVM_CMAKE_INCLUDE_FILE)
393
394    set(LLVM_LIBRARY_OUTPUT_INTDIR
395      ${LLVM_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib${LLVM_LIBDIR_SUFFIX})
396  endif()
397
398  # Finally set the cache variable now that `llvm-config` has also had a chance
399  # to set `LLVM_MAIN_SRC_DIR_DEFAULT`.
400  set(LLVM_MAIN_SRC_DIR "${LLVM_MAIN_SRC_DIR_DEFAULT}" CACHE PATH "Path to LLVM source tree")
401  message(STATUS "LLVM_MAIN_SRC_DIR: \"${LLVM_MAIN_SRC_DIR}\"")
402  if (NOT EXISTS "${LLVM_MAIN_SRC_DIR}")
403    # TODO(dliew): Make this a hard error
404    message(WARNING "LLVM_MAIN_SRC_DIR (${LLVM_MAIN_SRC_DIR}) does not exist. "
405                    "You can override the inferred path by adding "
406                    "`-DLLVM_MAIN_SRC_DIR=<path_to_llvm_src>` to your CMake invocation "
407                    "where `<path_to_llvm_src>` is the path to the `llvm` directory in "
408                    "the `llvm-project` repo. "
409                    "This will be treated as error in the future.")
410  endif()
411
412  if (NOT FOUND_LLVM_CMAKE_PATH)
413    # This configuration tries to configure without the prescence of `LLVMConfig.cmake`. It is
414    # intended for testing purposes (generating the lit test suites) and will likely not support
415    # a build of the runtimes in compiler-rt.
416    include(CompilerRTMockLLVMCMakeConfig)
417    compiler_rt_mock_llvm_cmake_config()
418  endif()
419
420endmacro()
421
422macro(construct_compiler_rt_default_triple)
423  if(COMPILER_RT_DEFAULT_TARGET_ONLY)
424    if(DEFINED COMPILER_RT_DEFAULT_TARGET_TRIPLE)
425      message(FATAL_ERROR "COMPILER_RT_DEFAULT_TARGET_TRIPLE isn't supported when building for default target only")
426    endif()
427    set(COMPILER_RT_DEFAULT_TARGET_TRIPLE ${CMAKE_C_COMPILER_TARGET})
428  else()
429    set(COMPILER_RT_DEFAULT_TARGET_TRIPLE ${TARGET_TRIPLE} CACHE STRING
430          "Default triple for which compiler-rt runtimes will be built.")
431  endif()
432
433  if(DEFINED COMPILER_RT_TEST_TARGET_TRIPLE)
434    # Backwards compatibility: this variable used to be called
435    # COMPILER_RT_TEST_TARGET_TRIPLE.
436    set(COMPILER_RT_DEFAULT_TARGET_TRIPLE ${COMPILER_RT_TEST_TARGET_TRIPLE})
437  endif()
438
439  string(REPLACE "-" ";" TARGET_TRIPLE_LIST ${COMPILER_RT_DEFAULT_TARGET_TRIPLE})
440  list(GET TARGET_TRIPLE_LIST 0 COMPILER_RT_DEFAULT_TARGET_ARCH)
441
442  # Map various forms of the architecture names to the canonical forms
443  # (as they are used by clang, see getArchNameForCompilerRTLib).
444  if("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "^i.86$")
445    # Android uses i686, but that's remapped at a later stage.
446    set(COMPILER_RT_DEFAULT_TARGET_ARCH "i386")
447  endif()
448
449  # Determine if test target triple is specified explicitly, and doesn't match the
450  # default.
451  if(NOT COMPILER_RT_DEFAULT_TARGET_TRIPLE STREQUAL TARGET_TRIPLE)
452    set(COMPILER_RT_HAS_EXPLICIT_DEFAULT_TARGET_TRIPLE TRUE)
453  else()
454    set(COMPILER_RT_HAS_EXPLICIT_DEFAULT_TARGET_TRIPLE FALSE)
455  endif()
456endmacro()
457
458# Filter out generic versions of routines that are re-implemented in an
459# architecture specific manner. This prevents multiple definitions of the same
460# symbols, making the symbol selection non-deterministic.
461#
462# We follow the convention that a source file that exists in a sub-directory
463# (e.g. `ppc/divtc3.c`) is architecture-specific and that if a generic
464# implementation exists it will be a top-level source file with the same name
465# modulo the file extension (e.g. `divtc3.c`).
466function(filter_builtin_sources inout_var name)
467  set(intermediate ${${inout_var}})
468  foreach(_file ${intermediate})
469    get_filename_component(_file_dir ${_file} DIRECTORY)
470    if (NOT "${_file_dir}" STREQUAL "")
471      # Architecture specific file. If a generic version exists, print a notice
472      # and ensure that it is removed from the file list.
473      get_filename_component(_name ${_file} NAME)
474      string(REGEX REPLACE "\\.S$" ".c" _cname "${_name}")
475      if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${_cname}")
476        message(STATUS "For ${name} builtins preferring ${_file} to ${_cname}")
477        list(REMOVE_ITEM intermediate ${_cname})
478      endif()
479    endif()
480  endforeach()
481  set(${inout_var} ${intermediate} PARENT_SCOPE)
482endfunction()
483
484function(get_compiler_rt_target arch variable)
485  string(FIND ${COMPILER_RT_DEFAULT_TARGET_TRIPLE} "-" dash_index)
486  string(SUBSTRING ${COMPILER_RT_DEFAULT_TARGET_TRIPLE} ${dash_index} -1 triple_suffix)
487  if(COMPILER_RT_DEFAULT_TARGET_ONLY)
488    # Use exact spelling when building only for the target specified to CMake.
489    set(target "${COMPILER_RT_DEFAULT_TARGET_TRIPLE}")
490  elseif(ANDROID AND ${arch} STREQUAL "i386")
491    set(target "i686${triple_suffix}")
492  else()
493    set(target "${arch}${triple_suffix}")
494  endif()
495  set(${variable} ${target} PARENT_SCOPE)
496endfunction()
497
498function(get_compiler_rt_install_dir arch install_dir)
499  if(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR AND NOT APPLE)
500    get_compiler_rt_target(${arch} target)
501    set(${install_dir} ${COMPILER_RT_INSTALL_LIBRARY_DIR}/${target} PARENT_SCOPE)
502  else()
503    set(${install_dir} ${COMPILER_RT_INSTALL_LIBRARY_DIR} PARENT_SCOPE)
504  endif()
505endfunction()
506
507function(get_compiler_rt_output_dir arch output_dir)
508  if(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR AND NOT APPLE)
509    get_compiler_rt_target(${arch} target)
510    set(${output_dir} ${COMPILER_RT_OUTPUT_LIBRARY_DIR}/${target} PARENT_SCOPE)
511  else()
512    set(${output_dir} ${COMPILER_RT_OUTPUT_LIBRARY_DIR} PARENT_SCOPE)
513  endif()
514endfunction()
515
516# compiler_rt_process_sources(
517#   <OUTPUT_VAR>
518#   <SOURCE_FILE> ...
519#  [ADDITIONAL_HEADERS <header> ...]
520# )
521#
522# Process the provided sources and write the list of new sources
523# into `<OUTPUT_VAR>`.
524#
525# ADDITIONAL_HEADERS     - Adds the supplied header to list of sources for IDEs.
526#
527# This function is very similar to `llvm_process_sources()` but exists here
528# because we need to support standalone builds of compiler-rt.
529function(compiler_rt_process_sources OUTPUT_VAR)
530  cmake_parse_arguments(
531    ARG
532    ""
533    ""
534    "ADDITIONAL_HEADERS"
535    ${ARGN}
536  )
537  set(sources ${ARG_UNPARSED_ARGUMENTS})
538  set(headers "")
539  if (XCODE OR MSVC_IDE OR CMAKE_EXTRA_GENERATOR)
540    # For IDEs we need to tell CMake about header files.
541    # Otherwise they won't show up in UI.
542    set(headers ${ARG_ADDITIONAL_HEADERS})
543    list(LENGTH headers headers_length)
544    if (${headers_length} GREATER 0)
545      set_source_files_properties(${headers}
546        PROPERTIES HEADER_FILE_ONLY ON)
547    endif()
548  endif()
549  set("${OUTPUT_VAR}" ${sources} ${headers} PARENT_SCOPE)
550endfunction()
551
552# Create install targets for a library and its parent component (if specified).
553function(add_compiler_rt_install_targets name)
554  cmake_parse_arguments(ARG "" "PARENT_TARGET" "" ${ARGN})
555
556  if(ARG_PARENT_TARGET AND NOT TARGET install-${ARG_PARENT_TARGET})
557    # The parent install target specifies the parent component to scrape up
558    # anything not installed by the individual install targets, and to handle
559    # installation when running the multi-configuration generators.
560    add_custom_target(install-${ARG_PARENT_TARGET}
561                      DEPENDS ${ARG_PARENT_TARGET}
562                      COMMAND "${CMAKE_COMMAND}"
563                              -DCMAKE_INSTALL_COMPONENT=${ARG_PARENT_TARGET}
564                              -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
565    add_custom_target(install-${ARG_PARENT_TARGET}-stripped
566                      DEPENDS ${ARG_PARENT_TARGET}
567                      COMMAND "${CMAKE_COMMAND}"
568                              -DCMAKE_INSTALL_COMPONENT=${ARG_PARENT_TARGET}
569                              -DCMAKE_INSTALL_DO_STRIP=1
570                              -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
571    set_target_properties(install-${ARG_PARENT_TARGET} PROPERTIES
572                          FOLDER "Compiler-RT Misc")
573    set_target_properties(install-${ARG_PARENT_TARGET}-stripped PROPERTIES
574                          FOLDER "Compiler-RT Misc")
575    add_dependencies(install-compiler-rt install-${ARG_PARENT_TARGET})
576    add_dependencies(install-compiler-rt-stripped install-${ARG_PARENT_TARGET}-stripped)
577  endif()
578
579  # We only want to generate per-library install targets if you aren't using
580  # an IDE because the extra targets get cluttered in IDEs.
581  if(NOT CMAKE_CONFIGURATION_TYPES)
582    add_custom_target(install-${name}
583                      DEPENDS ${name}
584                      COMMAND "${CMAKE_COMMAND}"
585                              -DCMAKE_INSTALL_COMPONENT=${name}
586                              -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
587    add_custom_target(install-${name}-stripped
588                      DEPENDS ${name}
589                      COMMAND "${CMAKE_COMMAND}"
590                              -DCMAKE_INSTALL_COMPONENT=${name}
591                              -DCMAKE_INSTALL_DO_STRIP=1
592                              -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
593    # If you have a parent target specified, we bind the new install target
594    # to the parent install target.
595    if(LIB_PARENT_TARGET)
596      add_dependencies(install-${LIB_PARENT_TARGET} install-${name})
597      add_dependencies(install-${LIB_PARENT_TARGET}-stripped install-${name}-stripped)
598    endif()
599  endif()
600endfunction()
601