1118 lines · plain
1# This directory contains a large amount of C code which provides2# generic implementations of the core runtime library along with optimized3# architecture-specific code in various subdirectories.4 5if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)6 cmake_minimum_required(VERSION 3.20.0)7 8 set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)9 project(CompilerRTBuiltins C CXX ASM)10 set(COMPILER_RT_STANDALONE_BUILD TRUE)11 set(COMPILER_RT_BUILTINS_STANDALONE_BUILD TRUE)12 13 set(COMPILER_RT_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../..")14 15 set(LLVM_COMMON_CMAKE_UTILS "${COMPILER_RT_SOURCE_DIR}/../cmake")16 17 # Add path for custom modules18 list(INSERT CMAKE_MODULE_PATH 019 "${COMPILER_RT_SOURCE_DIR}/cmake"20 "${COMPILER_RT_SOURCE_DIR}/cmake/Modules"21 "${LLVM_COMMON_CMAKE_UTILS}"22 "${LLVM_COMMON_CMAKE_UTILS}/Modules"23 )24 25 include(base-config-ix)26 include(CompilerRTUtils)27 28 if (NOT LLVM_RUNTIMES_BUILD)29 load_llvm_config()30 endif()31 construct_compiler_rt_default_triple()32 33 include(SetPlatformToolchainTools)34 if(APPLE)35 include(CompilerRTDarwinUtils)36 endif()37 if(APPLE)38 include(UseLibtool)39 endif()40 include(AddCompilerRT)41 42 if(MINGW)43 # Simplified version of what's set in cmake/config-ix.cmake; not including44 # builtins, which are linked separately.45 set(MINGW_LIBRARIES mingw32 moldname mingwex msvcrt advapi32 shell3246 user32 kernel32 mingw32 moldname mingwex msvcrt)47 endif()48endif()49 50if (COMPILER_RT_STANDALONE_BUILD)51 # When compiler-rt is being built standalone, possibly as a cross-compilation52 # target, the target may or may not want position independent code. This53 # option provides an avenue through which the flag may be controlled when an54 # LLVM configuration is not being utilized.55 option(COMPILER_RT_BUILTINS_ENABLE_PIC56 "Turns on or off -fPIC for the builtin library source"57 ON)58endif()59 60include(builtin-config-ix)61include(CMakeDependentOption)62include(CMakePushCheckState)63include(CheckAssemblerFlag)64 65option(COMPILER_RT_BUILTINS_HIDE_SYMBOLS66 "Do not export any symbols from the static library." ON)67 68include_directories(../../../third-party/siphash/include)69 70# TODO: Need to add a mechanism for logging errors when builtin source files are71# added to a sub-directory and not this CMakeLists file.72set(GENERIC_SOURCES73 absvdi2.c74 absvsi2.c75 absvti2.c76 adddf3.c77 addsf3.c78 addvdi3.c79 addvsi3.c80 addvti3.c81 apple_versioning.c82 ashldi3.c83 ashlti3.c84 ashrdi3.c85 ashrti3.c86 bswapdi2.c87 bswapsi2.c88 clzdi2.c89 clzsi2.c90 clzti2.c91 cmpdi2.c92 cmpti2.c93 comparedf2.c94 comparesf2.c95 ctzdi2.c96 ctzsi2.c97 ctzti2.c98 divdc3.c99 divdf3.c100 divdi3.c101 divmoddi4.c102 divmodsi4.c103 divmodti4.c104 divsc3.c105 divsf3.c106 divsi3.c107 divti3.c108 extendsfdf2.c109 extendhfsf2.c110 extendhfdf2.c111 ffsdi2.c112 ffssi2.c113 ffsti2.c114 fixdfdi.c115 fixdfsi.c116 fixdfti.c117 fixsfdi.c118 fixsfsi.c119 fixsfti.c120 fixunsdfdi.c121 fixunsdfsi.c122 fixunsdfti.c123 fixunssfdi.c124 fixunssfsi.c125 fixunssfti.c126 floatdidf.c127 floatdisf.c128 floatsidf.c129 floatsisf.c130 floattidf.c131 floattisf.c132 floatundidf.c133 floatundisf.c134 floatunsidf.c135 floatunsisf.c136 floatuntidf.c137 floatuntisf.c138 fp_mode.c139 int_util.c140 lshrdi3.c141 lshrti3.c142 moddi3.c143 modsi3.c144 modti3.c145 muldc3.c146 muldf3.c147 muldi3.c148 mulodi4.c149 mulosi4.c150 muloti4.c151 mulsc3.c152 mulsf3.c153 multi3.c154 mulvdi3.c155 mulvsi3.c156 mulvti3.c157 negdf2.c158 negdi2.c159 negsf2.c160 negti2.c161 negvdi2.c162 negvsi2.c163 negvti2.c164 os_version_check.c165 paritydi2.c166 paritysi2.c167 parityti2.c168 popcountdi2.c169 popcountsi2.c170 popcountti2.c171 powidf2.c172 powisf2.c173 subdf3.c174 subsf3.c175 subvdi3.c176 subvsi3.c177 subvti3.c178 trampoline_setup.c179 truncdfhf2.c180 truncdfsf2.c181 truncsfhf2.c182 ucmpdi2.c183 ucmpti2.c184 udivdi3.c185 udivmoddi4.c186 udivmodsi4.c187 udivmodti4.c188 udivsi3.c189 udivti3.c190 umoddi3.c191 umodsi3.c192 umodti3.c193)194 195# We only build BF16 files when "__bf16" is available.196set(BF16_SOURCES197 extendbfsf2.c198 truncdfbf2.c199 truncxfbf2.c200 truncsfbf2.c201 trunctfbf2.c202)203 204set(GENERIC_TF_SOURCES205 addtf3.c206 comparetf2.c207 divtc3.c208 divtf3.c209 extenddftf2.c210 extendhftf2.c211 extendsftf2.c212 fixtfdi.c213 fixtfsi.c214 fixtfti.c215 fixunstfdi.c216 fixunstfsi.c217 fixunstfti.c218 floatditf.c219 floatsitf.c220 floattitf.c221 floatunditf.c222 floatunsitf.c223 floatuntitf.c224 multc3.c225 multf3.c226 powitf2.c227 subtf3.c228 trunctfdf2.c229 trunctfhf2.c230 trunctfsf2.c231)232 233option(COMPILER_RT_EXCLUDE_ATOMIC_BUILTIN234 "Skip the atomic builtin (these should normally be provided by a shared library)"235 On)236option(COMPILER_RT_EXCLUDE_LIBC_PROVIDED_ARM_AEABI_BUILTINS237 "Skip the standard C library provided arm aeabi builtins from compiler-rt)"238 Off)239 240if(NOT FUCHSIA AND NOT COMPILER_RT_BAREMETAL_BUILD AND NOT COMPILER_RT_GPU_BUILD)241 set(GENERIC_SOURCES242 ${GENERIC_SOURCES}243 emutls.c244 enable_execute_stack.c245 eprintf.c246 )247endif()248 249option(COMPILER_RT_LIBATOMIC_USE_PTHREAD250 "Whether libatomic should use pthreads if available."251 Off)252 253if(COMPILER_RT_LIBATOMIC_USE_PTHREAD)254 add_compile_definitions(_LIBATOMIC_USE_PTHREAD)255endif()256 257if(COMPILER_RT_HAS_ATOMIC_KEYWORD AND NOT COMPILER_RT_EXCLUDE_ATOMIC_BUILTIN)258 set(GENERIC_SOURCES259 ${GENERIC_SOURCES}260 atomic.c261 )262endif()263 264if(APPLE)265 set(GENERIC_SOURCES266 ${GENERIC_SOURCES}267 atomic_flag_clear.c268 atomic_flag_clear_explicit.c269 atomic_flag_test_and_set.c270 atomic_flag_test_and_set_explicit.c271 atomic_signal_fence.c272 atomic_thread_fence.c273 )274endif()275 276if (HAVE_UNWIND_H)277 set(GENERIC_SOURCES278 ${GENERIC_SOURCES}279 gcc_personality_v0.c280 )281endif ()282 283if (NOT FUCHSIA)284 set(GENERIC_SOURCES285 ${GENERIC_SOURCES}286 clear_cache.c287 )288endif()289 290# These files are used on 32-bit and 64-bit x86.291set(x86_ARCH_SOURCES292 cpu_model/x86.c293 )294 295if (NOT MSVC)296 set(x86_ARCH_SOURCES297 ${x86_ARCH_SOURCES}298 i386/fp_mode.c299 )300endif ()301 302# Implement extended-precision builtins, assuming long double is 80 bits.303# long double is not 80 bits on Android or MSVC.304set(x86_80_BIT_SOURCES305 divxc3.c306 extendhfxf2.c307 extendxftf2.c308 fixxfdi.c309 fixxfti.c310 fixunsxfdi.c311 fixunsxfsi.c312 fixunsxfti.c313 floatdixf.c314 floattixf.c315 floatundixf.c316 floatuntixf.c317 mulxc3.c318 powixf2.c319 trunctfxf2.c320 truncxfhf2.c321)322 323if (NOT MSVC)324 set(x86_64_SOURCES325 ${GENERIC_SOURCES}326 ${GENERIC_TF_SOURCES}327 ${x86_ARCH_SOURCES}328 x86_64/floatdidf.c329 x86_64/floatdisf.c330 )331 if (NOT WIN32)332 set(x86_64_SOURCES333 ${x86_64_SOURCES}334 x86_64/floatundidf.S335 x86_64/floatundisf.S336 )337 endif()338 339 if (NOT ANDROID)340 set(x86_64_SOURCES341 ${x86_64_SOURCES}342 ${x86_80_BIT_SOURCES}343 x86_64/floatdixf.c344 )345 if (NOT WIN32)346 set(x86_64_SOURCES347 ${x86_64_SOURCES}348 x86_64/floatundixf.S349 )350 endif()351 endif()352 353 # Darwin x86_64 Haswell354 set(x86_64h_SOURCES ${x86_64_SOURCES})355 356 if (WIN32)357 set(x86_64_SOURCES358 ${x86_64_SOURCES}359 x86_64/chkstk.S360 )361 endif()362 363 set(i386_SOURCES364 ${GENERIC_SOURCES}365 ${x86_ARCH_SOURCES}366 i386/ashldi3.S367 i386/ashrdi3.S368 i386/divdi3.S369 i386/floatdidf.S370 i386/floatdisf.S371 i386/floatundidf.S372 i386/floatundisf.S373 i386/lshrdi3.S374 i386/moddi3.S375 i386/muldi3.S376 i386/udivdi3.S377 i386/umoddi3.S378 )379 380 if (NOT ANDROID)381 set(i386_SOURCES382 ${i386_SOURCES}383 ${x86_80_BIT_SOURCES}384 i386/floatdixf.S385 i386/floatundixf.S386 )387 endif()388 389 if (WIN32)390 set(i386_SOURCES391 ${i386_SOURCES}392 i386/chkstk.S393 i386/chkstk2.S394 )395 endif()396else () # MSVC397 # Use C versions of functions when building on MSVC398 # MSVC's assembler takes Intel syntax, not AT&T syntax.399 # Also use only MSVC compilable builtin implementations.400 set(x86_64_SOURCES401 ${GENERIC_SOURCES}402 ${x86_ARCH_SOURCES}403 x86_64/floatdidf.c404 x86_64/floatdisf.c405 )406 set(i386_SOURCES ${GENERIC_SOURCES} ${x86_ARCH_SOURCES})407endif () # if (NOT MSVC)408 409 410# builtin support for Targets that have Arm state or have Thumb2411set(arm_or_thumb2_base_SOURCES412 arm/fp_mode.c413 arm/bswapdi2.S414 arm/bswapsi2.S415 arm/clzdi2.S416 arm/clzsi2.S417 arm/comparesf2.S418 arm/divmodsi4.S419 arm/divsi3.S420 arm/modsi3.S421 arm/udivmodsi4.S422 arm/udivsi3.S423 arm/umodsi3.S424 ${GENERIC_SOURCES}425)426 427option(COMPILER_RT_ARM_OPTIMIZED_FP428 "On 32-bit Arm, use optimized assembly implementations of FP arithmetic. Likely to increase code size, but be faster." ON)429 430set(arm_or_thumb2_optimized_fp_SOURCES)431if(COMPILER_RT_ARM_OPTIMIZED_FP AND BUILTIN_SUPPORTED_ARCH MATCHES "arm")432 check_assembler_flag(COMPILER_RT_HAS_MIMPLICIT_IT -mimplicit-it=always)433 if(COMPILER_RT_HAS_MIMPLICIT_IT)434 set(implicit_it_flag -mimplicit-it=always)435 else()436 check_assembler_flag(437 COMPILER_RT_HAS_WA_MIMPLICIT_IT -Wa,-mimplicit-it=always)438 if(COMPILER_RT_HAS_WA_MIMPLICIT_IT)439 set(implicit_it_flag -Wa,-mimplicit-it=always)440 else()441 message(WARNING "Don't know how to set the -mimplicit-it=always flag in this assembler; not including Arm optimized implementations")442 set(implicit_it_flag "")443 endif()444 endif()445 446 if(implicit_it_flag)447 set(assembly_files448 arm/mulsf3.S449 arm/divsf3.S450 )451 set_source_files_properties(${assembly_files}452 PROPERTIES COMPILE_OPTIONS ${implicit_it_flag})453 set(arm_or_thumb2_optimized_fp_SOURCES454 ${assembly_files}455 arm/fnan2.c456 arm/fnorm2.c457 arm/funder.c458 )459 endif()460endif()461 462set(arm_sync_SOURCES463 arm/sync_fetch_and_add_4.S464 arm/sync_fetch_and_add_8.S465 arm/sync_fetch_and_and_4.S466 arm/sync_fetch_and_and_8.S467 arm/sync_fetch_and_max_4.S468 arm/sync_fetch_and_max_8.S469 arm/sync_fetch_and_min_4.S470 arm/sync_fetch_and_min_8.S471 arm/sync_fetch_and_nand_4.S472 arm/sync_fetch_and_nand_8.S473 arm/sync_fetch_and_or_4.S474 arm/sync_fetch_and_or_8.S475 arm/sync_fetch_and_sub_4.S476 arm/sync_fetch_and_sub_8.S477 arm/sync_fetch_and_umax_4.S478 arm/sync_fetch_and_umax_8.S479 arm/sync_fetch_and_umin_4.S480 arm/sync_fetch_and_umin_8.S481 arm/sync_fetch_and_xor_4.S482 arm/sync_fetch_and_xor_8.S483)484 485# builtin support for Thumb-only targets with very limited Thumb2 technology,486# such as v6-m and v8-m.baseline487set(thumb1_base_SOURCES488 arm/divsi3.S489 arm/udivsi3.S490 arm/comparesf2.S491 arm/addsf3.S492 ${GENERIC_SOURCES}493)494 495if(COMPILER_RT_ARM_OPTIMIZED_FP)496 set(thumb1_base_SOURCES497 arm/thumb1/mulsf3.S498 arm/fnan2.c499 arm/fnorm2.c500 arm/funder.c501 ${thumb1_base_SOURCES}502 )503endif()504 505set(arm_EABI_RT_SOURCES506 arm/aeabi_cdcmp.S507 arm/aeabi_cdcmpeq_check_nan.c508 arm/aeabi_cfcmp.S509 arm/aeabi_cfcmpeq_check_nan.c510 arm/aeabi_dcmp.S511 arm/aeabi_div0.c512 arm/aeabi_drsub.c513 arm/aeabi_fcmp.S514 arm/aeabi_frsub.c515 arm/aeabi_idivmod.S516 arm/aeabi_ldivmod.S517 arm/aeabi_uidivmod.S518 arm/aeabi_uldivmod.S519)520 521set(arm_EABI_CLIB_SOURCES522 arm/aeabi_memcmp.S523 arm/aeabi_memcpy.S524 arm/aeabi_memmove.S525 arm/aeabi_memset.S526)527 528if(NOT COMPILER_RT_EXCLUDE_LIBC_PROVIDED_ARM_AEABI_BUILTINS)529 set(arm_EABI_SOURCES530 ${arm_EABI_RT_SOURCES}531 ${arm_EABI_CLIB_SOURCES}532 )533else()534 message(STATUS "COMPILER_RT_EXCLUDE_LIBC_PROVIDED_ARM_AEABI_BUILTINS is ON, so skipping __aeabi_memcmp, __aeabi_memcpy, __aeabi_memmove and __aeabi_memset Sources")535 set(arm_EABI_SOURCES536 ${arm_EABI_RT_SOURCES}537 )538endif()539 540set(arm_Thumb1_JT_SOURCES541 arm/switch16.S542 arm/switch32.S543 arm/switch8.S544 arm/switchu8.S545)546set(arm_Thumb1_SjLj_EH_SOURCES547 arm/restore_vfp_d8_d15_regs.S548 arm/save_vfp_d8_d15_regs.S549)550set(arm_Thumb1_VFPv2_DP_SOURCES551 arm/adddf3vfp.S552 arm/divdf3vfp.S553 arm/eqdf2vfp.S554 arm/extendsfdf2vfp.S555 arm/fixdfsivfp.S556 arm/fixunsdfsivfp.S557 arm/floatsidfvfp.S558 arm/floatunssidfvfp.S559 arm/gedf2vfp.S560 arm/gtdf2vfp.S561 arm/ledf2vfp.S562 arm/ltdf2vfp.S563 arm/muldf3vfp.S564 arm/nedf2vfp.S565 arm/negdf2vfp.S566 arm/subdf3vfp.S567 arm/truncdfsf2vfp.S568 arm/unorddf2vfp.S569)570set(arm_Thumb1_VFPv2_SP_SOURCES571 arm/addsf3vfp.S572 arm/divsf3vfp.S573 arm/eqsf2vfp.S574 arm/fixsfsivfp.S575 arm/fixunssfsivfp.S576 arm/floatsisfvfp.S577 arm/floatunssisfvfp.S578 arm/gesf2vfp.S579 arm/gtsf2vfp.S580 arm/lesf2vfp.S581 arm/ltsf2vfp.S582 arm/mulsf3vfp.S583 arm/negsf2vfp.S584 arm/nesf2vfp.S585 arm/subsf3vfp.S586 arm/unordsf2vfp.S587)588set(arm_Thumb1_icache_SOURCES589 arm/sync_synchronize.S590)591 592# thumb1 calling into Arm to cover support593set(arm_Thumb1_SOURCES594 ${arm_Thumb1_JT_SOURCES}595 ${arm_Thumb1_SjLj_EH_SOURCES}596 ${arm_Thumb1_VFPv2_DP_SOURCES}597 ${arm_Thumb1_VFPv2_SP_SOURCES}598 ${arm_Thumb1_icache_SOURCES}599)600 601# base functionality for Arm Targets prior to Arm v7-a and Armv6-m such as v6,602# v5t, v4t603set(arm_min_SOURCES604 ${arm_or_thumb2_base_SOURCES}605 ${arm_EABI_SOURCES}606)607 608if(MINGW)609 set(arm_SOURCES610 arm/aeabi_idivmod.S611 arm/aeabi_ldivmod.S612 arm/aeabi_uidivmod.S613 arm/aeabi_uldivmod.S614 arm/chkstk.S615 ${arm_or_thumb2_base_SOURCES}616 ${arm_or_thumb2_optimized_fp_SOURCES}617 ${arm_sync_SOURCES}618 )619 620 set(thumb1_SOURCES621 ${thumb1_base_SOURCES}622 )623elseif(NOT WIN32)624 # TODO the EABI sources should only be added to EABI targets625 set(arm_SOURCES626 ${arm_or_thumb2_base_SOURCES}627 ${arm_or_thumb2_optimized_fp_SOURCES}628 ${arm_sync_SOURCES}629 ${arm_EABI_SOURCES}630 ${arm_Thumb1_SOURCES}631 )632 633 set(thumb1_SOURCES634 ${thumb1_base_SOURCES}635 ${arm_EABI_SOURCES}636 )637endif()638 639set(aarch64_SOURCES640 ${GENERIC_TF_SOURCES}641 ${GENERIC_SOURCES}642 cpu_model/aarch64.c643 aarch64/fp_mode.c644)645 646# Append sources specific to AArch64 targets that aren't supported by cl.exe647if(CLANG_CL OR NOT MSVC)648 list(APPEND aarch64_SOURCES649 aarch64/emupac.cpp650 )651endif()652 653set(COMPILER_RT_AARCH64_FMV_USES_GLOBAL_CONSTRUCTOR NOT(FUCHSIA OR APPLE))654 655if (COMPILER_RT_HAS_AARCH64_SME)656 if (APPLE)657 list(APPEND aarch64_SOURCES aarch64/arm_apple_sme_abi.s)658 set_source_files_properties(aarch64/arm_apple_sme_abi.s PROPERTIES COMPILE_FLAGS -march=armv8a+sme)659 message(STATUS "AArch64 Apple SME ABI routines enabled")660 elseif (NOT COMPILER_RT_DISABLE_AARCH64_FMV AND COMPILER_RT_HAS_FNO_BUILTIN_FLAG AND COMPILER_RT_AARCH64_FMV_USES_GLOBAL_CONSTRUCTOR)661 list(APPEND aarch64_SOURCES aarch64/sme-abi.S aarch64/sme-abi-assert.c)662 if(COMPILER_RT_HAS_ARM_UNALIGNED AND COMPILER_RT_HAS_ARM_FP)663 list(APPEND aarch64_SOURCES aarch64/sme-libc-opt-memset-memchr.S aarch64/sme-libc-opt-memcpy-memmove.S)664 elseif(COMPILER_RT_HAS_ARM_UNALIGNED)665 list(APPEND aarch64_SOURCES aarch64/sme-libc-memset-memchr.c aarch64/sme-libc-opt-memcpy-memmove.S)666 message(WARNING "AArch64 SME ABI assembly-optimized memset/memchr disabled: target does not have hardware floating-point support.")667 else()668 list(APPEND aarch64_SOURCES aarch64/sme-libc-memset-memchr.c aarch64/sme-libc-memcpy-memmove.c)669 message(WARNING "AArch64 SME ABI assembly-optimized routines disabled: target does not support unaligned accesses.")670 endif()671 message(STATUS "AArch64 SME ABI routines enabled")672 set_source_files_properties(aarch64/sme-libc-memset-memchr.c aarch64/sme-libc-memcpy-memmove.c PROPERTIES COMPILE_FLAGS "-fno-builtin")673 else()674 if(COMPILER_RT_DISABLE_AARCH64_FMV)675 message(WARNING "AArch64 SME ABI routines require function multiversioning support.")676 endif()677 if(NOT COMPILER_RT_HAS_FNO_BUILTIN_FLAG)678 message(WARNING "AArch64 SME ABI routines require '-fno-builtin'")679 endif()680 if(NOT (COMPILER_RT_HAS_AUXV OR COMPILER_RT_BAREMETAL_BUILD))681 message(WARNING "AArch64 SME ABI routines requires sys/auxv.h or COMPILER_RT_BAREMETAL_BUILD flag")682 endif()683 message(STATUS "AArch64 SME ABI routines disabled")684 endif()685endif()686 687# Generate outline atomics helpers from lse.S base688set(OA_HELPERS_DIR "${CMAKE_CURRENT_BINARY_DIR}/outline_atomic_helpers.dir")689file(MAKE_DIRECTORY "${OA_HELPERS_DIR}")690 691if(CMAKE_HOST_UNIX)692 set(COMPILER_RT_LINK_OR_COPY create_symlink)693else()694 set(COMPILER_RT_LINK_OR_COPY copy)695endif()696 697foreach(pat cas swp ldadd ldclr ldeor ldset)698 foreach(size 1 2 4 8 16)699 foreach(model 1 2 3 4 5)700 if(pat STREQUAL "cas" OR NOT size STREQUAL "16")701 set(source_asm "${CMAKE_CURRENT_SOURCE_DIR}/aarch64/lse.S")702 set(helper_asm "${OA_HELPERS_DIR}/outline_atomic_${pat}${size}_${model}.S")703 add_custom_command(704 OUTPUT "${helper_asm}"705 COMMAND ${CMAKE_COMMAND} -E ${COMPILER_RT_LINK_OR_COPY} "${source_asm}" "${helper_asm}"706 DEPENDS "${source_asm}"707 )708 set_source_files_properties("${helper_asm}"709 PROPERTIES710 COMPILE_DEFINITIONS "L_${pat};SIZE=${size};MODEL=${model}"711 INCLUDE_DIRECTORIES "${CMAKE_CURRENT_SOURCE_DIR}"712 )713 list(APPEND aarch64_SOURCES "${helper_asm}")714 endif()715 endforeach(model)716 endforeach(size)717endforeach(pat)718 719if (MINGW)720 set(aarch64_SOURCES721 ${aarch64_SOURCES}722 aarch64/chkstk.S723 )724endif()725 726# Don't build enable_execute_stack on arm64 darwin.727if (APPLE)728 list(REMOVE_ITEM aarch64_SOURCES enable_execute_stack.c)729endif()730 731set(amdgcn_SOURCES ${GENERIC_SOURCES})732 733set(armv4t_SOURCES ${arm_min_SOURCES})734set(armv5te_SOURCES ${arm_min_SOURCES})735set(armv6_SOURCES ${arm_min_SOURCES})736set(armhf_SOURCES ${arm_SOURCES})737set(armv7_SOURCES ${arm_SOURCES})738set(armv7s_SOURCES ${arm_SOURCES})739set(armv7k_SOURCES ${arm_SOURCES})740set(arm64_SOURCES ${aarch64_SOURCES})741set(arm64e_SOURCES ${aarch64_SOURCES})742set(arm64_32_SOURCES ${aarch64_SOURCES})743set(arm64ec_SOURCES ${aarch64_SOURCES})744 745# macho_embedded archs746set(armv6m_SOURCES ${thumb1_SOURCES})747set(armv7m_SOURCES ${arm_SOURCES})748set(armv7em_SOURCES ${arm_SOURCES})749set(armv8m.base_SOURCES ${thumb1_SOURCES})750set(armv8m.main_SOURCES ${arm_SOURCES})751set(armv8.1m.main_SOURCES ${arm_SOURCES})752 753# 8-bit AVR MCU754set(avr_SOURCES755 avr/mulqi3.S756 avr/mulhi3.S757 avr/exit.S758 avr/divmodhi4.S759 avr/udivmodhi4.S760 avr/divmodqi4.S761 avr/udivmodqi4.S762 ${GENERIC_SOURCES}763)764 765# hexagon arch766set(hexagon_SOURCES767 hexagon/common_entry_exit_abi1.S768 hexagon/common_entry_exit_abi2.S769 hexagon/common_entry_exit_legacy.S770 hexagon/dfaddsub.S771 hexagon/dfdiv.S772 hexagon/dffma.S773 hexagon/dfminmax.S774 hexagon/dfmul.S775 hexagon/dfsqrt.S776 hexagon/divdi3.S777 hexagon/divsi3.S778 hexagon/fastmath2_dlib_asm.S779 hexagon/fastmath2_ldlib_asm.S780 hexagon/fastmath_dlib_asm.S781 hexagon/memcpy_forward_vp4cp4n2.S782 hexagon/memcpy_likely_aligned.S783 hexagon/moddi3.S784 hexagon/modsi3.S785 hexagon/sfdiv_opt.S786 hexagon/sfsqrt_opt.S787 hexagon/udivdi3.S788 hexagon/udivmoddi4.S789 hexagon/udivmodsi4.S790 hexagon/udivsi3.S791 hexagon/umoddi3.S792 hexagon/umodsi3.S793 ${GENERIC_SOURCES}794 ${GENERIC_TF_SOURCES}795)796 797set(loongarch_SOURCES798 loongarch/fp_mode.c799 ${GENERIC_SOURCES}800 ${GENERIC_TF_SOURCES}801)802set(loongarch64_SOURCES803 ${loongarch_SOURCES}804)805 806set(mips_SOURCES ${GENERIC_SOURCES})807set(mipsel_SOURCES ${mips_SOURCES})808set(mips64_SOURCES ${GENERIC_TF_SOURCES}809 ${mips_SOURCES})810set(mips64el_SOURCES ${GENERIC_TF_SOURCES}811 ${mips_SOURCES})812 813set(nvptx64_SOURCES ${GENERIC_SOURCES})814 815set(powerpc_SOURCES ${GENERIC_SOURCES})816 817set(powerpcspe_SOURCES ${GENERIC_SOURCES})818 819set(powerpc64_SOURCES820 ppc/divtc3.c821 ppc/fixtfdi.c822 ppc/fixunstfdi.c823 ppc/floatditf.c824 ppc/floatunditf.c825 ppc/gcc_qadd.c826 ppc/gcc_qdiv.c827 ppc/gcc_qmul.c828 ppc/gcc_qsub.c829 ppc/multc3.c830 ${GENERIC_SOURCES}831)832# These routines require __int128, which isn't supported on AIX.833if (NOT OS_NAME MATCHES "AIX")834 set(powerpc64_SOURCES835 ppc/floattitf.c836 ppc/fixtfti.c837 ppc/fixunstfti.c838 ${powerpc64_SOURCES}839 )840endif()841set(powerpc64le_SOURCES ${powerpc64_SOURCES})842 843set(riscv_SOURCES844 cpu_model/riscv.c845 riscv/fp_mode.c846 riscv/save.S847 riscv/restore.S848 ${GENERIC_SOURCES}849 ${GENERIC_TF_SOURCES}850)851set(riscv32_SOURCES852 riscv/mulsi3.S853 ${riscv_SOURCES}854)855set(riscv64_SOURCES856 riscv/muldi3.S857 ${riscv_SOURCES}858)859 860set(sparc_SOURCES ${GENERIC_SOURCES} ${GENERIC_TF_SOURCES})861set(sparcv9_SOURCES ${GENERIC_SOURCES} ${GENERIC_TF_SOURCES})862 863set(s390x_SOURCES864 ${GENERIC_SOURCES}865 ${GENERIC_TF_SOURCES}866)867 868 869set(wasm_SOURCES870 wasm/__c_longjmp.S871 wasm/__cpp_exception.S872 ${GENERIC_TF_SOURCES}873 ${GENERIC_SOURCES}874)875set(wasm32_SOURCES ${wasm_SOURCES})876set(wasm64_SOURCES ${wasm_SOURCES})877 878set(ve_SOURCES879 ve/grow_stack.S880 ve/grow_stack_align.S881 ${GENERIC_TF_SOURCES}882 ${GENERIC_SOURCES})883 884set(m68k_SOURCES ${GENERIC_SOURCES})885 886add_custom_target(builtins)887set_target_properties(builtins PROPERTIES FOLDER "Compiler-RT/Metatargets")888 889option(COMPILER_RT_ENABLE_SOFTWARE_INT128890 "Enable the int128 builtin routines for all targets."891 OFF)892 893if (APPLE)894 add_subdirectory(Darwin-excludes)895 add_subdirectory(macho_embedded)896 darwin_add_builtin_libraries(${BUILTIN_SUPPORTED_OS})897else ()898 set(BUILTIN_CFLAGS "")899 add_security_warnings(BUILTIN_CFLAGS 0)900 901 if (COMPILER_RT_HAS_FCF_PROTECTION_FLAG)902 append_list_if(COMPILER_RT_ENABLE_CET -fcf-protection=full BUILTIN_CFLAGS)903 endif()904 905 append_list_if(COMPILER_RT_HAS_NOSTDINCXX_FLAG -nostdinc++ BUILTIN_CFLAGS)906 append_list_if(COMPILER_RT_HAS_WBUILTIN_DECLARATION_MISMATCH_FLAG -Werror=builtin-declaration-mismatch BUILTIN_CFLAGS)907 908 # Don't embed directives for picking any specific CRT909 if (MSVC)910 set(CMAKE_MSVC_RUNTIME_LIBRARY "")911 append_list_if(COMPILER_RT_HAS_ZL_FLAG /Zl BUILTIN_CFLAGS)912 endif()913 914 # These flags would normally be added to CMAKE_C_FLAGS by the llvm915 # cmake step. Add them manually if this is a standalone build.916 if(COMPILER_RT_STANDALONE_BUILD)917 if(COMPILER_RT_BUILTINS_ENABLE_PIC)918 append_list_if(COMPILER_RT_HAS_FPIC_FLAG -fPIC BUILTIN_CFLAGS)919 endif()920 append_list_if(COMPILER_RT_HAS_FNO_BUILTIN_FLAG -fno-builtin BUILTIN_CFLAGS)921 if(COMPILER_RT_BUILTINS_HIDE_SYMBOLS)922 append_list_if(COMPILER_RT_HAS_VISIBILITY_HIDDEN_FLAG -fvisibility=hidden BUILTIN_CFLAGS)923 endif()924 if(NOT COMPILER_RT_DEBUG)925 append_list_if(COMPILER_RT_HAS_OMIT_FRAME_POINTER_FLAG -fomit-frame-pointer BUILTIN_CFLAGS)926 endif()927 endif()928 929 # Directly targeting the GPU requires a few extra flags.930 if("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "amdgcn|nvptx")931 append_list_if(COMPILER_RT_HAS_FFREESTANDING_FLAG -ffreestanding BUILTIN_CFLAGS)932 append_list_if(COMPILER_RT_HAS_NOGPULIB_FLAG -nogpulib BUILTIN_CFLAGS)933 append_list_if(COMPILER_RT_HAS_FLTO_FLAG -flto BUILTIN_CFLAGS)934 append_list_if(COMPILER_RT_HAS_FCONVERGENT_FUNCTIONS_FLAG935 -fconvergent-functions BUILTIN_CFLAGS)936 937 # AMDGPU targets want to use a generic ABI.938 if("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "amdgcn")939 append_list_if(COMPILER_RT_HAS_CODE_OBJECT_VERSION_FLAG940 "SHELL:-Xclang -mcode-object-version=none" BUILTIN_CFLAGS)941 endif()942 endif()943 944 set(BUILTIN_DEFS "")945 946 if(COMPILER_RT_BUILTINS_HIDE_SYMBOLS)947 append_list_if(COMPILER_RT_HAS_VISIBILITY_HIDDEN_FLAG VISIBILITY_HIDDEN BUILTIN_DEFS)948 endif()949 950 if(COMPILER_RT_DISABLE_AARCH64_FMV)951 list(APPEND BUILTIN_DEFS DISABLE_AARCH64_FMV)952 elseif(COMPILER_RT_BAREMETAL_BUILD)953 foreach (arch ${BUILTIN_SUPPORTED_ARCH})954 if("${arch}" MATCHES "arm64|aarch64")955 list(APPEND BUILTIN_DEFS ENABLE_BAREMETAL_AARCH64_FMV)956 endif()957 endforeach ()958 endif()959 960 append_list_if(COMPILER_RT_HAS_ASM_LSE HAS_ASM_LSE BUILTIN_DEFS)961 962 foreach (arch ${BUILTIN_SUPPORTED_ARCH})963 if (CAN_TARGET_${arch})964 cmake_push_check_state()965 # TODO: we should probably make most of the checks in builtin-config depend on the target flags.966 set(BUILTIN_CFLAGS_${arch} ${BUILTIN_CFLAGS})967 # CMAKE_REQUIRED_FLAGS must be a space separated string968 # Join BUILTIN_CFLAGS_${arch} and TARGET_${arch}_CFLAGS as a969 # space-separated string.970 list(APPEND CMAKE_REQUIRED_FLAGS971 ${BUILTIN_CFLAGS_${arch}}972 ${TARGET_${arch}_CFLAGS})973 list(JOIN CMAKE_REQUIRED_FLAGS " " CMAKE_REQUIRED_FLAGS)974 message(STATUS "Performing additional configure checks with target flags: ${CMAKE_REQUIRED_FLAGS}")975 # For ARM archs, exclude any VFP builtins if VFP is not supported976 if (${arch} MATCHES "^(arm|armhf|armv7|armv7s|armv7k|armv7m|armv7em|armv8m.main|armv8.1m.main)$")977 check_compile_definition(__ARM_FP "${CMAKE_C_FLAGS}" COMPILER_RT_HAS_${arch}_VFP)978 check_compile_definition(__ARM_PCS_VFP "${CMAKE_C_FLAGS}" COMPILER_RT_HAS_${arch}_ARMHF)979 if(NOT COMPILER_RT_HAS_${arch}_VFP)980 list(REMOVE_ITEM ${arch}_SOURCES ${arm_Thumb1_VFPv2_DP_SOURCES} ${arm_Thumb1_VFPv2_SP_SOURCES} ${arm_Thumb1_SjLj_EH_SOURCES})981 else()982 # Exclude any double-precision builtins if VFP is single-precision-only983 try_compile_only(COMPILER_RT_HAS_${arch}_VFP_DP984 SOURCE "#if !(__ARM_FP & 0x8)985 #error No double-precision support!986 #endif987 int main(void) { return 0; }")988 if(NOT COMPILER_RT_HAS_${arch}_VFP_DP)989 list(REMOVE_ITEM ${arch}_SOURCES ${arm_Thumb1_VFPv2_DP_SOURCES})990 endif()991 endif()992 endif()993 check_c_source_compiles("_Float16 foo(_Float16 x) { return x; }994 int main(void) { return 0; }"995 COMPILER_RT_HAS_${arch}_FLOAT16)996 append_list_if(COMPILER_RT_HAS_${arch}_FLOAT16 -DCOMPILER_RT_HAS_FLOAT16 BUILTIN_CFLAGS_${arch})997 check_c_source_compiles("__bf16 foo(__bf16 x) { return x; }998 int main(void) { return 0; }"999 COMPILER_RT_HAS_${arch}_BFLOAT16)1000 # Build BF16 files only when "__bf16" is available.1001 if(COMPILER_RT_HAS_${arch}_BFLOAT16)1002 list(APPEND ${arch}_SOURCES ${BF16_SOURCES})1003 endif()1004 1005 # Remove a generic C builtin when an arch-specific builtin is specified.1006 filter_builtin_sources(${arch}_SOURCES ${arch})1007 1008 # Needed for clear_cache on debug mode, due to r7's usage in inline asm.1009 # Release mode already sets it via -O2/3, Debug mode doesn't.1010 if (COMPILER_RT_HAS_${arch}_ARMHF)1011 list(APPEND BUILTIN_CFLAGS_${arch} -fomit-frame-pointer -DCOMPILER_RT_ARMHF_TARGET)1012 endif()1013 1014 # For RISCV32 and 32-bit SPARC, we must force enable int128 for compiling long1015 # double routines.1016 if (COMPILER_RT_ENABLE_SOFTWARE_INT128 OR ("${arch}" MATCHES "riscv32|sparc$"1017 AND NOT CMAKE_COMPILER_IS_GNUCC))1018 list(APPEND BUILTIN_CFLAGS_${arch} -fforce-enable-int128)1019 endif()1020 1021 add_compiler_rt_runtime(clang_rt.builtins1022 STATIC1023 ARCHS ${arch}1024 DEPS ${deps_${arch}}1025 SOURCES ${${arch}_SOURCES}1026 DEFS ${BUILTIN_DEFS}1027 CFLAGS ${BUILTIN_CFLAGS_${arch}}1028 C_STANDARD 111029 CXX_STANDARD 171030 PARENT_TARGET builtins)1031 cmake_pop_check_state()1032 endif ()1033 endforeach ()1034endif ()1035 1036add_dependencies(compiler-rt builtins)1037 1038option(COMPILER_RT_BUILD_STANDALONE_LIBATOMIC1039 "Build standalone shared atomic library."1040 OFF)1041 1042if(COMPILER_RT_BUILD_STANDALONE_LIBATOMIC)1043 add_custom_target(builtins-standalone-atomic)1044 set(BUILTIN_TYPE SHARED)1045 if(OS_NAME MATCHES "AIX")1046 include(CompilerRTAIXUtils)1047 if(NOT COMPILER_RT_LIBATOMIC_LINK_FLAGS)1048 get_aix_libatomic_default_link_flags(COMPILER_RT_LIBATOMIC_LINK_FLAGS1049 "${CMAKE_CURRENT_SOURCE_DIR}/ppc/atomic.exp")1050 endif()1051 # For different versions of cmake, SHARED behaves differently. For some1052 # versions, we might need MODULE rather than SHARED.1053 get_aix_libatomic_type(BUILTIN_TYPE)1054 else()1055 list(APPEND COMPILER_RT_LIBATOMIC_LINK_FLAGS -nodefaultlibs)1056 endif()1057 foreach (arch ${BUILTIN_SUPPORTED_ARCH})1058 if(CAN_TARGET_${arch})1059 list(APPEND COMPILER_RT_LIBATOMIC_LINK_LIBS_${arch} clang_rt.builtins-${arch})1060 append_list_if(MINGW "${MINGW_LIBRARIES}" COMPILER_RT_LIBATOMIC_LINK_LIBS_${arch})1061 add_compiler_rt_runtime(clang_rt.atomic1062 ${BUILTIN_TYPE}1063 ARCHS ${arch}1064 SOURCES atomic.c1065 LINK_FLAGS ${COMPILER_RT_LIBATOMIC_LINK_FLAGS}1066 LINK_LIBS ${COMPILER_RT_LIBATOMIC_LINK_LIBS_${arch}}1067 PARENT_TARGET builtins-standalone-atomic)1068 endif()1069 endforeach()1070 # FIXME: On AIX, we have to archive built shared libraries into a static1071 # archive, i.e., libatomic.a. Once cmake adds support of such usage for AIX,1072 # this ad-hoc part can be removed.1073 if(OS_NAME MATCHES "AIX")1074 archive_aix_libatomic(clang_rt.atomic libatomic1075 ARCHS ${BUILTIN_SUPPORTED_ARCH}1076 PARENT_TARGET builtins-standalone-atomic)1077 endif()1078 add_dependencies(compiler-rt builtins-standalone-atomic)1079endif()1080 1081cmake_dependent_option(COMPILER_RT_BUILD_CRT "Build crtbegin.o/crtend.o" ON "COMPILER_RT_HAS_CRT" OFF)1082 1083if (COMPILER_RT_BUILD_CRT)1084 add_compiler_rt_component(crt)1085 1086 option(COMPILER_RT_CRT_USE_EH_FRAME_REGISTRY "Use eh_frame in crtbegin.o/crtend.o" ON)1087 1088 include(CheckSectionExists)1089 check_section_exists(".init_array" COMPILER_RT_HAS_INITFINI_ARRAY1090 SOURCE "volatile int x;\n__attribute__((constructor)) void f(void) {x = 0;}\nint main(void) { return 0; }\n")1091 1092 append_list_if(COMPILER_RT_HAS_STD_C11_FLAG -std=c11 CRT_CFLAGS)1093 append_list_if(COMPILER_RT_HAS_INITFINI_ARRAY -DCRT_HAS_INITFINI_ARRAY CRT_CFLAGS)1094 append_list_if(COMPILER_RT_CRT_USE_EH_FRAME_REGISTRY -DEH_USE_FRAME_REGISTRY CRT_CFLAGS)1095 append_list_if(COMPILER_RT_HAS_FPIC_FLAG -fPIC CRT_CFLAGS)1096 append_list_if(COMPILER_RT_HAS_WNO_PEDANTIC -Wno-pedantic CRT_CFLAGS)1097 if (COMPILER_RT_HAS_FCF_PROTECTION_FLAG)1098 append_list_if(COMPILER_RT_ENABLE_CET -fcf-protection=full CRT_CFLAGS)1099 endif()1100 1101 foreach(arch ${BUILTIN_SUPPORTED_ARCH})1102 add_compiler_rt_runtime(clang_rt.crtbegin1103 OBJECT1104 ARCHS ${arch}1105 SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/crtbegin.c1106 CFLAGS ${CRT_CFLAGS}1107 PARENT_TARGET crt)1108 add_compiler_rt_runtime(clang_rt.crtend1109 OBJECT1110 ARCHS ${arch}1111 SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/crtend.c1112 CFLAGS ${CRT_CFLAGS}1113 PARENT_TARGET crt)1114 endforeach()1115 1116 add_dependencies(compiler-rt crt)1117endif()1118