1 # This directory contains a large amount of C code which provides
2 # generic implementations of the core runtime library along with optimized
3 # architecture-specific code in various subdirectories.
5 if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
6 cmake_minimum_required(VERSION 3.20.0)
8 set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
9 project(CompilerRTBuiltins C ASM)
10 set(COMPILER_RT_STANDALONE_BUILD TRUE)
11 set(COMPILER_RT_BUILTINS_STANDALONE_BUILD TRUE)
13 set(COMPILER_RT_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../..")
15 set(LLVM_COMMON_CMAKE_UTILS "${COMPILER_RT_SOURCE_DIR}/../cmake")
17 # Add path for custom modules
18 list(INSERT CMAKE_MODULE_PATH 0
19 "${COMPILER_RT_SOURCE_DIR}/cmake"
20 "${COMPILER_RT_SOURCE_DIR}/cmake/Modules"
21 "${LLVM_COMMON_CMAKE_UTILS}"
22 "${LLVM_COMMON_CMAKE_UTILS}/Modules"
25 include(base-config-ix)
26 include(CompilerRTUtils)
28 if (NOT LLVM_RUNTIMES_BUILD)
31 construct_compiler_rt_default_triple()
33 include(SetPlatformToolchainTools)
35 include(CompilerRTDarwinUtils)
40 include(AddCompilerRT)
43 if (COMPILER_RT_STANDALONE_BUILD)
44 # When compiler-rt is being built standalone, possibly as a cross-compilation
45 # target, the target may or may not want position independent code. This
46 # option provides an avenue through which the flag may be controlled when an
47 # LLVM configuration is not being utilized.
48 option(COMPILER_RT_BUILTINS_ENABLE_PIC
49 "Turns on or off -fPIC for the builtin library source"
53 include(builtin-config-ix)
54 include(CMakeDependentOption)
55 include(CMakePushCheckState)
57 option(COMPILER_RT_BUILTINS_HIDE_SYMBOLS
58 "Do not export any symbols from the static library." ON)
60 # TODO: Need to add a mechanism for logging errors when builtin source files are
61 # added to a sub-directory and not this CMakeLists file.
184 # We only build BF16 files when "__bf16" is available.
190 set(GENERIC_TF_SOURCES
219 option(COMPILER_RT_EXCLUDE_ATOMIC_BUILTIN
220 "Skip the atomic builtin (these should normally be provided by a shared library)"
223 if(NOT FUCHSIA AND NOT COMPILER_RT_BAREMETAL_BUILD)
227 enable_execute_stack.c
232 if(COMPILER_RT_HAS_ATOMIC_KEYWORD AND NOT COMPILER_RT_EXCLUDE_ATOMIC_BUILTIN)
243 atomic_flag_clear_explicit.c
244 atomic_flag_test_and_set.c
245 atomic_flag_test_and_set_explicit.c
246 atomic_signal_fence.c
247 atomic_thread_fence.c
265 # These files are used on 32-bit and 64-bit x86.
277 # Implement extended-precision builtins, assuming long double is 80 bits.
278 # long double is not 80 bits on Android or MSVC.
279 set(x86_80_BIT_SOURCES
299 ${GENERIC_TF_SOURCES}
315 ${x86_80_BIT_SOURCES}
326 # Darwin x86_64 Haswell
327 set(x86_64h_SOURCES ${x86_64_SOURCES})
356 ${x86_80_BIT_SOURCES}
369 # Use C versions of functions when building on MSVC
370 # MSVC's assembler takes Intel syntax, not AT&T syntax.
371 # Also use only MSVC compilable builtin implementations.
378 set(i386_SOURCES ${GENERIC_SOURCES} ${x86_ARCH_SOURCES})
379 endif () # if (NOT MSVC)
382 # builtin support for Targets that have Arm state or have Thumb2
383 set(arm_or_thumb2_base_SOURCES
400 arm/sync_fetch_and_add_4.S
401 arm/sync_fetch_and_add_8.S
402 arm/sync_fetch_and_and_4.S
403 arm/sync_fetch_and_and_8.S
404 arm/sync_fetch_and_max_4.S
405 arm/sync_fetch_and_max_8.S
406 arm/sync_fetch_and_min_4.S
407 arm/sync_fetch_and_min_8.S
408 arm/sync_fetch_and_nand_4.S
409 arm/sync_fetch_and_nand_8.S
410 arm/sync_fetch_and_or_4.S
411 arm/sync_fetch_and_or_8.S
412 arm/sync_fetch_and_sub_4.S
413 arm/sync_fetch_and_sub_8.S
414 arm/sync_fetch_and_umax_4.S
415 arm/sync_fetch_and_umax_8.S
416 arm/sync_fetch_and_umin_4.S
417 arm/sync_fetch_and_umin_8.S
418 arm/sync_fetch_and_xor_4.S
419 arm/sync_fetch_and_xor_8.S
422 # builtin support for Thumb-only targets with very limited Thumb2 technology,
423 # such as v6-m and v8-m.baseline
424 set(thumb1_base_SOURCES
434 arm/aeabi_cdcmpeq_check_nan.c
436 arm/aeabi_cfcmpeq_check_nan.c
452 set(arm_Thumb1_JT_SOURCES
458 set(arm_Thumb1_SjLj_EH_SOURCES
459 arm/restore_vfp_d8_d15_regs.S
460 arm/save_vfp_d8_d15_regs.S
462 set(arm_Thumb1_VFPv2_DP_SOURCES
470 arm/floatunssidfvfp.S
482 set(arm_Thumb1_VFPv2_SP_SOURCES
489 arm/floatunssisfvfp.S
500 set(arm_Thumb1_icache_SOURCES
501 arm/sync_synchronize.S
504 # thumb1 calling into Arm to cover support
505 set(arm_Thumb1_SOURCES
506 ${arm_Thumb1_JT_SOURCES}
507 ${arm_Thumb1_SjLj_EH_SOURCES}
508 ${arm_Thumb1_VFPv2_DP_SOURCES}
509 ${arm_Thumb1_VFPv2_SP_SOURCES}
510 ${arm_Thumb1_icache_SOURCES}
513 # base functionality for Arm Targets prior to Arm v7-a and Armv6-m such as v6,
516 ${arm_or_thumb2_base_SOURCES}
527 ${arm_or_thumb2_base_SOURCES}
532 ${thumb1_base_SOURCES}
535 # TODO the EABI sources should only be added to EABI targets
537 ${arm_or_thumb2_base_SOURCES}
540 ${arm_Thumb1_SOURCES}
544 ${thumb1_base_SOURCES}
550 ${GENERIC_TF_SOURCES}
556 if(COMPILER_RT_HAS_ASM_SME AND (COMPILER_RT_HAS_AUXV OR COMPILER_RT_BAREMETAL_BUILD))
557 list(APPEND aarch64_SOURCES aarch64/sme-abi.S aarch64/sme-abi-init.c)
558 message(STATUS "AArch64 SME ABI routines enabled")
560 message(STATUS "AArch64 SME ABI routines disabled")
563 # Generate outline atomics helpers from lse.S base
564 set(OA_HELPERS_DIR "${CMAKE_CURRENT_BINARY_DIR}/outline_atomic_helpers.dir")
565 file(MAKE_DIRECTORY "${OA_HELPERS_DIR}")
568 set(COMPILER_RT_LINK_OR_COPY create_symlink)
570 set(COMPILER_RT_LINK_OR_COPY copy)
573 foreach(pat cas swp ldadd ldclr ldeor ldset)
574 foreach(size 1 2 4 8 16)
575 foreach(model 1 2 3 4 5)
576 if(pat STREQUAL "cas" OR NOT size STREQUAL "16")
577 set(source_asm "${CMAKE_CURRENT_SOURCE_DIR}/aarch64/lse.S")
578 set(helper_asm "${OA_HELPERS_DIR}/outline_atomic_${pat}${size}_${model}.S")
580 OUTPUT "${helper_asm}"
581 COMMAND ${CMAKE_COMMAND} -E ${COMPILER_RT_LINK_OR_COPY} "${source_asm}" "${helper_asm}"
582 DEPENDS "${source_asm}"
584 set_source_files_properties("${helper_asm}"
586 COMPILE_DEFINITIONS "L_${pat};SIZE=${size};MODEL=${model}"
587 INCLUDE_DIRECTORIES "${CMAKE_CURRENT_SOURCE_DIR}"
589 list(APPEND aarch64_SOURCES "${helper_asm}")
602 set(armv4t_SOURCES ${arm_min_SOURCES})
603 set(armv5te_SOURCES ${arm_min_SOURCES})
604 set(armv6_SOURCES ${arm_min_SOURCES})
605 set(armhf_SOURCES ${arm_SOURCES})
606 set(armv7_SOURCES ${arm_SOURCES})
607 set(armv7s_SOURCES ${arm_SOURCES})
608 set(armv7k_SOURCES ${arm_SOURCES})
609 set(arm64_SOURCES ${aarch64_SOURCES})
610 set(arm64e_SOURCES ${aarch64_SOURCES})
611 set(arm64_32_SOURCES ${aarch64_SOURCES})
613 # macho_embedded archs
614 set(armv6m_SOURCES ${thumb1_SOURCES})
615 set(armv7m_SOURCES ${arm_SOURCES})
616 set(armv7em_SOURCES ${arm_SOURCES})
617 set(armv8m.base_SOURCES ${thumb1_SOURCES})
618 set(armv8m.main_SOURCES ${arm_SOURCES})
619 set(armv8.1m.main_SOURCES ${arm_SOURCES})
635 hexagon/common_entry_exit_abi1.S
636 hexagon/common_entry_exit_abi2.S
637 hexagon/common_entry_exit_legacy.S
646 hexagon/fastmath2_dlib_asm.S
647 hexagon/fastmath2_ldlib_asm.S
648 hexagon/fastmath_dlib_asm.S
649 hexagon/memcpy_forward_vp4cp4n2.S
650 hexagon/memcpy_likely_aligned.S
662 ${GENERIC_TF_SOURCES}
665 set(loongarch_SOURCES
668 ${GENERIC_TF_SOURCES}
670 set(loongarch64_SOURCES
674 set(mips_SOURCES ${GENERIC_SOURCES})
675 set(mipsel_SOURCES ${mips_SOURCES})
676 set(mips64_SOURCES ${GENERIC_TF_SOURCES}
678 set(mips64el_SOURCES ${GENERIC_TF_SOURCES}
681 set(powerpc_SOURCES ${GENERIC_SOURCES})
683 set(powerpcspe_SOURCES ${GENERIC_SOURCES})
685 set(powerpc64_SOURCES
698 # These routines require __int128, which isn't supported on AIX.
699 if (NOT OS_NAME MATCHES "AIX")
700 set(powerpc64_SOURCES
707 set(powerpc64le_SOURCES ${powerpc64_SOURCES})
714 ${GENERIC_TF_SOURCES}
725 set(sparc_SOURCES ${GENERIC_SOURCES} ${GENERIC_TF_SOURCES})
726 set(sparcv9_SOURCES ${GENERIC_SOURCES} ${GENERIC_TF_SOURCES})
729 ${GENERIC_TF_SOURCES}
733 ${GENERIC_TF_SOURCES}
739 ve/grow_stack_align.S
740 ${GENERIC_TF_SOURCES}
743 add_custom_target(builtins)
744 set_target_properties(builtins PROPERTIES FOLDER "Compiler-RT Misc")
746 option(COMPILER_RT_ENABLE_SOFTWARE_INT128
747 "Enable the int128 builtin routines for all targets."
751 add_subdirectory(Darwin-excludes)
752 add_subdirectory(macho_embedded)
753 darwin_add_builtin_libraries(${BUILTIN_SUPPORTED_OS})
755 set(BUILTIN_CFLAGS "")
756 add_security_warnings(BUILTIN_CFLAGS 0)
758 if (COMPILER_RT_HAS_FCF_PROTECTION_FLAG)
759 append_list_if(COMPILER_RT_ENABLE_CET -fcf-protection=full BUILTIN_CFLAGS)
762 append_list_if(COMPILER_RT_HAS_STD_C11_FLAG -std=c11 BUILTIN_CFLAGS)
764 # Don't embed directives for picking any specific CRT
766 set(CMAKE_MSVC_RUNTIME_LIBRARY "")
767 append_list_if(COMPILER_RT_HAS_ZL_FLAG /Zl BUILTIN_CFLAGS)
770 # These flags would normally be added to CMAKE_C_FLAGS by the llvm
771 # cmake step. Add them manually if this is a standalone build.
772 if(COMPILER_RT_STANDALONE_BUILD)
773 if(COMPILER_RT_BUILTINS_ENABLE_PIC)
774 append_list_if(COMPILER_RT_HAS_FPIC_FLAG -fPIC BUILTIN_CFLAGS)
776 append_list_if(COMPILER_RT_HAS_FNO_BUILTIN_FLAG -fno-builtin BUILTIN_CFLAGS)
777 if(COMPILER_RT_BUILTINS_HIDE_SYMBOLS)
778 append_list_if(COMPILER_RT_HAS_VISIBILITY_HIDDEN_FLAG -fvisibility=hidden BUILTIN_CFLAGS)
780 if(NOT COMPILER_RT_DEBUG)
781 append_list_if(COMPILER_RT_HAS_OMIT_FRAME_POINTER_FLAG -fomit-frame-pointer BUILTIN_CFLAGS)
787 if(COMPILER_RT_BUILTINS_HIDE_SYMBOLS)
788 append_list_if(COMPILER_RT_HAS_VISIBILITY_HIDDEN_FLAG VISIBILITY_HIDDEN BUILTIN_DEFS)
791 if(COMPILER_RT_DISABLE_AARCH64_FMV)
792 list(APPEND BUILTIN_DEFS DISABLE_AARCH64_FMV)
795 append_list_if(COMPILER_RT_HAS_ASM_LSE HAS_ASM_LSE BUILTIN_DEFS)
797 foreach (arch ${BUILTIN_SUPPORTED_ARCH})
798 if (CAN_TARGET_${arch})
799 cmake_push_check_state()
800 # TODO: we should probably make most of the checks in builtin-config depend on the target flags.
801 message(STATUS "Performing additional configure checks with target flags: ${TARGET_${arch}_CFLAGS}")
802 set(BUILTIN_CFLAGS_${arch} ${BUILTIN_CFLAGS})
803 list(APPEND CMAKE_REQUIRED_FLAGS ${TARGET_${arch}_CFLAGS} ${BUILTIN_CFLAGS_${arch}})
804 # For ARM archs, exclude any VFP builtins if VFP is not supported
805 if (${arch} MATCHES "^(arm|armhf|armv7|armv7s|armv7k|armv7m|armv7em|armv8m.main|armv8.1m.main)$")
806 string(REPLACE ";" " " _TARGET_${arch}_CFLAGS "${TARGET_${arch}_CFLAGS}")
807 check_compile_definition(__ARM_FP "${CMAKE_C_FLAGS} ${_TARGET_${arch}_CFLAGS}" COMPILER_RT_HAS_${arch}_VFP)
808 if(NOT COMPILER_RT_HAS_${arch}_VFP)
809 list(REMOVE_ITEM ${arch}_SOURCES ${arm_Thumb1_VFPv2_DP_SOURCES} ${arm_Thumb1_VFPv2_SP_SOURCES} ${arm_Thumb1_SjLj_EH_SOURCES})
811 # Exclude any double-precision builtins if VFP is single-precision-only
812 try_compile_only(COMPILER_RT_HAS_${arch}_VFP_DP
813 SOURCE "#if !(__ARM_FP & 0x8)
814 #error No double-precision support!
816 int main(void) { return 0; }")
817 if(NOT COMPILER_RT_HAS_${arch}_VFP_DP)
818 list(REMOVE_ITEM ${arch}_SOURCES ${arm_Thumb1_VFPv2_DP_SOURCES})
822 check_c_source_compiles("_Float16 foo(_Float16 x) { return x; }"
823 COMPILER_RT_HAS_${arch}_FLOAT16)
824 append_list_if(COMPILER_RT_HAS_${arch}_FLOAT16 -DCOMPILER_RT_HAS_FLOAT16 BUILTIN_CFLAGS_${arch})
825 check_c_source_compiles("__bf16 foo(__bf16 x) { return x; }"
826 COMPILER_RT_HAS_${arch}_BFLOAT16)
827 # Build BF16 files only when "__bf16" is available.
828 if(COMPILER_RT_HAS_${arch}_BFLOAT16)
829 list(APPEND ${arch}_SOURCES ${BF16_SOURCES})
832 # Remove a generic C builtin when an arch-specific builtin is specified.
833 filter_builtin_sources(${arch}_SOURCES ${arch})
835 # Needed for clear_cache on debug mode, due to r7's usage in inline asm.
836 # Release mode already sets it via -O2/3, Debug mode doesn't.
837 if (${arch} STREQUAL "armhf")
838 list(APPEND BUILTIN_CFLAGS_${arch} -fomit-frame-pointer -DCOMPILER_RT_ARMHF_TARGET)
841 # For RISCV32, we must force enable int128 for compiling long
843 if(COMPILER_RT_ENABLE_SOFTWARE_INT128 OR "${arch}" STREQUAL "riscv32")
844 list(APPEND BUILTIN_CFLAGS_${arch} -fforce-enable-int128)
847 add_compiler_rt_runtime(clang_rt.builtins
851 SOURCES ${${arch}_SOURCES}
853 CFLAGS ${BUILTIN_CFLAGS_${arch}}
854 PARENT_TARGET builtins)
855 cmake_pop_check_state()
860 add_dependencies(compiler-rt builtins)
862 option(COMPILER_RT_BUILD_STANDALONE_LIBATOMIC
863 "Build standalone shared atomic library."
866 if(COMPILER_RT_BUILD_STANDALONE_LIBATOMIC)
867 add_custom_target(builtins-standalone-atomic)
868 set(BUILTIN_TYPE SHARED)
869 if(OS_NAME MATCHES "AIX")
870 include(CompilerRTAIXUtils)
871 if(NOT COMPILER_RT_LIBATOMIC_LINK_FLAGS)
872 get_aix_libatomic_default_link_flags(COMPILER_RT_LIBATOMIC_LINK_FLAGS
873 "${CMAKE_CURRENT_SOURCE_DIR}/ppc/atomic.exp")
875 # For different versions of cmake, SHARED behaves differently. For some
876 # versions, we might need MODULE rather than SHARED.
877 get_aix_libatomic_type(BUILTIN_TYPE)
879 list(APPEND COMPILER_RT_LIBATOMIC_LINK_FLAGS -nodefaultlibs)
881 foreach (arch ${BUILTIN_SUPPORTED_ARCH})
882 if(CAN_TARGET_${arch})
883 add_compiler_rt_runtime(clang_rt.atomic
887 LINK_FLAGS ${COMPILER_RT_LIBATOMIC_LINK_FLAGS}
888 LINK_LIBS clang_rt.builtins-${arch}
889 PARENT_TARGET builtins-standalone-atomic)
892 # FIXME: On AIX, we have to archive built shared libraries into a static
893 # archive, i.e., libatomic.a. Once cmake adds support of such usage for AIX,
894 # this ad-hoc part can be removed.
895 if(OS_NAME MATCHES "AIX")
896 archive_aix_libatomic(clang_rt.atomic libatomic
897 ARCHS ${BUILTIN_SUPPORTED_ARCH}
898 PARENT_TARGET builtins-standalone-atomic)
900 add_dependencies(compiler-rt builtins-standalone-atomic)
903 # TODO: COMPILER_RT_BUILD_CRT used to be a cached variable so we need to unset
904 # it first so cmake_dependent_option can set the local variable of the same
905 # name. This statement can be removed in the future.
906 unset(COMPILER_RT_BUILD_CRT CACHE)
908 cmake_dependent_option(COMPILER_RT_BUILD_CRT "Build crtbegin.o/crtend.o" ON "COMPILER_RT_HAS_CRT" OFF)
910 if (COMPILER_RT_BUILD_CRT)
911 add_compiler_rt_component(crt)
913 option(COMPILER_RT_CRT_USE_EH_FRAME_REGISTRY "Use eh_frame in crtbegin.o/crtend.o" ON)
915 include(CheckSectionExists)
916 check_section_exists(".init_array" COMPILER_RT_HAS_INITFINI_ARRAY
917 SOURCE "volatile int x;\n__attribute__((constructor)) void f(void) {x = 0;}\nint main(void) { return 0; }\n")
919 append_list_if(COMPILER_RT_HAS_STD_C11_FLAG -std=c11 CRT_CFLAGS)
920 append_list_if(COMPILER_RT_HAS_INITFINI_ARRAY -DCRT_HAS_INITFINI_ARRAY CRT_CFLAGS)
921 append_list_if(COMPILER_RT_CRT_USE_EH_FRAME_REGISTRY -DEH_USE_FRAME_REGISTRY CRT_CFLAGS)
922 append_list_if(COMPILER_RT_HAS_FPIC_FLAG -fPIC CRT_CFLAGS)
923 append_list_if(COMPILER_RT_HAS_WNO_PEDANTIC -Wno-pedantic CRT_CFLAGS)
924 if (COMPILER_RT_HAS_FCF_PROTECTION_FLAG)
925 append_list_if(COMPILER_RT_ENABLE_CET -fcf-protection=full CRT_CFLAGS)
928 foreach(arch ${BUILTIN_SUPPORTED_ARCH})
929 add_compiler_rt_runtime(clang_rt.crtbegin
932 SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/crtbegin.c
935 add_compiler_rt_runtime(clang_rt.crtend
938 SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/crtend.c
943 add_dependencies(compiler-rt crt)