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