[RISCV][compiler-rt] Small fixes for __riscv_feature_bits (#100158)
[llvm-project.git] / compiler-rt / lib / builtins / CMakeLists.txt
blob13adbd6c4d57d96e35ade6841de98d28cec46c36
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"
23     )
25   include(base-config-ix)
26   include(CompilerRTUtils)
28   if (NOT LLVM_RUNTIMES_BUILD)
29     load_llvm_config()
30   endif()
31   construct_compiler_rt_default_triple()
33   include(SetPlatformToolchainTools)
34   if(APPLE)
35     include(CompilerRTDarwinUtils)
36   endif()
37   if(APPLE)
38     include(UseLibtool)
39   endif()
40   include(AddCompilerRT)
42   if(MINGW)
43     # Simplified version of what's set in cmake/config-ix.cmake; not including
44     # builtins, which are linked separately.
45     set(MINGW_LIBRARIES mingw32 moldname mingwex msvcrt advapi32 shell32
46                         user32 kernel32 mingw32 moldname mingwex msvcrt)
47    endif()
48 endif()
50 if (COMPILER_RT_STANDALONE_BUILD)
51   # When compiler-rt is being built standalone, possibly as a cross-compilation
52   # target, the target may or may not want position independent code. This
53   # option provides an avenue through which the flag may be controlled when an
54   # LLVM configuration is not being utilized.
55   option(COMPILER_RT_BUILTINS_ENABLE_PIC
56     "Turns on or off -fPIC for the builtin library source"
57     ON)
58 endif()
60 include(builtin-config-ix)
61 include(CMakeDependentOption)
62 include(CMakePushCheckState)
64 option(COMPILER_RT_BUILTINS_HIDE_SYMBOLS
65   "Do not export any symbols from the static library." ON)
67 # TODO: Need to add a mechanism for logging errors when builtin source files are
68 # added to a sub-directory and not this CMakeLists file.
69 set(GENERIC_SOURCES
70   absvdi2.c
71   absvsi2.c
72   absvti2.c
73   adddf3.c
74   addsf3.c
75   addvdi3.c
76   addvsi3.c
77   addvti3.c
78   apple_versioning.c
79   ashldi3.c
80   ashlti3.c
81   ashrdi3.c
82   ashrti3.c
83   bswapdi2.c
84   bswapsi2.c
85   clzdi2.c
86   clzsi2.c
87   clzti2.c
88   cmpdi2.c
89   cmpti2.c
90   comparedf2.c
91   comparesf2.c
92   ctzdi2.c
93   ctzsi2.c
94   ctzti2.c
95   divdc3.c
96   divdf3.c
97   divdi3.c
98   divmoddi4.c
99   divmodsi4.c
100   divmodti4.c
101   divsc3.c
102   divsf3.c
103   divsi3.c
104   divti3.c
105   extendsfdf2.c
106   extendhfsf2.c
107   ffsdi2.c
108   ffssi2.c
109   ffsti2.c
110   fixdfdi.c
111   fixdfsi.c
112   fixdfti.c
113   fixsfdi.c
114   fixsfsi.c
115   fixsfti.c
116   fixunsdfdi.c
117   fixunsdfsi.c
118   fixunsdfti.c
119   fixunssfdi.c
120   fixunssfsi.c
121   fixunssfti.c
122   floatdidf.c
123   floatdisf.c
124   floatsidf.c
125   floatsisf.c
126   floattidf.c
127   floattisf.c
128   floatundidf.c
129   floatundisf.c
130   floatunsidf.c
131   floatunsisf.c
132   floatuntidf.c
133   floatuntisf.c
134   fp_mode.c
135   int_util.c
136   lshrdi3.c
137   lshrti3.c
138   moddi3.c
139   modsi3.c
140   modti3.c
141   muldc3.c
142   muldf3.c
143   muldi3.c
144   mulodi4.c
145   mulosi4.c
146   muloti4.c
147   mulsc3.c
148   mulsf3.c
149   multi3.c
150   mulvdi3.c
151   mulvsi3.c
152   mulvti3.c
153   negdf2.c
154   negdi2.c
155   negsf2.c
156   negti2.c
157   negvdi2.c
158   negvsi2.c
159   negvti2.c
160   os_version_check.c
161   paritydi2.c
162   paritysi2.c
163   parityti2.c
164   popcountdi2.c
165   popcountsi2.c
166   popcountti2.c
167   powidf2.c
168   powisf2.c
169   subdf3.c
170   subsf3.c
171   subvdi3.c
172   subvsi3.c
173   subvti3.c
174   trampoline_setup.c
175   truncdfhf2.c
176   truncdfsf2.c
177   truncsfhf2.c
178   ucmpdi2.c
179   ucmpti2.c
180   udivdi3.c
181   udivmoddi4.c
182   udivmodsi4.c
183   udivmodti4.c
184   udivsi3.c
185   udivti3.c
186   umoddi3.c
187   umodsi3.c
188   umodti3.c
191 # We only build BF16 files when "__bf16" is available.
192 set(BF16_SOURCES
193     extendbfsf2.c
194     truncdfbf2.c
195     truncsfbf2.c
198 set(GENERIC_TF_SOURCES
199   addtf3.c
200   comparetf2.c
201   divtc3.c
202   divtf3.c
203   extenddftf2.c
204   extendhftf2.c
205   extendsftf2.c
206   fixtfdi.c
207   fixtfsi.c
208   fixtfti.c
209   fixunstfdi.c
210   fixunstfsi.c
211   fixunstfti.c
212   floatditf.c
213   floatsitf.c
214   floattitf.c
215   floatunditf.c
216   floatunsitf.c
217   floatuntitf.c
218   multc3.c
219   multf3.c
220   powitf2.c
221   subtf3.c
222   trunctfdf2.c
223   trunctfhf2.c
224   trunctfsf2.c
227 option(COMPILER_RT_EXCLUDE_ATOMIC_BUILTIN
228   "Skip the atomic builtin (these should normally be provided by a shared library)"
229   On)
231 if(NOT FUCHSIA AND NOT COMPILER_RT_BAREMETAL_BUILD AND NOT COMPILER_RT_GPU_BUILD)
232   set(GENERIC_SOURCES
233     ${GENERIC_SOURCES}
234     emutls.c
235     enable_execute_stack.c
236     eprintf.c
237   )
238 endif()
240 option(COMPILER_RT_LIBATOMIC_USE_PTHREAD
241   "Whether libatomic should use pthreads if available."
242   Off)
244 if(COMPILER_RT_LIBATOMIC_USE_PTHREAD)
245   add_compile_definitions(_LIBATOMIC_USE_PTHREAD)
246 endif()
248 if(COMPILER_RT_HAS_ATOMIC_KEYWORD AND NOT COMPILER_RT_EXCLUDE_ATOMIC_BUILTIN)
249   set(GENERIC_SOURCES
250     ${GENERIC_SOURCES}
251     atomic.c
252   )
253 endif()
255 if(APPLE)
256   set(GENERIC_SOURCES
257     ${GENERIC_SOURCES}
258     atomic_flag_clear.c
259     atomic_flag_clear_explicit.c
260     atomic_flag_test_and_set.c
261     atomic_flag_test_and_set_explicit.c
262     atomic_signal_fence.c
263     atomic_thread_fence.c
264   )
265 endif()
267 if (HAVE_UNWIND_H)
268   set(GENERIC_SOURCES
269     ${GENERIC_SOURCES}
270     gcc_personality_v0.c
271   )
272 endif ()
274 if (NOT FUCHSIA)
275   set(GENERIC_SOURCES
276     ${GENERIC_SOURCES}
277     clear_cache.c
278   )
279 endif()
281 # These files are used on 32-bit and 64-bit x86.
282 set(x86_ARCH_SOURCES
283   cpu_model/x86.c
284   )
286 if (NOT MSVC)
287   set(x86_ARCH_SOURCES
288     ${x86_ARCH_SOURCES}
289     i386/fp_mode.c
290   )
291 endif ()
293 # Implement extended-precision builtins, assuming long double is 80 bits.
294 # long double is not 80 bits on Android or MSVC.
295 set(x86_80_BIT_SOURCES
296   divxc3.c
297   extendxftf2.c
298   fixxfdi.c
299   fixxfti.c
300   fixunsxfdi.c
301   fixunsxfsi.c
302   fixunsxfti.c
303   floatdixf.c
304   floattixf.c
305   floatundixf.c
306   floatuntixf.c
307   mulxc3.c
308   powixf2.c
309   trunctfxf2.c
312 if (NOT MSVC)
313   set(x86_64_SOURCES
314     ${GENERIC_SOURCES}
315     ${GENERIC_TF_SOURCES}
316     ${x86_ARCH_SOURCES}
317     x86_64/floatdidf.c
318     x86_64/floatdisf.c
319   )
320   if (NOT WIN32)
321     set(x86_64_SOURCES
322       ${x86_64_SOURCES}
323       x86_64/floatundidf.S
324       x86_64/floatundisf.S
325     )
326   endif()
328   if (NOT ANDROID)
329     set(x86_64_SOURCES
330       ${x86_64_SOURCES}
331       ${x86_80_BIT_SOURCES}
332       x86_64/floatdixf.c
333     )
334     if (NOT WIN32)
335       set(x86_64_SOURCES
336         ${x86_64_SOURCES}
337         x86_64/floatundixf.S
338       )
339     endif()
340   endif()
342   # Darwin x86_64 Haswell
343   set(x86_64h_SOURCES ${x86_64_SOURCES})
345   if (WIN32)
346     set(x86_64_SOURCES
347       ${x86_64_SOURCES}
348       x86_64/chkstk.S
349     )
350   endif()
352   set(i386_SOURCES
353     ${GENERIC_SOURCES}
354     ${x86_ARCH_SOURCES}
355     i386/ashldi3.S
356     i386/ashrdi3.S
357     i386/divdi3.S
358     i386/floatdidf.S
359     i386/floatdisf.S
360     i386/floatundidf.S
361     i386/floatundisf.S
362     i386/lshrdi3.S
363     i386/moddi3.S
364     i386/muldi3.S
365     i386/udivdi3.S
366     i386/umoddi3.S
367   )
369   if (NOT ANDROID)
370     set(i386_SOURCES
371       ${i386_SOURCES}
372       ${x86_80_BIT_SOURCES}
373       i386/floatdixf.S
374       i386/floatundixf.S
375     )
376   endif()
378   if (WIN32)
379     set(i386_SOURCES
380       ${i386_SOURCES}
381       i386/chkstk.S
382     )
383   endif()
384 else () # MSVC
385   # Use C versions of functions when building on MSVC
386   # MSVC's assembler takes Intel syntax, not AT&T syntax.
387   # Also use only MSVC compilable builtin implementations.
388   set(x86_64_SOURCES
389     ${GENERIC_SOURCES}
390     ${x86_ARCH_SOURCES}
391     x86_64/floatdidf.c
392     x86_64/floatdisf.c
393   )
394   set(i386_SOURCES ${GENERIC_SOURCES} ${x86_ARCH_SOURCES})
395 endif () # if (NOT MSVC)
398 # builtin support for Targets that have Arm state or have Thumb2
399 set(arm_or_thumb2_base_SOURCES
400   arm/fp_mode.c
401   arm/bswapdi2.S
402   arm/bswapsi2.S
403   arm/clzdi2.S
404   arm/clzsi2.S
405   arm/comparesf2.S
406   arm/divmodsi4.S
407   arm/divsi3.S
408   arm/modsi3.S
409   arm/udivmodsi4.S
410   arm/udivsi3.S
411   arm/umodsi3.S
412   ${GENERIC_SOURCES}
415 set(arm_sync_SOURCES
416   arm/sync_fetch_and_add_4.S
417   arm/sync_fetch_and_add_8.S
418   arm/sync_fetch_and_and_4.S
419   arm/sync_fetch_and_and_8.S
420   arm/sync_fetch_and_max_4.S
421   arm/sync_fetch_and_max_8.S
422   arm/sync_fetch_and_min_4.S
423   arm/sync_fetch_and_min_8.S
424   arm/sync_fetch_and_nand_4.S
425   arm/sync_fetch_and_nand_8.S
426   arm/sync_fetch_and_or_4.S
427   arm/sync_fetch_and_or_8.S
428   arm/sync_fetch_and_sub_4.S
429   arm/sync_fetch_and_sub_8.S
430   arm/sync_fetch_and_umax_4.S
431   arm/sync_fetch_and_umax_8.S
432   arm/sync_fetch_and_umin_4.S
433   arm/sync_fetch_and_umin_8.S
434   arm/sync_fetch_and_xor_4.S
435   arm/sync_fetch_and_xor_8.S
438 # builtin support for Thumb-only targets with very limited Thumb2 technology,
439 # such as v6-m and v8-m.baseline
440 set(thumb1_base_SOURCES
441   arm/divsi3.S
442   arm/udivsi3.S
443   arm/comparesf2.S
444   arm/addsf3.S
445   ${GENERIC_SOURCES}
448 set(arm_EABI_SOURCES
449   arm/aeabi_cdcmp.S
450   arm/aeabi_cdcmpeq_check_nan.c
451   arm/aeabi_cfcmp.S
452   arm/aeabi_cfcmpeq_check_nan.c
453   arm/aeabi_dcmp.S
454   arm/aeabi_div0.c
455   arm/aeabi_drsub.c
456   arm/aeabi_fcmp.S
457   arm/aeabi_frsub.c
458   arm/aeabi_idivmod.S
459   arm/aeabi_ldivmod.S
460   arm/aeabi_memcmp.S
461   arm/aeabi_memcpy.S
462   arm/aeabi_memmove.S
463   arm/aeabi_memset.S
464   arm/aeabi_uidivmod.S
465   arm/aeabi_uldivmod.S
468 set(arm_Thumb1_JT_SOURCES
469   arm/switch16.S
470   arm/switch32.S
471   arm/switch8.S
472   arm/switchu8.S
474 set(arm_Thumb1_SjLj_EH_SOURCES
475   arm/restore_vfp_d8_d15_regs.S
476   arm/save_vfp_d8_d15_regs.S
478 set(arm_Thumb1_VFPv2_DP_SOURCES
479   arm/adddf3vfp.S
480   arm/divdf3vfp.S
481   arm/eqdf2vfp.S
482   arm/extendsfdf2vfp.S
483   arm/fixdfsivfp.S
484   arm/fixunsdfsivfp.S
485   arm/floatsidfvfp.S
486   arm/floatunssidfvfp.S
487   arm/gedf2vfp.S
488   arm/gtdf2vfp.S
489   arm/ledf2vfp.S
490   arm/ltdf2vfp.S
491   arm/muldf3vfp.S
492   arm/nedf2vfp.S
493   arm/negdf2vfp.S
494   arm/subdf3vfp.S
495   arm/truncdfsf2vfp.S
496   arm/unorddf2vfp.S
498 set(arm_Thumb1_VFPv2_SP_SOURCES
499   arm/addsf3vfp.S
500   arm/divsf3vfp.S
501   arm/eqsf2vfp.S
502   arm/fixsfsivfp.S
503   arm/fixunssfsivfp.S
504   arm/floatsisfvfp.S
505   arm/floatunssisfvfp.S
506   arm/gesf2vfp.S
507   arm/gtsf2vfp.S
508   arm/lesf2vfp.S
509   arm/ltsf2vfp.S
510   arm/mulsf3vfp.S
511   arm/negsf2vfp.S
512   arm/nesf2vfp.S
513   arm/subsf3vfp.S
514   arm/unordsf2vfp.S
516 set(arm_Thumb1_icache_SOURCES
517   arm/sync_synchronize.S
520 # thumb1 calling into Arm to cover support
521 set(arm_Thumb1_SOURCES
522   ${arm_Thumb1_JT_SOURCES}
523   ${arm_Thumb1_SjLj_EH_SOURCES}
524   ${arm_Thumb1_VFPv2_DP_SOURCES}
525   ${arm_Thumb1_VFPv2_SP_SOURCES}
526   ${arm_Thumb1_icache_SOURCES}
529 # base functionality for Arm Targets prior to Arm v7-a and Armv6-m such as v6,
530 # v5t, v4t
531 set(arm_min_SOURCES
532   ${arm_or_thumb2_base_SOURCES}
533   ${arm_EABI_SOURCES}
536 if(MINGW)
537   set(arm_SOURCES
538     arm/aeabi_idivmod.S
539     arm/aeabi_ldivmod.S
540     arm/aeabi_uidivmod.S
541     arm/aeabi_uldivmod.S
542     arm/chkstk.S
543     ${arm_or_thumb2_base_SOURCES}
544     ${arm_sync_SOURCES}
545   )
547   set(thumb1_SOURCES
548     ${thumb1_base_SOURCES}
549   )
550 elseif(NOT WIN32)
551   # TODO the EABI sources should only be added to EABI targets
552   set(arm_SOURCES
553     ${arm_or_thumb2_base_SOURCES}
554     ${arm_sync_SOURCES}
555     ${arm_EABI_SOURCES}
556     ${arm_Thumb1_SOURCES}
557   )
559   set(thumb1_SOURCES
560     ${thumb1_base_SOURCES}
561     ${arm_EABI_SOURCES}
562   )
563 endif()
565 set(aarch64_SOURCES
566   ${GENERIC_TF_SOURCES}
567   ${GENERIC_SOURCES}
568   cpu_model/aarch64.c
569   aarch64/fp_mode.c
572 if (COMPILER_RT_HAS_AARCH64_SME)
573   if (NOT COMPILER_RT_DISABLE_AARCH64_FMV AND COMPILER_RT_HAS_FNO_BUILTIN_FLAG AND (COMPILER_RT_HAS_AUXV OR COMPILER_RT_BAREMETAL_BUILD))
574     list(APPEND aarch64_SOURCES aarch64/sme-abi.S aarch64/sme-libc-mem-routines.S aarch64/sme-abi-init.c aarch64/sme-abi-vg.c aarch64/sme-libc-routines.c)
575     message(STATUS "AArch64 SME ABI routines enabled")
576     set_source_files_properties(aarch64/sme-libc-routines.c PROPERTIES COMPILE_FLAGS "-fno-builtin")
577   else()
578     if(COMPILER_RT_DISABLE_AARCH64_FMV)
579       message(WARNING "AArch64 SME ABI routines require function multiversioning support.")
580     endif()
581     if(NOT COMPILER_RT_HAS_FNO_BUILTIN_FLAG)
582       message(WARNING "AArch64 SME ABI routines require '-fno-builtin'")
583     endif()
584     if(NOT (COMPILER_RT_HAS_AUXV OR COMPILER_RT_BAREMETAL_BUILD))
585       message(WARNING "AArch64 SME ABI routines requires sys/auxv.h or COMPILER_RT_BAREMETAL_BUILD flag")
586     endif()
587     message(STATUS "AArch64 SME ABI routines disabled")
588   endif()
589 endif()
591 # Generate outline atomics helpers from lse.S base
592 set(OA_HELPERS_DIR "${CMAKE_CURRENT_BINARY_DIR}/outline_atomic_helpers.dir")
593 file(MAKE_DIRECTORY "${OA_HELPERS_DIR}")
595 if(CMAKE_HOST_UNIX)
596   set(COMPILER_RT_LINK_OR_COPY create_symlink)
597 else()
598   set(COMPILER_RT_LINK_OR_COPY copy)
599 endif()
601 foreach(pat cas swp ldadd ldclr ldeor ldset)
602   foreach(size 1 2 4 8 16)
603     foreach(model 1 2 3 4 5)
604       if(pat STREQUAL "cas" OR NOT size STREQUAL "16")
605         set(source_asm "${CMAKE_CURRENT_SOURCE_DIR}/aarch64/lse.S")
606         set(helper_asm "${OA_HELPERS_DIR}/outline_atomic_${pat}${size}_${model}.S")
607         add_custom_command(
608           OUTPUT "${helper_asm}"
609           COMMAND ${CMAKE_COMMAND} -E ${COMPILER_RT_LINK_OR_COPY} "${source_asm}" "${helper_asm}"
610           DEPENDS "${source_asm}"
611         )
612         set_source_files_properties("${helper_asm}"
613           PROPERTIES
614           COMPILE_DEFINITIONS "L_${pat};SIZE=${size};MODEL=${model}"
615           INCLUDE_DIRECTORIES "${CMAKE_CURRENT_SOURCE_DIR}"
616         )
617         list(APPEND aarch64_SOURCES "${helper_asm}")
618       endif()
619     endforeach(model)
620   endforeach(size)
621 endforeach(pat)
623 if (MINGW)
624   set(aarch64_SOURCES
625     ${aarch64_SOURCES}
626     aarch64/chkstk.S
627   )
628 endif()
630 set(amdgcn_SOURCES ${GENERIC_SOURCES})
632 set(armv4t_SOURCES ${arm_min_SOURCES})
633 set(armv5te_SOURCES ${arm_min_SOURCES})
634 set(armv6_SOURCES ${arm_min_SOURCES})
635 set(armhf_SOURCES ${arm_SOURCES})
636 set(armv7_SOURCES ${arm_SOURCES})
637 set(armv7s_SOURCES ${arm_SOURCES})
638 set(armv7k_SOURCES ${arm_SOURCES})
639 set(arm64_SOURCES ${aarch64_SOURCES})
640 set(arm64e_SOURCES ${aarch64_SOURCES})
641 set(arm64_32_SOURCES ${aarch64_SOURCES})
643 # macho_embedded archs
644 set(armv6m_SOURCES ${thumb1_SOURCES})
645 set(armv7m_SOURCES ${arm_SOURCES})
646 set(armv7em_SOURCES ${arm_SOURCES})
647 set(armv8m.base_SOURCES ${thumb1_SOURCES})
648 set(armv8m.main_SOURCES ${arm_SOURCES})
649 set(armv8.1m.main_SOURCES ${arm_SOURCES})
651 # 8-bit AVR MCU
652 set(avr_SOURCES
653   avr/mulqi3.S
654   avr/mulhi3.S
655   avr/exit.S
656   avr/divmodhi4.S
657   avr/udivmodhi4.S
658   avr/divmodqi4.S
659   avr/udivmodqi4.S
660   ${GENERIC_SOURCES}
663 # hexagon arch
664 set(hexagon_SOURCES
665   hexagon/common_entry_exit_abi1.S
666   hexagon/common_entry_exit_abi2.S
667   hexagon/common_entry_exit_legacy.S
668   hexagon/dfaddsub.S
669   hexagon/dfdiv.S
670   hexagon/dffma.S
671   hexagon/dfminmax.S
672   hexagon/dfmul.S
673   hexagon/dfsqrt.S
674   hexagon/divdi3.S
675   hexagon/divsi3.S
676   hexagon/fastmath2_dlib_asm.S
677   hexagon/fastmath2_ldlib_asm.S
678   hexagon/fastmath_dlib_asm.S
679   hexagon/memcpy_forward_vp4cp4n2.S
680   hexagon/memcpy_likely_aligned.S
681   hexagon/moddi3.S
682   hexagon/modsi3.S
683   hexagon/sfdiv_opt.S
684   hexagon/sfsqrt_opt.S
685   hexagon/udivdi3.S
686   hexagon/udivmoddi4.S
687   hexagon/udivmodsi4.S
688   hexagon/udivsi3.S
689   hexagon/umoddi3.S
690   hexagon/umodsi3.S
691   ${GENERIC_SOURCES}
692   ${GENERIC_TF_SOURCES}
695 set(loongarch_SOURCES
696   loongarch/fp_mode.c
697   ${GENERIC_SOURCES}
698   ${GENERIC_TF_SOURCES}
700 set(loongarch64_SOURCES
701   ${loongarch_SOURCES}
704 set(mips_SOURCES ${GENERIC_SOURCES})
705 set(mipsel_SOURCES ${mips_SOURCES})
706 set(mips64_SOURCES ${GENERIC_TF_SOURCES}
707                    ${mips_SOURCES})
708 set(mips64el_SOURCES ${GENERIC_TF_SOURCES}
709                      ${mips_SOURCES})
711 set(nvptx64_SOURCES ${GENERIC_SOURCES})
713 set(powerpc_SOURCES ${GENERIC_SOURCES})
715 set(powerpcspe_SOURCES ${GENERIC_SOURCES})
717 set(powerpc64_SOURCES
718   ppc/divtc3.c
719   ppc/fixtfdi.c
720   ppc/fixunstfdi.c
721   ppc/floatditf.c
722   ppc/floatunditf.c
723   ppc/gcc_qadd.c
724   ppc/gcc_qdiv.c
725   ppc/gcc_qmul.c
726   ppc/gcc_qsub.c
727   ppc/multc3.c
728   ${GENERIC_SOURCES}
730 # These routines require __int128, which isn't supported on AIX.
731 if (NOT OS_NAME MATCHES "AIX")
732   set(powerpc64_SOURCES
733     ppc/floattitf.c
734     ppc/fixtfti.c
735     ppc/fixunstfti.c
736     ${powerpc64_SOURCES}
737   )
738 endif()
739 set(powerpc64le_SOURCES ${powerpc64_SOURCES})
741 set(riscv_SOURCES
742   cpu_model/riscv.c
743   riscv/fp_mode.c
744   riscv/save.S
745   riscv/restore.S
746   ${GENERIC_SOURCES}
747   ${GENERIC_TF_SOURCES}
749 set(riscv32_SOURCES
750   riscv/mulsi3.S
751   ${riscv_SOURCES}
753 set(riscv64_SOURCES
754   riscv/muldi3.S
755   ${riscv_SOURCES}
758 set(sparc_SOURCES ${GENERIC_SOURCES} ${GENERIC_TF_SOURCES})
759 set(sparcv9_SOURCES ${GENERIC_SOURCES} ${GENERIC_TF_SOURCES})
761 set(wasm32_SOURCES
762   ${GENERIC_TF_SOURCES}
763   ${GENERIC_SOURCES}
765 set(wasm64_SOURCES
766   ${GENERIC_TF_SOURCES}
767   ${GENERIC_SOURCES}
770 set(ve_SOURCES
771   ve/grow_stack.S
772   ve/grow_stack_align.S
773   ${GENERIC_TF_SOURCES}
774   ${GENERIC_SOURCES})
776 add_custom_target(builtins)
777 set_target_properties(builtins PROPERTIES FOLDER "Compiler-RT/Metatargets")
779 option(COMPILER_RT_ENABLE_SOFTWARE_INT128
780   "Enable the int128 builtin routines for all targets."
781   OFF)
783 if (APPLE)
784   add_subdirectory(Darwin-excludes)
785   add_subdirectory(macho_embedded)
786   darwin_add_builtin_libraries(${BUILTIN_SUPPORTED_OS})
787 else ()
788   set(BUILTIN_CFLAGS "")
789   add_security_warnings(BUILTIN_CFLAGS 0)
791   if (COMPILER_RT_HAS_FCF_PROTECTION_FLAG)
792     append_list_if(COMPILER_RT_ENABLE_CET -fcf-protection=full BUILTIN_CFLAGS)
793   endif()
795   append_list_if(COMPILER_RT_HAS_STD_C11_FLAG -std=c11 BUILTIN_CFLAGS)
796   append_list_if(COMPILER_RT_HAS_WBUILTIN_DECLARATION_MISMATCH_FLAG -Werror=builtin-declaration-mismatch BUILTIN_CFLAGS)
798   # Don't embed directives for picking any specific CRT
799   if (MSVC)
800     set(CMAKE_MSVC_RUNTIME_LIBRARY "")
801     append_list_if(COMPILER_RT_HAS_ZL_FLAG /Zl BUILTIN_CFLAGS)
802   endif()
804   # These flags would normally be added to CMAKE_C_FLAGS by the llvm
805   # cmake step. Add them manually if this is a standalone build.
806   if(COMPILER_RT_STANDALONE_BUILD)
807     if(COMPILER_RT_BUILTINS_ENABLE_PIC)
808       append_list_if(COMPILER_RT_HAS_FPIC_FLAG -fPIC BUILTIN_CFLAGS)
809     endif()
810     append_list_if(COMPILER_RT_HAS_FNO_BUILTIN_FLAG -fno-builtin BUILTIN_CFLAGS)
811     if(COMPILER_RT_BUILTINS_HIDE_SYMBOLS)
812       append_list_if(COMPILER_RT_HAS_VISIBILITY_HIDDEN_FLAG -fvisibility=hidden BUILTIN_CFLAGS)
813     endif()
814     if(NOT COMPILER_RT_DEBUG)
815       append_list_if(COMPILER_RT_HAS_OMIT_FRAME_POINTER_FLAG -fomit-frame-pointer BUILTIN_CFLAGS)
816     endif()
817   endif()
819   # Directly targeting the GPU requires a few extra flags.
820   if("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "amdgcn|nvptx")
821     append_list_if(COMPILER_RT_HAS_FFREESTANDING_FLAG -ffreestanding BUILTIN_CFLAGS)
822     append_list_if(COMPILER_RT_HAS_NOGPULIB_FLAG -nogpulib BUILTIN_CFLAGS)
823     append_list_if(COMPILER_RT_HAS_FLTO_FLAG -flto BUILTIN_CFLAGS)
824     append_list_if(COMPILER_RT_HAS_FCONVERGENT_FUNCTIONS_FLAG
825                    -fconvergent-functions BUILTIN_CFLAGS)
827     # AMDGPU targets want to use a generic ABI.
828     if("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "amdgcn")
829       append_list_if(COMPILER_RT_HAS_CODE_OBJECT_VERSION_FLAG
830                      "SHELL:-Xclang -mcode-object-version=none" BUILTIN_CFLAGS)
831     endif()
832   endif()
834   set(BUILTIN_DEFS "")
836   if(COMPILER_RT_BUILTINS_HIDE_SYMBOLS)
837     append_list_if(COMPILER_RT_HAS_VISIBILITY_HIDDEN_FLAG VISIBILITY_HIDDEN BUILTIN_DEFS)
838   endif()
840   if(COMPILER_RT_DISABLE_AARCH64_FMV)
841     list(APPEND BUILTIN_DEFS DISABLE_AARCH64_FMV)
842   endif()
844   append_list_if(COMPILER_RT_HAS_ASM_LSE HAS_ASM_LSE BUILTIN_DEFS)
846   foreach (arch ${BUILTIN_SUPPORTED_ARCH})
847     if (CAN_TARGET_${arch})
848       cmake_push_check_state()
849       # TODO: we should probably make most of the checks in builtin-config depend on the target flags.
850       message(STATUS "Performing additional configure checks with target flags: ${TARGET_${arch}_CFLAGS}")
851       set(BUILTIN_CFLAGS_${arch} ${BUILTIN_CFLAGS})
852       list(APPEND CMAKE_REQUIRED_FLAGS ${TARGET_${arch}_CFLAGS} ${BUILTIN_CFLAGS_${arch}})
853       # For ARM archs, exclude any VFP builtins if VFP is not supported
854       if (${arch} MATCHES "^(arm|armhf|armv7|armv7s|armv7k|armv7m|armv7em|armv8m.main|armv8.1m.main)$")
855         string(REPLACE ";" " " _TARGET_${arch}_CFLAGS "${TARGET_${arch}_CFLAGS}")
856         check_compile_definition(__ARM_FP "${CMAKE_C_FLAGS} ${_TARGET_${arch}_CFLAGS}" COMPILER_RT_HAS_${arch}_VFP)
857         if(NOT COMPILER_RT_HAS_${arch}_VFP)
858           list(REMOVE_ITEM ${arch}_SOURCES ${arm_Thumb1_VFPv2_DP_SOURCES} ${arm_Thumb1_VFPv2_SP_SOURCES} ${arm_Thumb1_SjLj_EH_SOURCES})
859         else()
860           # Exclude any double-precision builtins if VFP is single-precision-only
861           try_compile_only(COMPILER_RT_HAS_${arch}_VFP_DP
862                            SOURCE "#if !(__ARM_FP & 0x8)
863                                    #error No double-precision support!
864                                    #endif
865                                    int main(void) { return 0; }")
866           if(NOT COMPILER_RT_HAS_${arch}_VFP_DP)
867             list(REMOVE_ITEM ${arch}_SOURCES ${arm_Thumb1_VFPv2_DP_SOURCES})
868           endif()
869         endif()
870       endif()
871       check_c_source_compiles("_Float16 foo(_Float16 x) { return x; }"
872                               COMPILER_RT_HAS_${arch}_FLOAT16)
873       append_list_if(COMPILER_RT_HAS_${arch}_FLOAT16 -DCOMPILER_RT_HAS_FLOAT16 BUILTIN_CFLAGS_${arch})
874       check_c_source_compiles("__bf16 foo(__bf16 x) { return x; }"
875                               COMPILER_RT_HAS_${arch}_BFLOAT16)
876       # Build BF16 files only when "__bf16" is available.
877       if(COMPILER_RT_HAS_${arch}_BFLOAT16)
878         list(APPEND ${arch}_SOURCES ${BF16_SOURCES})
879       endif()
881       # Remove a generic C builtin when an arch-specific builtin is specified.
882       filter_builtin_sources(${arch}_SOURCES ${arch})
884       # Needed for clear_cache on debug mode, due to r7's usage in inline asm.
885       # Release mode already sets it via -O2/3, Debug mode doesn't.
886       if (${arch} STREQUAL "armhf")
887         list(APPEND BUILTIN_CFLAGS_${arch} -fomit-frame-pointer -DCOMPILER_RT_ARMHF_TARGET)
888       endif()
890       # For RISCV32, we must force enable int128 for compiling long
891       # double routines.
892       if(COMPILER_RT_ENABLE_SOFTWARE_INT128 OR "${arch}" STREQUAL "riscv32")
893         list(APPEND BUILTIN_CFLAGS_${arch} -fforce-enable-int128)
894       endif()
896       add_compiler_rt_runtime(clang_rt.builtins
897                               STATIC
898                               ARCHS ${arch}
899                               DEPS ${deps_${arch}}
900                               SOURCES ${${arch}_SOURCES}
901                               DEFS ${BUILTIN_DEFS}
902                               CFLAGS ${BUILTIN_CFLAGS_${arch}}
903                               PARENT_TARGET builtins)
904       cmake_pop_check_state()
905     endif ()
906   endforeach ()
907 endif ()
909 add_dependencies(compiler-rt builtins)
911 option(COMPILER_RT_BUILD_STANDALONE_LIBATOMIC
912   "Build standalone shared atomic library."
913   OFF)
915 if(COMPILER_RT_BUILD_STANDALONE_LIBATOMIC)
916   add_custom_target(builtins-standalone-atomic)
917   set(BUILTIN_TYPE SHARED)
918   if(OS_NAME MATCHES "AIX")
919     include(CompilerRTAIXUtils)
920     if(NOT COMPILER_RT_LIBATOMIC_LINK_FLAGS)
921       get_aix_libatomic_default_link_flags(COMPILER_RT_LIBATOMIC_LINK_FLAGS
922         "${CMAKE_CURRENT_SOURCE_DIR}/ppc/atomic.exp")
923     endif()
924     # For different versions of cmake, SHARED behaves differently. For some
925     # versions, we might need MODULE rather than SHARED.
926     get_aix_libatomic_type(BUILTIN_TYPE)
927   else()
928     list(APPEND COMPILER_RT_LIBATOMIC_LINK_FLAGS -nodefaultlibs)
929   endif()
930   foreach (arch ${BUILTIN_SUPPORTED_ARCH})
931     if(CAN_TARGET_${arch})
932       list(APPEND COMPILER_RT_LIBATOMIC_LINK_LIBS_${arch} clang_rt.builtins-${arch})
933       append_list_if(MINGW "${MINGW_LIBRARIES}" COMPILER_RT_LIBATOMIC_LINK_LIBS_${arch})
934       add_compiler_rt_runtime(clang_rt.atomic
935                               ${BUILTIN_TYPE}
936                               ARCHS ${arch}
937                               SOURCES atomic.c
938                               LINK_FLAGS ${COMPILER_RT_LIBATOMIC_LINK_FLAGS}
939                               LINK_LIBS ${COMPILER_RT_LIBATOMIC_LINK_LIBS_${arch}}
940                               PARENT_TARGET builtins-standalone-atomic)
941     endif()
942   endforeach()
943   # FIXME: On AIX, we have to archive built shared libraries into a static
944   # archive, i.e., libatomic.a. Once cmake adds support of such usage for AIX,
945   # this ad-hoc part can be removed.
946   if(OS_NAME MATCHES "AIX")
947     archive_aix_libatomic(clang_rt.atomic libatomic
948                           ARCHS ${BUILTIN_SUPPORTED_ARCH}
949                           PARENT_TARGET builtins-standalone-atomic)
950   endif()
951   add_dependencies(compiler-rt builtins-standalone-atomic)
952 endif()
954 cmake_dependent_option(COMPILER_RT_BUILD_CRT "Build crtbegin.o/crtend.o" ON "COMPILER_RT_HAS_CRT" OFF)
956 if (COMPILER_RT_BUILD_CRT)
957   add_compiler_rt_component(crt)
959   option(COMPILER_RT_CRT_USE_EH_FRAME_REGISTRY "Use eh_frame in crtbegin.o/crtend.o" ON)
961   include(CheckSectionExists)
962   check_section_exists(".init_array" COMPILER_RT_HAS_INITFINI_ARRAY
963     SOURCE "volatile int x;\n__attribute__((constructor)) void f(void) {x = 0;}\nint main(void) { return 0; }\n")
965   append_list_if(COMPILER_RT_HAS_STD_C11_FLAG -std=c11 CRT_CFLAGS)
966   append_list_if(COMPILER_RT_HAS_INITFINI_ARRAY -DCRT_HAS_INITFINI_ARRAY CRT_CFLAGS)
967   append_list_if(COMPILER_RT_CRT_USE_EH_FRAME_REGISTRY -DEH_USE_FRAME_REGISTRY CRT_CFLAGS)
968   append_list_if(COMPILER_RT_HAS_FPIC_FLAG -fPIC CRT_CFLAGS)
969   append_list_if(COMPILER_RT_HAS_WNO_PEDANTIC -Wno-pedantic CRT_CFLAGS)
970   if (COMPILER_RT_HAS_FCF_PROTECTION_FLAG)
971     append_list_if(COMPILER_RT_ENABLE_CET -fcf-protection=full CRT_CFLAGS)
972   endif()
974   foreach(arch ${BUILTIN_SUPPORTED_ARCH})
975     add_compiler_rt_runtime(clang_rt.crtbegin
976       OBJECT
977       ARCHS ${arch}
978       SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/crtbegin.c
979       CFLAGS ${CRT_CFLAGS}
980       PARENT_TARGET crt)
981     add_compiler_rt_runtime(clang_rt.crtend
982       OBJECT
983       ARCHS ${arch}
984       SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/crtend.c
985       CFLAGS ${CRT_CFLAGS}
986       PARENT_TARGET crt)
987   endforeach()
989   add_dependencies(compiler-rt crt)
990 endif()